1 31 package org.blojsom.authorization.database; 32 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 import org.blojsom.ConfigurationException; 36 import org.blojsom.util.BlojsomUtils; 37 import org.blojsom.authorization.AuthorizationException; 38 import org.blojsom.authorization.AuthorizationProvider; 39 import org.blojsom.blog.Blog; 40 import org.blojsom.blog.database.DatabaseUser; 41 import org.hibernate.*; 42 import org.hibernate.criterion.Restrictions; 43 44 import java.util.Map ; 45 46 53 public class DatabaseAuthorizationProvider implements AuthorizationProvider { 54 55 private Log _logger = LogFactory.getLog(DatabaseAuthorizationProvider.class); 56 private static final String ALL_PERMISSIONS_PERMISSION = "all_permissions_permission"; 57 58 protected SessionFactory _sessionFactory; 59 60 63 public DatabaseAuthorizationProvider() { 64 } 65 66 72 public void init() throws ConfigurationException { 73 } 74 75 80 public void setSessionFactory(SessionFactory sessionFactory) { 81 _sessionFactory = sessionFactory; 82 } 83 84 94 public void authorize(Blog blog, Map authorizationContext, String userLogin, String password) throws AuthorizationException { 95 if (userLogin == null) { 96 throw new AuthorizationException("Username was null"); 97 } 98 99 if (password == null) { 100 throw new AuthorizationException("Password was null"); 101 } 102 103 try { 104 Session session = _sessionFactory.openSession(); 105 Transaction tx = session.beginTransaction(); 106 107 Criteria userCriteria = session.createCriteria(DatabaseUser.class); 108 userCriteria.add(Restrictions.eq("userLogin", userLogin)).add(Restrictions.eq("blogId", blog.getId())); 109 110 DatabaseUser user = (DatabaseUser) userCriteria.uniqueResult(); 111 112 if (user == null) { 113 tx.commit(); 114 session.close(); 115 116 throw new AuthorizationException("User login not found"); 117 } 118 119 tx.commit(); 120 session.close(); 121 122 if (blog.getUseEncryptedPasswords().booleanValue()) { 123 password = BlojsomUtils.digestString(password, blog.getDigestAlgorithm()); 124 } 125 126 if (!password.equals(user.getUserPassword())) { 127 throw new AuthorizationException("Password authorization failure"); 128 } 129 } catch (HibernateException e) { 130 if (_logger.isErrorEnabled()) { 131 _logger.error(e); 132 } 133 134 throw new AuthorizationException(e); 135 } 136 } 137 138 148 public void checkPermission(Blog blog, Map permissionContext, String userLogin, String permission) throws AuthorizationException { 149 if (userLogin == null) { 150 throw new AuthorizationException("Username was null"); 151 } 152 153 if (permission == null) { 154 throw new AuthorizationException("Password was null"); 155 } 156 157 try { 158 Session session = _sessionFactory.openSession(); 159 Transaction tx = session.beginTransaction(); 160 161 Criteria userCriteria = session.createCriteria(DatabaseUser.class); 162 userCriteria.add(Restrictions.eq("userLogin", userLogin)).add(Restrictions.eq("blogId", blog.getId())); 163 164 DatabaseUser user = (DatabaseUser) userCriteria.uniqueResult(); 165 166 tx.commit(); 167 session.close(); 168 169 if (!user.getMetaData().containsKey(ALL_PERMISSIONS_PERMISSION)) { 170 if (!user.getMetaData().containsKey(permission)) { 171 throw new AuthorizationException("Permission authorization failure"); 172 } 173 } 174 } catch (HibernateException e) { 175 if (_logger.isErrorEnabled()) { 176 _logger.error(e); 177 } 178 179 throw new AuthorizationException(e); 180 } 181 } 182 } 183 | Popular Tags |