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
RevLine 
[919]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
[449]19package org.expeditee.items;
20
[1402]21import java.util.Arrays;
22import java.util.List;
23
[570]24import org.expeditee.settings.UserSettings;
[449]25
[1402]26public class PermissionTriple {
[1190]27
[449]28 private UserAppliedPermission ownerPermission;
[1402]29 private UserAppliedPermission groupPermission;
30 private UserAppliedPermission otherPermission;
[449]31
[1402]32 public PermissionTriple(String permissionCode, UserAppliedPermission defaultPermission) {
[449]33 ownerPermission = defaultPermission;
[1402]34 groupPermission = defaultPermission;
35 otherPermission = defaultPermission;
36
37 if (permissionCode != null && permissionCode.length() > 0) {
[449]38 permissionCode = permissionCode.trim().toLowerCase();
[1402]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 }
[449]57 }
58 }
[1402]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 }
[449]71 }
72 }
73
[1402]74 public PermissionTriple(UserAppliedPermission ownerPermission, UserAppliedPermission groupPermission, UserAppliedPermission otherPermission) {
[1190]75 this.ownerPermission = ownerPermission;
[1402]76 this.groupPermission = groupPermission;
77 this.otherPermission = otherPermission;
[449]78 }
[1190]79
[1402]80 public PermissionTriple(UserAppliedPermission permissionForAll) {
81 this(permissionForAll, permissionForAll, permissionForAll);
[449]82 }
[1190]83
[1402]84 public PermissionTriple(PermissionTriple pt) {
85 this.ownerPermission = pt.ownerPermission;
86 this.groupPermission = pt.groupPermission;
87 this.otherPermission = pt.otherPermission;
[449]88 }
[1190]89
[1402]90 public UserAppliedPermission getPermission(String ownerOfItem, List<String> group) {
[1193]91 if (UserSettings.UserName.get().equals(ownerOfItem)) {
[449]92 return ownerPermission;
[1402]93 } else if (group.contains(UserSettings.UserName.get())) {
94 return groupPermission;
[1190]95 } else {
[1402]96 return otherPermission;
[449]97 }
98 }
[1190]99
[449]100 /**
[1190]101 * Converts the given Expeditee permission code into a PermissionPair
102 * corresponding to the constants defined in Item.
[449]103 *
104 * @param permissionCode
105 * The Expeditee permission code to convert
[1190]106 * @return The resulting PermissionPair corresponding to a pair of constants as
107 * defined in Item
[449]108 */
[1402]109 public static PermissionTriple convertString(String permissionCode) {
110 PermissionTriple pt = new PermissionTriple(permissionCode, UserAppliedPermission.full);
111 return pt;
[449]112 }
[1190]113
[449]114 public String getCode() {
[1402]115 return Integer.toString(ownerPermission.getCode()) + Integer.toString(groupPermission.getCode()) + Integer.toString(otherPermission.getCode());
[449]116 }
117
[1190]118 @Override
119 public String toString() {
[1402]120 return ownerPermission + ":" + groupPermission + ":" + otherPermission;
[449]121 }
122}
Note: See TracBrowser for help on using the repository browser.