/** @param roleNames the roleNames to set */ public void setRoleNames(List<String> roleNames) { removeList = new ArrayList<UserRole>(); addedList = new ArrayList<UserRole>(); this.roleNames = new ArrayList<String>(); if (roleNames == null) { for (UserRole role : roles) { removeList.add(role); } for (UserRole role : removeList) { this.removeRole(role); } return; } this.roleNames.addAll(roleNames); for (UserRole role : roles) { if (roleNames.contains(role.getRole())) { // we keep the role, and remove the name roleNames.remove(role.getRole()); } else { // this role has been removed removeList.add(role); } } for (UserRole role : removeList) { this.removeRole(role); } // now we need to add what's left in rNames for (String name : roleNames) { addedList.add(new UserRole(this, name)); roles.add(new UserRole(this, name)); } }
public User getUser(String username) { con = new DBConnection(); User objUser = null; try { cstmt = con.connection.prepareCall("{call getUser(?)}"); cstmt.setString(1, username); ResultSet rsUser = cstmt.executeQuery(); if (rsUser.next()) { objUser = new User(); objUser.setUserName(rsUser.getString("UserName")); objUser.setPassword(rsUser.getString("Password")); UserRole objUserRole = new UserRole(); objUserRole.setRole(rsUser.getString("Role")); objUser.setUsertype(objUserRole); } } catch (Exception e) { e.getMessage(); } finally { con.closeConnection(); } return objUser; }
/** * Converts the given integer to a <code>Role</code>. If the given integer doesn't map to any * enumeration value, returns <code>Role.EVERYONE</code>. * * @param value The integer to be converted into a role * @return The <code>Role</code> corresponding to the given integer, or <code>Role.EVERYONE</code> * if no match is found */ public static UserRole getRole(int value) { for (UserRole role : values()) { if (role.getValue() == value) { return role; } } return EVERYONE; }
@Override public Collection<? extends GrantedAuthority> getAuthorities() { Collection<GrantedAuthority> grantedAuthorities = new HashSet<GrantedAuthority>(); for (UserRole role : this.getUserRoles()) { grantedAuthorities.add(new SimpleGrantedAuthority(role.getRoleName())); } return grantedAuthorities; }
/** * TODO Consider making this cacheable * * @param authorities The required authorities * @return True if the user has all the required authorities */ public boolean hasAllAuthorities(Authority[] authorities) { Set<Authority> requiredAuthorities = Sets.newHashSet(authorities); Set<Authority> grantedAuthorities = Sets.newHashSet(); for (UserRole userRole : userRoles) { grantedAuthorities.addAll(userRole.getRole().getAuthorities()); } return grantedAuthorities.containsAll(requiredAuthorities); }
public void mapRoleMatrix(Map<String, String> allRoles) { userRoleMap = new HashMap<String, String>(); List<String> roleKeys = new ArrayList<String>(); for (UserRole role : this.roles) roleKeys.add(role.getRole()); for (String key : allRoles.keySet()) { if (roleKeys.contains(key)) { userRoleMap.put(key, allRoles.get(key)); } } }
/** * Remove role from group roles by name * * @param role * @return true if remove successful otherwise false */ public boolean removeRole(String roleName) { if (StringUtil.isEmpty(roleName)) return false; for (UserRole role : getRoles()) { if (roleName.equals(role.getRole())) { removeRole(role); return true; } } return false; }
public User getUserWithRole(za.org.rfm.utils.Role role) { for (User user : getUsers()) { for (UserRole userRole : user.getUserRoles()) { if (userRole.getRole().getName().equalsIgnoreCase(role.name())) { return user; } } } return null; }
public List<Role> getRoleChildren() { if (this.get("roleChildren") == null) { // 查询当前用户的角色 UserRole userRole = UserRole.dao.findFirstBy("`userRole`.user_id=" + this.get("id")); // 当前用户的子集角色 List<Role> roles = Role.dao.findChildrenById("`role`.deleted_at is null", userRole.get("role_id")); this.put("roleChildren", roles); } return this.get("roleChildren"); }
public List<User> getUsersWithRole(za.org.rfm.utils.Role role) { List<User> userList = new ArrayList<User>(); for (User user : getUsers()) { for (UserRole userRole : user.getUserRoles()) { if (userRole.getRole().getName().equalsIgnoreCase(role.name())) { userList.add(user); } } } return userList; }
public GrantedAuthority[] getAuthorities() { // assert this.getAuthoritieses() != null: "Granted Authorities cannot be null"; GrantedAuthority[] grantedAutorities = new GrantedAuthority[this.getUserRoles().size()]; Iterator itr = this.getUserRoles().iterator(); int i = 0; while (itr.hasNext()) { UserRole auth = (UserRole) itr.next(); String role = auth.getId().getRole(); GrantedAuthority grantedAuthority = new GrantedAuthorityImpl(role); grantedAutorities[i++] = grantedAuthority; } return grantedAutorities; }
public User addRole(Role role) { if (ValidateKit.isNullOrEmpty(role)) { role = Role.dao.findFirstBy("`role`.value='R_USER'"); if (ValidateKit.isNullOrEmpty(role)) { throw new NullPointerException("角色不存在"); } } UserRole userRole = new UserRole(); userRole.set("user_id", this.get("id")); userRole.set("role_id", role.get("id")); userRole.save(); return this; }
/** * Add role to group roles * * @param role * @return true if add successful otherwise false */ public boolean addRole(UserRole role) { if (role == null) throw new IllegalArgumentException("Null role."); if (roles != null) { roles.remove(role); } role.setUserGroup(this); return roles.add(role); }
@RequestMapping(value = "/admin/addUser.html", method = RequestMethod.GET) public ModelAndView addUser() { ModelAndView modelAndView = new ModelAndView(); ModelMap modelMap = modelAndView.getModelMap(); modelMap.put(ROLE, UserRole.values()); modelAndView.setViewName(createUser); return modelAndView; }
@CacheNameRemove(name = AppConstants.DEFAULT_CACHENAME) @Before({AdminValidator.UpdateRoleValidator.class, Tx.class}) public void updateRole() { keepModel(UserRole.class); UserRole userRole = getModel(UserRole.class); boolean result = true; List<UserRole> aroles = UserRole.dao.findBy("`userRole`.user_id=" + userRole.get("user_id")); boolean mustAdd = true; if (!ValidateUtils.me().isNullOrEmpty(aroles)) { // delete for (UserRole ar : aroles) { if (ar.get("role_id") != userRole.get("role_id")) { ar.delete(); } else { mustAdd = false; } } } // add if (mustAdd) { result = result && userRole.save(); } if (result) setAttr("state", "success"); else setAttr("state", "failure"); dynaRender("/view/admin/user.ftl"); }
@RequestMapping(value = "/admin/updateUser.html", method = RequestMethod.GET) public ModelAndView ajaxUpdateUser(String guid) { User currentUser = userService.findUserByGuid(guid); ModelAndView modelAndView = new ModelAndView(userInfo); ModelMap modelMap = modelAndView.getModelMap(); modelMap.put(USER, currentUser); modelMap.put(ROLE, UserRole.values()); return modelAndView; }
@CacheName(AppConstants.DEFAULT_CACHENAME) public void role() { User user = SubjectUtils.me().getUser(); keepPara("user_search"); // 查询当前用户的角色 UserRole userRole = UserRole.dao.findFirstBy("`userRole`.user_id=" + user.get("id")); // 当前用户的子集角色 List<Role> roles = Role.dao.findChildrenById("`role`.deleted_at is null", userRole.get("role_id")); roles.add(0, user.getRole()); if (!ValidateUtils.me().isNullOrEmpty(roles)) setAttr("role", user.getRole()); List<Permission> authories = Permission.dao.findBy("`permission`.deleted_at is NULL"); setAttr("rolestree", TreeUtils.toTree(roles)); setAttr("permissionestree", TreeUtils.toTreeLevel(authories, 2)); dynaRender("/view/admin/role.ftl"); }
@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + (int) (id ^ (id >>> 32)); result = prime * result + ((password == null) ? 0 : password.hashCode()); result = prime * result + ((role == null) ? 0 : role.hashCode()); result = prime * result + ((username == null) ? 0 : username.hashCode()); return result; }
public UserRole getGroupRoles(String groupId) { UserRole role = new UserRole(); if (roles == null) { roles = new HashSet<UserRole>(); role.setGroupId(groupId); roles.add(role); } else { for (UserRole userRole : roles) { if (userRole.getGroupId().equals(groupId)) { role = userRole; break; } } role.setGroupId(groupId); roles.add(role); } return role; }
private UserDomain createUserDomain( User user, Long domainId, DomainType domainType, UserRole userRole) { UserDomain userDomain = new UserDomain(); userDomain.setUser(user); userDomain.setDomainId(domainId); userDomain.setDomainType(domainType); userDomain.setRole(roleService.getRoleByAuthority(userRole.name())); user.getUserDomains().add(userDomain); return userDomain; }
@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashCode()); result = prime * result + ((firstName == null) ? 0 : firstName.hashCode()); result = prime * result + ((lastName == null) ? 0 : lastName.hashCode()); result = prime * result + ((birthdate == null) ? 0 : birthdate.hashCode()); result = prime * result + ((active == null) ? 0 : active.hashCode()); result = prime * result + ((role == null) ? 0 : role.hashCode()); result = prime * result + ((email == null) ? 0 : email.hashCode()); return result; }
public boolean isUserInRole(String role) { int checkRole = UserRole.getRoleId(role); Session session = this.user.getSession(); if (session == null) { logger.debug("session is null no allow role " + role); return false; } boolean ret = session.isUserInRole(checkRole); if (ret == false) { logger.info("user " + this.user.getUserId() + " not in role " + role); } return ret; }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; final SignUpResponse other = (SignUpResponse) obj; if ((id == null && other.id != null) || !id.equals(other.id)) return false; if ((firstName == null && other.firstName != null) || !firstName.equals(other.firstName)) return false; if ((lastName == null && other.lastName != null) || !lastName.equals(other.lastName)) return false; if ((birthdate == null && other.birthdate != null) || !birthdate.equals(other.birthdate)) return false; if ((active == null && other.active != null) || !active.equals(other.active)) return false; if ((role == null && other.role != null) || !role.equals(other.role)) return false; if ((email == null && other.email != null) || !email.equals(other.email)) return false; return true; }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; User other = (User) obj; if (id != other.id) return false; if (password == null) { if (other.password != null) return false; } else if (!password.equals(other.password)) return false; if (role == null) { if (other.role != null) return false; } else if (!role.equals(other.role)) return false; if (username == null) { if (other.username != null) return false; } else if (!username.equals(other.username)) return false; return true; }
private User getUser(UserRole organizationUserRole, UserRole groupUserRole) { User user = getUser(); Role orgRole = null; if (organizationUserRole != null) { orgRole = roleService.getRoleByAuthority(organizationUserRole.name()); user.getRoles().add(orgRole); } Role groupRole = null; if (groupUserRole != null) { groupRole = roleService.getRoleByAuthority(groupUserRole.name()); user.getRoles().add(groupRole); } Organization organization = createOrganization(); Application application = createApplication( organization.getCategories().get(0), "Test Application", AppState.GROUP_PUBLISH); Application application2 = createApplication( organization.getCategories().get(0), "Test Application 2", AppState.ORGANIZATION_PUBLISH); Group group = createGroup(organization); group.getOwnedApplications().add(application); group.getOwnedApplications().add(application2); organization.getGroups().add(group); userService.save(user); organizationService.getAll(); if (groupRole != null) { UserDomain userDomainGroup = new UserDomain(); userDomainGroup.setUser(user); userDomainGroup.setDomainId(group.getId()); userDomainGroup.setDomainType(DomainType.GROUP); userDomainGroup.setRole(groupRole); userDomainGroup.setDomainId(group.getId()); user.getUserDomains().add(userDomainGroup); } if (orgRole != null) { UserDomain userDomainOrg = new UserDomain(); userDomainOrg.setUser(user); userDomainOrg.setDomainId(group.getId()); userDomainOrg.setDomainType(DomainType.ORGANIZATION); userDomainOrg.setRole(orgRole); userDomainOrg.setDomainId(organization.getId()); user.getUserDomains().add(userDomainOrg); } userService.save(user); entityManager.flush(); return user; }
@CacheName(AppConstants.DEFAULT_CACHENAME) public void user() { User user = SubjectUtils.me().getUser(); keepPara("user_search"); // 查询当前用户的角色 UserRole userRole = UserRole.dao.findFirstBy("`userRole`.user_id=" + user.get("id")); // 当前用户的子集角色 List<Role> roles = Role.dao.findChildrenById("`role`.deleted_at is null", userRole.get("role_id")); String roleIds = ""; if (roles != null) { int size = roles.size(); int i = 0; for (Role role : roles) { roleIds += role.get("id"); if (i < size - 1) { roleIds += ","; } i++; } } // 只能查询当前用户以下的角色 String where = " `user`.id <> " + user.get("id") + " AND `userRole`.role_id in (" + roleIds + ")"; String user_search = getPara("user_search"); if (!ValidateUtils.me().isNullOrEmpty(user_search)) { where += " AND (INSTR(`user`.username,'" + user_search + "')>0 OR INSTR(`user`.full_name,'" + user_search + "')>0 " + "OR INSTR(`user`.mobile,'" + user_search + "')>0 OR INSTR(`province`.name,'" + user_search + "')>0 " + "OR INSTR(`city`.name,'" + user_search + "')>0 OR INSTR(`county`.name,'" + user_search + "')>0 " + "OR INSTR(`userInfo`.street,'" + user_search + "')>0 OR INSTR(`userInfo`.zip_code,'" + user_search + "')>0 " + "OR INSTR(`user`.created_at,'" + user_search + "')>0 OR INSTR(`user`.email,'" + user_search + "')>0) "; } // String start_at = getPara("start_at"); // if (ValidateUtils.me().isDateTime(start_at)) { // where += " AND `user`.created_at >= '" + start_at + "'"; // } // // String end_at = getPara("end_time"); // if (ValidateUtils.me().isDateTime(end_at)) { // where += " AND `user`.created_at <= '" + end_at + "'"; // } // // Boolean deleted = getParaToBoolean("deleted"); // if (!ValidateUtils.me().isNullOrEmpty(deleted) && deleted) { // where += " AND `user`.deleted_at is not null"; // } else { // where += " AND `user`.deleted_at is null"; // } Page<User> users = User.dao.paginateInfoBy(getParaToInt(0, 1), getParaToInt("pageSize", 15), where); Map userGroup = SortUtils.me().sort(users.getList(), "last_name"); setAttr("roles", roles); setAttr("users", users); setAttr("userGroup", userGroup); setAttr("userStates", State.dao.findBy("`state`.type='user.state'")); dynaRender("/view/admin/user.ftl"); }
public UserRole removeUserRole(UserRole userRole) { getUserRoles().remove(userRole); userRole.setUser(null); return userRole; }
public UserRole addUserRole(UserRole userRole) { getUserRoles().add(userRole); userRole.setUser(this); return userRole; }
public void syncRoleNames() { roleNames = new ArrayList<String>(); for (UserRole role : roles) { roleNames.add(role.getRole()); } }