Subversion Repositories SmartDukaan

Rev

Rev 3206 | Rev 4793 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3206 Rev 3390
Line 3... Line 3...
3
 */
3
 */
4
package in.shop2020.serving.auth;
4
package in.shop2020.serving.auth;
5
 
5
 
6
import in.shop2020.crm.Agent;
6
import in.shop2020.crm.Agent;
7
import in.shop2020.crm.CRMService.Client;
7
import in.shop2020.crm.CRMService.Client;
-
 
8
import in.shop2020.crm.SearchFilter;
8
import in.shop2020.thrift.clients.CRMClient;
9
import in.shop2020.thrift.clients.CRMClient;
9
 
10
 
-
 
11
import java.util.ArrayList;
-
 
12
import java.util.HashMap;
10
import java.util.HashSet;
13
import java.util.HashSet;
11
import java.util.List;
14
import java.util.List;
-
 
15
import java.util.Map;
12
import java.util.Set;
16
import java.util.Set;
13
 
17
 
14
import org.apache.commons.logging.Log;
18
import org.apache.commons.logging.Log;
15
import org.apache.commons.logging.LogFactory;
19
import org.apache.commons.logging.LogFactory;
16
import org.apache.shiro.authc.AuthenticationException;
20
import org.apache.shiro.authc.AuthenticationException;
Line 27... Line 31...
27
import org.apache.thrift.TException;
31
import org.apache.thrift.TException;
28
 
32
 
29
/**
33
/**
30
 * @author mandeep
34
 * @author mandeep
31
 * 
35
 * 
32
 * This class is realm for fetching authentication and authorization details for an agent.
36
 *         This class is realm for fetching authentication and authorization
-
 
37
 *         details for an agent.
33
 */
38
 */
34
public class CRMAuthorizingRealm extends AuthorizingRealm {
39
public class CRMAuthorizingRealm extends AuthorizingRealm {
-
 
40
    private static final Log   log = LogFactory
35
    private static final Log log = LogFactory.getLog(CRMAuthorizingRealm.class);
41
                                           .getLog(CRMAuthorizingRealm.class);
-
 
42
    private static Map<Long, Agent>   agentsMapById;
-
 
43
    private static Map<String, Agent> agentsMapByEmailId;
36
 
44
 
37
    @Override
45
    @Override
38
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
46
    protected AuthorizationInfo doGetAuthorizationInfo(
-
 
47
            PrincipalCollection principals) {
39
        //null usernames are invalid
48
        // null usernames are invalid
40
        if (principals == null) {
49
        if (principals == null) {
-
 
50
            throw new AuthorizationException(
41
            throw new AuthorizationException("PrincipalCollection method argument cannot be null.");
51
                    "PrincipalCollection method argument cannot be null.");
42
        }
52
        }
43
 
53
 
44
        String username = (String) getAvailablePrincipal(principals);
54
        String username = (String) getAvailablePrincipal(principals);
45
        List<String> roleNames = null;
55
        List<String> roleNames = null;
46
        Set<String> permissions = new HashSet<String>();
56
        Set<String> permissions = new HashSet<String>();
Line 50... Line 60...
50
 
60
 
51
            // Retrieve roles and permissions from database
61
            // Retrieve roles and permissions from database
52
            roleNames = crmServiceClient.getRoleNamesForAgent(username);
62
            roleNames = crmServiceClient.getRoleNamesForAgent(username);
53
 
63
 
54
            for (String roleName : roleNames) {
64
            for (String roleName : roleNames) {
-
 
65
                permissions.addAll(crmServiceClient
55
                permissions.addAll(crmServiceClient.getPermissionsForRoleName(roleName));
66
                        .getPermissionsForRoleName(roleName));
56
            }
67
            }
57
        } catch (TException e) {
68
        } catch (TException e) {
-
 
69
            throw new AuthorizationException(
58
            throw new AuthorizationException("Error fetching roles' information", e);
70
                    "Error fetching roles' information", e);
59
        } catch (Exception e) {
71
        } catch (Exception e) {
60
            throw new AuthorizationException("Error creating CRM client", e);
72
            throw new AuthorizationException("Error creating CRM client", e);
61
        }
73
        }
62
 
74
 
63
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(new HashSet<String>(roleNames));
75
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(
-
 
76
                new HashSet<String>(roleNames));
64
        info.setStringPermissions(permissions);
77
        info.setStringPermissions(permissions);
65
        return info;
78
        return info;
66
    }
