NetworkAssociationBean.java
10.9 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
* Copyright Codecrew Ry
*
* All rights reserved.
*
* This license applies to any software containing a notice placed by the
* copyright holder. Such software is herein referred to as the Software.
* This license covers modification, distribution and use of the Software.
*
* Any distribution and use in source and binary forms, with or without
* modification is not permitted without explicit written permission from the
* copyright owner.
*
* A non-exclusive royalty-free right is granted to the copyright owner of the
* Software to use, modify and distribute all modifications to the Software in
* future versions of the Software.
*
*/
package fi.codecrew.moya.beans;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import javax.annotation.security.RolesAllowed;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import fi.codecrew.moya.enums.BortalApplication;
import fi.codecrew.moya.enums.NetworkAssociationStatus;
import fi.codecrew.moya.enums.apps.IAppPermission;
import fi.codecrew.moya.enums.apps.NetworkAssociationPermission;
import fi.codecrew.moya.facade.NetworkAssociationFacade;
import fi.codecrew.moya.model.ApplicationPermission;
import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.GroupMembership;
import fi.codecrew.moya.model.NetworkAssociation;
import fi.codecrew.moya.model.Place;
import fi.codecrew.moya.model.PrintedCard;
import fi.codecrew.moya.model.Role;
/**
* Session Bean implementation class NetworkAssociationBean
*/
@Stateless
@LocalBean
public class NetworkAssociationBean implements NetworkAssociationBeanLocal {
@EJB
private UserBean userBean;
@EJB
private BarcodeBean barcodeBean;
@EJB
private RoleBean roleBean;
@EJB
private NetworkAssociationFacade networkAssociationFacade;
@EJB
private EventBean eventBean;
@EJB
private CardTemplateBean cardBean;
@EJB
private PermissionBean permissionBean;
public NetworkAssociationBean() {}
@Override
@RolesAllowed(NetworkAssociationPermission.S_CAN_ADMINISTER_ASSOCIATIONS)
public List<NetworkAssociation> getStatusByIPAndMAC(String ip, String mac) {
return networkAssociationFacade.findByIPAndMAC(eventBean.getCurrentEvent(), ip, mac);
}
@Override
@RolesAllowed(NetworkAssociationPermission.S_CAN_ADMINISTER_ASSOCIATIONS)
public List<NetworkAssociation> getActiveAssociations(boolean activatePending) {
if(activatePending)
activatePendingAssociations();
return networkAssociationFacade.findByStatus(eventBean.getCurrentEvent(), NetworkAssociationStatus.ACTIVE);
}
@Override
@RolesAllowed(NetworkAssociationPermission.S_CAN_ADMINISTER_ASSOCIATIONS)
public NetworkAssociation tryAssociate(String ip, String mac) {
EventUser eu = permissionBean.getCurrentUser();
HashSet<IAppPermission> userPerms = buildPermsFor(eu);
NetworkAssociation nasc = associate(eu, userPerms, null, ip, mac);
return nasc;
}
@Override
@RolesAllowed(NetworkAssociationPermission.S_CAN_ADMINISTER_ASSOCIATIONS)
public NetworkAssociation tryAssociate(String username, String password,
String ip, String mac, String code, boolean codeRequired)
throws Exception {
EventUser authUser = userBean.validateUser(username, password);
if(authUser == null)
throw new Exception("INVALID_USER_OR_PASSWORD");
return tryAssociateInternal(authUser, ip, mac, code, codeRequired);
}
@Override
@RolesAllowed(NetworkAssociationPermission.S_CAN_ADMINISTER_ASSOCIATIONS)
public NetworkAssociation tryAssociate(String usercode, String ip, String mac, String code, Boolean codeRequired) throws Exception {
EventUser authUser = userBean.getUser(usercode);
if(authUser == null)
throw new Exception("INVALID_USERCODE");
return tryAssociateInternal(authUser, ip, mac, code, codeRequired);
}
private NetworkAssociation tryAssociateInternal(EventUser authUser, String ip, String mac, String code, boolean codeRequired) throws Exception {
NetworkAssociation association;
HashSet<IAppPermission> userPerms = buildPermsFor(authUser);
if(!userPerms.contains(NetworkAssociationPermission.CAN_ASSOCIATE))
throw new Exception("USER_HAS_NO_ASSOCIATE_PERMISSION");
if(codeRequired) {
Place place = resolvePlaceFromCode(code);
if(place == null) {
// This is a valid case when user has permissions that allow them to
// override any place requirements, effectively allowing for free
// associations without the need of a real place.
if(!userPerms.contains(NetworkAssociationPermission.OVERRIDE_PLACE_REQUIREMENT))
throw new Exception("INVALID_PLACECODE");
association = associate(authUser, userPerms, place, ip, mac);
} else {
if(place.getPlaceReserver().getUser().equals(authUser)) {
// This is the simple case when user goes to their own place
association = associate(authUser, userPerms, place, ip, mac);
} else {
// In this case, the user has probably shuffled their seats
// within their group
if(!userPerms.contains(NetworkAssociationPermission.CAN_SHUFFLE_IN_GROUP))
throw new Exception("INVALID_PLACECODE");
boolean userIsInGroup = false;
for(GroupMembership gm : place.getGroup().getMembers()) {
EventUser member = gm.getUser();
if(member != null && gm.getUser().equals(authUser)) {
userIsInGroup = true;
break;
}
}
if(!userIsInGroup)
throw new Exception("INVALID_PLACECODE");
association = associate(authUser, userPerms, place, ip, mac);
}
}
} else {
// Code was not required, this is often a case for WLAN
association = associate(authUser, userPerms, null, ip, mac);
}
return association;
}
@Override
@RolesAllowed(NetworkAssociationPermission.S_CAN_ADMINISTER_ASSOCIATIONS)
public List<NetworkAssociation> getPendingAssociations() {
return networkAssociationFacade.findByStatus(eventBean.getCurrentEvent(), NetworkAssociationStatus.PENDING);
}
@Override
@RolesAllowed(NetworkAssociationPermission.S_CAN_ADMINISTER_ASSOCIATIONS)
public void dropAssociationById(Integer associd) {
NetworkAssociation na = networkAssociationFacade.find(associd);
if(na.getEvent().equals(eventBean.getCurrentEvent())) {
na.setModifyTime(Calendar.getInstance());
na.setStatus(NetworkAssociationStatus.EXPIRED);
}
}
private HashSet<IAppPermission> buildPermsFor(EventUser authUser) {
HashSet<IAppPermission> userPerms = new HashSet<>();
if (authUser.getUser().isSuperadmin()) {
// Iterate through all permissions & add
for (BortalApplication app : BortalApplication.values()) {
for (IAppPermission perm : app.getPermissions()) {
userPerms.add(perm);
}
}
} else {
for(Role r : userBean.localFindUsersRoles(authUser)) {
for(ApplicationPermission appPerm : r.getPermissions()) {
IAppPermission iap = appPerm.getPermission();
if(!userPerms.contains(iap)) {
userPerms.add(iap);
}
}
}
}
return userPerms;
}
private void activatePendingAssociations() {
List<NetworkAssociation> netAssocs = networkAssociationFacade.findByStatus(
eventBean.getCurrentEvent(), NetworkAssociationStatus.PENDING);
for(NetworkAssociation na : netAssocs) {
na.setStatus(NetworkAssociationStatus.ACTIVE);
na.setModifyTime(Calendar.getInstance());
}
}
private NetworkAssociation associate(EventUser authUser, HashSet<IAppPermission> userPerms,
Place place, String ip, String mac) {
// This tests the case where association request is still pending, we will not
// create new one but rather return an old one
List<NetworkAssociation> nas = networkAssociationFacade.findByIPAndMACAndStatus(
eventBean.getCurrentEvent(), ip, mac, NetworkAssociationStatus.PENDING);
if(nas.size() > 0)
return nas.get(0);
nas = networkAssociationFacade.findByIPAndMACAndStatus(
eventBean.getCurrentEvent(), ip, mac, NetworkAssociationStatus.ACTIVE);
if(nas.size() > 0)
return nas.get(0);
NetworkAssociation na = new NetworkAssociation();
na.setIP(ip);
na.setMAC(mac);
na.setPlace(place);
na.setEventUser(authUser);
na.setEvent(authUser.getEvent());
na.setStatus(NetworkAssociationStatus.PENDING);
List<NetworkAssociation> activeAssocs;
if(place == null) {
// When in this branch, we are doing an association which will never
// dis-associate things EXCEPT in the case where IP collision happens
// which usually is a result of network ops change...
activeAssocs = networkAssociationFacade.findActiveAssociationsByIP(
eventBean.getCurrentEvent(), ip);
for(NetworkAssociation activeAssoc : activeAssocs) {
activeAssoc.setModifyTime(Calendar.getInstance());
activeAssoc.setStatus(NetworkAssociationStatus.EXPIRED);
}
} else {
// Disassociate any colliding IPs
activeAssocs = networkAssociationFacade.findActiveAssociationsByIP(
eventBean.getCurrentEvent(), ip);
for(NetworkAssociation activeAssoc : activeAssocs) {
activeAssoc.setModifyTime(Calendar.getInstance());
activeAssoc.setStatus(NetworkAssociationStatus.EXPIRED);
}
// In here we disassociate old associations from this place unless the user has many
// associations per place
if(!userPerms.contains(NetworkAssociationPermission.CAN_ASSOCIATE_MANY_PER_PLACE)) {
activeAssocs = networkAssociationFacade.findActiveAssociationsByPlace(place);
for(NetworkAssociation activeAssoc : activeAssocs) {
activeAssoc.setModifyTime(Calendar.getInstance());
activeAssoc.setStatus(NetworkAssociationStatus.EXPIRED);
}
}
}
// Finally persist the association and return
networkAssociationFacade.create(na);
return na;
}
private Place resolvePlaceFromCode(String code) {
if(code == null) return null;
return barcodeBean.getPlaceFromTextCode(code);
}
@Override
@RolesAllowed(NetworkAssociationPermission.S_CAN_ADMINISTER_ASSOCIATIONS)
public NetworkAssociation getActiveAssociationByIP(String ipAddress) {
List<NetworkAssociation> na = this.networkAssociationFacade.findActiveAssociationsByIP(
eventBean.getCurrentEvent(),
ipAddress
);
if(na.size() > 0) return na.get(0);
else return null;
}
@Override
@RolesAllowed(NetworkAssociationPermission.S_CAN_ADMINISTER_ASSOCIATIONS)
public List<NetworkAssociation> getActiveAssociationsByHorizon(Boolean activate, String horizon) {
if(activate)
activatePendingAssociationsByHorizon(horizon);
return networkAssociationFacade.findByStatusAndHorizon(eventBean.getCurrentEvent(), NetworkAssociationStatus.ACTIVE, horizon);
}
private void activatePendingAssociationsByHorizon(String horizon) {
List<NetworkAssociation> netAssocs = networkAssociationFacade.findByStatusAndHorizon(
eventBean.getCurrentEvent(), NetworkAssociationStatus.PENDING, horizon);
for(NetworkAssociation na : netAssocs) {
na.setStatus(NetworkAssociationStatus.ACTIVE);
na.setModifyTime(Calendar.getInstance());
}
}
}