[jboss-svn-commits] JBL Code SVN: r18672 - in labs/jbosslabs/labs-3.0-build: core/core-api/src/main/java/org/jboss/labs/auth/custom and 5 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Mar 4 05:57:49 EST 2008


Author: szimano
Date: 2008-03-04 05:57:49 -0500 (Tue, 04 Mar 2008)
New Revision: 18672

Added:
   labs/jbosslabs/labs-3.0-build/core/core-api/src/main/java/org/jboss/labs/auth/custom/CustomIDService.java
Removed:
   labs/jbosslabs/labs-3.0-build/core/core-api/src/main/java/org/jboss/labs/auth/custom/CustomIDHandler.java
   labs/jbosslabs/labs-3.0-build/services/credential/src/main/java/org/jboss/labs/auth/CustomIDService.java
Modified:
   labs/jbosslabs/labs-3.0-build/core/core-api/src/main/java/org/jboss/labs/auth/AuthenticationService.java
   labs/jbosslabs/labs-3.0-build/core/core-api/src/main/java/org/jboss/labs/auth/RoleBinderService.java
   labs/jbosslabs/labs-3.0-build/core/core-api/src/main/java/org/jboss/labs/auth/custom/CustomIDProvider.java
   labs/jbosslabs/labs-3.0-build/core/core-api/src/main/java/org/jboss/labs/injection/LabsGuiceModule.java
   labs/jbosslabs/labs-3.0-build/services/credential/src/main/java/org/jboss/labs/auth/AuthHelper.java
   labs/jbosslabs/labs-3.0-build/services/credential/src/main/java/org/jboss/labs/auth/impl/AuthenticationServiceImpl.java
   labs/jbosslabs/labs-3.0-build/services/credential/src/main/java/org/jboss/labs/auth/impl/CustomIDServiceImpl.java
   labs/jbosslabs/labs-3.0-build/services/credential/src/main/java/org/jboss/labs/auth/impl/RoleBinderServiceImpl.java
   labs/jbosslabs/labs-3.0-build/services/credential/src/main/java/org/jboss/labs/auth/impl/UserServiceImpl.java
   labs/jbosslabs/labs-3.0-build/services/credential/src/main/resources/META-INF/persistence.xml
   labs/jbosslabs/labs-3.0-build/views/labs-test/src/main/java/org/jboss/labs/test/credential/RoleBinderAndAuthTest.java
Log:
auth servie finished JBLAB-901

Modified: labs/jbosslabs/labs-3.0-build/core/core-api/src/main/java/org/jboss/labs/auth/AuthenticationService.java
===================================================================
--- labs/jbosslabs/labs-3.0-build/core/core-api/src/main/java/org/jboss/labs/auth/AuthenticationService.java	2008-03-04 10:07:04 UTC (rev 18671)
+++ labs/jbosslabs/labs-3.0-build/core/core-api/src/main/java/org/jboss/labs/auth/AuthenticationService.java	2008-03-04 10:57:49 UTC (rev 18672)
@@ -27,6 +27,7 @@
 import javax.ejb.Local;
 
 import org.jboss.labs.core.model.auth.CustomID;
+import org.jboss.labs.exception.auth.NoSuchCustomIDException;
 import org.jboss.labs.exception.auth.NoSuchUserException;
 
 @Local
@@ -46,7 +47,7 @@
 	 *            Id of a project to check. If null, then global privileges are
 	 *            checked
 	 * @return True if a user is authorized. False otherwise.
-	 * @throws NoSuchUserException 
+	 * @throws NoSuchUserException
 	 */
 	public <E extends Enum<E>> boolean isAllowedProject(Principal user,
 			E serviceRole, String projectId) throws NoSuchUserException;
@@ -65,11 +66,15 @@
 	 *            Custom if of a service to check. If null, then global
 	 *            privileges are checked
 	 * @return True if a user is authorized. False otherwise.
-	 * @throws NoSuchUserException 
+	 * @throws NoSuchUserException
+	 *             If user doesn't exist
+	 * @throws NoSuchCustomIDException
+	 *             If customID doesn't exist (probably wasn't persisted)
 	 * @see org.jboss.labs.core.model.auth.CustomID
 	 * @see org.jboss.labs.auth.custom.CustomIDProvider
 	 */
 	public <E extends Enum<E>> boolean isAllowedCustom(Principal user,
-			E serviceRole, CustomID customId) throws NoSuchUserException;
+			E serviceRole, CustomID customId) throws NoSuchUserException,
+			NoSuchCustomIDException;
 
 }

Modified: labs/jbosslabs/labs-3.0-build/core/core-api/src/main/java/org/jboss/labs/auth/RoleBinderService.java
===================================================================
--- labs/jbosslabs/labs-3.0-build/core/core-api/src/main/java/org/jboss/labs/auth/RoleBinderService.java	2008-03-04 10:07:04 UTC (rev 18671)
+++ labs/jbosslabs/labs-3.0-build/core/core-api/src/main/java/org/jboss/labs/auth/RoleBinderService.java	2008-03-04 10:57:49 UTC (rev 18672)
@@ -27,8 +27,10 @@
 import javax.ejb.Local;
 
 import org.jboss.labs.auth.custom.CustomIDProvider;
+import org.jboss.labs.core.model.auth.AuthID;
 import org.jboss.labs.core.model.auth.CustomID;
 import org.jboss.labs.core.model.auth.ProjectID;