79
    }
67
 
80
 
68
    @Override
81
    @Override
69
    protected AuthenticationInfo doGetAuthenticationInfo(
82
    protected AuthenticationInfo doGetAuthenticationInfo(
70
            AuthenticationToken token) throws AuthenticationException
83
            AuthenticationToken token) throws AuthenticationException {
71
    {
-
 
72
        UsernamePasswordToken upToken = (UsernamePasswordToken) token;
-
 
73
        String username = upToken.getUsername();
-
 
74
        SimpleAuthenticationInfo info = null;
84
        SimpleAuthenticationInfo info = null;
75
 
85
 
76
        log.info("Trying to fetch passowrd for " + username);
-
 
77
        try {
86
        try {
78
            Client crmServiceClient = new CRMClient().getClient();
87
            UsernamePasswordToken upToken = (UsernamePasswordToken) token;
79
            Agent agent = crmServiceClient.getAgentByEmailId(username);
88
            String username = upToken.getUsername();
80
 
89
 
-
 
90
            log.info("Trying to fetch password for " + username);
-
 
91
            Agent agent = getAgent(username);
81
            if (agent != null) {
92
            if (agent != null) {
82
                info = new SimpleAuthenticationInfo(username, agent.getPassword().toCharArray(), getName());
93
                info = new SimpleAuthenticationInfo(username, agent.getPassword().toCharArray(), getName());
-
 
94
            } else {
-
 
95
                throw new UnknownAccountException("No account found for user ["
-
 
96
                        + username + "]");
83
            }
97
            }
84
            else {
-
 
85
                throw new UnknownAccountException("No account found for user [" + username + "]");
-
 
86
            }
-
 
87
        }
-
 
88
        catch (Exception e) {
98
        } catch (TException e) {
89
            String error = "Error while creating CRM client";
99
            log.info("Could not create CRM client", e);
90
            log.error(error, e);
-
 
91
            throw new AuthenticationException(error, e);
-
 
92
        }
100
        }
93
 
101
 
94
        return info;
102
        return info;
95
    }
103
    }
-
 
104
 
-
 
105
    public static Agent getAgent(String username) throws TException {
-
 
106
        if (agentsMapByEmailId == null || !agentsMapByEmailId.containsKey(username)) {
-
 
107
            loadAgents();
-
 
108
        }
-
 
109
 
-
 
110
        return agentsMapByEmailId.get(username);
-
 
111
    }
-
 
112
 
-
 
113
    public static Agent getAgent(long agentId) throws TException {
-
 
114
        if (agentsMapById == null || !agentsMapById.containsKey(agentId)) {
-
 
115
            loadAgents();
-
 
116
        }
-
 
117
 
-
 
118
        return agentsMapById.get(agentId);
-
 
119
    }
-
 
120
 
-
 
121
    private static void loadAgents() throws TException {
-
 
122
        Client crmServiceClient = new CRMClient().getClient();
-
 
123
        List<Agent> agents = crmServiceClient.getAgents(new SearchFilter());
-
 
124
        Map<Long, Agent> agentsMapByIdLocal = new HashMap<Long, Agent>();
-
 
125
        Map<String, Agent> agentsMapByEmailIdLocal = new HashMap<String, Agent>();
-
 
126
 
-
 
127
        for (Agent agent : agents) {
-
 
128
            agentsMapByIdLocal.put(agent.getId(), agent);
-
 
129
            agentsMapByEmailIdLocal.put(agent.getEmailId(), agent);
-
 
130
        }
-
 
131
 
-
 
132
        synchronized(CRMAuthorizingRealm.class) {
-
 
133
            agentsMapById = agentsMapByIdLocal;
-
 
134
            agentsMapByEmailId = agentsMapByEmailIdLocal;
-
 
135
        }
-
 
136
    }
-
 
137
 
-
 
138
    public static List<Agent> getAgents() {
-
 
139
        return new ArrayList<Agent>(agentsMapById.values());
-
 
140
    }
96
}
141
}