RoleView.java
5.28 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package fi.codecrew.moya.web.cdiview.user;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.ejb.EJB;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Named;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fi.codecrew.moya.beans.EventBeanLocal;
import fi.codecrew.moya.beans.RoleBeanLocal;
import fi.codecrew.moya.beans.UserBeanLocal;
import fi.codecrew.moya.enums.BortalApplication;
import fi.codecrew.moya.enums.apps.IAppPermission;
import fi.codecrew.moya.enums.apps.UserPermission;
import fi.codecrew.moya.model.ApplicationPermission;
import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.Role;
import fi.codecrew.moya.model.User;
import fi.codecrew.moya.util.UserSearchQuery;
import fi.codecrew.moya.utilities.SearchResult;
import fi.codecrew.moya.web.cdiview.GenericCDIView;
import fi.codecrew.moya.web.helpers.BortalApplicationWrapper;
@ConversationScoped
@Named
public class RoleView extends GenericCDIView {
/**
*
*/
private static final long serialVersionUID = -5165373936500592099L;
private Role role;
private Integer roleid;
@EJB
private transient RoleBeanLocal rolebean;
@EJB
private transient EventBeanLocal eventbean;
private ArrayList<BortalApplicationWrapper> rolePermissions;
@EJB
private UserBeanLocal userbean;
private User addableUser;
private static final Logger logger = LoggerFactory.getLogger(RoleView.class);
public void permissionCreate() {
requirePermissions(permbean.hasPermission(UserPermission.WRITE_ROLES));
}
public void permissionRead() {
requirePermissions(permbean.hasPermission(UserPermission.READ_ROLES));
}
public void initForCreate() {
if (requirePermissions(permbean.hasPermission(UserPermission.WRITE_ROLES))) {
setRole(new Role(eventbean.getCurrentEvent()));
super.beginConversation();
}
}
public void addUser()
{
EventUser eu = userbean.getEventUser(addableUser);
role = rolebean.addRole(eu, role);
addableUser = null;
}
public List<User> searchUser(String user)
{
// By default this returns only 20 first results.
UserSearchQuery usq = new UserSearchQuery();
usq.setSearch(user);
usq.setOnlyThisEvent(false);
SearchResult<User> ret = userbean.getUsers(usq);
return ret.getResults();
}
public boolean isCanReadRoles() {
return permbean.hasPermission(UserPermission.READ_ROLES);
}
public boolean isCanWriteRoles() {
return permbean.hasPermission(UserPermission.WRITE_ROLES);
}
public void initViewFromId() {
if (requirePermissions(UserPermission.READ_ROLES) && role == null) {
super.beginConversation();
setRole(rolebean.find(getRoleid()));
logger.debug("Initialized role: {} from id ", getRole(), getRoleid());
}
}
private void initPermissions() {
logger.info("Initializing permissions for role {}", role);
EnumMap<BortalApplication, Set<IAppPermission>> rolePermissionMap = new EnumMap<BortalApplication, Set<IAppPermission>>(BortalApplication.class);
if (role.getPermissions() == null) {
role.setPermissions(new ArrayList<ApplicationPermission>());
}
for (ApplicationPermission perm : role.getPermissions()) {
Set<IAppPermission> permlist = rolePermissionMap.get(perm.getApplication());
if (permlist == null) {
permlist = new HashSet<IAppPermission>();
rolePermissionMap.put(perm.getApplication(), permlist);
logger.info("Initing Role permisions App {}, role {}", perm.getApplication(), perm.getPermission());
}
permlist.add(perm.getPermission());
}
setRolePermissions(new ArrayList<BortalApplicationWrapper>());
for (BortalApplication bApp : BortalApplication.values()) {
getRolePermissions().add(new BortalApplicationWrapper(bApp, rolePermissionMap.get(bApp)));
}
}
public String create() {
super.requirePermissions(permbean.hasPermission(UserPermission.WRITE_ROLES));
rolebean.create(getRole());
return "/role/edit";
}
public String save() {
setRole(rolebean.mergeChanges(getRole()));
return "roleSaved";
}
public String savePermissions() {
if (isCanWriteRoles()) {
ArrayList<IAppPermission> newPerms = new ArrayList<IAppPermission>();
for (BortalApplicationWrapper appWrap : rolePermissions) {
for (IAppPermission apWrap : appWrap.getPermissions()) {
if (appWrap.getSelected().contains(apWrap.toString())) {
newPerms.add(apWrap);
}
}
}
setRole(rolebean.setPermissions(getRole(), newPerms));
this.addFaceMessage("role.permissionsSaved");
} else {
this.addFaceMessage("global.notAuthorizedExecute");
}
return null;
}
public Role getRole() {
return role;
}
/**
* @return the possibleParents
*/
public List<Role> getPossibleParents() {
return rolebean.getPossibleParents(getRole());
}
public void setRoleid(Integer roleid) {
this.roleid = roleid;
}
public Integer getRoleid() {
return roleid;
}
public void setRolePermissions(ArrayList<BortalApplicationWrapper> rolePermissions) {
this.rolePermissions = rolePermissions;
}
public ArrayList<BortalApplicationWrapper> getRolePermissions() {
return rolePermissions;
}
public void setRole(Role role) {
this.role = role;
initPermissions();
}
public User getAddableUser() {
return addableUser;
}
public void setAddableUser(User addableUser) {
this.addableUser = addableUser;
}
}