001: package ru.emdev.EmForge.security;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005:
006: import org.acegisecurity.GrantedAuthority;
007: import org.acegisecurity.context.SecurityContextHolder;
008:
009: import ru.emdev.EmForge.security.dao.Role;
010: import ru.emdev.EmForge.security.dao.UserDao;
011:
012: /**
013: *
014: */
015: public class UserFactoryImpl implements UserFactory {
016:
017: EmForgeUserDetailsService m_userDetailsService;
018: UserDao m_userDao;
019:
020: /**
021: * @param i_userDetailsService
022: */
023: public void setUserDetailsService(
024: EmForgeUserDetailsService i_userDetailsService) {
025:
026: m_userDetailsService = i_userDetailsService;
027: }
028:
029: /**
030: * @param i_userDao
031: */
032: public void setUserDao(UserDao i_userDao) {
033:
034: m_userDao = i_userDao;
035: }
036:
037: /**
038: * @see ru.emdev.EmForge.security.UserFactory#getCurrentUser()
039: */
040: public EmForgeUserDetails getCurrentUser() {
041: if (SecurityContextHolder.getContext().getAuthentication() != null) {
042: return (EmForgeUserDetails) SecurityContextHolder
043: .getContext().getAuthentication().getPrincipal();
044: } else {
045: return null;
046: }
047: }
048:
049: /**
050: * @see ru.emdev.EmForge.security.UserFactory#getCurrentPassword()
051: */
052: public String getCurrentPassword() {
053:
054: return SecurityContextHolder.getContext().getAuthentication()
055: .getCredentials().toString();
056: }
057:
058: /**
059: * @see ru.emdev.EmForge.security.UserFactory#getUser(java.lang.String)
060: */
061: public EmForgeUserDetails getUser(String i_userName) {
062:
063: return (EmForgeUserDetails) m_userDetailsService
064: .loadUserByUsername(i_userName);
065: }
066:
067: /**
068: * Not implemented yet
069: *
070: * @see ru.emdev.EmForge.security.UserFactory#getUsers()
071: */
072: @SuppressWarnings("unchecked")
073: public Collection<EmForgeUserDetails> getUsers() {
074:
075: Collection users = m_userDetailsService.getAllUsers();
076:
077: return (Collection<EmForgeUserDetails>) users;
078: }
079:
080: /**
081: * @see ru.emdev.EmForge.security.UserFactory#getUsers(java.lang.String)
082: * @todo Can be improved to get required users not by {@link #getUsers()}
083: */
084: public Collection<EmForgeUserDetails> getUsers(String i_role) {
085:
086: Collection<EmForgeUserDetails> result = new ArrayList<EmForgeUserDetails>();
087:
088: for (EmForgeUserDetails user : getUsers()) {
089: if (user.hasRole(i_role)) {
090: result.add(user);
091: }
092: }
093:
094: return result;
095: }
096:
097: /**
098: * Returns try if current user has role with specified name
099: *
100: * @param i_role
101: * @return
102: */
103: public boolean hasRole(String i_role) {
104:
105: EmForgeUserDetails currentUser = getCurrentUser();
106:
107: if (currentUser == null) {
108: return false;
109: }
110:
111: GrantedAuthority[] roles = currentUser.getAuthorities();
112:
113: for (GrantedAuthority role : roles) {
114: if (role.getAuthority().equals(i_role)) {
115: return true;
116: }
117: }
118:
119: return false;
120: }
121:
122: /**
123: * @see ru.emdev.EmForge.security.UserFactory#getRoles()
124: */
125: public Collection<Role> getRoles() {
126:
127: return m_userDao.getAllRoles();
128: }
129:
130: /**
131: * @see ru.emdev.EmForge.security.UserFactory#getRole(java.lang.String)
132: */
133: public Role getRole(String i_roleName) {
134:
135: return m_userDao.getRoleByName(i_roleName);
136: }
137: }
|