Subversion Repositories SmartDukaan

Rev

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

Rev 23951 Rev 25139
Line 6... Line 6...
6
 
6
 
7
import org.apache.logging.log4j.LogManager;
7
import org.apache.logging.log4j.LogManager;
8
import org.apache.logging.log4j.Logger;
8
import org.apache.logging.log4j.Logger;
9
import org.springframework.beans.factory.annotation.Autowired;
9
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.stereotype.Component;
10
import org.springframework.stereotype.Component;
11
import org.springframework.transaction.annotation.Transactional;
-
 
12
 
11
 
13
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
12
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
14
import com.spice.profitmandi.common.model.ProfitMandiConstants;
13
import com.spice.profitmandi.common.model.ProfitMandiConstants;
15
import com.spice.profitmandi.dao.entity.dtr.Api;
14
import com.spice.profitmandi.dao.entity.dtr.Api;
16
import com.spice.profitmandi.dao.entity.dtr.Role;
15
import com.spice.profitmandi.dao.entity.dtr.Role;
Line 19... Line 18...
19
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
18
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
20
import com.spice.profitmandi.dao.repository.dtr.ApiRepository;
19
import com.spice.profitmandi.dao.repository.dtr.ApiRepository;
21
import com.spice.profitmandi.dao.repository.dtr.RoleApiRepository;
20
import com.spice.profitmandi.dao.repository.dtr.RoleApiRepository;
22
import com.spice.profitmandi.dao.repository.dtr.RoleRepository;
21
import com.spice.profitmandi.dao.repository.dtr.RoleRepository;
23
 
22
 
24
@Transactional
-
 
25
@Component
23
@Component
26
public class RoleManager {
24
public class RoleManager {
27
	
25
 
28
	@Autowired
26
	@Autowired
29
	private RoleApiRepository roleApiRepository;
27
	private RoleApiRepository roleApiRepository;
30
	
28
 
31
	@Autowired
29
	@Autowired
32
	private RoleRepository roleRepository;
30
	private RoleRepository roleRepository;
33
	
31
 
34
	@Autowired
32
	@Autowired
35
	private ApiRepository apiRepository;
33
	private ApiRepository apiRepository;
36
	
34
 
37
	private static final Logger LOGGER = LogManager.getLogger(RoleManager.class);
35
	private static final Logger LOGGER = LogManager.getLogger(RoleManager.class);
38
 
36
 
39
	
-
 
40
	public boolean isAuthorizedURI(Set<Integer> roleIds, String contextPath, String uri, String method) throws ProfitMandiBusinessException{
37
	public boolean isAuthorizedURI(Set<Integer> roleIds, String contextPath, String uri, String method)
-
 
38
			throws ProfitMandiBusinessException {
41
		if(isAdmin(roleIds)) {
39
		if (isAdmin(roleIds)) {
42
			LOGGER.info(uri + "[" + method+ "]");
40
			LOGGER.info(uri + "[" + method + "]");
43
			return true;
41
			return true;
44
		}
42
		}
45
		List<RoleApi> roleApis = roleApiRepository.selectByRoleIds(roleIds);
43
		List<RoleApi> roleApis = roleApiRepository.selectByRoleIds(roleIds);
46
		Set<Integer> apiIds = new HashSet<>();
44
		Set<Integer> apiIds = new HashSet<>();
47
		for(RoleApi roleApi : roleApis) {
45
		for (RoleApi roleApi : roleApis) {
48
			apiIds.add(roleApi.getApiId());
46
			apiIds.add(roleApi.getApiId());
49
		}
47
		}
50
		List<Api> apis = apiRepository.selectByIds(apiIds);
48
		List<Api> apis = apiRepository.selectByIds(apiIds);
51
		for(Api api : apis) {
49
		for (Api api : apis) {
52
			if((uri.matches(contextPath + api.getUri()) || (uri + "/").matches(contextPath + api.getUri()) ||
50
			if ((uri.matches(contextPath + api.getUri()) || (uri + "/").matches(contextPath + api.getUri())
-
 
51
					|| uri.matches(contextPath + api.getUri() + "/"))
53
				uri.matches(contextPath + api.getUri() + "/")) && api.getMethod().equals(Method.valueOf(method))) {
52
					&& api.getMethod().equals(Method.valueOf(method))) {
54
				return true;
53
				return true;
55
			}
54
			}
56
		}
55
		}
57
		throw new ProfitMandiBusinessException(ProfitMandiConstants.URI, uri + "[" + method+ "]", "GE_1004");
56
		throw new ProfitMandiBusinessException(ProfitMandiConstants.URI, uri + "[" + method + "]", "GE_1004");
58
	}
57
	}
59
	
58
 
60
	public boolean isAdmin(Set<Integer> roleIds) {
59
	public boolean isAdmin(Set<Integer> roleIds) {
61
		try {
60
		try {
62
			Role roleFofoAdmin = roleRepository.selectByName(RoleType.FOFO_ADMIN.name());
61
			Role roleFofoAdmin = roleRepository.selectByName(RoleType.FOFO_ADMIN.name());
63
			return roleIds.contains(roleFofoAdmin.getId());
62
			return roleIds.contains(roleFofoAdmin.getId());
64
		} catch(Exception e) {
63
		} catch (Exception e) {
65
			//This 
64
			// This
66
			return false;
65
			return false;
67
		}
66
		}
68
	}
67
	}
-
 
68
 
69
	public boolean isPartner(Set<Integer> roleIds) {
69
	public boolean isPartner(Set<Integer> roleIds) {
70
		try {
70
		try {
71
			Role rolePartner = roleRepository.selectByName(RoleType.FOFO.name());
71
			Role rolePartner = roleRepository.selectByName(RoleType.FOFO.name());
72
			return roleIds.contains(rolePartner.getId());
72
			return roleIds.contains(rolePartner.getId());
73
		} catch(Exception e) {
73
		} catch (Exception e) {
74
			//This 
74
			// This
75
			return false;
75
			return false;
76
		}
76
		}
77
	}
77
	}
-
 
78
 
78
	public boolean isRetailer(Set<Integer> roleIds) {
79
	public boolean isRetailer(Set<Integer> roleIds) {
79
		try {
80
		try {
80
			Role rolePartner = roleRepository.selectByName(RoleType.RETAILER.name());
81
			Role rolePartner = roleRepository.selectByName(RoleType.RETAILER.name());
81
			return roleIds.contains(rolePartner.getId());
82
			return roleIds.contains(rolePartner.getId());
82
		} catch(Exception e) {
83
		} catch (Exception e) {
83
			//This 
84
			// This
84
			return false;
85
			return false;
85
		}
86
		}
86
	}
87
	}
-
 
88
 
87
	public boolean isUser(Set<Integer> roleIds) {
89
	public boolean isUser(Set<Integer> roleIds) {
88
		try {
90
		try {
89
			Role rolePartner = roleRepository.selectByName(RoleType.USER.name());
91
			Role rolePartner = roleRepository.selectByName(RoleType.USER.name());
90
			return roleIds.contains(rolePartner.getId());
92
			return roleIds.contains(rolePartner.getId());
91
		} catch(Exception e) {
93
		} catch (Exception e) {
92
			//This 
94
			// This
93
			return false;
95
			return false;
94
		}
96
		}
95
	}
97
	}
96
}
98
}