[jboss-svn-commits] JBL Code SVN: r18316 - in labs/jbosslabs/labs-3.0-build: core/core-model/src/main/java/org/jboss/labs/exception and 2 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Feb 5 11:20:53 EST 2008


Author: szimano
Date: 2008-02-05 11:20:53 -0500 (Tue, 05 Feb 2008)
New Revision: 18316

Added:
   labs/jbosslabs/labs-3.0-build/core/core-model/src/main/java/org/jboss/labs/exception/LabsRuntimeException.java
   labs/jbosslabs/labs-3.0-build/core/core-model/src/main/java/org/jboss/labs/exception/auth/BindingAlreadyExistsException.java
   labs/jbosslabs/labs-3.0-build/core/core-model/src/main/java/org/jboss/labs/exception/auth/NoSuchServiceRoleException.java
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/UserService.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
Log:
role binder implementation JBLAB-901

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-02-05 16:15:47 UTC (rev 18315)
+++ labs/jbosslabs/labs-3.0-build/core/core-api/src/main/java/org/jboss/labs/auth/RoleBinderService.java	2008-02-05 16:20:53 UTC (rev 18316)
@@ -25,8 +25,11 @@
 import java.security.Principal;
 
 import org.jboss.labs.auth.custom.CustomIDProvider;
+import org.jboss.labs.core.model.auth.CustomID;
 import org.jboss.labs.core.model.auth.ServiceRole;
 import org.jboss.labs.core.model.auth.UserRole;
+import org.jboss.labs.exception.auth.BindingAlreadyExistsException;
+import org.jboss.labs.exception.auth.NoSuchServiceRoleException;
 import org.jboss.labs.exception.auth.NoSuchUserRoleException;
 
 /**
@@ -73,9 +76,11 @@
 	 *            User role
 	 * @param projectId
 	 *            Project id
+	 * @throws NoSuchUserRoleException 
+	 * @throws BindingAlreadyExistsException
 	 */
 	public <E extends Enum<E>> void bindRolesWithProject(E serviceRole,
-			UserRole role, String projectId) throws NoSuchUserRoleException;
+			UserRole role, String projectId) throws NoSuchUserRoleException, BindingAlreadyExistsException;
 
 	/**
 	 * Binds given service role with a user role for a given custom id.
@@ -88,11 +93,13 @@
 	 *            User role
 	 * @param customId
 	 *            Custom id
+	 * @throws NoSuchUserRoleException If user role doesn't exist
+	 * @throws BindingAlreadyExistsException If binding already exists
 	 * @see org.jboss.labs.core.model.auth.CustomID
 	 * @see org.jboss.labs.auth.custom.CustomIDProvider
 	 */
 	public <E extends Enum<E>> void bindRolesWithId(E serviceRole,
-			UserRole role, String customId) throws NoSuchUserRoleException;
+			UserRole role, CustomID customId) throws NoSuchUserRoleException, BindingAlreadyExistsException;
 
 	/**
 	 * Binds service role to a user role globally. This means that members of

Modified: labs/jbosslabs/labs-3.0-build/core/core-api/src/main/java/org/jboss/labs/auth/UserService.java
===================================================================
--- labs/jbosslabs/labs-3.0-build/core/core-api/src/main/java/org/jboss/labs/auth/UserService.java	2008-02-05 16:15:47 UTC (rev 18315)
+++ labs/jbosslabs/labs-3.0-build/core/core-api/src/main/java/org/jboss/labs/auth/UserService.java	2008-02-05 16:20:53 UTC (rev 18316)
@@ -110,4 +110,18 @@
 	 * @throws NoSuchUserRoleException If role doesn't exist
 	 */
 	public void removeUserRole(UserRole role) throws NoSuchUserRoleException;
+	
+	/** Get's user role by checking for the actual existence of UserRole in database
+	 * @param role Role to get (looks on id)
+	 * @return Role if it exists
+	 * @throws NoSuchUserRoleException if the role doesn't exist
+	 */
+	public UserRole getUserRole(UserRole role) throws NoSuchUserRoleException;
+	
+	/** Get's role by it's ID
+	 * @param roleID Id of a role
+	 * @return Role if it exists
+	 * @throws NoSuchUserRoleException if teh role doesn't exist
+	 */
+	public UserRole getUserRole(String roleID) throws NoSuchUserRoleException;
 }

Added: labs/jbosslabs/labs-3.0-build/core/core-model/src/main/java/org/jboss/labs/exception/LabsRuntimeException.java
===================================================================
--- labs/jbosslabs/labs-3.0-build/core/core-model/src/main/java/org/jboss/labs/exception/LabsRuntimeException.java	                        (rev 0)
+++ labs/jbosslabs/labs-3.0-build/core/core-model/src/main/java/org/jboss/labs/exception/LabsRuntimeException.java	2008-02-05 16:20:53 UTC (rev 18316)
@@ -0,0 +1,49 @@
+/*
+ * 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.exception;
+
+/**
+ * @author tomaszszymanski
+ *
+ */
+public class LabsRuntimeException extends RuntimeException {
+	/**
+	 * 
+	 */
+	private static final long serialVersionUID = 1L;
+
+	public LabsRuntimeException() {
+		super();
+	}
+	
+	public LabsRuntimeException(String msg) {
+		super(msg);
+	}
+	
+	public LabsRuntimeException(Throwable t) {
+		super(t);
+	}
+}

