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.NodeData; 20 import info.magnolia.cms.core.Path; 21 import info.magnolia.context.MgnlContext; 22 23 import java.util.Iterator ; 24 25 import javax.jcr.ItemNotFoundException; 26 import javax.jcr.PathNotFoundException; 27 import javax.jcr.RepositoryException; 28 29 import org.apache.commons.lang.StringUtils; 30 import org.slf4j.Logger; 31 import org.slf4j.LoggerFactory; 32 33 34 37 public class MgnlGroup implements Group { 38 39 42 public static Logger log = LoggerFactory.getLogger(MgnlGroup.class); 43 44 47 private static final String NODE_ROLES = "roles"; 49 private static final String NODE_GROUPS = "groups"; 51 54 private Content groupNode; 55 56 59 MgnlGroup(Content groupNode) { 60 this.groupNode = groupNode; 61 } 62 63 67 public String getName() { 68 return this.groupNode.getName(); 69 } 70 71 77 public void addRole(String roleName) throws UnsupportedOperationException , AccessDeniedException { 78 this.add(roleName, NODE_ROLES); 79 } 80 81 87 public void addGroup(String groupName) throws UnsupportedOperationException , AccessDeniedException { 88 this.add(groupName, NODE_GROUPS); 89 } 90 91 97 public void removeRole(String roleName) throws UnsupportedOperationException , AccessDeniedException { 98 this.remove(roleName, NODE_ROLES); 99 } 100 101 107 public void removeGroup(String groupName) throws UnsupportedOperationException , AccessDeniedException { 108 this.remove(groupName, NODE_GROUPS); 109 } 110 111 117 public boolean hasRole(String roleName) throws UnsupportedOperationException , AccessDeniedException { 118 return this.hasAny(roleName, NODE_ROLES); 119 } 120 121 126 private boolean hasAny(String name, String nodeName) { 127 try { 128 HierarchyManager hm; 129 if (StringUtils.equalsIgnoreCase(nodeName, NODE_ROLES)) { 130 hm = MgnlContext.getHierarchyManager(ContentRepository.USER_ROLES); 131 } 132 else { 133 hm = MgnlContext.getHierarchyManager(ContentRepository.USER_GROUPS); 134 } 135 136 Content node = groupNode.getContent(nodeName); 137 for (Iterator iter = node.getNodeDataCollection().iterator(); iter.hasNext();) { 138 NodeData nodeData = (NodeData) iter.next(); 139 try { 141 if (hm.getContentByUUID(nodeData.getString()).getName().equalsIgnoreCase(name)) { 142 return true; 143 } 144 } 145 catch (ItemNotFoundException e) { 146 if (log.isDebugEnabled()) { 147 log.debug("Role [ " + name + " ] does not exist in the ROLES repository"); 148 } 149 } 150 catch (IllegalArgumentException e) { 151 if (log.isDebugEnabled()) { 152 log.debug(nodeData.getHandle() + " has invalid value"); 153 } 154 } 155 } 156 } 157 catch (RepositoryException e) { 158 log.debug(e.getMessage(), e); 159 } 160 return false; 161 } 162 163 168 private void remove(String name, String nodeName) { 169 try { 170 HierarchyManager hm; 171 if (StringUtils.equalsIgnoreCase(nodeName, NODE_ROLES)) { 172 hm = MgnlContext.getHierarchyManager(ContentRepository.USER_ROLES); 173 } 174 else { 175 hm = MgnlContext.getHierarchyManager(ContentRepository.USER_GROUPS); 176 } 177 Content node = groupNode.getContent(nodeName); 178 179 for (Iterator iter = node.getNodeDataCollection().iterator(); iter.hasNext();) { 180 NodeData nodeData = (NodeData) iter.next(); 181 try { 183 if (hm.getContentByUUID(nodeData.getString()).getName().equalsIgnoreCase(name)) { 184 nodeData.delete(); 185 } 186 } 187 catch (ItemNotFoundException e) { 188 if (log.isDebugEnabled()) { 189 log.debug("Role [ " + name + " ] does not exist in the ROLES repository"); 190 } 191 } 192 catch (IllegalArgumentException e) { 193 if (log.isDebugEnabled()) { 194 log.debug(nodeData.getHandle() + " has invalid value"); 195 } 196 } 197 } 198 groupNode.save(); 199 } 200 catch (RepositoryException e) { 201 log.error("failed to remove " + name + " from user [" + this.getName() + "]", e); 202 } 203 } 204 205 208 private void add(String name, String nodeName) { 209 try { 210 HierarchyManager hm; 211 if (StringUtils.equalsIgnoreCase(nodeName, NODE_ROLES)) { 212 hm = MgnlContext.getHierarchyManager(ContentRepository.USER_ROLES); 213 } 214 else { 215 hm = MgnlContext.getHierarchyManager(ContentRepository.USER_GROUPS); 216 } 217 218 if (!this.hasAny(name, nodeName)) { 219 if (!groupNode.hasContent(nodeName)) { 220 groupNode.createContent(nodeName, ItemType.CONTENTNODE); 221 } 222 Content node = groupNode.getContent(nodeName); 223 try { 225 String value = hm.getContent("/" + name).getUUID(); HierarchyManager usersHM = ContentRepository.getHierarchyManager(ContentRepository.USERS); 228 String newName = Path.getUniqueLabel(usersHM, node.getHandle(), "0"); 229 node.createNodeData(newName).setValue(value); 230 groupNode.save(); 231 } 232 catch (PathNotFoundException e) { 233 if (log.isDebugEnabled()) { 234 log.debug("Role [ " + name + " ] does not exist in the ROLES repository"); 235 } 236 } 237 } 238 } 239 catch (RepositoryException e) { 240 log.error("failed to add " + name + " to user [" + this.getName() + "]", e); 241 } 242 } 243 244 247 protected HierarchyManager getHierarchyManager() { 248 return MgnlContext.getHierarchyManager(ContentRepository.USER_GROUPS); 249 } 250 251 } 252 | Popular Tags |