+import org.jboss.labs.core.model.auth.Service;
 import org.jboss.labs.core.model.auth.ServiceRole;
 import org.jboss.labs.core.model.auth.UserRole;
 import org.jboss.labs.exception.auth.BindingAlreadyExistsException;
@@ -104,7 +106,8 @@
 	 *             If user role doesn't exist
 	 * @throws BindingAlreadyExistsException
 	 *             If binding already exists
-	 * @throws NoSuchCustomIDException If custom id doesn't exist
+	 * @throws NoSuchCustomIDException
+	 *             If custom id doesn't exist
 	 * @see org.jboss.labs.core.model.auth.CustomID
 	 * @see org.jboss.labs.auth.custom.CustomIDProvider
 	 */
@@ -217,7 +220,7 @@
 	 *            User role
 	 * @param projectId
 	 *            Project id
-	 * @throws NoSuchUserRoleException 
+	 * @throws NoSuchUserRoleException
 	 */
 	public <E extends Enum<E>> void removeBindingWithProject(E serviceRole,
 			UserRole role, String projectId) throws NoSuchUserRoleException;
@@ -231,10 +234,14 @@
 	 *            User role
 	 * @param customId
 	 *            custom id
-	 * @throws NoSuchUserRoleException 
+	 * @throws NoSuchUserRoleException
+	 *             If user role doesn't exist
+	 * @throws NoSuchCustomIDException
+	 *             If custom id doesn't exist
 	 */
 	public <E extends Enum<E>> void removeBindingWithId(E serviceRole,
-			UserRole role, CustomID customId) throws NoSuchUserRoleException;
+			UserRole role, CustomID customId) throws NoSuchUserRoleException,
+			NoSuchCustomIDException;
 
 	/**
 	 * Removes global binding
@@ -243,13 +250,35 @@
 	 *            Service role
 	 * @param role
 	 *            User role
-	 * @throws NoSuchUserRoleException 
+	 * @throws NoSuchUserRoleException
 	 */
 	public <E extends Enum<E>> void removeBindingGlobal(E serviceRole,
 			UserRole role) throws NoSuchUserRoleException;
-	
-	/** Removes all bindings bound to giver user role
-	 * @param role User role
+
+	/**
+	 * Removes all bindings bound to giver user role
+	 * 
+	 * @param role
+	 *            User role
 	 */
 	public void removeAllBindingsForUserRole(UserRole role);
+
+	/**
+	 * Removes all bindings for given authid
+	 * 
+	 * @param id
+	 *            Auth id
+	 */
+	public void removeAllBindingsForAuthID(AuthID authId);
+
+	/**
+	 * Returns service object from given serviceRole. Service will get created
+	 * if it doesn't exist.
+	 * 
+	 * @param <E>
+	 * @param serviceRoleList
+	 *            Service role
+	 * @return Service object
+	 */
+	public <E extends Enum<E>> Service getService(E... serviceRoleList);
 }

Deleted: labs/jbosslabs/labs-3.0-build/core/core-api/src/main/java/org/jboss/labs/auth/custom/CustomIDHandler.java
===================================================================
--- labs/jbosslabs/labs-3.0-build/core/core-api/src/main/java/org/jboss/labs/auth/custom/CustomIDHandler.java	2008-03-04 10:07:04 UTC (rev 18671)
+++ labs/jbosslabs/labs-3.0-build/core/core-api/src/main/java/org/jboss/labs/auth/custom/CustomIDHandler.java	2008-03-04 10:57:49 UTC (rev 18672)
@@ -1,43 +0,0 @@
-/*
- * JBoss Labs. http://labs.jboss.com/jbosslabs
- * 
- * Copyright © 2008  Red Hat Middleware, LLC. All rights reserved.
- * 
- * This copyrighted material is made available to anyone wishing to use,
- * modify, copy, or redistribute it subject to the terms and conditions
- * of the GNU Lesser General Public License, v. 2.1.
- * 
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT A WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
- * 
- * You should have received a copy of the GNU Lesser General Public
- * License, v.2.1 along with this distribution; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301, USA.
- * 
- * Red Hat Author(s): Bob McWhirter, Przemyslaw Dej, Ryszard Kozmik, 
- * 	Tomasz Szymanski, Adam Warski, Pawel Wrzeszcz
- */
-/**
- * 
- */
-package org.jboss.labs.auth.custom;
-
-import org.jboss.labs.core.model.auth.CustomID;
-import org.jboss.labs.exception.auth.NoSuchCustomIDException;
-
-/**
- * @author tomaszszymanski
- *
- */
-public interface CustomIDHandler {
-
-	/**
-	 * @param newIds
-	 */
-	public void addCustomIDs(CustomIDProvider provider, CustomID... newIds);
-
-	public void removeCustomIDs(CustomIDProvider provider, CustomID... ids) throws NoSuchCustomIDException;
-}

Modified: labs/jbosslabs/labs-3.0-build/core/core-api/src/main/java/org/jboss/labs/auth/custom/CustomIDProvider.java
===================================================================
--- labs/jbosslabs/labs-3.0-build/core/core-api/src/main/java/org/jboss/labs/auth/custom/CustomIDProvider.java	2008-03-04 10:07:04 UTC (rev 18671)
+++ labs/jbosslabs/labs-3.0-build/core/core-api/src/main/java/org/jboss/labs/auth/custom/CustomIDProvider.java	2008-03-04 10:57:49 UTC (rev 18672)
@@ -28,6 +28,8 @@
 import org.jboss.labs.core.model.auth.CustomID;
 import org.jboss.labs.exception.auth.NoSuchCustomIDException;
 
