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