Subversion Repositories SmartDukaan

Rev

Rev 23647 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3090 mandeep.dh 1
/**
2
 * 
3
 */
4
package in.shop2020.serving.auth;
5
 
6
import in.shop2020.crm.Agent;
7
import in.shop2020.crm.CRMService.Client;
3390 mandeep.dh 8
import in.shop2020.crm.SearchFilter;
3128 rajveer 9
import in.shop2020.thrift.clients.CRMClient;
3090 mandeep.dh 10
 
3390 mandeep.dh 11
import java.util.ArrayList;
12
import java.util.HashMap;
3090 mandeep.dh 13
import java.util.HashSet;
14
import java.util.List;
3390 mandeep.dh 15
import java.util.Map;
3090 mandeep.dh 16
import java.util.Set;
17
 
18
import org.apache.commons.logging.Log;
19
import org.apache.commons.logging.LogFactory;
20
import org.apache.shiro.authc.AuthenticationException;
21
import org.apache.shiro.authc.AuthenticationInfo;
22
import org.apache.shiro.authc.AuthenticationToken;
23
import org.apache.shiro.authc.SimpleAuthenticationInfo;
24
import org.apache.shiro.authc.UnknownAccountException;
25
import org.apache.shiro.authc.UsernamePasswordToken;
26
import org.apache.shiro.authz.AuthorizationException;
27
import org.apache.shiro.authz.AuthorizationInfo;
28
import org.apache.shiro.authz.SimpleAuthorizationInfo;
29
import org.apache.shiro.realm.AuthorizingRealm;
30
import org.apache.shiro.subject.PrincipalCollection;
31
import org.apache.thrift.TException;
32
 
33
/**
34
 * @author mandeep
35
 * 
3390 mandeep.dh 36
 *         This class is realm for fetching authentication and authorization
37
 *         details for an agent.
3090 mandeep.dh 38
 */
