1 24 package org.riotfamily.riot.hibernate.security; 25 26 import org.hibernate.Criteria; 27 import org.hibernate.criterion.Restrictions; 28 import org.riotfamily.common.beans.PropertyUtils; 29 import org.riotfamily.common.util.HashUtils; 30 import org.riotfamily.riot.hibernate.dao.HqlDao; 31 import org.riotfamily.riot.list.support.ListParamsImpl; 32 import org.riotfamily.riot.security.auth.RiotUser; 33 import org.riotfamily.riot.security.auth.RiotUserDao; 34 import org.springframework.beans.factory.InitializingBean; 35 import org.springframework.util.Assert; 36 37 43 public class HibernateUserDao extends HqlDao implements RiotUserDao, 44 InitializingBean { 45 46 public static final String DEFAULT_USERNAME = "admin"; 47 48 public static final String DEFAULT_PASSWORD = "admin"; 49 50 private String usernameProperty = "id"; 51 52 private String passwordProperty = "password"; 53 54 private String newPasswordProperty = "newPassword"; 55 56 private boolean hashPasswords = true; 57 58 private RiotUser initialUser; 59 60 65 public void setEntityClass(Class entityClass) { 66 Assert.isAssignable(RiotUser.class, entityClass); 67 super.setEntityClass(entityClass); 68 } 69 70 75 public void setUsernameProperty(String usernameProperty) { 76 Assert.notNull(usernameProperty); 77 this.usernameProperty = usernameProperty; 78 } 79 80 85 public void setPasswordProperty(String passwordProperty) { 86 Assert.notNull(passwordProperty); 87 this.passwordProperty = passwordProperty; 88 } 89 90 93 public void setHashPasswords(boolean hashPasswords) { 94 this.hashPasswords = hashPasswords; 95 } 96 97 103 public void setNewPasswordProperty(String newPasswordProperty) { 104 this.newPasswordProperty = newPasswordProperty; 105 } 106 107 112 public void setInitialUser(RiotUser initialUser) { 113 this.initialUser = initialUser; 114 } 115 116 public void afterPropertiesSet() throws Exception { 117 if (initialUser != null) { 118 Assert.isInstanceOf(getEntityClass(), initialUser); 119 } 120 else { 121 initialUser = (RiotUser) getEntityClass().newInstance(); 122 PropertyUtils.setProperty(initialUser, usernameProperty, 123 DEFAULT_USERNAME); 124 125 String password = hashPasswords 126 ? HashUtils.md5(DEFAULT_PASSWORD) 127 : DEFAULT_PASSWORD; 128 129 PropertyUtils.setProperty(initialUser, passwordProperty, password); 130 } 131 } 132 133 public RiotUser findUserByCredentials(String username, String password) { 134 if (hashPasswords) { 135 password = HashUtils.md5(password); 136 } 137 Criteria c = createCriteria(getEntityClass()) 138 .add(Restrictions.eq(usernameProperty, username)) 139 .add(Restrictions.eq(passwordProperty, password)); 140 141 RiotUser user = (RiotUser) c.uniqueResult(); 142 if (user == null && !anyUserExists()) { 143 save(initialUser, null); 144 String initialUsername = PropertyUtils.getPropertyAsString(initialUser, usernameProperty); 145 String initialPassword = PropertyUtils.getPropertyAsString(initialUser, passwordProperty); 146 if (initialUsername.equals(username) 147 && initialPassword.equals(password)) { 148 149 return initialUser; 150 } 151 } 152 return user; 153 } 154 155 protected boolean anyUserExists() { 156 return getListSize(null, new ListParamsImpl()) > 0; 157 } 158 159 protected void hashNewPassword(Object user) { 160 if (hashPasswords) { 161 String newPassword = PropertyUtils.getPropertyAsString(user, newPasswordProperty); 162 if (newPassword != null) { 163 String hash = HashUtils.md5(newPassword); 164 PropertyUtils.setProperty(user, passwordProperty, hash); 165 PropertyUtils.setProperty(user, newPasswordProperty, null); 166 } 167 } 168 } 169 170 public void save(Object entity, Object parent) { 171 hashNewPassword(entity); 172 super.save(entity, parent); 173 } 174 175 public void update(Object entity) { 176 hashNewPassword(entity); 177 super.update(entity); 178 } 179 180 } 181 | Popular Tags |