RoleBean.java
3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package fi.insomnia.bortal.beans;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fi.insomnia.bortal.enums.BeanRole;
import fi.insomnia.bortal.enums.Permission;
import fi.insomnia.bortal.facade.RoleFacade;
import fi.insomnia.bortal.facade.RoleRightFacade;
import fi.insomnia.bortal.model.AccessRight;
import fi.insomnia.bortal.model.Role;
import fi.insomnia.bortal.model.RoleRight;
/**
*
* @author tuukka
*/
@Stateless
public class RoleBean implements RoleBeanLocal {
private static final String PUBLIC_ROLE_NAME = BeanRole.ANONYMOUS.toString();
@EJB
private EventBeanLocal eventBean;
@EJB
private RoleFacade roleFacade;
@EJB
private RoleRightFacade rrfacade;
@EJB
private AccessRightBeanLocal accessRightBean;
private static final Logger logger = LoggerFactory.getLogger(RoleBean.class);
public List<Role> listRoles() {
return roleFacade.findAll();
}
public Role mergeChanges(Role role) {
return roleFacade.merge(role);
}
public Role create(Role role) {
roleFacade.create(role);
return role;
}
public List<Role> getPossibleParents(Role role) {
List<Role> roleList = listRoles();
if (role == null)
return roleList;
List<Role> children = getAllChilds(role, new HashSet<Role>());
for (Role unit : children) {
if (roleList.contains(unit)) {
roleList.remove(unit);
}
}
return roleList;
}
private static List<Role> getAllChilds(Role role, Set<Role> checkedRoles) {
List<Role> returnList = new ArrayList<Role>();
if (checkedRoles.contains(role) || role == null) {
return returnList;
}
for (Role unit : role.getChildren()) {
List<Role> someList = getAllChilds(unit, checkedRoles);
returnList.addAll(someList);
}
checkedRoles.add(role);
return returnList;
}
public List<RoleRight> getRoleRights(Role r) {
List<AccessRight> rights = accessRightBean.findAll();
List<RoleRight> ret = new ArrayList<RoleRight>();
for (AccessRight ar : rights) {
ret.add(findRoleRight(r, ar));
}
return ret;
}
@Override
public RoleRight mergeChanges(RoleRight row) {
return rrfacade.merge(row);
}
public Role getOrCreatePublicRole() {
Role ret = roleFacade.findByName(PUBLIC_ROLE_NAME);
if (ret == null) {
ret = new Role(eventBean.getCurrentEvent());
roleFacade.create(ret);
RoleRight rr = findRoleRight(ret, Permission.LOGIN);
rr.setRead(true);
}
return ret;
}
public RoleRight findRoleRight(Role role, Permission perm) {
AccessRight acr = accessRightBean.findOrCreate(perm);
return findRoleRight(role, acr);
}
public RoleRight findRoleRight(Role role, AccessRight acr) {
RoleRight rr = rrfacade.find(eventBean.getCurrentEvent(), acr, role);
if (rr == null) {
rr = new RoleRight(eventBean.getCurrentEvent(), role, acr, false, false, false);
rrfacade.create(rr);
}
return rr;
}
}