3206 mandeep.dh 39
public class CRMAuthorizingRealm extends AuthorizingRealm {
3390 mandeep.dh 40
    private static final Log   log = LogFactory
41
                                           .getLog(CRMAuthorizingRealm.class);
42
    private static Map<Long, Agent>   agentsMapById;
43
    private static Map<String, Agent> agentsMapByEmailId;
5286 amar.kumar 44
    private static Map<Long, Agent>   inactiveAgentsMapById;
45
    private static Map<String, Agent> inactiveAgentsMapByEmailId;
23648 amit.gupta 46
    private static Map<String, List<String>> userRoleNames = new HashMap<String, List<String>>();
47
    private static Map<String, Set<String>> userPermissions = new HashMap<String, Set<String>>();
5286 amar.kumar 48
 
3090 mandeep.dh 49
    @Override
3390 mandeep.dh 50
    protected AuthorizationInfo doGetAuthorizationInfo(
51
            PrincipalCollection principals) {
52
        // null usernames are invalid
3090 mandeep.dh 53
        if (principals == null) {
3390 mandeep.dh 54
            throw new AuthorizationException(
55
                    "PrincipalCollection method argument cannot be null.");
3090 mandeep.dh 56
        }
57
 
58
        String username = (String) getAvailablePrincipal(principals);
59
        List<String> roleNames = null;
23647 amit.gupta 60
        Set<String> permissions = null;
3090 mandeep.dh 61
 
62
        try {
3128 rajveer 63
            Client crmServiceClient = new CRMClient().getClient();
3090 mandeep.dh 64
 
65
            // Retrieve roles and permissions from database
23647 amit.gupta 66
            if(!userRoleNames.containsKey(username)) {
67
            	roleNames = crmServiceClient.getRoleNamesForAgent(username);
68
            	userRoleNames.put(username, roleNames);
69
            } else {
70
            	roleNames = userRoleNames.get(username);
3206 mandeep.dh 71
            }
23647 amit.gupta 72
            if(userPermissions.containsKey(username)) {
73
            	permissions = userPermissions.get(username);
74
            } else {
75
            	permissions = new HashSet<String>();
76
	            for (String roleName : roleNames) {
77
	                permissions.addAll(crmServiceClient
78
	                        .getPermissionsForRoleName(roleName));
79
	            }
80
            }
3090 mandeep.dh 81
        } catch (TException e) {
3390 mandeep.dh 82
            throw new AuthorizationException(
83
                    "Error fetching roles' information", e);
3090 mandeep.dh 84
        } catch (Exception e) {
85
            throw new AuthorizationException("Error creating CRM client", e);
86
        }
87
 
3390 mandeep.dh 88
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(
89
                new HashSet<String>(roleNames));
3090 mandeep.dh 90
        info.setStringPermissions(permissions);
91
        return info;
92
    }
93
 
94
    @Override
95
    protected AuthenticationInfo doGetAuthenticationInfo(
3390 mandeep.dh 96
            AuthenticationToken token) throws AuthenticationException {
3090 mandeep.dh 97
        SimpleAuthenticationInfo info = null;
98
 
99
        try {
3390 mandeep.dh 100
            UsernamePasswordToken upToken = (UsernamePasswordToken) token;
101
            String username = upToken.getUsername();
3090 mandeep.dh 102
 
3390 mandeep.dh 103
            log.info("Trying to fetch password for " + username);
104
            Agent agent = getAgent(username);
7059 kshitij.so 105
            if (agent != null && agent.isIs_active()) {
3090 mandeep.dh 106
                info = new SimpleAuthenticationInfo(username, agent.getPassword().toCharArray(), getName());
3390 mandeep.dh 107
            } else {
108
                throw new UnknownAccountException("No account found for user ["
109
                        + username + "]");
3090 mandeep.dh 110
            }
3390 mandeep.dh 111
        } catch (TException e) {
112
            log.info("Could not create CRM client", e);
3090 mandeep.dh 113
        }
114
 
115
        return info;
116
    }
3390 mandeep.dh 117
 
118
    public static Agent getAgent(String username) throws TException {
5286 amar.kumar 119
        if (agentsMapByEmailId == null || (!agentsMapByEmailId.containsKey(username)
120
        		&&!inactiveAgentsMapByEmailId.containsKey(username))) {
3390 mandeep.dh 121
            loadAgents();
122
        }
5286 amar.kumar 123
        if(agentsMapByEmailId.get(username)!=null) {
124
        	return agentsMapByEmailId.get(username);
125
        } else {
126
        	return inactiveAgentsMapByEmailId.get(username);
127
        }
3390 mandeep.dh 128
    }
129
 
130
    public static Agent getAgent(long agentId) throws TException {
5286 amar.kumar 131
        if (agentsMapById == null || (!agentsMapById.containsKey(agentId)
132
        		&&!inactiveAgentsMapById.containsKey(agentId))) {
3390 mandeep.dh 133
            loadAgents();
134
        }
5286 amar.kumar 135
 
136
        if(agentsMapById.get(agentId)!=null) {
137
        	return agentsMapById.get(agentId);
138
        } else {
139
        	return inactiveAgentsMapById.get(agentId);
140
        }
3390 mandeep.dh 141
    }
142
 
143
    private static void loadAgents() throws TException {
144
        Client crmServiceClient = new CRMClient().getClient();
145
        List<Agent> agents = crmServiceClient.getAgents(new SearchFilter());
5286 amar.kumar 146
        List<Agent> inactiveAgents = crmServiceClient.getInactiveAgents(new SearchFilter());
3390 mandeep.dh 147
        Map<Long, Agent> agentsMapByIdLocal = new HashMap<Long, Agent>();
148
        Map<String, Agent> agentsMapByEmailIdLocal = new HashMap<String, Agent>();
5286 amar.kumar 149
        Map<Long, Agent> inactiveAgentsMapByIdLocal = new HashMap<Long, Agent>();
150
        Map<String, Agent> inactiveAgentsMapByEmailIdLocal = new HashMap<String, Agent>();
3390 mandeep.dh 151
 
152
        for (Agent agent : agents) {
153
            agentsMapByIdLocal.put(agent.getId(), agent);
154
            agentsMapByEmailIdLocal.put(agent.getEmailId(), agent);
155
        }
5286 amar.kumar 156
 
157
        for (Agent agent : inactiveAgents) {
158
            inactiveAgentsMapByIdLocal.put(agent.getId(), agent);
159
            inactiveAgentsMapByEmailIdLocal.put(agent.getEmailId(), agent);
160
        }
3390 mandeep.dh 161
 
162
        synchronized(CRMAuthorizingRealm.class) {
163
            agentsMapById = agentsMapByIdLocal;
164
            agentsMapByEmailId = agentsMapByEmailIdLocal;
5286 amar.kumar 165
            inactiveAgentsMapById = inactiveAgentsMapByIdLocal;
166
            inactiveAgentsMapByEmailId = inactiveAgentsMapByEmailIdLocal;
3390 mandeep.dh 167
        }
168
    }
169
 
170
    public static List<Agent> getAgents() {
171
        return new ArrayList<Agent>(agentsMapById.values());
172
    }
4793 amar.kumar 173
 
174
    public static void removeAgent(Long id, String emailId) {
5286 amar.kumar 175
    	inactiveAgentsMapById.put(id, agentsMapById.get(id));
176
    	inactiveAgentsMapByEmailId.put(emailId, agentsMapByEmailId.get(emailId));
4793 amar.kumar 177
    	agentsMapById.remove(id);
178
    	agentsMapByEmailId.remove(emailId);
179
    }
180
 
181
    public static void addAgent(Agent agent) {
182
    	agentsMapById.put(agent.getId(), agent);
183
    	agentsMapByEmailId.put(agent.getEmailId(), agent);
184
    }
185
 
3090 mandeep.dh 186
}