1 13 package info.magnolia.cms.security; 14 15 import info.magnolia.cms.beans.config.ContentRepository; 16 import info.magnolia.cms.core.Content; 17 import info.magnolia.cms.core.HierarchyManager; 18 import info.magnolia.cms.core.ItemType; 19 import info.magnolia.cms.core.Path; 20 21 import java.util.Collection ; 22 import java.util.Iterator ; 23 24 import javax.jcr.PathNotFoundException; 25 import javax.jcr.RepositoryException; 26 27 import org.slf4j.Logger; 28 import org.slf4j.LoggerFactory; 29 30 31 36 public class MgnlRole implements Role { 37 38 public static Logger log = LoggerFactory.getLogger(MgnlRole.class); 39 40 43 public static long PERMISSION_ANY = -1; 44 45 48 private Content roleNode; 49 50 53 protected MgnlRole(Content roleNode) { 54 super(); 55 this.roleNode = roleNode; 56 } 57 58 public String getName() { 59 return roleNode.getName(); 60 } 61 62 public void addPermission(String repository, String path, long permission) { 63 try { 64 Content aclNode = getAclNode(repository); 65 if (!this.existsPermission(aclNode, path, permission)) { 66 HierarchyManager hm = ContentRepository.getHierarchyManager(ContentRepository.USER_ROLES); 67 String nodename = Path.getUniqueLabel(hm, aclNode.getHandle(), "0"); 68 Content node = aclNode.createContent(nodename, ItemType.CONTENTNODE); 69 node.createNodeData("path").setValue(path); 70 node.createNodeData("permissions").setValue(String.valueOf(permission)); 71 roleNode.save(); 72 } 73 } 74 catch (Exception e) { 75 log.error("can't add permission", e); 76 } 77 } 78 79 public void removePermission(String repository, String path) { 80 this.removePermission(repository, path, MgnlRole.PERMISSION_ANY); 81 } 82 83 public void removePermission(String repository, String path, long permission) { 84 try { 85 Content aclNode = getAclNode(repository); 86 Collection children = aclNode.getChildren(); 87 for (Iterator iter = children.iterator(); iter.hasNext();) { 88 Content child = (Content) iter.next(); 89 if (child.getNodeData("path").getString().equals("path")) { 90 if (permission == MgnlRole.PERMISSION_ANY 91 || child.getNodeData("permissions").getString().equals(String.valueOf(permission))) { 92 child.delete(); 93 } 94 } 95 } 96 roleNode.save(); 97 } 98 catch (Exception e) { 99 log.error("can't remove permission", e); 100 } 101 } 102 103 111 private Content getAclNode(String repository) throws RepositoryException, PathNotFoundException, 112 AccessDeniedException { 113 Content aclNode; 114 if (!roleNode.hasContent("acl_" + repository)) { 115 aclNode = roleNode.createContent("acl_" + repository, ItemType.CONTENTNODE); 116 } 117 else { 118 aclNode = roleNode.getContent("acl_" + repository); 119 } 120 return aclNode; 121 } 122 123 129 private boolean existsPermission(Content aclNode, String path, long permission) { 130 Collection children = aclNode.getChildren(); 131 for (Iterator iter = children.iterator(); iter.hasNext();) { 132 Content child = (Content) iter.next(); 133 if (child.getNodeData("path").getString().equals(path)) { 134 if (permission == MgnlRole.PERMISSION_ANY 135 || child.getNodeData("permission").getString().equals(String.valueOf(permission))) { 136 return true; 137 } 138 } 139 } 140 return false; 141 } 142 } 143 | Popular Tags |