+import com.google.inject.Inject;
+
 /**
  * Base for providing custom IDs from a service
  * 
@@ -40,7 +42,8 @@
  */
 public abstract class CustomIDProvider {
 
-	private Set<CustomIDHandler> handlers = new HashSet<CustomIDHandler>();
+	@Inject
+	private CustomIDService customIDService;
 
 	/**
 	 * @return Returns set of customIDs available in this project
@@ -61,9 +64,8 @@
 		// execute method that user can place same logic to
 		addCustomIDsOps(newIds);
 
-		for (CustomIDHandler handler : handlers) {
-			handler.addCustomIDs(this, newIds);
-		}
+		customIDService.addCustomIDs(this, newIds);
+
 	}
 
 	/**
@@ -82,15 +84,16 @@
 	 * 
 	 * @param ids
 	 *            IDs that are no longer available.
-	 * @throws NoSuchCustomIDException If customId doesn't exist
+	 * @throws NoSuchCustomIDException
+	 *             If customId doesn't exist
 	 */
-	public final void removeCustomIDs(CustomID... ids) throws NoSuchCustomIDException {
+	public final void removeCustomIDs(CustomID... ids)
+			throws NoSuchCustomIDException {
 		// execute logic method
 		removeCustomIDsOps(ids);
 
-		for (CustomIDHandler handler : handlers) {
-			handler.removeCustomIDs(this, ids);
-		}
+		customIDService.removeCustomIDs(this, ids);
+
 	}
 
 	/**
@@ -104,24 +107,6 @@
 	}
 
 	/**
-	 * Adds a handler that's executed when addCustomIDs is executed.
-	 * 
-	 * @param handler
-	 */
-	public void addHandler(CustomIDHandler handler) {
-		handlers.add(handler);
-	}
-
-	/**
-	 * Removes a handler that's executed when addCustomIDs is executed.
-	 * 
-	 * @param handler
-	 */
-	public void removeHandler(CustomIDHandler handler) {
-		handlers.remove(handler);
-	}
-
-	/**
 	 * If this method returns true - customIDs will be buffered and the designer
 	 * has to execute addCustomIDs every time new ID is created. If it returns
 	 * false, getCustomIDs will be executed every time.

Copied: labs/jbosslabs/labs-3.0-build/core/core-api/src/main/java/org/jboss/labs/auth/custom/CustomIDService.java (from rev 18667, labs/jbosslabs/labs-3.0-build/services/credential/src/main/java/org/jboss/labs/auth/CustomIDService.java)
===================================================================
--- labs/jbosslabs/labs-3.0-build/core/core-api/src/main/java/org/jboss/labs/auth/custom/CustomIDService.java	                        (rev 0)
+++ labs/jbosslabs/labs-3.0-build/core/core-api/src/main/java/org/jboss/labs/auth/custom/CustomIDService.java	2008-03-04 10:57:49 UTC (rev 18672)
@@ -0,0 +1,20 @@
+package org.jboss.labs.auth.custom;
+
+import java.util.Set;
+
+import javax.ejb.Local;
+
+import org.jboss.labs.core.model.auth.CustomID;
+import org.jboss.labs.core.model.auth.Service;
+import org.jboss.labs.exception.auth.NoSuchCustomIDException;
+
+ at Local
+public interface CustomIDService{
+	public void registerCustomIDProvider(CustomIDProvider provider, Service service);
+	
+	public Set<CustomID> getCustomIDs(Service service);
+	
+	public void addCustomIDs(CustomIDProvider provider, CustomID... newIds);
+
+	public void removeCustomIDs(CustomIDProvider provider, CustomID... ids) throws NoSuchCustomIDException;
+}

Modified: labs/jbosslabs/labs-3.0-build/core/core-api/src/main/java/org/jboss/labs/injection/LabsGuiceModule.java
===================================================================
--- labs/jbosslabs/labs-3.0-build/core/core-api/src/main/java/org/jboss/labs/injection/LabsGuiceModule.java	2008-03-04 10:07:04 UTC (rev 18671)
+++ labs/jbosslabs/labs-3.0-build/core/core-api/src/main/java/org/jboss/labs/injection/LabsGuiceModule.java	2008-03-04 10:57:49 UTC (rev 18672)
@@ -27,6 +27,7 @@
 import org.jboss.labs.auth.AuthenticationService;
 import org.jboss.labs.auth.UserService;
 import org.jboss.labs.auth.RoleBinderService;
+import org.jboss.labs.auth.custom.CustomIDService;
 import org.jboss.labs.core.service.ProjectService;
 import org.jboss.labs.core.service.LicenseService;
 import org.jboss.labs.foo.FooService;
@@ -34,12 +35,22 @@
 public class LabsGuiceModule implements Module {
 
 	public void configure(Binder binder) {
-		binder.bind(FooService.class).toProvider(new JNDIProvider<FooService>("FooServiceImpl/local"));
-		binder.bind(RoleBinderService.class).toProvider( new JNDIProvider<RoleBinderService>("RoleBinderServiceImpl/local"));
-		binder.bind(AuthenticationService.class).toProvider(new JNDIProvider<AuthenticationService>("AuthenticationServiceImpl/local"));
-	    binder.bind(ProjectService.class).toProvider(new JNDIProvider<ProjectService>("ProjectService/local"));
-	    binder.bind(UserService.class).toProvider(new JNDIProvider<UserService>("UserServiceImpl/local"));
-        binder.bind(LicenseService.class).toProvider(new JNDIProvider<LicenseService>("LicenseService/local"));
-    }
+		binder.bind(FooService.class).toProvider(
+				new JNDIProvider<FooService>("FooServiceImpl/local"));
+		binder.bind(RoleBinderService.class).toProvider(
+				new JNDIProvider<RoleBinderService>(
+						"RoleBinderServiceImpl/local"));
+		binder.bind(AuthenticationService.class).toProvider(
+				new JNDIProvider<AuthenticationService>(
+						"AuthenticationServiceImpl/local"));
+		binder.bind(ProjectService.class).toProvider(
+				new JNDIProvider<ProjectService>("ProjectService/local"));
+		binder.bind(UserService.class).toProvider(
+				new JNDIProvider<UserService>("UserServiceImpl/local"));
+		binder.bind(LicenseService.class).toProvider(
+				new JNDIProvider<LicenseService>("LicenseService/local"));
+		binder.bind(CustomIDService.class).toProvider(
+				new JNDIProvider<CustomIDService>("CustomIDServiceImpl/local"));
+	}
 
 }

Modified: labs/jbosslabs/labs-3.0-build/services/credential/src/main/java/org/jboss/labs/auth/AuthHelper.java
===================================================================
--- labs/jbosslabs/labs-3.0-build/services/credential/src/main/java/org/jboss/labs/auth/AuthHelper.java	2008-03-04 10:07:04 UTC (rev 18671)
+++ labs/jbosslabs/labs-3.0-build/services/credential/src/main/java/org/jboss/labs/auth/AuthHelper.java	2008-03-04 10:57:49 UTC (rev 18672)
@@ -34,7 +34,7 @@
 	
 	private final static Logger log = Logger.getLogger(AuthHelper.class);
 	
-	public static final String GLOBAL_PROJECT = "Labs::Global::Project";
+	public static final String GLOBAL_PROJECT = "Labs::Global::Superuser::Project";
 	
 	public static final String COMMON_ROLE_SUFIX = "_USERS";
 	

Deleted: labs/jbosslabs/labs-3.0-build/services/credential/src/main/java/org/jboss/labs/auth/CustomIDService.java
===================================================================
--- labs/jbosslabs/labs-3.0-build/services/credential/src/main/java/org/jboss/labs/auth/CustomIDService.java	2008-03-04 10:07:04 UTC (rev 18671)
+++ labs/jbosslabs/labs-3.0-build/services/credential/src/main/java/org/jboss/labs/auth/CustomIDService.java	2008-03-04 10:57:49 UTC (rev 18672)
@@ -1,15 +0,0 @@
-package org.jboss.labs.auth;
-
-import java.util.Set;
-
-import javax.ejb.Local;
-
-import org.jboss.labs.auth.custom.CustomIDProvider;
-import org.jboss.labs.core.model.auth.CustomID;
-
- at Local
-public interface CustomIDService{
-	public void registerCustomIDProvider(CustomIDProvider provider, String serviceID);
-	
-	public Set<CustomID> getCustomIDs(String serviceID);
-}

Modified: labs/jbosslabs/labs-3.0-build/services/credential/src/main/java/org/jboss/labs/auth/impl/AuthenticationServiceImpl.java
===================================================================
--- labs/jbosslabs/labs-3.0-build/services/credential/src/main/java/org/jboss/labs/auth/impl/AuthenticationServiceImpl.java	2008-03-04 10:07:04 UTC (rev 18671)
+++ labs/jbosslabs/labs-3.0-build/services/credential/src/main/java/org/jboss/labs/auth/impl/AuthenticationServiceImpl.java	2008-03-04 10:57:49 UTC (rev 18672)
@@ -34,6 +34,7 @@
 import org.jboss.labs.auth.AuthenticationService;
 import org.jboss.labs.auth.RoleBinderService;
 import org.jboss.labs.auth.UserService;
+import org.jboss.labs.auth.custom.CustomIDService;
 import org.jboss.labs.core.model.auth.AuthID;
 import org.jboss.labs.core.model.auth.CustomID;
 import org.jboss.labs.core.model.auth.ProjectID;
@@ -41,6 +42,7 @@
 import org.jboss.labs.core.model.auth.ServiceRole;
 import org.jboss.labs.core.model.auth.User;
 import org.jboss.labs.core.model.auth.UserRole;
+import org.jboss.labs.exception.auth.NoSuchCustomIDException;
 import org.jboss.labs.exception.auth.NoSuchUserException;
 
 @Stateless
@@ -51,6 +53,9 @@
 
 	@EJB
 	private RoleBinderService binderService;
+	
+	@EJB
+	private CustomIDService customIDService;
 
 	@PersistenceContext(name = "auth_model")
 	private EntityManager manager;
@@ -65,9 +70,16 @@
 	}
 
 	public <E extends Enum<E>> boolean isAllowedCustom(Principal user,
-			E serviceRole, CustomID customId) throws NoSuchUserException {
+			E serviceRole, CustomID customId) throws NoSuchUserException, NoSuchCustomIDException {
+		// refresh list of custom ids
+		customIDService.getCustomIDs(binderService.getService(serviceRole));
+		
 		User userEn = userService.getUserByLogin(user.getName());
 		ServiceRole sRole = binderService.getServiceRole(serviceRole);
+		
+		if (manager.find(CustomID.class, customId.getAuthId()) == null) {
+			throw new NoSuchCustomIDException("CustomID with id "+customId.getAuthId()+" doesn't exist");
+		}
 
 		return isAllowed(userEn, sRole, customId);
 	}

Modified: labs/jbosslabs/labs-3.0-build/services/credential/src/main/java/org/jboss/labs/auth/impl/CustomIDServiceImpl.java
===================================================================
--- labs/jbosslabs/labs-3.0-build/services/credential/src/main/java/org/jboss/labs/auth/impl/CustomIDServiceImpl.java	2008-03-04 10:07:04 UTC (rev 18671)
+++ labs/jbosslabs/labs-3.0-build/services/credential/src/main/java/org/jboss/labs/auth/impl/CustomIDServiceImpl.java	2008-03-04 10:57:49 UTC (rev 18672)
@@ -1,29 +1,34 @@
 package org.jboss.labs.auth.impl;
 
 import java.util.Arrays;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 
+import javax.ejb.EJB;
 import javax.persistence.EntityManager;
 import javax.persistence.PersistenceContext;
 
 import org.apache.log4j.Logger;
-import org.jboss.annotation.ejb.Service;
-import org.jboss.labs.auth.CustomIDService;
-import org.jboss.labs.auth.custom.CustomIDHandler;
+import org.jboss.labs.auth.RoleBinderService;
+import org.jboss.labs.core.model.auth.Service;
 import org.jboss.labs.auth.custom.CustomIDProvider;
+import org.jboss.labs.auth.custom.CustomIDService;
 import org.jboss.labs.core.model.auth.CustomID;
+import org.jboss.labs.exception.LabsRuntimeException;
 import org.jboss.labs.exception.auth.NoSuchCustomIDException;
+import org.jboss.labs.injection.LabsInjection;
 
- at Service
-public class CustomIDServiceImpl implements CustomIDService, CustomIDHandler {
+ at org.jboss.annotation.ejb.Service
+public class CustomIDServiceImpl implements CustomIDService {
 
 	@PersistenceContext(name = "auth_model")
 	private EntityManager manager;
 
+	@EJB
+	private RoleBinderService binderService;
+
 	private static final Logger log = Logger
 			.getLogger(CustomIDServiceImpl.class);
 
@@ -31,10 +36,11 @@
 	private Map<CustomIDProvider, Set<CustomID>> customIDs = new HashMap<CustomIDProvider, Set<CustomID>>();
 
 	public void registerCustomIDProvider(CustomIDProvider provider,
-			String serviceID) {
-		providers.put(serviceID, provider);
-		provider.addHandler(this);
+			Service service) {
+		providers.put(service.getId(), provider);
 
+		LabsInjection.getInjector().injectMembers(provider);
+
 		if (provider.isBuffered()) {
 			customIDs.put(provider, new HashSet<CustomID>(provider
 					.getCustomIDs()));
@@ -58,8 +64,8 @@
 	 */
 	public void addCustomIDs(CustomIDProvider provider, CustomID... newIds) {
 		if (!provider.isBuffered()) {
-			log
-					.warn("You're using addCustomIDs method, even tho provider is not buffered ! Provider: "
+			throw new LabsRuntimeException(
+					"You're using addCustomIDs method, even tho provider is not buffered ! Provider: "
 							+ provider);
 		} else {
 			customIDs.get(provider).addAll(Arrays.asList(newIds));
@@ -77,19 +83,27 @@
 	 */
 	public void removeCustomIDs(CustomIDProvider provider, CustomID... ids)
 			throws NoSuchCustomIDException {
-		if (!provider.isBuffered()) {
-			log
-					.warn("You're using removeCustomIDs method, even tho provider is not buffered ! Provider: "
-							+ provider);
-		} else {
+
+		// if buffered remove from buffer
+		if (provider.isBuffered()) {
 			customIDs.get(provider).removeAll(Arrays.asList(ids));
+		}
+		
+		// remove from db
+		for (CustomID id : ids) {
+			if (manager.find(CustomID.class, id.getAuthId()) != null) {
+				CustomID idEn = manager.find(CustomID.class, id.getAuthId());
 
-			for (CustomID id : ids) {
-				if (manager.find(CustomID.class, id) != null) {
-					manager.remove(id);
-				}
+				// before removing entity, remove all bindings for it
+				binderService.removeAllBindingsForAuthID(idEn);
+
+				manager.remove(idEn);
+			} else {
+				throw new NoSuchCustomIDException("CustomID " + id
+						+ " dosen't exist");
 			}
 		}
+
 	}
 
 	/*
@@ -97,13 +111,19 @@
 	 * 
 	 * @see org.jboss.labs.auth.CustomIDService#getCustomIDs(java.lang.String)
 	 */
-	public Set<CustomID> getCustomIDs(String serviceID) {
-		CustomIDProvider provider = providers.get(serviceID);
+	public Set<CustomID> getCustomIDs(Service service) {
+		CustomIDProvider provider = providers.get(service.getId());
 
 		if (provider.isBuffered()) {
 			return new HashSet<CustomID>(customIDs.get(provider));
 		} else {
-			return new HashSet<CustomID>(provider.getCustomIDs());
+			Set<CustomID> cids = new HashSet<CustomID>(provider.getCustomIDs());
+			
+			for (CustomID id : cids) {
+				checkAndPersist(id);
+			}
+			
+			return cids;
 		}
 	}
 

Modified: labs/jbosslabs/labs-3.0-build/services/credential/src/main/java/org/jboss/labs/auth/impl/RoleBinderServiceImpl.java
===================================================================
--- labs/jbosslabs/labs-3.0-build/services/credential/src/main/java/org/jboss/labs/auth/impl/RoleBinderServiceImpl.java	2008-03-04 10:07:04 UTC (rev 18671)
+++ labs/jbosslabs/labs-3.0-build/services/credential/src/main/java/org/jboss/labs/auth/impl/RoleBinderServiceImpl.java	2008-03-04 10:57:49 UTC (rev 18672)
@@ -27,17 +27,17 @@
 import java.util.Set;
 import java.util.TreeSet;
 
-import javax.ejb.EJB;
 import javax.ejb.Stateless;
+import javax.interceptor.Interceptors;
 import javax.persistence.EntityManager;
 import javax.persistence.PersistenceContext;
 
 import org.apache.log4j.Logger;
 import org.jboss.labs.auth.AuthHelper;
-import org.jboss.labs.auth.CustomIDService;
 import org.jboss.labs.auth.RoleBinderService;
 import org.jboss.labs.auth.UserService;
 import org.jboss.labs.auth.custom.CustomIDProvider;
+import org.jboss.labs.auth.custom.CustomIDService;
 import org.jboss.labs.core.model.auth.AuthID;
 import org.jboss.labs.core.model.auth.Binding;
 import org.jboss.labs.core.model.auth.CustomID;
@@ -52,17 +52,21 @@
 import org.jboss.labs.exception.auth.NoSuchSuperUserException;
 import org.jboss.labs.exception.auth.NoSuchUserException;
 import org.jboss.labs.exception.auth.NoSuchUserRoleException;
+import org.jboss.labs.injection.ejb3.LabsInjectionInterceptor;
 
+import com.google.inject.Inject;
+
 @Stateless
+ at Interceptors( { LabsInjectionInterceptor.class })
 public class RoleBinderServiceImpl implements RoleBinderService {
 
 	@PersistenceContext(name = "auth_model")
 	private EntityManager manager;
 
-	@EJB
+	@Inject
 	private CustomIDService customIDService;
 
-	@EJB
+	@Inject
 	private UserService userService;
 
 	private final static Logger log = Logger
@@ -72,6 +76,9 @@
 			UserRole role, CustomID customId) throws NoSuchUserRoleException,
 			BindingAlreadyExistsException, NoSuchCustomIDException {
 
+		// get custom IDs - this will persist new ones
+		customIDService.getCustomIDs(getService(serviceRole));
+
 		CustomID cidEn = manager.find(CustomID.class, customId.getAuthId());
 
 		if (cidEn == null) {
@@ -175,8 +182,11 @@
 			E[] serviceRoleList, CustomIDProvider provider) {
 		exposeServiceRoles(serviceRoleList);
 
-		customIDService.registerCustomIDProvider(provider, AuthHelper
-				.getServiceID(serviceRoleList[0]));
+		customIDService.registerCustomIDProvider(provider,
+				getService(serviceRoleList));
+
+		// invoke to initialize
+		customIDService.getCustomIDs(getService(serviceRoleList));
 	}
 
 	public <E extends Enum<E>> void bindRolesGlobal(E serviceRole, UserRole role)
@@ -220,7 +230,7 @@
 		return serviceRoleEn;
 	}
 
-	protected <E extends Enum<E>> Service getService(E... serviceRoleList) {
+	public <E extends Enum<E>> Service getService(E... serviceRoleList) {
 		if (serviceRoleList.length == 0) {
 			throw new RuntimeException(
 					"Service role list has to have at least one role");
@@ -230,8 +240,7 @@
 				.getServiceID(serviceRoleList[0]));
 
 		if (service == null) {
-			service = new Service(serviceRoleList[0].getDeclaringClass()
-					.getName());
+			service = new Service(AuthHelper.getServiceID(serviceRoleList[0]));
 			service.init();
 		}
 
@@ -311,7 +320,18 @@
 	 *      org.jboss.labs.core.model.auth.CustomID)
 	 */
 	public <E extends Enum<E>> void removeBindingWithId(E serviceRole,
-			UserRole role, CustomID customId) throws NoSuchUserRoleException {
+			UserRole role, CustomID customId) throws NoSuchUserRoleException,
+			NoSuchCustomIDException {
+		// get custom IDs - this will persist new ones
+		customIDService.getCustomIDs(getService(serviceRole));
+
+		CustomID cidEn = manager.find(CustomID.class, customId.getAuthId());
+
+		if (cidEn == null) {
+			throw new NoSuchCustomIDException("CustomID with an id: "
+					+ customId.getAuthId() + " doesn't exist");
+		}
+
 		removeBindingWith(serviceRole, role, customId);
 	}
 
@@ -353,4 +373,22 @@
 		}
 	}
 
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see org.jboss.labs.auth.RoleBinderService#removeAllBindingsForAuthID(org.jboss.labs.core.model.auth.CustomID)
+	 */
+	@SuppressWarnings("unchecked")
+	public void removeAllBindingsForAuthID(AuthID authId) {
+		List<Binding> bl = manager.createQuery(
+				"SELECT b from Binding b WHERE b.authId = :authId")
+				.setParameter("authId", authId).getResultList();
+
+		if (bl.size() > 0) {
+			for (Binding b : bl) {
+				manager.remove(b);
+			}
+		}
+	}
+
 }

Modified: labs/jbosslabs/labs-3.0-build/services/credential/src/main/java/org/jboss/labs/auth/impl/UserServiceImpl.java
===================================================================
--- labs/jbosslabs/labs-3.0-build/services/credential/src/main/java/org/jboss/labs/auth/impl/UserServiceImpl.java	2008-03-04 10:07:04 UTC (rev 18671)
+++ labs/jbosslabs/labs-3.0-build/services/credential/src/main/java/org/jboss/labs/auth/impl/UserServiceImpl.java	2008-03-04 10:57:49 UTC (rev 18672)
@@ -25,13 +25,12 @@
 
 import java.security.Principal;
 import java.util.HashSet;
-import java.util.List;
 import java.util.Set;
 import java.util.TreeSet;
 
+import javax.ejb.EJB;
 import javax.ejb.Stateless;
 import javax.interceptor.AroundInvoke;
-import javax.interceptor.Interceptors;
 import javax.interceptor.InvocationContext;
 import javax.persistence.EntityManager;
 import javax.persistence.PersistenceContext;
@@ -49,18 +48,14 @@
 import org.jboss.labs.exception.auth.NoSuchUserRoleException;
 import org.jboss.labs.exception.auth.RoleExistsException;
 import org.jboss.labs.exception.auth.UserExistsException;
-import org.jboss.labs.injection.ejb3.LabsInjectionInterceptor;
 
-import com.google.inject.Inject;
-
 @Stateless
- at Interceptors( { LabsInjectionInterceptor.class })
 public class UserServiceImpl implements UserService {
 
 	@PersistenceContext(name = "auth_model")
 	private EntityManager manager;
 
-	@Inject
+	@EJB
 	private RoleBinderService binderService;
 
 	private final static Logger log = Logger.getLogger(UserServiceImpl.class);

Modified: labs/jbosslabs/labs-3.0-build/services/credential/src/main/resources/META-INF/persistence.xml
===================================================================
--- labs/jbosslabs/labs-3.0-build/services/credential/src/main/resources/META-INF/persistence.xml	2008-03-04 10:07:04 UTC (rev 18671)
+++ labs/jbosslabs/labs-3.0-build/services/credential/src/main/resources/META-INF/persistence.xml	2008-03-04 10:57:49 UTC (rev 18672)
@@ -5,7 +5,7 @@
                 <properties>
                         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
                         <!-- property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" /-->
-                        <property name="hibernate.hbm2ddl.auto" value="create-drop" />
+                        <property name="hibernate.hbm2ddl.auto" value="update" />
                 </properties>
                 <class>org.jboss.labs.core.model.auth.AuthID</class>
                 <class>org.jboss.labs.core.model.auth.Binding</class>

Modified: labs/jbosslabs/labs-3.0-build/views/labs-test/src/main/java/org/jboss/labs/test/credential/RoleBinderAndAuthTest.java
===================================================================
--- labs/jbosslabs/labs-3.0-build/views/labs-test/src/main/java/org/jboss/labs/test/credential/RoleBinderAndAuthTest.java	2008-03-04 10:07:04 UTC (rev 18671)
+++ labs/jbosslabs/labs-3.0-build/views/labs-test/src/main/java/org/jboss/labs/test/credential/RoleBinderAndAuthTest.java	2008-03-04 10:57:49 UTC (rev 18672)
@@ -4,11 +4,13 @@
 package org.jboss.labs.test.credential;
 
 import java.util.Collections;
+import java.util.HashSet;
 import java.util.Set;
 
 import org.jboss.labs.auth.custom.CustomIDProvider;
 import org.jboss.labs.core.model.auth.*;
 import org.jboss.labs.exception.LabsException;
+import org.jboss.labs.exception.LabsRuntimeException;
 import org.jboss.labs.exception.auth.BindingAlreadyExistsException;
 import org.jboss.labs.exception.auth.NoSuchCustomIDException;
 import org.jboss.labs.exception.auth.NoSuchUserException;
@@ -153,7 +155,7 @@
 
 		// bind with customID
 		binderService.bindRolesWithId(TestRoles.KOPYTKO, ur, testCustomID1);
-		
+
 		// bind with customID that wasn't registered
 		try {
 			binderService.bindRolesWithId(TestRoles.KOPYTKO, ur, testCustomID2);
@@ -161,24 +163,159 @@
 		} catch (NoSuchCustomIDException e) {
 			// this was expected
 		}
-		
+
 		// check if suer is allowed
-		assertTrue(authService.isAllowedCustom(u1, TestRoles.KOPYTKO, testCustomID1));
-		
+		assertTrue(authService.isAllowedCustom(u1, TestRoles.KOPYTKO,
+				testCustomID1));
+
 		// user 2 shoulnd't be allowed
-		assertFalse(authService.isAllowedCustom(u2, TestRoles.KOPYTKO, testCustomID1));
-		
+		assertFalse(authService.isAllowedCustom(u2, TestRoles.KOPYTKO,
+				testCustomID1));
+
+		// try checking for nonexisting customid
+		try {
+			assertFalse(authService.isAllowedCustom(u1, TestRoles.KOPYTKO,
+					testCustomID2));
+			fail("Successfuly checked permissions for nonexisting custom id");
+		} catch (NoSuchCustomIDException e) {
+			// that's fine
+		}
+
 		// revoke customid1
 		binderService.removeBindingWithId(TestRoles.KOPYTKO, ur, testCustomID1);
-		
+
 		// now user shouldn't be allowed
-		assertFalse(authService.isAllowedCustom(u1, TestRoles.KOPYTKO, testCustomID1));
-		
+		assertFalse(authService.isAllowedCustom(u1, TestRoles.KOPYTKO,
+				testCustomID1));
+
 		// give it again
 		binderService.bindRolesWithId(TestRoles.KOPYTKO, ur, testCustomID1);
-		
-		//remove custom id in use
+
+		// remove custom id in use
 		providerBuf.removeCustomIDs(testCustomID1);
-		
+
+		// it should automatically remove bindings and we'll get exception
+		// thrown
+		try {
+			assertFalse(authService.isAllowedCustom(u1, TestRoles.KOPYTKO,
+					testCustomID1));
+			fail("Successfuly checked permissions for nonexisting custom id");
+		} catch (NoSuchCustomIDException e) {
+			// that's fine
+		}
 	}
+
+	public void testNonBufferedCustomIDProviders()
+			throws NoSuchUserRoleException, BindingAlreadyExistsException,
+			NoSuchUserException, RoleExistsException, UserExistsException,
+			NoSuchCustomIDException {
+
+		final Set<CustomID> cidSet = new HashSet<CustomID>();
+
+		CustomID testCustomID1 = new CustomID("TestCustomID1",
+				"This is test custom id");
+		CustomID testCustomID2 = new CustomID("TestCustomID2",
+				"This is test custom id");
+		CustomID testCustomID3 = new CustomID("TestCustomID3",
+				"This is test custom id");
+
+		// add only first two
+		cidSet.add(testCustomID1);
+		cidSet.add(testCustomID2);
+
+		// create provider non-buffered
+		CustomIDProvider providerNonBuf = new CustomIDProvider() {
+
+			@Override
+			public boolean isBuffered() {
+				return false;
+			}
+
+			@Override
+			public Set<CustomID> getCustomIDs() {
+
+				return cidSet;
+			}
+
+		};
+
+		binderService.exposeServiceRolesAndRegisterCustomIDProvider(TestRoles
+				.values(), providerNonBuf);
+
+		// create users and role
+		User u1 = getNewUser();
+		User u2 = getNewUser();
+
+		UserRole ur = getNewUserRole();
+
+		// give one user a role
+		userService.bindRoleToUser(u1, ur);
+
+		// bind with customID
+		binderService.bindRolesWithId(TestRoles.KOPYTKO, ur, testCustomID1);
+
+		// bind with customID that wasn't registered
+		try {
+			binderService.bindRolesWithId(TestRoles.KOPYTKO, ur, testCustomID3);
+			fail("Bound with an non-existing custom id");
+		} catch (NoSuchCustomIDException e) {
+			// this was expected
+		}
+
+		// check if suer is allowed
+		assertTrue(authService.isAllowedCustom(u1, TestRoles.KOPYTKO,
+				testCustomID1));
+
+		// user 2 shoulnd't be allowed
+		assertFalse(authService.isAllowedCustom(u2, TestRoles.KOPYTKO,
+				testCustomID1));
+
+		// revoke customid1
+		binderService.removeBindingWithId(TestRoles.KOPYTKO, ur, testCustomID1);
+
+		// now user shouldn't be allowed
+		assertFalse(authService.isAllowedCustom(u1, TestRoles.KOPYTKO,
+				testCustomID1));
+
+		// give it again
+		binderService.bindRolesWithId(TestRoles.KOPYTKO, ur, testCustomID1);
+
+		// remove custom id in use
+		providerNonBuf.removeCustomIDs(testCustomID1);
+
+		// it should automatically remove bindings, but getCustomIDs will return
+		// testCustomID1 again, so this shouldn't throw NoSuchCustomID exception
+		assertFalse(authService.isAllowedCustom(u1, TestRoles.KOPYTKO,
+				testCustomID1));
+
+		// now try adding custom id
+		try {
+			providerNonBuf.addCustomIDs(testCustomID3);
+			// fail - this shouldn't work
+			fail("Successfuly invoked addCustomID() on non-buffered provider !");
+		} catch (Throwable e) {
+			// it's ok. using addCustomID with nonbuffered provider is forbidden
+		}
+
+		// try checking for nonexisting customid
+		try {
+			assertFalse(authService.isAllowedCustom(u1, TestRoles.KOPYTKO,
+					testCustomID3));
+			fail("Successfuly checked permissions for nonexisting custom id");
+		} catch (NoSuchCustomIDException e) {
+			// that's fine
+		}
+
+		// add customid3 to set
+		cidSet.add(testCustomID3);
+
+		// now above will get persisted, but we added no bindings. just check
+		// permissions
+		assertFalse(authService.isAllowedCustom(u1, TestRoles.KOPYTKO,
+				testCustomID3));
+
+		// removde customIds
+		providerNonBuf.removeCustomIDs(testCustomID1, testCustomID2,
+				testCustomID3);
+	}
 }




More information about the jboss-svn-commits mailing list