Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.mycompany.CRMFly.Security; import com.mycompany.CRMFly.entities.UserAccount; import com.mycompany.CRMFly.entities.UserAuthority; import com.mycompany.CRMFly.entities.UserGroup; import com.mycompany.CRMFly.hibernateAccess.CustomUserDetailService; //import com.mycompany.CRMFly.hibernateAccess.MyUserDetailService; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import javax.faces.application.FacesMessage; import javax.faces.context.FacesContext; import javax.inject.Inject; import javax.inject.Named; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.transaction.annotation.Transactional; /** * * @author ?? */ @Named @Scope("view") public class AuthorityController { @Inject private IChangePassword changePasswordDao; @Autowired private CustomUserDetailService userService; @Autowired private AccountController accountController; private List<String> selectedRoles; private Map<String, String> roles; private String authority; private String username; private UserAccount account; private String groupname; private List<String> userRoles; private List<String> userGroupes; private String newPass; private Boolean render = false; // private UserAccount account = new UserAccount(); public AuthorityController() { roles = new HashMap<String, String>(); roles.put("USER", "USER"); roles.put("AUTHORISED", "AUTHORISED"); roles.put("ADMIN", "ADMIN"); } public void toggleBool() { if (getRender() == false) setRender((Boolean) true); else setRender((Boolean) false); } public boolean checkIfAdmin() { Collection<? extends GrantedAuthority> authorities = SecurityContextHolder.getContext().getAuthentication() .getAuthorities(); String adminAuth = "ROLE_ADMIN"; for (GrantedAuthority grantedAuthority : authorities) { if (adminAuth.equals(grantedAuthority.getAuthority())) { return true; } } return false; } public boolean checkIfAuthorised() { Collection<? extends GrantedAuthority> authorities = SecurityContextHolder.getContext().getAuthentication() .getAuthorities(); String adminAuth = "ROLE_ADMIN"; String authAuth = "ROLE_AUTHORISED"; for (GrantedAuthority grantedAuthority : authorities) { if (adminAuth.equals(grantedAuthority.getAuthority()) || authAuth.equals(grantedAuthority.getAuthority())) { return true; } } return false; } private UserAuthority transformAuthority(String authority) { UserAuthority userRole = new UserAuthority(); if (authority.equals("USER")) userRole.setAuthority(UserAuthority.ROLE_USER); else if (authority.equals("AUTHORISED")) userRole.setAuthority(UserAuthority.ROLE_AUTHORISED); else if (authority.equals("ADMIN")) userRole.setAuthority(UserAuthority.ROLE_ADMIN); return userRole; } public void getThisUser(String name) { account = userService.loadUserByUsername(name); } public void getUser() { // account = new UserAccount(); // userRoles = new ArrayList<String>(); // userGroupes = new ArrayList<String>(); account = userService.loadUserByUsername(username); userRoles = new ArrayList<String>(); userGroupes = new ArrayList<String>(); Collection<UserAuthority> authorities = account.getUserAuthorities(); if (authorities != null && authorities.size() != 0) { for (UserAuthority auth : authorities) { userRoles.add(auth.getAuthority()); } } Collection<UserGroup> groups = userService.getGroupsOfUser(account.getId()); if (groups != null && groups.size() != 0) for (UserGroup group : groups) { userGroupes.add(group.getGroupName()); } // return account; } @Transactional public List<String> getAllGroups() { List<UserGroup> groups = userService.getAllGroups(); List<String> groupnames = new ArrayList<String>(); for (UserGroup group : groups) { groupnames.add(group.toString()); } return groupnames; } public List<UserAccount> getAllUsers() { return userService.getAllUsers(); } public void addAuthority() { UserAccount account = (UserAccount) userService.loadUserByUsername(getUsername()); Collection<UserAuthority> currentAuth = account.getUserAuthorities(); boolean newAuthBool = true; for (String auth : selectedRoles) { auth = "ROLE_" + auth; for (UserAuthority curAuth : currentAuth) { if (curAuth.getAuthority().equals(auth)) newAuthBool = false; } if (newAuthBool == true) { // UserAuthority newAuth = transformAuthority(auth);\ UserAuthority newAuth = new UserAuthority(); newAuth.setAuthority(auth); newAuth.setAccount(account); newAuth.setUsername(username); account.getUserAuthorities().add(newAuth); userService.saveAuthority(newAuth); userRoles.add(auth); } } userService.updateUser(account); } public void deleteAuthority() { UserAccount account = (UserAccount) userService.loadUserByUsername(getUsername()); Boolean NotPermitted = false; for (String auth : selectedRoles) { UserAuthority authFromDB; if (auth.equals("USER")) authFromDB = userService.getAuthority(username, UserAuthority.ROLE_USER); else if (auth.equals("AUTHORISED")) authFromDB = userService.getAuthority(username, UserAuthority.ROLE_AUTHORISED); else if (auth.equals("ADMIN")) authFromDB = userService.getAuthority(username, UserAuthority.ROLE_ADMIN); else authFromDB = new UserAuthority(); List<UserAuthority> nessAuth = userService.getAllGroupAuthoritiesFotUser(account); for (UserAuthority ness : nessAuth) { if (authFromDB.getAuthority().equals(ness.getAuthority())) { NotPermitted = true; break; } } if (NotPermitted == false) { account.getUserAuthorities().remove(authFromDB); authFromDB.setAccount(null); userService.updateUser(account); userService.deleteAuthority(authFromDB); userRoles.remove("ROLE_" + auth); } else { FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "? , ? .", null)); } } } public void deleteUser() { UserAccount account = (UserAccount) userService.loadUserByUsername(getUsername()); userService.deleteUser(account); userRoles = new ArrayList<String>(); userGroupes = new ArrayList<String>(); /* FacesContext context = FacesContext.getCurrentInstance(); UIComponent component = component.findComponent(""); if (component != null && component instanceof UIInput) { UIInput input = (UIInput)component; input.resetValue();*/ } public void changeUserPassword() { changePasswordDao.changePassword(username, newPass); SecurityContextHolder.clearContext(); FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, " ? .", null)); } public void changeUsername(String newUsername) { userService.changeUsername(username, newUsername); } public void addGroup() { UserGroup newGroup = new UserGroup(); newGroup.setGroupName(getGroupname()); for (String auth : selectedRoles) { UserAuthority newAuth = transformAuthority(auth); newGroup.getAuthorities().add(newAuth); userService.saveAuthority(newAuth); } userService.addGroup(newGroup); } public void deleteGroup() { UserGroup group = (UserGroup) userService.loadGroupByGroupname(getGroupname()); userService.clearGroup(group); Set<UserAuthority> groupAuth = group.getAuthorities(); for (UserAuthority auth : groupAuth) { group.setAuthorities(null); userService.updateGroup(group); userService.deleteAuthority(auth); } userService.deleteGroup(group); } public void addUserToGroup() { //userRoles = new ArrayList<String>(); UserAccount account = (UserAccount) userService.loadUserByUsername(getUsername()); UserGroup group = (UserGroup) userService.loadGroupByGroupname(getGroupname()); Set<UserAuthority> groupAuth = group.getAuthorities(); Collection<UserAuthority> userAuth = account.getUserAuthorities(); Boolean newAuthBool = true; /* if (userAuth!=null && userAuth.size()!=0) { account.setUserAuthorities(new ArrayList<UserAuthority>()); userService.updateUser(account); for (UserAuthority auth : userAuth)*/ // {auth=userService.getAuthority(username, auth.getAuthority()); // auth.setAccount(null); // userService.deleteAuthority(auth);}} if (groupAuth != null && groupAuth.size() != 0) { for (UserAuthority auth : groupAuth) { for (UserAuthority curAuth : userAuth) { if (curAuth.getAuthority().equals(auth.getAuthority())) newAuthBool = false; } if (newAuthBool == true) { UserAuthority newAuth = new UserAuthority(); newAuth.setUsername(username); newAuth.setAuthority(auth.getAuthority()); account.getUserAuthorities().add(newAuth); newAuth.setAccount(account); userRoles.add(newAuth.getAuthority()); userService.saveAuthority(newAuth); } } } userService.addUserToGroup(account, group); userGroupes.add(groupname); } public void removeUserFromGroup() { UserAccount account = userService.loadUserByUsername(getUsername()); UserGroup group = userService.loadGroupByGroupname(getGroupname()); userService.removeUserFromGroup(account, group); userGroupes.remove(groupname); // userRoles = new ArrayList<String>(); // userGroupes = new ArrayList<String>(); } /* public void flushAccount() { account = new UserAccount(); }*/ public String getAuthority() { return authority; } /** * @param authority the authority to set */ public void setAuthority(String authority) { this.authority = authority; } /** * @return the username */ public String getUsername() { return username; } /** * @param username the username to set */ public void setUsername(String username) { this.username = username; } /** * @return the selectedRoles */ public List<String> getSelectedRoles() { return selectedRoles; } /** * @param selectedRoles the selectedRoles to set */ public void setSelectedRoles(List<String> selectedRoles) { this.selectedRoles = selectedRoles; } /** * @return the roles */ public Map<String, String> getRoles() { return roles; } /** * @param roles the roles to set */ public void setRoles(Map<String, String> roles) { this.roles = roles; } /** * @return the account */ /* public UserAccount getAccount() { return account; } /** * @param account the account to set */ /* public void setAccount(UserAccount account) { this.account = account; }*/ /** * @return the groupname */ public String getGroupname() { return groupname; } /** * @param groupname the groupname to set */ public void setGroupname(String groupname) { this.groupname = groupname; } /** * @return the account */ public UserAccount getAccount() { return account; } /** * @param account the account to set */ public void setAccount(UserAccount account) { this.account = account; } /** * @return the userRoles */ public Collection<String> getUserRoles() { return userRoles; } /** * @param userRoles the userRoles to set */ public void setUserRoles(List<String> userRoles) { this.userRoles = userRoles; } /** * @return the userGroupes */ public Collection<String> getUserGroupes() { return userGroupes; } /** * @param userGroupes the userGroupes to set */ public void setUserGroupes(List<String> userGroupes) { this.userGroupes = userGroupes; } /** * @return the newPass */ public String getNewPass() { return newPass; } /** * @param newPass the newPass to set */ public void setNewPass(String newPass) { this.newPass = newPass; } /** * @return the render */ public Boolean getRender() { return render; } /** * @param render the render to set */ public void setRender(Boolean render) { this.render = render; } }