Subversion Repositories SmartDukaan

Rev

Rev 5286 | Rev 23647 | Go to most recent revision | 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;
46
 
3090 mandeep.dh 47
    @Override
3390 mandeep.dh 48
    protected AuthorizationInfo doGetAuthorizationInfo(
49
            PrincipalCollection principals) {
50
        // null usernames are invalid
3090 mandeep.dh 51
        if (principals == null) {
3390 mandeep.dh 52
            throw new AuthorizationException(
53
                    "PrincipalCollection method argument cannot be null.");
3090 mandeep.dh 54
        }
55
 
56
        String username = (String) getAvailablePrincipal(principals);
57
        List<String> roleNames = null;
3206 mandeep.dh 58
        Set<String> permissions = new HashSet<String>();
3090 mandeep.dh 59
 
60
        try {
3128 rajveer 61
            Client crmServiceClient = new CRMClient().getClient();
3090 mandeep.dh 62
 
63
            // Retrieve roles and permissions from database
64
            roleNames = crmServiceClient.getRoleNamesForAgent(username);
65
 
3206 mandeep.dh 66
            for (String roleName : roleNames) {
3390 mandeep.dh 67
                permissions.addAll(crmServiceClient
68
                        .getPermissionsForRoleName(roleName));
3206 mandeep.dh 69
            }
3090 mandeep.dh 70
        } catch (TException e) {
3390 mandeep.dh 71
            throw new AuthorizationException(
72
                    "Error fetching roles' information", e);
3090 mandeep.dh 73
        } catch (Exception e) {
74
            throw new AuthorizationException("Error creating CRM client", e);
75
        }
76
 
3390 mandeep.dh 77
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(
78
                new HashSet<String>(roleNames));
3090 mandeep.dh 79
        info.setStringPermissions(permissions);
80
        return info;
81
    }
82
 
83
    @Override
84
    protected AuthenticationInfo doGetAuthenticationInfo(
3390 mandeep.dh 85
            AuthenticationToken token) throws AuthenticationException {
3090 mandeep.dh 86
        SimpleAuthenticationInfo info = null;
87
 
88
        try {
3390 mandeep.dh 89
            UsernamePasswordToken upToken = (UsernamePasswordToken) token;
90
            String username = upToken.getUsername();
3090 mandeep.dh 91
 
3390 mandeep.dh 92
            log.info("Trying to fetch password for " + username);
93
            Agent agent = getAgent(username);
7059 kshitij.so 94
            if (agent != null && agent.isIs_active()) {
3090 mandeep.dh 95
                info = new SimpleAuthenticationInfo(username, agent.getPassword().toCharArray(), getName());
3390 mandeep.dh 96
            } else {
97
                throw new UnknownAccountException("No account found for user ["
98
                        + username + "]");
3090 mandeep.dh 99
            }
3390 mandeep.dh 100
        } catch (TException e) {
101
            log.info("Could not create CRM client", e);
3090 mandeep.dh 102
        }
103
 
104
        return info;
105
    }
3390 mandeep.dh 106
 
107
    public static Agent getAgent(String username) throws TException {
5286 amar.kumar 108
        if (agentsMapByEmailId == null || (!agentsMapByEmailId.containsKey(username)
109
        		&&!inactiveAgentsMapByEmailId.containsKey(username))) {
3390 mandeep.dh 110
            loadAgents();
111
        }
5286 amar.kumar 112
        if(agentsMapByEmailId.get(username)!=null) {
113
        	return agentsMapByEmailId.get(username);
114
        } else {
115
        	return inactiveAgentsMapByEmailId.get(username);
116
        }
3390 mandeep.dh 117
    }
118
 
119
    public static Agent getAgent(long agentId) throws TException {
5286 amar.kumar 120
        if (agentsMapById == null || (!agentsMapById.containsKey(agentId)
121
        		&&!inactiveAgentsMapById.containsKey(agentId))) {
3390 mandeep.dh 122
            loadAgents();
123
        }
5286 amar.kumar 124
 
125
        if(agentsMapById.get(agentId)!=null) {
126
        	return agentsMapById.get(agentId);
127
        } else {
128
        	return inactiveAgentsMapById.get(agentId);
129
        }
3390 mandeep.dh 130
    }
131
 
132
    private static void loadAgents() throws TException {
133
        Client crmServiceClient = new CRMClient().getClient();
134
        List<Agent> agents = crmServiceClient.getAgents(new SearchFilter());
5286 amar.kumar 135
        List<Agent> inactiveAgents = crmServiceClient.getInactiveAgents(new SearchFilter());
3390 mandeep.dh 136
        Map<Long, Agent> agentsMapByIdLocal = new HashMap<Long, Agent>();
137
        Map<String, Agent> agentsMapByEmailIdLocal = new HashMap<String, Agent>();
5286 amar.kumar 138
        Map<Long, Agent> inactiveAgentsMapByIdLocal = new HashMap<Long, Agent>();
139
        Map<String, Agent> inactiveAgentsMapByEmailIdLocal = new HashMap<String, Agent>();
3390 mandeep.dh 140
 
141
        for (Agent agent : agents) {
142
            agentsMapByIdLocal.put(agent.getId(), agent);
143
            agentsMapByEmailIdLocal.put(agent.getEmailId(), agent);
144
        }
5286 amar.kumar 145
 
146
        for (Agent agent : inactiveAgents) {
147
            inactiveAgentsMapByIdLocal.put(agent.getId(), agent);
148
            inactiveAgentsMapByEmailIdLocal.put(agent.getEmailId(), agent);
149
        }
3390 mandeep.dh 150
 
151
        synchronized(CRMAuthorizingRealm.class) {
152
            agentsMapById = agentsMapByIdLocal;
153
            agentsMapByEmailId = agentsMapByEmailIdLocal;
5286 amar.kumar 154
            inactiveAgentsMapById = inactiveAgentsMapByIdLocal;
155
            inactiveAgentsMapByEmailId = inactiveAgentsMapByEmailIdLocal;
3390 mandeep.dh 156
        }
157
    }
158
 
159
    public static List<Agent> getAgents() {
160
        return new ArrayList<Agent>(agentsMapById.values());
161
    }
4793 amar.kumar 162
 
163
    public static void removeAgent(Long id, String emailId) {
5286 amar.kumar 164
    	inactiveAgentsMapById.put(id, agentsMapById.get(id));
165
    	inactiveAgentsMapByEmailId.put(emailId, agentsMapByEmailId.get(emailId));
4793 amar.kumar 166
    	agentsMapById.remove(id);
167
    	agentsMapByEmailId.remove(emailId);
168
    }
169
 
170
    public static void addAgent(Agent agent) {
171
    	agentsMapById.put(agent.getId(), agent);
172
    	agentsMapByEmailId.put(agent.getEmailId(), agent);
173
    }
174
 
3090 mandeep.dh 175
}