1 | package org.expeditee.encryption.items;
|
---|
2 |
|
---|
3 | import java.util.List;
|
---|
4 |
|
---|
5 | import org.expeditee.settings.UserSettings;
|
---|
6 | import org.expeditee.settings.encryption.EncryptionDefaults;
|
---|
7 |
|
---|
8 | public class EncryptionPermissionTriple {
|
---|
9 |
|
---|
10 | private UserAppliedEncryptionPermission ownerPermission;
|
---|
11 | private UserAppliedEncryptionPermission groupPermission;
|
---|
12 | private UserAppliedEncryptionPermission otherPermission;
|
---|
13 |
|
---|
14 | public EncryptionPermissionTriple(String permissionCode, String defaultPermission) {
|
---|
15 | ownerPermission = defaultPermission.length() > 0 ?
|
---|
16 | UserAppliedEncryptionPermission.getPermission(defaultPermission.charAt(0) + "", UserAppliedEncryptionPermission.none) :
|
---|
17 | UserAppliedEncryptionPermission.none;
|
---|
18 | groupPermission = defaultPermission.length() > 1 ?
|
---|
19 | UserAppliedEncryptionPermission.getPermission(defaultPermission.charAt(1) + "", UserAppliedEncryptionPermission.none) :
|
---|
20 | UserAppliedEncryptionPermission.none;
|
---|
21 | otherPermission = defaultPermission.length() > 2 ?
|
---|
22 | UserAppliedEncryptionPermission.getPermission(defaultPermission.charAt(2) + "", UserAppliedEncryptionPermission.none) :
|
---|
23 | UserAppliedEncryptionPermission.none;
|
---|
24 |
|
---|
25 | if (permissionCode != null) {
|
---|
26 | permissionCode = permissionCode.trim().toLowerCase();
|
---|
27 | if (permissionCode.length() != 0) {
|
---|
28 | // If the permission code has a character at index 0, then it is the owner permission.
|
---|
29 | // If it does not then it will remain as the default permission passed to the function.
|
---|
30 | String ownerPermissionCode = permissionCode.charAt(0) + "";
|
---|
31 | this.ownerPermission = UserAppliedEncryptionPermission.getPermission(ownerPermissionCode, ownerPermission);
|
---|
32 |
|
---|
33 | // If the permission code has a character at index 1, then it is the group permission.
|
---|
34 | // If it does not then it will remain as the default permission passed to the function.
|
---|
35 | if (permissionCode.length() < 2) { return; }
|
---|
36 | String groupPermissionCode = permissionCode.charAt(1) + "";
|
---|
37 | this.groupPermission = UserAppliedEncryptionPermission.getPermission(groupPermissionCode, groupPermission);
|
---|
38 |
|
---|
39 | if (permissionCode.length() < 3) { return; }
|
---|
40 | String otherPermissionCode = permissionCode.charAt(2) + "";
|
---|
41 | this.otherPermission = UserAppliedEncryptionPermission.getPermission(otherPermissionCode, otherPermission);
|
---|
42 | }
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public EncryptionPermissionTriple(
|
---|
47 | UserAppliedEncryptionPermission ownerPermission,
|
---|
48 | UserAppliedEncryptionPermission groupPermission,
|
---|
49 | UserAppliedEncryptionPermission otherPermission) {
|
---|
50 | this.ownerPermission = ownerPermission;
|
---|
51 | this.groupPermission = groupPermission;
|
---|
52 | this.otherPermission = otherPermission;
|
---|
53 | }
|
---|
54 |
|
---|
55 | public EncryptionPermissionTriple(EncryptionPermissionTriple pt) {
|
---|
56 | this.ownerPermission = pt.ownerPermission;
|
---|
57 | this.groupPermission = pt.groupPermission;
|
---|
58 | this.otherPermission = pt.otherPermission;
|
---|
59 | }
|
---|
60 |
|
---|
61 | public UserAppliedEncryptionPermission getPermission(String ownerOfItem, List<String> group) {
|
---|
62 | if (UserSettings.UserName.get().equals(ownerOfItem)) {
|
---|
63 | return ownerPermission;
|
---|
64 | } else if (group.contains(UserSettings.UserName.get())) {
|
---|
65 | return groupPermission;
|
---|
66 | } else {
|
---|
67 | return otherPermission;
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | public static EncryptionPermissionTriple convertString(String permissionCode) {
|
---|
72 | String defaultEncryptionPermission = EncryptionDefaults.DefaultEncryptionPermission.get();
|
---|
73 | return new EncryptionPermissionTriple(permissionCode, defaultEncryptionPermission);
|
---|
74 | }
|
---|
75 |
|
---|
76 | public String getCode() {
|
---|
77 | return Integer.toString(ownerPermission.getCode()) + Integer.toString(groupPermission.getCode()) + Integer.toString(otherPermission.getCode());
|
---|
78 | }
|
---|
79 |
|
---|
80 | @Override
|
---|
81 | public String toString() {
|
---|
82 | return ownerPermission.toString() + ":" + groupPermission.toString() + ":" + otherPermission.toString();
|
---|
83 | }
|
---|
84 | }
|
---|