securityservice.java
来自「一个很好的开源项目管理系统源代码」· Java 代码 · 共 346 行
JAVA
346 行
package net.java.workeffort.service;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import net.java.workeffort.infrastructure.context.RequestContextHolder;import net.java.workeffort.service.dao.IRowCallback;import net.java.workeffort.service.domain.Operation;import net.java.workeffort.service.domain.PageResult;import net.java.workeffort.service.domain.PasswordModification;import net.java.workeffort.service.domain.Role;import net.java.workeffort.service.domain.RoleChildren;import net.java.workeffort.service.domain.RolePermission;import net.java.workeffort.service.domain.SecurityQuery;import net.java.workeffort.service.domain.Target;import net.java.workeffort.service.support.ChildRowExistsFkException;import net.java.workeffort.service.support.IllegalHierarchyException;import net.java.workeffort.service.support.InvalidHierarchyException;import net.java.workeffort.service.support.NoParentRowFkException;import net.java.workeffort.service.support.OptimisticLockingException;import net.java.workeffort.service.support.UniqueConstraintException;/** * Service for security. * <p> * This is fronted by a spring proxy which implements transaction management * @author Antony Joseph */public class SecurityService extends BaseService implements ISecurityService { // This is to avoid infinite recursion if for some reason we have // circular parent/child relationship for role hierarchy private static int MAX_DEPTH = 20; public Operation getOperation(String pk) { return (Operation) dao.queryForObject("Security.getOperation", pk); } public PageResult getPageResultOperation(SecurityQuery query) { return dao.getPageResult(dbPrefix+"Security.getPageResultOperation", query); } public void insertOperation(Operation operation) throws UniqueConstraintException { dao.insert("Security.insertOperation", operation, false, true); } public void updateOperation(Operation operation) throws OptimisticLockingException { dao.update("Security.updateOperation", operation); } public void deleteOperation(Operation operation) throws OptimisticLockingException, ChildRowExistsFkException { dao.delete("Security.deleteOperation", operation); } public Target getTarget(String pk) { return (Target) dao.queryForObject("Security.getTarget", pk); } public PageResult getPageResultTarget(SecurityQuery query) { return dao.getPageResult(dbPrefix+"Security.getPageResultTarget", query); } public void insertTarget(Target target) throws UniqueConstraintException { dao.insert("Security.insertTarget", target, false, true); } public void updateTarget(Target target) throws OptimisticLockingException { dao.update("Security.updateTarget", target); } public void deleteTarget(Target target) throws OptimisticLockingException, ChildRowExistsFkException { dao.delete("Security.deleteTarget", target); } public Role getRole(String pk) { return (Role) dao.queryForObject("Security.getRole", pk); } public PageResult getPageResultRole(SecurityQuery query) { return dao.getPageResult(dbPrefix+"Security.getPageResultRole", query); } public void insertRole(Role role) throws UniqueConstraintException { dao.insert("Security.insertRole", role, false, true); } public void updateRole(Role role) throws OptimisticLockingException { dao.update("Security.updateRole", role); } public void deleteRole(Role role) throws OptimisticLockingException, ChildRowExistsFkException { dao.delete("Security.deleteRole", role); } public PageResult getPageResultRolePermission(SecurityQuery query) { return dao.getPageResult(dbPrefix+"Security.getPageResultRolePermission", query); } public Role getRoleWithPermission(String pk) { Role role = (Role) dao.queryForObject("Security.getRole", pk); if (role != null) { List rolePermission = dao.queryForList( "Security.getListRolePermission", pk); role.setRolePermission(rolePermission); } return role; } public void saveRoleWithPermission(Role role) throws OptimisticLockingException, UniqueConstraintException { dao .processRows(role, "rolePermission", new RolePermissionRowCallback()); } public Role getRoleWithChildren(String pk) { Role role = (Role) dao.queryForObject("Security.getRole", pk); if (role != null) { List roleChildren = dao.queryForList( "Security.getListRoleChildren", pk); role.setRoleChildren(roleChildren); } return role; } public void saveRoleWithChildren(Role role) throws OptimisticLockingException, UniqueConstraintException { dao.processRows(role, "roleChildren", new RoleChildrenRowCallback()); } public List getListRoleHierarchy(String roleCd) { List hierarchy = new ArrayList(); //A role could have multiple parents. List parents = new ArrayList(); getTopLevelParents(parents, roleCd, 0); //System.out.println("parents = " + parents); for (int i = 0; i < parents.size(); i++) { Role parent = (Role) dao.queryForObject("Security.getRole", parents .get(i)); RoleChildren roleChildren = new RoleChildren(); roleChildren.setRoleCd(parent.getRoleCd()); roleChildren.setDescription(parent.getDescription()); roleChildren.setLevel(new Integer(0)); hierarchy.add(roleChildren); List children = new ArrayList(); // getRoleCdChildren() gets all the children // going down the hierarchy. getRoleCdChildren(children, (String) parents.get(i), 1); for (int j = 0; j < children.size(); j++) { hierarchy.add(children.get(j)); } //System.out.println("children=" + children); //System.out.println("hierarchy = " + hierarchy); } return hierarchy; } public List getListRolePermissionHierarchy(String roleCd) { List resultList = new ArrayList(); List hierarchy = getListRoleHierarchy(roleCd); //Now we have all the roles lets get their permissions for (int i = 0; i < hierarchy.size(); i++) { List list = dao.queryForList("Security.getListRolePermission", ((RoleChildren) hierarchy.get(i)).getRoleCd()); // Some roles may not have permissions. We still want to show // them. if (list == null || list.size() == 0) { RolePermission emptyRolePermission = new RolePermission(); emptyRolePermission.setRoleCd(((RoleChildren) hierarchy.get(i)) .getRoleCd()); emptyRolePermission.setLevel(((RoleChildren) hierarchy.get(i)) .getLevel()); resultList.add(emptyRolePermission); } else { for (int j = 0; j < list.size(); j++) { RolePermission rolePermission = (RolePermission) list .get(j); rolePermission.setLevel(((RoleChildren) hierarchy.get(i)) .getLevel()); resultList.add(rolePermission); } } } return resultList; } private void getRoleCdChildren(List children, String roleCd, int level) { if (level > MAX_DEPTH) throw new IllegalStateException("Level=" + level + " went beyond max depth " + MAX_DEPTH); List list = dao.queryForList("Security.getListRoleChildren", roleCd); for (int i = 0; i < list.size(); i++) { ((RoleChildren) list.get(i)).setLevel(new Integer(level)); children.add(list.get(i)); getRoleCdChildren(children, ((RoleChildren) list.get(i)) .getRoleCd(), level + 1); } } private void getTopLevelParents(List parents, String roleCd, int level) { if (level > MAX_DEPTH) throw new IllegalStateException("Level=" + level + " went beyond max depth " + MAX_DEPTH); List list = dao.queryForList("Security.getListRoleParent", roleCd); if (list == null || list.size() == 0) { // need the found flag to avoid duplicate top level parents. boolean found = false; for (int i = 0; i < parents.size(); i++) { if (roleCd.equals((String) parents.get(i))) { found = true; break; } } if (!found) parents.add(roleCd); return; } for (int i = 0; i < list.size(); i++) { getTopLevelParents(parents, (String) list.get(i), level + 1); } } public PasswordModification changePassword( PasswordModification passwordModification) { passwordModification.setPartyCd(RequestContextHolder .getRequestContext().getSecurityProfile().getPartyCd()); dao.update("Security.updatePassword", passwordModification, false); return passwordModification; } public PasswordModification resetPassword( PasswordModification passwordModification) { try { dao.update("Security.updatePassword", passwordModification, false); } catch (OptimisticLockingException e) { // OptimisticLockingException will occur in this case only if // the party record is not found in table. dao.insert("Security.insertPassword", passwordModification, false, false); } return passwordModification; } private boolean isTargetValid(String targetCd) { if (dao.queryForObject("Security.getTarget", targetCd) == null) return false; else return true; } private boolean isOperationValid(String operationCd) { if (dao.queryForObject("Security.getOperation", operationCd) == null) return false; else return true; } private class RolePermissionRowCallback implements IRowCallback { public void add(Object obj, Object row) { ((RolePermission) row).setRoleCd(((Role) obj).getRoleCd()); try { dao.insert("Security.insertRolePermission", row, false, true); } catch (UniqueConstraintException uce) { uce.setPropertyName("targetCd, operationCd"); throw uce; } catch (NoParentRowFkException fke) { if (!isTargetValid(((RolePermission) row).getTargetCd())) fke.setPropertyName("targetCd"); if (!isOperationValid(((RolePermission) row).getOperationCd())) fke.setPropertyName("operationCd"); throw fke; } } public void modify(Object obj, Object row) { throw new UnsupportedOperationException("modify() unsupported"); } public void remove(Object obj, Object row) { ((RolePermission) row).setRoleCd(((Role) obj).getRoleCd()); dao.delete("Security.deleteRolePermission", row); } } private class RoleChildrenRowCallback implements IRowCallback { public void add(Object obj, Object row) { ((RoleChildren) row).setParentCd(((Role) obj).getRoleCd()); try { validateHierarchy(((RoleChildren) row).getRoleCd(), ((Role) obj).getRoleCd(), "Security.getListRoleParent", 0); } catch (IllegalHierarchyException e) { throw new InvalidHierarchyException("roleCd"); } try { dao.insert("Security.insertRoleChild", row, false); } catch (UniqueConstraintException uce) { uce.setPropertyName("roleCd"); throw uce; } catch (NoParentRowFkException fke) { fke.setPropertyName("roleCd"); throw fke; } } public void modify(Object obj, Object row) { throw new UnsupportedOperationException("modify() unsupported"); } public void remove(Object obj, Object row) { ((RoleChildren) row).setParentCd(((Role) obj).getRoleCd()); dao.delete("Security.deleteRoleChild", row); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?