source: trunk/src/org/expeditee/items/PermissionTriple.java@ 1402

Last change on this file since 1402 was 1402, checked in by bln4, 5 years ago

Permission is now a triple rather than a pair. The new second bit is not yet used but will be used for group level permissions.

gazumpts

  • Property svn:executable set to *
File size: 4.5 KB
Line 
1/**
2 * PermissionPair.java
3 * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package org.expeditee.items;
20
21import java.util.Arrays;
22import java.util.List;
23
24import org.expeditee.settings.UserSettings;
25
26public class PermissionTriple {
27
28 private UserAppliedPermission ownerPermission;
29 private UserAppliedPermission groupPermission;
30 private UserAppliedPermission otherPermission;
31
32 public PermissionTriple(String permissionCode, UserAppliedPermission defaultPermission) {
33 ownerPermission = defaultPermission;
34 groupPermission = defaultPermission;
35 otherPermission = defaultPermission;
36
37 if (permissionCode != null && permissionCode.length() > 0) {
38 permissionCode = permissionCode.trim().toLowerCase();
39
40 int numberOfMinuses = permissionCode.chars().reduce(0, (a, b) -> b == '-' ? a + 1 : a);
41 String[] codes = new String[3];
42 Arrays.fill(codes, "");
43 if (permissionCode.length() == 1 + numberOfMinuses) {
44 codes[0] = permissionCode;
45 } else {
46 int i = 0;
47 for (char c : permissionCode.toCharArray()) {
48 try {
49 int asInt = Integer.parseInt(c + "");
50 codes[i] += asInt + "";
51 i++;
52 } catch (NumberFormatException e) {
53 if (c == '-') {
54 codes[i] += c + "";
55 }
56 }
57 }
58 }
59
60 this.ownerPermission = UserAppliedPermission.getPermission(codes[0], this.ownerPermission);
61 if (codes[1].length() == 0) { // If there is only a permission code for owner.
62 this.groupPermission = UserAppliedPermission.getPermission(codes[0], this.groupPermission);
63 this.otherPermission = UserAppliedPermission.getPermission(codes[0], this.otherPermission);
64 } else if (codes[2].length() == 0) { // If there is only two of three permission codes specified.
65 this.groupPermission = UserAppliedPermission.getPermission(codes[1], this.groupPermission);
66 this.otherPermission = UserAppliedPermission.getPermission(codes[1], this.otherPermission);
67 } else { // If all permission codes are specified.
68 this.groupPermission = UserAppliedPermission.getPermission(codes[1], this.groupPermission);
69 this.otherPermission = UserAppliedPermission.getPermission(codes[2], this.otherPermission);
70 }
71 }
72 }
73
74 public PermissionTriple(UserAppliedPermission ownerPermission, UserAppliedPermission groupPermission, UserAppliedPermission otherPermission) {
75 this.ownerPermission = ownerPermission;
76 this.groupPermission = groupPermission;
77 this.otherPermission = otherPermission;
78 }
79
80 public PermissionTriple(UserAppliedPermission permissionForAll) {
81 this(permissionForAll, permissionForAll, permissionForAll);
82 }
83
84 public PermissionTriple(PermissionTriple pt) {
85 this.ownerPermission = pt.ownerPermission;
86 this.groupPermission = pt.groupPermission;
87 this.otherPermission = pt.otherPermission;
88 }
89
90 public UserAppliedPermission getPermission(String ownerOfItem, List<String> group) {
91 if (UserSettings.UserName.get().equals(ownerOfItem)) {
92 return ownerPermission;
93 } else if (group.contains(UserSettings.UserName.get())) {
94 return groupPermission;
95 } else {
96 return otherPermission;
97 }
98 }
99
100 /**
101 * Converts the given Expeditee permission code into a PermissionPair
102 * corresponding to the constants defined in Item.
103 *
104 * @param permissionCode
105 * The Expeditee permission code to convert
106 * @return The resulting PermissionPair corresponding to a pair of constants as
107 * defined in Item
108 */
109 public static PermissionTriple convertString(String permissionCode) {
110 PermissionTriple pt = new PermissionTriple(permissionCode, UserAppliedPermission.full);
111 return pt;
112 }
113
114 public String getCode() {
115 return Integer.toString(ownerPermission.getCode()) + Integer.toString(groupPermission.getCode()) + Integer.toString(otherPermission.getCode());
116 }
117
118 @Override
119 public String toString() {
120 return ownerPermission + ":" + groupPermission + ":" + otherPermission;
121 }
122}
Note: See TracBrowser for help on using the repository browser.