Added: labs/jbosslabs/labs-3.0-build/core/core-model/src/main/java/org/jboss/labs/exception/auth/BindingAlreadyExistsException.java
===================================================================
--- labs/jbosslabs/labs-3.0-build/core/core-model/src/main/java/org/jboss/labs/exception/auth/BindingAlreadyExistsException.java	                        (rev 0)
+++ labs/jbosslabs/labs-3.0-build/core/core-model/src/main/java/org/jboss/labs/exception/auth/BindingAlreadyExistsException.java	2008-02-05 16:20:53 UTC (rev 18316)
@@ -0,0 +1,44 @@
+/*
+ * 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.exception.auth;
+
+import org.jboss.labs.exception.LabsException;
+
+/**
+ * @author tomaszszymanski
+ *
+ */
+public class BindingAlreadyExistsException extends LabsException {
+
+	/**
+	 * 
+	 */
+	private static final long serialVersionUID = 1L;
+
+	public BindingAlreadyExistsException(String msg) {
+		super(msg);
+	}
+}

Added: labs/jbosslabs/labs-3.0-build/core/core-model/src/main/java/org/jboss/labs/exception/auth/NoSuchServiceRoleException.java
===================================================================
--- labs/jbosslabs/labs-3.0-build/core/core-model/src/main/java/org/jboss/labs/exception/auth/NoSuchServiceRoleException.java	                        (rev 0)
+++ labs/jbosslabs/labs-3.0-build/core/core-model/src/main/java/org/jboss/labs/exception/auth/NoSuchServiceRoleException.java	2008-02-05 16:20:53 UTC (rev 18316)
@@ -0,0 +1,44 @@
+/*
+ * 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.exception.auth;
+
+import org.jboss.labs.exception.LabsRuntimeException;
+
+/**
+ * @author tomaszszymanski
+ *
+ */
+public class NoSuchServiceRoleException extends LabsRuntimeException {
+
+	/**
+	 * 
+	 */
+	private static final long serialVersionUID = 1L;
+
+	public NoSuchServiceRoleException(String msg){
+		super(msg);
+	}
+}

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-02-05 16:15:47 UTC (rev 18315)
+++ labs/jbosslabs/labs-3.0-build/services/credential/src/main/java/org/jboss/labs/auth/impl/RoleBinderServiceImpl.java	2008-02-05 16:20:53 UTC (rev 18316)
@@ -37,65 +37,104 @@
 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.core.model.auth.AuthID;
