NetworkAssociationBean.java
6.97 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
package fi.codecrew.moya.beans;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
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.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;
public NetworkAssociationBean() {}
@Override
public List<NetworkAssociation> getStatusByIPAndMAC(String ip, String mac) {
return networkAssociationFacade.findByIPAndMAC(eventBean.getCurrentEvent(), ip, mac);
}
@Override
public List<NetworkAssociation> getActiveAssociations(boolean activatePending) {
if(activatePending)
activatePendingAssociations();
return networkAssociationFacade.findByStatus(eventBean.getCurrentEvent(), NetworkAssociationStatus.ACTIVE);
}
@Override
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");
NetworkAssociation association;
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);
}
}
}
}
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;
}
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
na = networkAssociationFacade.create(na);
return na;
}
private Place resolvePlaceFromCode(String code) {
if(code == null) return null;
return barcodeBean.getPlaceFromTextCode(code);
}
}