| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
package br.com.arsmachina.authentication.dao.hibernate; |
| 16 | |
|
| 17 | |
import java.util.List; |
| 18 | |
|
| 19 | |
import org.hibernate.Query; |
| 20 | |
import org.hibernate.Session; |
| 21 | |
import org.hibernate.SessionFactory; |
| 22 | |
|
| 23 | |
import br.com.arsmachina.authentication.controller.PasswordEncrypter; |
| 24 | |
import br.com.arsmachina.authentication.dao.UserDAO; |
| 25 | |
import br.com.arsmachina.authentication.entity.Role; |
| 26 | |
import br.com.arsmachina.authentication.entity.User; |
| 27 | |
import br.com.arsmachina.dao.SortCriterion; |
| 28 | |
import br.com.arsmachina.dao.hibernate.GenericDAOImpl; |
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
public class UserDAOImpl extends GenericDAOImpl<User, Integer> implements UserDAO { |
| 36 | |
|
| 37 | |
private PasswordEncrypter passwordEncrypter; |
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
public UserDAOImpl(SessionFactory sessionFactory, PasswordEncrypter passwordEncrypter) { |
| 46 | |
|
| 47 | 0 | super(sessionFactory); |
| 48 | |
|
| 49 | 0 | if (passwordEncrypter == null) { |
| 50 | 0 | throw new IllegalArgumentException("Parameter passwordEncrypter cannot be null"); |
| 51 | |
} |
| 52 | |
|
| 53 | 0 | } |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
public User findByLoginAndPassword(String login, String password) { |
| 62 | |
|
| 63 | 0 | Session session = getSession(); |
| 64 | |
|
| 65 | 0 | Query query = session.createQuery("from User where lowercase(login) = :login and " |
| 66 | |
+ "password = :password"); |
| 67 | |
|
| 68 | 0 | query.setParameter("login", login.toLowerCase()); |
| 69 | 0 | query.setParameter("password", passwordEncrypter.encrypt(password)); |
| 70 | |
|
| 71 | 0 | return (User) query.uniqueResult(); |
| 72 | |
|
| 73 | |
} |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
public User findByLogin(String login) { |
| 81 | |
|
| 82 | 0 | Session session = getSession(); |
| 83 | |
|
| 84 | 0 | Query query = session.createQuery("from User where lower(login) = :login"); |
| 85 | 0 | query.setParameter("login", login.toLowerCase()); |
| 86 | |
|
| 87 | 0 | return (User) query.uniqueResult(); |
| 88 | |
|
| 89 | |
} |
| 90 | |
|
| 91 | |
@SuppressWarnings("unchecked") |
| 92 | |
public <T extends Role> List<User> findByRole(Class<T> roleClass) { |
| 93 | |
|
| 94 | 0 | Query query = getSession().createQuery( |
| 95 | |
"select distinct(m.user) from Manager m order by m.user.login"); |
| 96 | |
|
| 97 | 0 | return query.list(); |
| 98 | |
|
| 99 | |
} |
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
@Override |
| 107 | |
public SortCriterion[] getDefaultSortCriteria() { |
| 108 | 0 | return Constants.ASCENDING_NAME_SORT_CRITERIA; |
| 109 | |
} |
| 110 | |
|
| 111 | |
public boolean hasUserWithLogin(String login) { |
| 112 | |
|
| 113 | 0 | Query query = getSession().createQuery( |
| 114 | |
"select count (distinct u) from User u where lower(login) = :login"); |
| 115 | 0 | query.setParameter("login", login.toLowerCase()); |
| 116 | |
|
| 117 | 0 | Long result = (Long) query.uniqueResult(); |
| 118 | |
|
| 119 | 0 | return result > 0; |
| 120 | |
|
| 121 | |
} |
| 122 | |
|
| 123 | |
} |