Skip to content

Commit 0f2ef10

Browse files
author
jdvfarrington
committed
Adding groups is implemented
1 parent c3ded6c commit 0f2ef10

File tree

7 files changed

+147
-12
lines changed

7 files changed

+147
-12
lines changed

exampleDevices.csv

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
scan value,type,available,currently assigned,rule id
2+
testscan11,test,true,false,ruleSet1

exampleUsers.csv

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
scanValue,deviceLimit,devicesRemoved,canRemove,groupID
1+
scan value,device limit,devices removed,can remove,group id
22
csvuser1,2,0,True,groupOne

lumberjack_spring/src/main/java/uk/ac/bris/cs/rfideasalreadytaken/lumberjack/web/WebBackend.java

+1
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ private Iterable<CSVRecord> multipartFileToRecords(MultipartFile csv) throws Fil
367367
.parse(in);
368368
return records;
369369
} catch (IOException e) {
370+
e.printStackTrace();
370371
throw new FileUploadException();
371372
}
372373
}

lumberjack_spring/src/main/java/uk/ac/bris/cs/rfideasalreadytaken/lumberjack/web/WebController.java

+46-8
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ public String dashboard(Model model) {
6868
} catch (Exception e) {
6969
System.out.println("SQL Error");
7070
}
71-
// // Spoof Values
72-
// takeouts = Arrays.asList(4, 8, 12, 2, 6, 0);
73-
// returns = Arrays.asList(0, 2, 4, 10, 2, 2);
71+
// Spoof Values
72+
takeouts = Arrays.asList(4, 8, 12, 2, 6, 0);
73+
returns = Arrays.asList(0, 2, 4, 10, 2, 2);
7474
// Add list model attributes separately
7575
for (int i = 0; i < times.size(); i++) {
7676
String timeI = "time" + Integer.toString(i);
@@ -294,6 +294,7 @@ public String add(Model model) {
294294
public String addType(@PathVariable String type, Model model) {
295295
model.addAttribute("type", type);
296296
model.addAttribute("groups", new ArrayList<UserGroup>());
297+
List<Rule> rules;
297298
switch (type) {
298299
case "user":
299300
List<UserGroup> groups = new ArrayList<>();
@@ -304,18 +305,23 @@ public String addType(@PathVariable String type, Model model) {
304305
}
305306
model.addAttribute("groups", groups);
306307
break;
307-
case "device":
308-
List<Rule> rules = new ArrayList<>();
308+
case "group":
309+
rules = new ArrayList<>();
309310
try {
310311
rules = webBackend.getRules();
311312
} catch (Exception e) {
312313
System.out.println("SQL Exception");
313314
}
314315
model.addAttribute("rules", rules);
315316
break;
316-
case "group":
317-
UserGroup group = new UserGroup();
318-
model.addAttribute(group);
317+
case "device":
318+
rules = new ArrayList<>();
319+
try {
320+
rules = webBackend.getRules();
321+
} catch (Exception e) {
322+
System.out.println("SQL Exception");
323+
}
324+
model.addAttribute("rules", rules);
319325
break;
320326
default:
321327
break;
@@ -371,6 +377,38 @@ public String addDevice(@RequestParam Map<String, String> request, Model model)
371377
return "message";
372378
}
373379

380+
@PostMapping("/add/group")
381+
public String addGroup(@RequestParam Map<String, String> request, Model model) {
382+
// Set group and permission(s) attributes
383+
UserGroup newGroup = new UserGroup();
384+
newGroup.setId(request.get("groupName"));
385+
// Get selected rules
386+
List<GroupPermission> permissions = new ArrayList<>();
387+
List<String> keys = new ArrayList<>(request.keySet());
388+
for (int i = 1; i < request.size(); i++) {
389+
GroupPermission permission = new GroupPermission();
390+
permission.setId(UUID.randomUUID().toString());
391+
permission.setUserGroupID(request.get("groupName"));
392+
permission.setRuleID(request.get(keys.get(i)));
393+
permissions.add(permission);
394+
}
395+
// Add group and permissions(s) to the database
396+
try {
397+
webBackend.insertUserGroup(newGroup);
398+
for (GroupPermission permission : permissions) {
399+
webBackend.insertGroupPermission(permission);
400+
}
401+
} catch (Exception e) {
402+
System.out.println("SQL Exception");
403+
model.addAttribute("messageType", "Group Adding Failed");
404+
model.addAttribute("messageString", e.getMessage());
405+
return "message";
406+
}
407+
model.addAttribute("messageType", "Group Added");
408+
model.addAttribute("messageString", "The group has been added!");
409+
return "message";
410+
}
411+
374412
/**
375413
* CSV file upload POST mapping. CSV must include headers that match user object.
376414
* Any missing columns will be filled in with default values.

lumberjack_spring/src/main/resources/templates/add.html

+81-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ <h2>
2424
<span th:if="${type} == user">Users</span>
2525
<span th:if="${type} == device">Devices</span>
2626
<span th:if="${type} == group">User Groups</span>
27+
<span th:if="${type} == rule">Rules</span>
2728
</h2>
2829

2930
<span id="type" style="display: none;" th:text="${type}"></span>
@@ -35,11 +36,14 @@ <h2>
3536
<div class="col-sm-auto">
3637
<a class="btn btn-primary" href="/add/user" role="button">Users</a>
3738
</div>
39+
<div class="col-sm-auto">
40+
<a class="btn btn-primary" href="/add/group" role="button">User Groups</a>
41+
</div>
3842
<div class="col-sm-auto">
3943
<a class="btn btn-primary" href="/add/device" role="button">Devices</a>
4044
</div>
4145
<div class="col-sm-auto">
42-
<a class="btn btn-primary" href="/add/group" role="button">User Groups</a>
46+
<a class="btn btn-primary" href="/add/rule" role="button">Rules</a>
4347
</div>
4448
</div>
4549
</div>
@@ -206,6 +210,82 @@ <h2>
206210
</div>
207211
</div>
208212

213+
<!-- Adding Groups -->
214+
215+
<div th:if="${type} == group" class="container-fluid">
216+
<div class="row">
217+
<div class="col-lg">
218+
219+
<p class="lead">
220+
Add a group:
221+
</p>
222+
223+
<div class="container-fluid">
224+
<div class="row">
225+
<form action="#" th:action="@{/add/group}" method="post">
226+
227+
<div class="form-group row" >
228+
<label for="groupName" class="col-sm-2 col-form-label">Group ID</label>
229+
<div class="col-sm-4">
230+
<input type="text" class="form-control" id="groupName" name="groupName" placeholder="Yr 1 CompSci" required="required" />
231+
<p id="nameHelp" class="form-text text-muted">
232+
Must be unique.
233+
</p>
234+
</div>
235+
</div>
236+
237+
<!--<fieldset class="form-group row">-->
238+
<!--<p class="lead col-lg">Rules</p>-->
239+
<!--<div class="col-lg">-->
240+
<!--<div class="form-check">-->
241+
242+
<!--<label class="form-check-label">-->
243+
<!--<input class="form-check-input" type="radio" name="ruleName" id="ruleName" value="option1" checked="checked" />-->
244+
<!--No Rule-->
245+
<!--</label>-->
246+
247+
<!--<th:block th:each="rule : ${rules}">-->
248+
<!--<label class="form-check-label">-->
249+
<!--<input class="form-check-input" type="radio" name="ruleName" th:value="${rule.getId()}" />-->
250+
<!--<span th:text="${rule.getId()}">ID</span> - <span th:text="${rule.getMaximumRemovalTime()}">20</span> hours-->
251+
<!--</label>-->
252+
<!--</th:block>-->
253+
254+
<!--</div>-->
255+
<!--</div>-->
256+
<!--</fieldset>-->
257+
258+
<fieldset class="form-group row">
259+
<p class="col-form-legend col-sm-2">Rules</p>
260+
<div class="col-sm-10">
261+
262+
<th:block th:each="rule : ${rules}">
263+
<div class="form-check">
264+
<label th:for="'rule' + ${rule.getId()}" class="col-check-label">
265+
<input th:id="'rule' + ${rule.getId()}" type="checkbox" class="form-check-input" th:name="'rule' + ${rule.getId()}" th:value="${rule.getId()}" />
266+
<span th:text="${rule.getId()}">ID</span> - <span th:text="${rule.getMaximumRemovalTime()}">2</span> hours
267+
</label>
268+
</div>
269+
</th:block>
270+
271+
</div>
272+
</fieldset>
273+
<p id="ruleHelp" class="form-text text-muted row">
274+
Devices have rules, and if a group and device share a rule, that group's users can take out the device.
275+
(You can select no rules).
276+
</p>
277+
278+
<div class="form-group row">
279+
<button type="submit" class="btn btn-primary">Submit</button>
280+
</div>
281+
282+
</form>
283+
</div>
284+
</div>
285+
</div>
286+
</div>
287+
</div>
288+
209289
</main>
210290

211291
</div>

lumberjack_spring/src/main/resources/templates/fragments/sidebar.html

+11-1
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,19 @@
4141
Add Users
4242
</a>
4343
</li>
44+
</ul>
45+
<ul class="nav nav-pills flex-column">
46+
<li class="nav-link">
47+
<strong>User Groups</strong>
48+
</li>
4449
<li>
4550
<a class="nav-link" th:classappend="${#httpServletRequest.getRequestURI() == '/groups' ? 'active':''}" href="/groups">
46-
User Groups
51+
All User Groups
52+
</a>
53+
</li>
54+
<li>
55+
<a class="nav-link" th:classappend="${#httpServletRequest.getRequestURI() == '/add/group' ? 'active':''}" href="/add/group">
56+
Add User Groups
4757
</a>
4858
</li>
4959
</ul>

lumberjack_spring/src/main/resources/templates/search.html

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ <h2>
2424
<span th:if="${type} == user">Users</span>
2525
<span th:if="${type} == device">Devices</span>
2626
<span th:if="${type} == group">User Groups</span>
27+
<span th:if="${type} == rule">Rules</span>
2728
</h2>
2829

2930
<span id="type" style="display: none;" th:text="${type}"></span>
@@ -35,11 +36,14 @@ <h2>
3536
<div class="col-sm-auto">
3637
<a class="btn btn-primary" href="/search/user" role="button">Users</a>
3738
</div>
39+
<div class="col-sm-auto">
40+
<a class="btn btn-primary" href="/search/group" role="button">User Groups</a>
41+
</div>
3842
<div class="col-sm-auto">
3943
<a class="btn btn-primary" href="/search/device" role="button">Devices</a>
4044
</div>
4145
<div class="col-sm-auto">
42-
<a class="btn btn-primary" href="/search/group" role="button">User Groups</a>
46+
<a class="btn btn-primary" href="/search/rule" role="button">Rules</a>
4347
</div>
4448
</div>
4549
</div>

0 commit comments

Comments
 (0)