📄 usermanagerbean.java
字号:
try { removeRolesForUser(data.login); addRolesForUser(data.login, data.roles ); } catch (Exception ex) { getSessionContext().setRollbackOnly(); throw new UserException("Error updating roles for User "+data.login, ex); } } } /** * Gets resources within the specified category that are * associated with the specified user. * * @ejb.interface-method * @ejb.transaction type="Required" * * @param id User ID * @param catKey Key to identify the category of resource to extract, or null to extract all resources. * @return Array of resource IDs. */ public String[] getResourcesInUser( Integer id, String catKey ) throws UserException { LocalUser user = doFindUserById(id); String[] resourceIds = null; try { LocalResourceReference[] resourceRefs = user.getResources( catKey ); resourceIds = new String[ resourceRefs.length ]; for (int ridx=0; ridx<resourceIds.length; ridx++) { resourceIds[ridx] = resourceRefs[ridx].getResourceId(); } } catch (Exception ex) { throw new UserException("Error finding ResourceReferences for User "+user.getName(),ex); } return resourceIds; } /** * Sets a single resource ID as a ResourceReference instance to a user. * If the category doesn't exist, a new ResourceReference instance is created. * If the category already exists, the existing ResourceReference instance is updated * provided only a single ResourceReference of this category exists. If * this category contains multiple ResourceReference instances, a UserException * is thrown because this is not a "single resource". * * @ejb.interface-method * @ejb.transaction type="Required" * * @param catKey Resource category key. * @param resId Resource Id to add/update. * @param userId ID of user to set resource reference to. */ public void setSingleResourceToUser( String catKey, String resId, Integer userId ) throws UserException { LocalUser user = doFindUserById(userId); LocalResourceReference[] resourceRefs = user.getResources( catKey ); if (resourceRefs.length>1) { throw new UserException(UserException.UNSPECIFIED, "Resource category \""+catKey+"\" has more than a single resource reference." ); } try { user.setLastModifiedMillis( System.currentTimeMillis() ); if (resourceRefs.length==1) { resourceRefs[0].setResourceId(resId); } else { try { LocalResourceReference resRef = s_resourceRefHome.create(catKey, resId); user.addResource( resRef ); } catch (CreateException crex) { throw new UserException("Error creating ResourceReference with categoryKey=\""+catKey+"\", resourceId="+resId+", userId="+userId, crex); } } } catch (UserException uex) { getSessionContext().setRollbackOnly(); throw uex; } catch (Exception ex) { getSessionContext().setRollbackOnly(); throw new UserException("Error setting ResourceReferences for User "+user.getName(),ex); } } /** * Sets resource IDs as ResourceReference instances to a user. * It first clears all existing references with the specified category, * then adds the new ones. * * @ejb.interface-method * @ejb.transaction type="Required" * * @param catKey Resource category key (all references with this category will first be removed). * @param resIds Array of new resource Ids. * @param userId ID of user to set resource references to. */ public void setResourcesToUser( String catKey, String[] resIds, Integer userId ) throws UserException { LocalUser user = doFindUserById(userId); try { user.setLastModifiedMillis( System.currentTimeMillis() ); removeResourcesByCategoryForUser(catKey, userId); for (int r=0; r<resIds.length; r++) { try { LocalResourceReference resRef = s_resourceRefHome.create(catKey, resIds[r]); user.addResource( resRef ); } catch (CreateException crex) { throw new UserException("Error creating ResourceReference with categoryKey="+catKey+", resourceId="+resIds[r]+", userId="+userId, crex); } } } catch (UserException uex) { getSessionContext().setRollbackOnly(); throw uex; } catch (Exception ex) { getSessionContext().setRollbackOnly(); throw new UserException("Error setting ResourceReferences for User "+user.getName(),ex); } } /** * Gets roles that are associated with the specified user login. * * @ejb.interface-method * @ejb.transaction type="Required" * * @param login User login name * @return Array of role names, or empty array if none. */ public String[] getRolesInUser( String login ) { return doGetRolesInUser(login); } /** * Dummy create method. * * @ejb.create-method view-type="remote" */ public void ejbCreate() { // Nothing here } static LocalUser doFindUserById(Integer id) throws UserException { LocalUser user = null; try { user = s_userHome.findByPrimaryKey(id); } catch (FinderException fex) { throw new UserException(UserException.USER_NOT_FOUND, "Cannot find User with ID "+id, fex); } return user; } static UserData[] convertUserCollectionToDataArray( Collection userCollection, boolean includeRoles ) { UserData[] userDataArr = new UserData[userCollection.size()]; Iterator userIter = userCollection.iterator(); for (int uidx=0; userIter.hasNext(); uidx++) { LocalUser user = (LocalUser)userIter.next(); userDataArr[uidx] = createUserData(user, includeRoles); } return userDataArr; } private static String[] doGetRolesInUser( String login ) { ArrayList roleList = new ArrayList(); try { Collection uRoles = s_userRoleHome.findByLoginName(login); Iterator uRoleIter = uRoles.iterator(); while (uRoleIter.hasNext()) { roleList.add( ((LocalUserRole)uRoleIter.next()).getRoleName() ); } } catch (FinderException fex2) { // Its alright for a user not to have any role } return (String[])roleList.toArray(new String[0]); } /** * Associates roles with specified user. */ private static void addRolesForUser( String login, String[] roles) throws CreateException { if (roles!=null) { for (int r=0; r<roles.length; r++) { s_userRoleHome.create( login, roles[r]); } } } /** * Removes any roles associated with specified user id. */ private static void removeRolesForUser( String login ) throws RemoveException { try { Collection uRoles = s_userRoleHome.findByLoginName(login); Iterator uRoleIter = uRoles.iterator(); while (uRoleIter.hasNext()) { ((LocalUserRole)uRoleIter.next()).remove(); } } catch (FinderException fex2) { // Its alright for a user not to have any role } } private static LocalUser findUserByLogin(String login) { LocalUser user = null; try { Iterator userIter = s_userHome.findByLogin(login).iterator(); if (userIter.hasNext()) { user = (LocalUser)userIter.next(); } } catch (FinderException fex) { // Ignore, user remains null. } return user; } /** * Removes all ResourceReference instances of a specified category from a user. * * @param catKey Resource category key. * @param userId Id of user to remove resource references from. */ private static void removeResourcesByCategoryForUser( String catKey, Integer userId ) throws UserException { try { Iterator resRefIter = s_resourceRefHome.findByCategoryAndUser(catKey, userId).iterator(); while (resRefIter.hasNext()) { ((LocalResourceReference)resRefIter.next()).remove(); } } catch (Exception rex) { throw new UserException("Error removing ResourceReferences with categoryKey="+catKey+", userId="+userId, rex); } } private static UserData createUserData(LocalUser user, boolean includeRoles) { UserData data = new UserData(); data.id = user.getId(); data.email = user.getEmail(); data.title = user.getTitle(); data.firstName = user.getFirstName(); data.lastName = user.getLastName(); data.login = user.getName(); data.createdMillis = user.getCreatedMillis(); data.lastModifiedMillis = user.getLastModifiedMillis(); data.passwordExpiryMillis = user.getPasswordExpiryMillis(); if (includeRoles) { data.roles = doGetRolesInUser( data.getLogin() ) ; for (int r=0; r<data.roles.length; r++) { if ( "admin".equals(data.roles[r]) ) { data.admin = true; } else if ( "testee".equals(data.roles[r]) ) { data.testee = true; } } } return data; } private static LocalUserHome s_userHome = null; private static LocalUserRoleHome s_userRoleHome = null; private static LocalResourceReferenceHome s_resourceRefHome = null; static { try { Context ctx = new InitialContext(); try { s_userHome = (LocalUserHome)ctx.lookup(JNDI_PREFIX+LocalUserHome.JNDI_NAME); s_userRoleHome = (LocalUserRoleHome)ctx.lookup(JNDI_PREFIX+LocalUserRoleHome.JNDI_NAME); s_resourceRefHome = (LocalResourceReferenceHome)ctx.lookup(JNDI_PREFIX+LocalResourceReferenceHome.JNDI_NAME); } finally { ctx.close(); } } catch (NamingException nex) { throw new EJBException("Error retrieving homes", nex); } } private static Logger s_logger = Logger.getLogger(UserManagerBean.class.getName());}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -