Author: shane.bryzak(a)jboss.com
Date: 2008-04-07 04:58:53 -0400 (Mon, 07 Apr 2008)
New Revision: 7821
Added:
trunk/src/main/org/jboss/seam/security/management/action/
trunk/src/main/org/jboss/seam/security/management/action/RoleAction.java
trunk/src/main/org/jboss/seam/security/management/action/RoleSearch.java
trunk/src/main/org/jboss/seam/security/management/action/UserAction.java
trunk/src/main/org/jboss/seam/security/management/action/UserSearch.java
Removed:
trunk/src/main/org/jboss/seam/security/management/UserAction.java
Modified:
trunk/src/main/org/jboss/seam/security/management/IdentityManager.java
trunk/src/main/org/jboss/seam/security/management/IdentityStore.java
trunk/src/main/org/jboss/seam/security/management/JpaIdentityStore.java
trunk/src/main/org/jboss/seam/security/management/LdapIdentityStore.java
Log:
various fixed, added action components
Modified: trunk/src/main/org/jboss/seam/security/management/IdentityManager.java
===================================================================
--- trunk/src/main/org/jboss/seam/security/management/IdentityManager.java 2008-04-05
15:08:57 UTC (rev 7820)
+++ trunk/src/main/org/jboss/seam/security/management/IdentityManager.java 2008-04-07
08:58:53 UTC (rev 7821)
@@ -213,6 +213,11 @@
return roleIdentityStore.getImpliedRoles(name);
}
+ public List<String> getRoleGroups(String name)
+ {
+ return roleIdentityStore.getRoleGroups(name);
+ }
+
public boolean authenticate(String username, String password)
{
return identityStore.authenticate(username, password);
Modified: trunk/src/main/org/jboss/seam/security/management/IdentityStore.java
===================================================================
--- trunk/src/main/org/jboss/seam/security/management/IdentityStore.java 2008-04-05
15:08:57 UTC (rev 7820)
+++ trunk/src/main/org/jboss/seam/security/management/IdentityStore.java 2008-04-07
08:58:53 UTC (rev 7821)
@@ -87,6 +87,7 @@
List<String> getGrantedRoles(String name);
List<String> getImpliedRoles(String name);
+ List<String> getRoleGroups(String name);
boolean authenticate(String username, String password);
}
Modified: trunk/src/main/org/jboss/seam/security/management/JpaIdentityStore.java
===================================================================
--- trunk/src/main/org/jboss/seam/security/management/JpaIdentityStore.java 2008-04-05
15:08:57 UTC (rev 7820)
+++ trunk/src/main/org/jboss/seam/security/management/JpaIdentityStore.java 2008-04-07
08:58:53 UTC (rev 7821)
@@ -104,15 +104,15 @@
String methodName = propertyMethod.getName();
this.name = methodName.startsWith("get") ?
- (methodName.substring(3,1).toLowerCase() + methodName.substring(4)) :
- (methodName.substring(2,1).toLowerCase() + methodName.substring(3));
+ (methodName.substring(3,4).toLowerCase() + methodName.substring(4)) :
+ (methodName.substring(2,3).toLowerCase() + methodName.substring(3));
String setterName = propertyMethod.getName().startsWith("get") ?
("set" + methodName.substring(3)) : ("set" +
methodName.substring(2));
try
{
- propertySetter = propertyMethod.getClass().getMethod(setterName, new Class[]
{propertyMethod.getReturnType()});
+ propertySetter = propertyMethod.getDeclaringClass().getMethod(setterName, new
Class[] {propertyMethod.getReturnType()});
}
catch (NoSuchMethodException ex)
{
@@ -273,25 +273,25 @@
if (userPrincipalProperty == null)
{
- throw new RuntimeException("Invalid userClass " + userClass.getName()
+
+ throw new IdentityManagementException("Invalid userClass " +
userClass.getName() +
" - required annotation @UserPrincipal not found on any Field or
Method.");
}
if (userPasswordProperty == null)
{
- throw new RuntimeException("Invalid userClass " + userClass.getName()
+
+ throw new IdentityManagementException("Invalid userClass " +
userClass.getName() +
" - required annotation @UserPassword not found on any Field or
Method.");
}
if (userRolesProperty == null)
{
- throw new RuntimeException("Invalid userClass " + userClass.getName()
+
+ throw new IdentityManagementException("Invalid userClass " +
userClass.getName() +
" - required annotation @UserRoles not found on any Field or
Method.");
}
if (roleNameProperty == null)
{
- throw new RuntimeException("Invalid roleClass " + roleClass.getName()
+
+ throw new IdentityManagementException("Invalid roleClass " +
roleClass.getName() +
" - required annotation @RoleName not found on any Field or Method.");
}
}
@@ -595,6 +595,31 @@
return roles;
}
+ public List<String> getRoleGroups(String name)
+ {
+ Object role = lookupRole(name);
+ if (role == null)
+ {
+ throw new NoSuchUserException("No such role '" + name +
"'");
+ }
+
+ List<String> groups = new ArrayList<String>();
+
+ if (roleGroupsProperty != null)
+ {
+ Collection roleGroups = (Collection) roleGroupsProperty.getValue(role);
+ if (roleGroups != null)
+ {
+ for (Object group : roleGroups)
+ {
+ groups.add((String) roleNameProperty.getValue(group));
+ }
+ }
+ }
+
+ return groups;
+ }
+
public List<String> getImpliedRoles(String name)
{
Object user = lookupUser(name);
@@ -622,13 +647,16 @@
{
Object instance = lookupRole(role);
- Collection groups = (Collection) roleGroupsProperty.getValue(instance);
-
- if (groups != null)
+ if (roleGroupsProperty != null)
{
- for (Object group : groups)
+ Collection groups = (Collection) roleGroupsProperty.getValue(instance);
+
+ if (groups != null)
{
- addRoleAndMemberships((String) roleNameProperty.getValue(group), roles);
+ for (Object group : groups)
+ {
+ addRoleAndMemberships((String) roleNameProperty.getValue(group),
roles);
+ }
}
}
}
@@ -673,7 +701,7 @@
try
{
Object user = lookupEntityManager().createQuery(
- "select u from " + userClass.getName() + "u where " +
userPrincipalProperty.getName() +
+ "select u from " + userClass.getName() + " u where " +
userPrincipalProperty.getName() +
" = :username")
.setParameter("username", username)
.getSingleResult();
@@ -691,7 +719,7 @@
try
{
Object value = lookupEntityManager().createQuery(
- "select r from " + roleClass.getName() + "r where " +
roleNameProperty.getName() +
+ "select r from " + roleClass.getName() + " r where " +
roleNameProperty.getName() +
" = :role")
.setParameter("role", role)
.getSingleResult();
@@ -715,7 +743,7 @@
{
return lookupEntityManager().createQuery(
"select u." + userPrincipalProperty.getName() + " from "
+ userClass.getName() +
- "u where lower(" + userPrincipalProperty.getName() + ") like
:username")
+ " u where lower(" + userPrincipalProperty.getName() + ") like
:username")
.setParameter("username", "%" + (filter != null ?
filter.toLowerCase() : "") +
"%")
.getResultList();
Modified: trunk/src/main/org/jboss/seam/security/management/LdapIdentityStore.java
===================================================================
--- trunk/src/main/org/jboss/seam/security/management/LdapIdentityStore.java 2008-04-05
15:08:57 UTC (rev 7820)
+++ trunk/src/main/org/jboss/seam/security/management/LdapIdentityStore.java 2008-04-07
08:58:53 UTC (rev 7821)
@@ -1191,4 +1191,10 @@
}
}
}
+
+ public List<String> getRoleGroups(String name)
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
}
Deleted: trunk/src/main/org/jboss/seam/security/management/UserAction.java
===================================================================
--- trunk/src/main/org/jboss/seam/security/management/UserAction.java 2008-04-05 15:08:57
UTC (rev 7820)
+++ trunk/src/main/org/jboss/seam/security/management/UserAction.java 2008-04-07 08:58:53
UTC (rev 7821)
@@ -1,212 +0,0 @@
-package org.jboss.seam.security.management;
-
-import static org.jboss.seam.ScopeType.CONVERSATION;
-
-import java.io.Serializable;
-import java.util.ArrayList;
-import java.util.List;
-
-import org.jboss.seam.annotations.Begin;
-import org.jboss.seam.annotations.In;
-import org.jboss.seam.annotations.Name;
-import org.jboss.seam.annotations.Scope;
-import org.jboss.seam.core.Conversation;
-import org.jboss.seam.faces.FacesMessages;
-import org.jboss.seam.security.management.IdentityManager;
-
-/**
- * A conversation-scoped component for creating and managing user accounts
- *
- * @author Shane Bryzak
- */
-(a)Name("org.jboss.seam.security.userAction")
-@Scope(CONVERSATION)
-public class UserAction implements Serializable
-{
- private String firstname;
- private String lastname;
- private String username;
- private String password;
- private String confirm;
- private List<String> roles;
- private boolean enabled;
-
- private boolean newUserFlag;
-
- @In IdentityManager identityManager;
-
- @Begin
- public void createUser()
- {
- roles = new ArrayList<String>();
- newUserFlag = true;
- }
-
- @Begin
- public void editUser(String username)
- {
- this.username = username;
- roles = identityManager.getGrantedRoles(username);
- enabled = identityManager.isUserEnabled(username);
- newUserFlag = false;
- }
-
- public String save()
- {
- if (newUserFlag)
- {
- return saveNewUser();
- }
- else
- {
- return saveExistingUser();
- }
- }
-
- private String saveNewUser()
- {
- if (!password.equals(confirm))
- {
- FacesMessages.instance().addToControl("password", "Passwords do
not match");
- return "failure";
- }
-
- boolean success = identityManager.createUser(username, password, firstname,
lastname);
-
- if (success)
- {
- for (String role : roles)
- {
- identityManager.grantRole(username, role);
- }
-
- if (!enabled)
- {
- identityManager.disableUser(username);
- }
-
- Conversation.instance().end();
-
- return "success";
- }
-
- return "failure";
- }
-
- private String saveExistingUser()
- {
- // Check if a new password has been entered
- if (password != null && !"".equals(password))
- {
- if (!password.equals(confirm))
- {
- FacesMessages.instance().addToControl("password", "Passwords
do not match");
- return "failure";
- }
- else
- {
- identityManager.changePassword(username, password);
- }
- }
-
- List<String> grantedRoles = identityManager.getGrantedRoles(username);
-
- if (grantedRoles != null)
- {
- for (String role : grantedRoles)
- {
- if (!roles.contains(role)) identityManager.revokeRole(username, role);
- }
- }
-
- for (String role : roles)
- {
- if (grantedRoles == null || !grantedRoles.contains(role))
- {
- identityManager.grantRole(username, role);
- }
- }
-
- if (enabled)
- {
- identityManager.enableUser(username);
- }
- else
- {
- identityManager.disableUser(username);
- }
-
- Conversation.instance().end();
- return "success";
- }
-
- public String getFirstname()
- {
- return firstname;
- }
-
- public void setFirstname(String firstname)
- {
- this.firstname = firstname;
- }
-
- public String getLastname()
- {
- return lastname;
- }
-
- public void setLastname(String lastname)
- {
- this.lastname = lastname;
- }
-
- public String getUsername()
- {
- return username;
- }
-
- public void setUsername(String username)
- {
- this.username = username;
- }
-
- public String getPassword()
- {
- return password;
- }
-
- public void setPassword(String password)
- {
- this.password = password;
- }
-
- public String getConfirm()
- {
- return confirm;
- }
-
- public void setConfirm(String confirm)
- {
- this.confirm = confirm;
- }
-
- public List<String> getRoles()
- {
- return roles;
- }
-
- public void setRoles(List<String> roles)
- {
- this.roles = roles;
- }
-
- public boolean isEnabled()
- {
- return enabled;
- }
-
- public void setEnabled(boolean enabled)
- {
- this.enabled = enabled;
- }
-}
\ No newline at end of file
Added: trunk/src/main/org/jboss/seam/security/management/action/RoleAction.java
===================================================================
--- trunk/src/main/org/jboss/seam/security/management/action/RoleAction.java
(rev 0)
+++ trunk/src/main/org/jboss/seam/security/management/action/RoleAction.java 2008-04-07
08:58:53 UTC (rev 7821)
@@ -0,0 +1,106 @@
+package org.jboss.seam.security.management.action;
+
+import static org.jboss.seam.ScopeType.CONVERSATION;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.jboss.seam.annotations.Begin;
+import org.jboss.seam.annotations.In;
+import org.jboss.seam.annotations.Name;
+import org.jboss.seam.annotations.Scope;
+import org.jboss.seam.core.Conversation;
+import org.jboss.seam.security.management.IdentityManager;
+
+@Name("roleAction")
+@Scope(CONVERSATION)
+public class RoleAction
+{
+ private String role;
+ private List<String> groups;
+
+ @In IdentityManager identityManager;
+
+ @Begin
+ public void createRole()
+ {
+ groups = new ArrayList<String>();
+ }
+
+ @Begin
+ public void editRole(String role)
+ {
+ this.role = role;
+ groups = identityManager.getRoleGroups(role);
+ }
+
+ public String save()
+ {
+ if (identityManager.roleExists(role))
+ {
+ return saveExistingRole();
+ }
+ else
+ {
+ return saveNewRole();
+ }
+ }
+
+ private String saveNewRole()
+ {
+ boolean success = identityManager.createRole(role);
+
+ if (success)
+ {
+ for (String r : groups)
+ {
+ identityManager.grantRole(role, r);
+ }
+
+ Conversation.instance().end();
+ }
+
+ return "success";
+ }
+
+ private String saveExistingRole()
+ {
+ List<String> grantedRoles = identityManager.getRoleGroups(role);
+
+ if (grantedRoles != null)
+ {
+ for (String r : grantedRoles)
+ {
+ if (!groups.contains(r)) identityManager.revokeRole(role, r);
+ }
+ }
+
+ for (String r : groups)
+ {
+ if (grantedRoles == null || !grantedRoles.contains(r))
identityManager.grantRole(role, r);
+ }
+
+ Conversation.instance().end();
+ return "success";
+ }
+
+ public String getRole()
+ {
+ return role;
+ }
+
+ public void setRole(String role)
+ {
+ this.role = role;
+ }
+
+ public List<String> getGroups()
+ {
+ return groups;
+ }
+
+ public void setGroups(List<String> groups)
+ {
+ this.groups = groups;
+ }
+}
\ No newline at end of file
Added: trunk/src/main/org/jboss/seam/security/management/action/RoleSearch.java
===================================================================
--- trunk/src/main/org/jboss/seam/security/management/action/RoleSearch.java
(rev 0)
+++ trunk/src/main/org/jboss/seam/security/management/action/RoleSearch.java 2008-04-07
08:58:53 UTC (rev 7821)
@@ -0,0 +1,52 @@
+package org.jboss.seam.security.management.action;
+
+import static org.jboss.seam.ScopeType.SESSION;
+
+import java.io.Serializable;
+import java.util.List;
+
+import org.jboss.seam.annotations.In;
+import org.jboss.seam.annotations.Name;
+import org.jboss.seam.annotations.Scope;
+import org.jboss.seam.annotations.datamodel.DataModel;
+import org.jboss.seam.annotations.datamodel.DataModelSelection;
+import org.jboss.seam.security.management.IdentityManager;
+
+(a)Name("org.jboss.seam.security.roleSearch")
+@Scope(SESSION)
+public class RoleSearch implements Serializable
+{
+ @DataModel
+ List<String> roles;
+
+ @DataModelSelection
+ String selectedRole;
+
+ @In IdentityManager identityManager;
+
+ public void loadRoles()
+ {
+ roles = identityManager.listRoles();
+ }
+
+ public String getRoleGroups(String role)
+ {
+ List<String> roles = identityManager.getRoleGroups(role);
+
+ if (roles == null) return "";
+
+ StringBuilder sb = new StringBuilder();
+
+ for (String r : roles)
+ {
+ sb.append((sb.length() > 0 ? ", " : "") + r);
+ }
+
+ return sb.toString();
+ }
+
+ public String getSelectedRole()
+ {
+ return selectedRole;
+ }
+}
\ No newline at end of file
Added: trunk/src/main/org/jboss/seam/security/management/action/UserAction.java
===================================================================
--- trunk/src/main/org/jboss/seam/security/management/action/UserAction.java
(rev 0)
+++ trunk/src/main/org/jboss/seam/security/management/action/UserAction.java 2008-04-07
08:58:53 UTC (rev 7821)
@@ -0,0 +1,212 @@
+package org.jboss.seam.security.management.action;
+
+import static org.jboss.seam.ScopeType.CONVERSATION;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.jboss.seam.annotations.Begin;
+import org.jboss.seam.annotations.In;
+import org.jboss.seam.annotations.Name;
+import org.jboss.seam.annotations.Scope;
+import org.jboss.seam.core.Conversation;
+import org.jboss.seam.faces.FacesMessages;
+import org.jboss.seam.security.management.IdentityManager;
+
+/**
+ * A conversation-scoped component for creating and managing user accounts
+ *
+ * @author Shane Bryzak
+ */
+(a)Name("org.jboss.seam.security.userAction")
+@Scope(CONVERSATION)
+public class UserAction implements Serializable
+{
+ private String firstname;
+ private String lastname;
+ private String username;
+ private String password;
+ private String confirm;
+ private List<String> roles;
+ private boolean enabled;
+
+ private boolean newUserFlag;
+
+ @In IdentityManager identityManager;
+
+ @Begin
+ public void createUser()
+ {
+ roles = new ArrayList<String>();
+ newUserFlag = true;
+ }
+
+ @Begin
+ public void editUser(String username)
+ {
+ this.username = username;
+ roles = identityManager.getGrantedRoles(username);
+ enabled = identityManager.isUserEnabled(username);
+ newUserFlag = false;
+ }
+
+ public String save()
+ {
+ if (newUserFlag)
+ {
+ return saveNewUser();
+ }
+ else
+ {
+ return saveExistingUser();
+ }
+ }
+
+ private String saveNewUser()
+ {
+ if (!password.equals(confirm))
+ {
+ FacesMessages.instance().addToControl("password", "Passwords do
not match");
+ return "failure";
+ }
+
+ boolean success = identityManager.createUser(username, password, firstname,
lastname);
+
+ if (success)
+ {
+ for (String role : roles)
+ {
+ identityManager.grantRole(username, role);
+ }
+
+ if (!enabled)
+ {
+ identityManager.disableUser(username);
+ }
+
+ Conversation.instance().end();
+
+ return "success";
+ }
+
+ return "failure";
+ }
+
+ private String saveExistingUser()
+ {
+ // Check if a new password has been entered
+ if (password != null && !"".equals(password))
+ {
+ if (!password.equals(confirm))
+ {
+ FacesMessages.instance().addToControl("password", "Passwords
do not match");
+ return "failure";
+ }
+ else
+ {
+ identityManager.changePassword(username, password);
+ }
+ }
+
+ List<String> grantedRoles = identityManager.getGrantedRoles(username);
+
+ if (grantedRoles != null)
+ {
+ for (String role : grantedRoles)
+ {
+ if (!roles.contains(role)) identityManager.revokeRole(username, role);
+ }
+ }
+
+ for (String role : roles)
+ {
+ if (grantedRoles == null || !grantedRoles.contains(role))
+ {
+ identityManager.grantRole(username, role);
+ }
+ }
+
+ if (enabled)
+ {
+ identityManager.enableUser(username);
+ }
+ else
+ {
+ identityManager.disableUser(username);
+ }
+
+ Conversation.instance().end();
+ return "success";
+ }
+
+ public String getFirstname()
+ {
+ return firstname;
+ }
+
+ public void setFirstname(String firstname)
+ {
+ this.firstname = firstname;
+ }
+
+ public String getLastname()
+ {
+ return lastname;
+ }
+
+ public void setLastname(String lastname)
+ {
+ this.lastname = lastname;
+ }
+
+ public String getUsername()
+ {
+ return username;
+ }
+
+ public void setUsername(String username)
+ {
+ this.username = username;
+ }
+
+ public String getPassword()
+ {
+ return password;
+ }
+
+ public void setPassword(String password)
+ {
+ this.password = password;
+ }
+
+ public String getConfirm()
+ {
+ return confirm;
+ }
+
+ public void setConfirm(String confirm)
+ {
+ this.confirm = confirm;
+ }
+
+ public List<String> getRoles()
+ {
+ return roles;
+ }
+
+ public void setRoles(List<String> roles)
+ {
+ this.roles = roles;
+ }
+
+ public boolean isEnabled()
+ {
+ return enabled;
+ }
+
+ public void setEnabled(boolean enabled)
+ {
+ this.enabled = enabled;
+ }
+}
\ No newline at end of file
Added: trunk/src/main/org/jboss/seam/security/management/action/UserSearch.java
===================================================================
--- trunk/src/main/org/jboss/seam/security/management/action/UserSearch.java
(rev 0)
+++ trunk/src/main/org/jboss/seam/security/management/action/UserSearch.java 2008-04-07
08:58:53 UTC (rev 7821)
@@ -0,0 +1,57 @@
+package org.jboss.seam.security.management.action;
+
+import static org.jboss.seam.ScopeType.SESSION;
+
+import java.io.Serializable;
+import java.util.List;
+
+import org.jboss.seam.annotations.In;
+import org.jboss.seam.annotations.Name;
+import org.jboss.seam.annotations.Scope;
+import org.jboss.seam.annotations.Synchronized;
+import org.jboss.seam.annotations.datamodel.DataModel;
+import org.jboss.seam.annotations.datamodel.DataModelSelection;
+
+import org.jboss.seam.security.management.IdentityManager;
+
+@Name("userSearch")
+@Scope(SESSION)
+@Synchronized
+public class UserSearch implements Serializable
+{
+ private static final long serialVersionUID = 8592034786339372510L;
+
+ @DataModel
+ List<String> users;
+
+ @DataModelSelection
+ String selectedUser;
+
+ @In IdentityManager identityManager;
+
+ public void loadUsers()
+ {
+ users = identityManager.listUsers();
+ }
+
+ public String getUserRoles(String username)
+ {
+ List<String> roles = identityManager.getGrantedRoles(username);
+
+ if (roles == null) return "";
+
+ StringBuilder sb = new StringBuilder();
+
+ for (String role : roles)
+ {
+ sb.append((sb.length() > 0 ? ", " : "") + role);
+ }
+
+ return sb.toString();
+ }
+
+ public String getSelectedUser()
+ {
+ return selectedUser;
+ }
+}
\ No newline at end of file