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.cms.util.NodeDataUtil; 22 import info.magnolia.context.MgnlContext; 23 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.GregorianCalendar ; 27 import java.util.Iterator ; 28 29 import javax.jcr.ItemNotFoundException; 30 import javax.jcr.PathNotFoundException; 31 import javax.jcr.PropertyType; 32 import javax.jcr.RepositoryException; 33 34 import org.apache.commons.codec.binary.Base64; 35 import org.apache.commons.lang.StringUtils; 36 import org.slf4j.Logger; 37 import org.slf4j.LoggerFactory; 38 39 40 45 public class MgnlUser implements User { 46 47 public static Logger log = LoggerFactory.getLogger(User.class); 48 49 52 private static final String NODE_ROLES = "roles"; 54 private static final String NODE_GROUPS = "groups"; 56 59 private Content userNode; 60 61 64 protected MgnlUser(Content userNode) { 65 this.userNode = userNode; 66 } 67 68 73 public boolean inGroup(String groupName) { 74 return this.hasAny(groupName, NODE_GROUPS); 75 } 76 77 81 public void removeGroup(String groupName) throws UnsupportedOperationException { 82 this.remove(groupName, NODE_GROUPS); 83 } 84 85 89 public void addGroup(String groupName) throws UnsupportedOperationException { 90 this.add(groupName, NODE_GROUPS); 91 } 92 93 98 public boolean hasRole(String roleName) { 99 return this.hasAny(roleName, NODE_ROLES); 100 } 101 102 public void removeRole(String roleName) { 103 this.remove(roleName, NODE_ROLES); 104 } 105 106 110 public void addRole(String roleName) { 111 this.add(roleName, NODE_ROLES); 112 } 113 114 119 private boolean hasAny(String name, String nodeName) { 120 try { 121 HierarchyManager hm; 122 if (StringUtils.equalsIgnoreCase(nodeName, NODE_ROLES)) { 123 hm = MgnlContext.getHierarchyManager(ContentRepository.USER_ROLES); 124 } 125 else { 126 hm = MgnlContext.getHierarchyManager(ContentRepository.USER_GROUPS); 127 } 128 129 Content node = userNode.getContent(nodeName); 130 for (Iterator iter = node.getNodeDataCollection().iterator(); iter.hasNext();) { 131 NodeData nodeData = (NodeData) iter.next(); 132 try { 134 if (hm.getContentByUUID(nodeData.getString()).getName().equalsIgnoreCase(name)) { 135 return true; 136 } 137 } 138 catch (ItemNotFoundException e) { 139 if (log.isDebugEnabled()) { 140 log.debug("Role [ " + name + " ] does not exist in the ROLES repository"); 141 } 142 } 143 catch (IllegalArgumentException e) { 144 if (log.isDebugEnabled()) { 145 log.debug(nodeData.getHandle() + " has invalid value"); 146 } 147 } 148 } 149 } 150 catch (RepositoryException e) { 151 log.debug(e.getMessage(), e); 152 } 153 return false; 154 } 155 156 161 private void remove(String name, String nodeName) { 162 try { 163 HierarchyManager hm; 164 if (StringUtils.equalsIgnoreCase(nodeName, NODE_ROLES)) { 165 hm = MgnlContext.getHierarchyManager(ContentRepository.USER_ROLES); 166 } 167 else { 168 hm = MgnlContext.getHierarchyManager(ContentRepository.USER_GROUPS); 169 } 170 Content node = userNode.getContent(nodeName); 171 172 for (Iterator iter = node.getNodeDataCollection().iterator(); iter.hasNext();) { 173 NodeData nodeData = (NodeData) iter.next(); 174 try { 176 if (hm.getContentByUUID(nodeData.getString()).getName().equalsIgnoreCase(name)) { 177 nodeData.delete(); 178 } 179 } 180 catch (ItemNotFoundException e) { 181 if (log.isDebugEnabled()) { 182 log.debug("Role [ " + name + " ] does not exist in the ROLES repository"); 183 } 184 } 185 catch (IllegalArgumentException e) { 186 if (log.isDebugEnabled()) { 187 log.debug(nodeData.getHandle() + " has invalid value"); 188 } 189 } 190 } 191 userNode.save(); 192 } 193 catch (RepositoryException e) { 194 log.error("failed to remove " + name + " from user [" + this.getName() + "]", e); 195 } 196 } 197 198 201 private void add(String name, String nodeName) { 202 try { 203 HierarchyManager hm; 204 if (StringUtils.equalsIgnoreCase(nodeName, NODE_ROLES)) { 205 hm = MgnlContext.getHierarchyManager(ContentRepository.USER_ROLES); 206 } 207 else { 208 hm = MgnlContext.getHierarchyManager(ContentRepository.USER_GROUPS); 209 } 210 211 if (!this.hasAny(name, nodeName)) { 212 if (!userNode.hasContent(nodeName)) { 213 userNode.createContent(nodeName, ItemType.CONTENTNODE); 214 } 215 Content node = userNode.getContent(nodeName); 216 try { 218 String value = hm.getContent("/" + name).getUUID(); HierarchyManager usersHM = ContentRepository.getHierarchyManager(ContentRepository.USERS); 221 String newName = Path.getUniqueLabel(usersHM, node.getHandle(), "0"); 222 node.createNodeData(newName).setValue(value); 223 userNode.save(); 224 } 225 catch (PathNotFoundException e) { 226 if (log.isDebugEnabled()) { 227 log.debug("Role [ " + name + " ] does not exist in the ROLES repository"); 228 } 229 } 230 } 231 } 232 catch (RepositoryException e) { 233 log.error("failed to add " + name + " to user [" + this.getName() + "]", e); 234 } 235 } 236 237 241 public String getName() { 242 return this.userNode.getName(); 243 } 244 245 249 public String getPassword() { 250 String pswd = this.userNode.getNodeData("pswd").getString().trim(); 251 return new String (Base64.decodeBase64(pswd.getBytes())); 252 } 253 254 257 public String getLanguage() { 258 return userNode.getNodeData("language").getString(); } 260 261 public Collection getGroups() { 262 ArrayList list = new ArrayList (); 263 264 try { 265 Content groups = null; 266 try { 267 groups = userNode.getContent("groups"); 269 } 270 catch (javax.jcr.PathNotFoundException e) { 271 log.warn("the user " + getName() + " does have not groups node"); 272 } 273 274 if (groups != null) { 275 Collection c = groups.getNodeDataCollection(); 276 Iterator it = c.iterator(); 277 while (it.hasNext()) { 278 NodeData nd = (NodeData) it.next(); 279 String uuid = nd.getString(); 280 Content group = MgnlContext 281 .getSystemContext() 282 .getHierarchyManager(ContentRepository.USER_GROUPS) 283 .getContentByUUID(uuid); 284 list.add(group.getName()); 285 } 286 } 287 288 } 289 catch (Exception e) { 290 log.warn("cant read groups of user.", e); 291 } 292 293 return list; 294 } 295 296 public Collection getRoles() { 297 ArrayList list = new ArrayList (); 298 299 try { 300 Content roles = null; 301 try { 302 roles = userNode.getContent("roles"); 304 } 305 catch (javax.jcr.PathNotFoundException e) { 306 log.warn("the user " + getName() + " does have not roles node"); 307 } 308 309 if (roles != null) { 310 Collection c = roles.getNodeDataCollection(); 311 Iterator it = c.iterator(); 312 while (it.hasNext()) { 313 NodeData nd = (NodeData) it.next(); 314 String uuid = nd.getString(); 315 Content role = MgnlContext 316 .getSystemContext() 317 .getHierarchyManager(ContentRepository.USER_ROLES) 318 .getContentByUUID(uuid); 319 list.add(role.getName()); 320 } 321 } 322 323 } 324 catch (Exception e) { 325 log.warn("can't read roles of user.", e); 326 } 327 328 return list; 329 } 330 331 334 public void setLastAccess() { 335 336 NodeData lastaccess; 337 try { 338 lastaccess = NodeDataUtil.getOrCreate(userNode, "lastaccess", PropertyType.DATE); 339 synchronized (lastaccess) { 340 lastaccess.setValue(new GregorianCalendar ()); 341 lastaccess.save(); 342 } 343 } 344 catch (RepositoryException e) { 345 log.debug( 346 "Unable to set the last access date due to a " + e.getClass().getName() + " - " + e.getMessage(), 347 e); 348 } 349 350 } 351 } | Popular Tags |