+import org.jboss.labs.core.model.auth.Binding;
+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;
+import org.jboss.labs.exception.auth.NoSuchServiceRoleException;
+import org.jboss.labs.exception.auth.NoSuchUserRoleException;
 
 @Stateless
 @Local
 public class RoleBinderServiceImpl implements RoleBinderService {
-	
-	@PersistenceContext(name="auth_model")
+
+	@PersistenceContext(name = "auth_model")
 	private EntityManager manager;
-	
+
 	@EJB
 	private CustomIDService customIDService;
-	
-	private final static Logger log = Logger.getLogger(RoleBinderServiceImpl.class);
 
+	@EJB
+	private UserService userService;
+
+	private final static Logger log = Logger
+			.getLogger(RoleBinderServiceImpl.class);
+
 	@PostConstruct
 	public void init() {
 		log.info(customIDService);
 	}
-	
-	public <E extends Enum<E>> void bindRolesWithId(E serviceRole, UserRole role,
-			String customId) {
-		// TODO Auto-generated method stub
-		
+
+	public <E extends Enum<E>> void bindRolesWithId(E serviceRole,
+			UserRole role, CustomID customId) throws NoSuchUserRoleException, BindingAlreadyExistsException {
+		bindRolesWith(serviceRole, role, customId);
+
 	}
 
 	public <E extends Enum<E>> void bindRolesWithProject(E serviceRole,
-			UserRole role, String projectId) {
-		ServiceRole sr = getServiceRole(serviceRole);
+			UserRole role, String projectId) throws NoSuchUserRoleException, BindingAlreadyExistsException {
+		
+		ProjectID pid = getProjectID(projectId);
+		
+		bindRolesWith(serviceRole, role, pid);
+		
 	}
+	
+	private <E extends Enum<E>> void bindRolesWith(E serviceRole,
+			UserRole role, AuthID authID) throws NoSuchUserRoleException, BindingAlreadyExistsException {
+		ServiceRole srEn = getServiceRole(serviceRole);
+		UserRole roleEn = userService.getUserRole(role);
+		
+		Service service = getService(serviceRole);
 
-	public <E extends Enum<E>> void exposeServiceRoles(E[] serviceRoleList) {		
+		if (manager
+				.createQuery(
+						"SELECT * from Binding b where b.authId = :id and b.userRole = :userRole and b.serviceRole = :serviceRole and b.service = :service")
+				.setParameter("id", authID).setParameter("userRole", roleEn)
+				.setParameter("serviceRole", srEn).setParameter("service",
+						service).getResultList().size() > 1) {
+			throw new BindingAlreadyExistsException("Binding for servicerole: "+serviceRole+", role: "+role+" and id: "+authID + " already exists");
+		}
+		
+		Binding b = new Binding();
+		b.setAuthId(authID); b.setService(service); b.setServiceRole(srEn); b.setUserRole(roleEn);
+
+		manager.persist(b);
+	}
+
+	public <E extends Enum<E>> void exposeServiceRoles(E[] serviceRoleList) {
 		Service service = getService(serviceRoleList);
-		
+
 		// get all current roles
-		Set<ServiceRole> existingRoles = new TreeSet<ServiceRole>(service.getServiceRoles());
-		
+		Set<ServiceRole> existingRoles = new TreeSet<ServiceRole>(service
+				.getServiceRoles());
+
 		for (E role : serviceRoleList) {
 			ServiceRole sr = service.getServiceRole(role);
-			
+
 			if (sr == null) {
-				sr = new ServiceRole(role.name());
+				sr = new ServiceRole(AuthHelper.getServiceID(role));
 				manager.persist(sr);
-				
+
 				service.addServiceRole(sr);
 			}
-			
+
 			// this role is still available - remove it from the previous list
 			existingRoles.remove(sr);
 		}
-		
+
 		// set not available roles to unavailable
 		for (ServiceRole sr : existingRoles) {
 			sr.setAvailable(false);
 			manager.merge(sr);
 		}
-		
+
 		// merge service with new roles
 		manager.merge(service);
 	}
@@ -103,44 +142,68 @@
 	public <E extends Enum<E>> void exposeServiceRolesAndRegisterCustomIDProvider(
 			E[] serviceRoleList, CustomIDProvider provider) {
 		exposeServiceRoles(serviceRoleList);
-		
-		customIDService.registerCustomIDProvider(provider, AuthHelper.getServiceID(serviceRoleList[0]));
+
+		customIDService.registerCustomIDProvider(provider, AuthHelper
+				.getServiceID(serviceRoleList[0]));
 	}
 
 	public <E extends Enum<E>> void bindRolesGlobal(E serviceRole, UserRole role) {
 		// TODO Auto-generated method stub
-		
+
 	}
 
 	public void grantUserProjectSuperrole(Principal user, String projectId) {
 		// TODO Auto-generated method stub
-		
+
 	}
 
 	public void grantUserSuperrole(Principal user) {
 		// TODO Auto-generated method stub
-		
+
 	}
 
-	public <E extends Enum<E>> ServiceRole getServiceRole(E serviceRole) {
-		// TODO Auto-generated method stub
-		return null;
+	public <E extends Enum<E>> ServiceRole getServiceRole(E serviceRole)
+			throws NoSuchServiceRoleException {
+		ServiceRole serviceRoleEn = manager.find(ServiceRole.class, AuthHelper
+				.getServiceID(serviceRole));
+
+		if (serviceRoleEn == null) {
+			throw new NoSuchServiceRoleException(
+					"Service role exception "
+							+ serviceRole
+							+ " doesn't exist. Please make sure you've exposed service roles properly. Refer to Javadocs.");
+		}
+
+		return serviceRoleEn;
 	}
 
 	protected <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");
+			throw new RuntimeException(
+					"Service role list has to have at least one role");
 		}
-		
-		Service service = manager.find(Service.class, serviceRoleList[0].getDeclaringClass().getName());
-		
+
+		Service service = manager.find(Service.class, serviceRoleList[0]
+				.getDeclaringClass().getName());
+
 		if (service == null) {
-			service = new Service(serviceRoleList[0].getDeclaringClass().getName());
+			service = new Service(serviceRoleList[0].getDeclaringClass()
+					.getName());
 			service.init();
 		}
-		
+
 		manager.persist(service);
-		
+
 		return service;
 	}
+
+	private ProjectID getProjectID(String projectID) {
+		ProjectID pidEn = manager.find(ProjectID.class, projectID);
+
+		if (pidEn == null) {
+			manager.persist(pidEn);
+		}
+
+		return pidEn;
+	}
 }

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-02-05 16:15:47 UTC (rev 18315)
+++ labs/jbosslabs/labs-3.0-build/services/credential/src/main/java/org/jboss/labs/auth/impl/UserServiceImpl.java	2008-02-05 16:20:53 UTC (rev 18316)
@@ -146,11 +146,11 @@
 		return userEn;
 	}
 	
-	private UserRole getUserRole(UserRole role) throws NoSuchUserRoleException {
+	public UserRole getUserRole(UserRole role) throws NoSuchUserRoleException {
 		return getUserRole(role.getId());
 	}
 	
-	private UserRole getUserRole(String roleID) throws NoSuchUserRoleException {
+	public UserRole getUserRole(String roleID) throws NoSuchUserRoleException {
 		UserRole roleEn = manager.find(UserRole.class, roleID);
 		
 		if (roleEn == null) {




More information about the jboss-svn-commits mailing list