001: package ru.emdev.EmForge.security.dao;
002:
003: import java.util.Collection;
004: import java.util.Date;
005: import java.util.List;
006:
007: import org.acegisecurity.userdetails.UserDetails;
008: import org.acegisecurity.userdetails.UsernameNotFoundException;
009: import org.emforge.projectmanager.base.ProjectRole;
010: import org.springframework.dao.DataAccessException;
011: import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
012:
013: import ru.emdev.EmForge.security.EmForgeUserDetailsService;
014: import ru.emdev.EmForge.security.web.SiteRole;
015:
016: /**
017: *
018: */
019: public class UserDao extends HibernateDaoSupport implements
020: EmForgeUserDetailsService {
021:
022: /**
023: * @see org.springframework.dao.support.DaoSupport#initDao()
024: */
025: @Override
026: protected void initDao() throws Exception {
027:
028: super .initDao();
029:
030: // initialize default site roles
031: for (SiteRole role : SiteRole.values()) {
032: initializeRole(role.getId(), role.getName());
033: }
034: }
035:
036: /**
037: * @return all existing roles
038: */
039: @SuppressWarnings("unchecked")
040: public Collection<Role> getAllRoles() {
041:
042: Collection roles = getHibernateTemplate().loadAll(Role.class);
043:
044: return roles;
045: }
046:
047: /**
048: * @see ru.emdev.EmForge.security.EmForgeUserDetailsService#getAllUsers()
049: */
050: @SuppressWarnings("unchecked")
051: public Collection<UserDetails> getAllUsers() {
052:
053: Collection users = getHibernateTemplate().loadAll(User.class);
054:
055: return users;
056: }
057:
058: /**
059: * @see org.acegisecurity.userdetails.UserDetailsService#loadUserByUsername(java.lang.String)
060: */
061: public UserDetails loadUserByUsername(String username)
062: throws UsernameNotFoundException, DataAccessException {
063:
064: List users = getHibernateTemplate().findByNamedQuery(
065: "User.loadUserByName", username);
066:
067: // get firstelement
068: if (users.size() > 0) {
069: return (UserDetails) users.get(0);
070: } else {
071: throw new UsernameNotFoundException("Username: " + username
072: + " not found in database");
073: }
074: }
075:
076: /**
077: * @param i_user
078: */
079: public void saveUser(User i_user) {
080:
081: if (i_user.getId() == null) {
082: i_user.setRegisteredAt(new Date());
083: }
084: getHibernateTemplate().saveOrUpdate(i_user);
085: getHibernateTemplate().flush();
086: }
087:
088: /**
089: * @param i_role
090: */
091: public void saveRole(Role i_role) {
092:
093: getHibernateTemplate().saveOrUpdate(i_role);
094: }
095:
096: /**
097: * Seems like we should do it manually, not delegate this to hibernate or db
098: *
099: * @param i_role
100: */
101: public void deleteRole(Role i_role) {
102:
103: getHibernateTemplate().deleteAll(
104: getHibernateTemplate().findByNamedQuery(
105: "ProjectRole.findByRole", i_role.getId()));
106: getHibernateTemplate().deleteAll(
107: getHibernateTemplate().findByNamedQuery(
108: "ProjectUserRole.findByRole", i_role.getId()));
109: getHibernateTemplate().delete(i_role);
110: getHibernateTemplate().flush();
111: }
112:
113: /**
114: * @param projectRole
115: */
116: public void saveProjectRole(ProjectRole projectRole) {
117:
118: getHibernateTemplate().saveOrUpdate(projectRole);
119: }
120:
121: /**
122: * @param i_roleName is role name to search
123: * @return <code>Role</code> if specified role exists, otherwise <code>null</code>
124: */
125: public Role getRoleByName(String i_roleName) {
126:
127: List<?> roles = getHibernateTemplate().findByNamedQuery(
128: "Role.loadRoleByName", i_roleName);
129:
130: // get first element
131: if (roles.size() > 0) {
132: return (Role) roles.get(0);
133: } else {
134: return null;
135: }
136: }
137:
138: /**
139: * Creates required role if it does not exist and return it
140: *
141: * @param i_roleName is role name to get
142: * @return Required <code>Role</code>
143: */
144: public Role ensureRole(String i_roleName) {
145:
146: Role role = getRoleByName(i_roleName);
147: if (role == null) {
148: role = new Role();
149: role.setName(i_roleName);
150: saveRole(role);
151: }
152: return role;
153: }
154:
155: /**
156: * @param rolename
157: * @param projectId
158: * @return
159: */
160: public ProjectRole getEmailByRoleNameAndProjectId(String rolename,
161: Long projectId) {
162:
163: List projectRoles = getHibernateTemplate().findByNamedQuery(
164: "ProjectRole.loadEmailByRoleNameAndProjectId",
165: new Object[] { rolename, projectId });
166: // get first element
167: if (projectRoles.size() > 0) {
168: return (ProjectRole) projectRoles.get(0);
169: } else {
170: return null;
171: }
172: }
173:
174: /**
175: * Activate User by Activation Code
176: *
177: * @returns true if user with specified code was found and activated, false - if not
178: */
179: public boolean activateUser(String activationCode) {
180:
181: List users = getHibernateTemplate().findByNamedQuery(
182: "User.loadUserByCode", activationCode);
183:
184: // get firstelement
185: if (users.size() == 1) {
186: User user = (User) users.get(0);
187: user.setActive(true);
188: saveUser(user);
189:
190: return true;
191: } else {
192: return false;
193: }
194: }
195:
196: /**
197: * @param i_roleName
198: * @param i_roleDisplayName
199: */
200: protected void initializeRole(String i_roleName,
201: String i_roleDisplayName) {
202:
203: if (getRoleByName(i_roleName) == null) {
204: Role role = new Role();
205: role.setName(i_roleName);
206: role.setDisplayName(i_roleDisplayName);
207: role.setRoleType(Role.ROLE_TYPE_SITE);
208:
209: saveRole(role);
210: }
211: }
212:
213: }
|