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.security.auth.Entity; 20 21 import java.util.ArrayList ; 22 import java.util.Collection ; 23 import java.util.Iterator ; 24 import java.util.Set ; 25 26 import javax.jcr.PathNotFoundException; 27 import javax.jcr.RepositoryException; 28 import javax.security.auth.Subject ; 29 30 import org.apache.commons.codec.binary.Base64; 31 import org.slf4j.Logger; 32 import org.slf4j.LoggerFactory; 33 34 35 40 public class MgnlUserManager implements UserManager { 41 42 45 public static Logger log = LoggerFactory.getLogger(MgnlUserManager.class); 46 47 50 public MgnlUserManager() { 51 } 52 53 58 public User getUser(String name) { 59 try { 60 Content node = getHierarchyManager().getContent(name); 61 MgnlUser user = new MgnlUser(node); 62 user.setLastAccess(); 63 return user; 64 } 65 catch (PathNotFoundException e) { 66 log.debug("User not found: [{}]", name); 67 return null; 68 } 69 catch (RepositoryException e) { 70 log.info("Unable to load user [" + name + "] due to: " + e.getClass().getName() 72 + " - " 73 + e.getMessage(), e); 74 return null; 75 } 76 } 77 78 82 public User getSystemUser() { 83 try { 84 return new MgnlUser(getHierarchyManager().getContent(UserManager.SYSTEM_USER)); 85 } 86 catch (Exception e) { 87 log.error("failed to get System user", e); 88 log.info("Try to create new system user with default password"); 89 return this.createUser(UserManager.SYSTEM_USER, UserManager.SYSTEM_PSWD); 90 } 91 } 92 93 97 public User getAnonymousUser() { 98 try { 99 return new MgnlUser(getHierarchyManager().getContent(UserManager.ANONYMOUS_USER)); 100 } 101 catch (Exception e) { 102 log.error("failed to get Anonymous user", e); 103 log.info("Try to create new system user with default password"); 104 return this.createUser(UserManager.ANONYMOUS_USER, ""); 105 } 106 } 107 108 111 public Collection getAllUsers() { 112 Collection users = new ArrayList (); 113 try { 114 Collection nodes = getHierarchyManager().getRoot().getChildren(ItemType.USER); 115 for (Iterator iter = nodes.iterator(); iter.hasNext();) { 116 users.add(new MgnlUser((Content) iter.next())); 117 } 118 } 119 catch (Exception e) { 120 log.error("can't find user"); 121 } 122 return users; 123 } 124 125 130 public User createUser(String name, String pw) { 131 try { 132 Content node; 133 node = getHierarchyManager().createContent("/", name, ItemType.USER.getSystemName()); 134 node.createNodeData("name").setValue(name); 135 node.createNodeData("pswd").setValue(new String (Base64.encodeBase64(pw.getBytes()))); 136 node.createNodeData("language").setValue("en"); 137 getHierarchyManager().save(); 138 return new MgnlUser(node); 139 } 140 catch (Exception e) { 141 log.info("can't create user [" + name + "]", e); 142 return null; 143 } 144 } 145 146 151 public User getUser(Subject subject) throws UnsupportedOperationException { 152 User user = null; 153 if (subject == null) { 155 return new DummyUser(); 156 } 157 158 Set principalSet = subject.getPrincipals(Entity.class); 159 Iterator entityIterator = principalSet.iterator(); 160 Entity userDetails = (Entity) entityIterator.next(); 161 String name = (String ) userDetails.getProperty(Entity.NAME); 162 try { 163 Content node = getHierarchyManager().getContent(name); 164 user = new MgnlUser(node); 165 ((MgnlUser) user).setLastAccess(); 166 } 167 catch (PathNotFoundException e) { 168 log.error("user not registered in magnolia itself [" + name + "]"); 169 } 170 catch (Exception e) { 171 log.error("can't get jcr-node of current user", e); 172 } 173 if (user == null) { 174 user = new DummyUser(); 175 } 176 177 return user; 178 } 179 180 183 protected HierarchyManager getHierarchyManager() { 184 return ContentRepository.getHierarchyManager(ContentRepository.USERS); 185 } 186 187 } 188 | Popular Tags |