1 25 26 package org.snipsnap.user; 27 28 import org.radeox.util.logging.Logger; 29 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.Map ; 33 import java.util.StringTokenizer ; 34 35 42 43 public class Permissions { 44 public final static String EDIT_SNIP = "Edit"; 45 public final static String ATTACH_TO_SNIP = "Attach"; 46 public final static String POST_TO_SNIP = "Post"; 47 48 private Map permissions; 49 50 public Permissions() { 51 } 52 53 public Permissions(Map permissions) { 54 this.permissions = permissions; 55 } 56 57 public Permissions(String permissions) { 58 this.permissions = deserialize(permissions); 60 } 61 62 private void init() { 63 if (null == permissions) { 64 permissions = new HashMap (); 65 } 66 } 67 68 public String toString() { 69 return serialize(); 70 } 71 72 public boolean empty() { 73 return null == permissions || permissions.isEmpty(); 74 } 75 76 public void remove(String permission, String role) { 77 init(); 78 if (permissions.containsKey(permission)) { 79 Roles roles = (Roles) permissions.get(permission); 80 roles.remove(role); 81 if (roles.isEmpty()) { 83 Logger.debug("Empty."); 84 permissions.remove(permission); 85 } else { 86 Logger.debug("not Empty." + permissions.toString()); 87 } 88 } 89 } 90 91 public void add(String permission) { 92 init(); 93 if (!permissions.containsKey(permission)) { 94 permissions.put(permission, new Roles()); 95 } 96 } 97 98 public void add(String permission, String role) { 99 init(); 100 if (!permissions.containsKey(permission)) { 101 permissions.put(permission, new Roles()); 102 } 103 ((Roles) permissions.get(permission)).add(role); 104 return; 105 } 106 107 public void add(String permission, Roles roles) { 108 init(); 109 if (!permissions.containsKey(permission)) { 110 permissions.put(permission, new Roles()); 111 } 112 ((Roles) permissions.get(permission)).addAll(roles); 113 return; 114 } 115 116 public boolean exists(String permission, Roles roles) { 117 if (null == permissions || !permissions.containsKey(permission)) { return false; } 119 Roles permRoles = (Roles) permissions.get(permission); 120 return permRoles.containsAny(roles); 121 } 122 123 public boolean check(String permission, Roles roles) { 124 if (null == permissions || !permissions.containsKey(permission)) { return true; } 126 Roles permRoles = (Roles) permissions.get(permission); 127 return permRoles.containsAny(roles); 133 } 134 135 public Map deserialize(String permissions) { 136 Map perms = new HashMap (); 137 if (permissions == null || "".equals(permissions)) { return perms; } 138 139 StringTokenizer tokenizer = new StringTokenizer (permissions, "|"); 140 while (tokenizer.hasMoreTokens()) { 141 String permission = tokenizer.nextToken(); 142 Roles roles = getRoles(permission); 143 permission = getPermission(permission); 144 perms.put(permission, roles); 145 } 146 147 return perms; 148 } 149 150 private String serialize() { 151 if (null == permissions || permissions.isEmpty()) { return ""; } 152 153 StringBuffer permBuffer = new StringBuffer (); 154 Iterator iterator = permissions.keySet().iterator(); 155 while (iterator.hasNext()) { 156 String permission = (String ) iterator.next(); 157 permBuffer.append(permission); 158 permBuffer.append(":"); 159 Roles roles = (Roles) permissions.get(permission); 160 Iterator rolesIterator = roles.iterator(); 161 while (rolesIterator.hasNext()) { 162 String role = (String ) rolesIterator.next(); 163 permBuffer.append(role); 164 if (rolesIterator.hasNext()) { 165 permBuffer.append(","); 166 } 167 } 168 if (iterator.hasNext()) { 169 permBuffer.append("|"); 170 } 171 } 172 return permBuffer.toString(); 173 } 174 175 private String after(String string, String delimiter) { 176 return string.substring(string.indexOf(delimiter) + 1); 177 } 178 179 private String before(String string, String delimiter) { 180 return string.substring(0, string.indexOf(delimiter)); 181 } 182 183 private String getPermission(String rolesString) { 184 return before(rolesString, ":"); 185 } 186 187 private Roles getRoles(String rolesString) { 188 Roles roles = new Roles(); 189 StringTokenizer tokenizer = new StringTokenizer (after(rolesString, ":"), ","); 190 while (tokenizer.hasMoreTokens()) { 191 String role = tokenizer.nextToken(); 192 roles.add(role); 193 } 194 return roles; 195 } 196 197 } 198 | Popular Tags |