1 31 32 package org.opencms.security; 33 34 import org.opencms.file.CmsGroup; 35 import org.opencms.file.CmsObject; 36 import org.opencms.file.CmsUser; 37 import org.opencms.main.CmsException; 38 import org.opencms.util.CmsStringUtil; 39 import org.opencms.util.CmsUUID; 40 41 import java.util.Iterator ; 42 import java.util.List ; 43 44 54 public abstract class CmsPrincipal implements I_CmsPrincipal { 55 56 57 protected String m_description; 58 59 60 protected int m_flags; 61 62 63 protected CmsUUID m_id; 64 65 66 protected String m_name; 67 68 71 protected CmsPrincipal() { 72 73 } 75 76 85 public static List filterCore(List principals) { 86 87 Iterator it = principals.iterator(); 88 while (it.hasNext()) { 89 CmsPrincipal p = (CmsPrincipal)it.next(); 90 if (p.getFlags() > I_CmsPrincipal.FLAG_CORE_LIMIT) { 91 it.remove(); 92 } 93 } 94 return principals; 95 } 96 97 108 public static List filterCoreFlag(List principals, int flag) { 109 110 Iterator it = principals.iterator(); 111 while (it.hasNext()) { 112 CmsPrincipal p = (CmsPrincipal)it.next(); 113 if (p.getFlags() > I_CmsPrincipal.FLAG_CORE_LIMIT && (p.getFlags() & flag) != flag) { 114 it.remove(); 115 } 116 } 117 return principals; 118 } 119 120 130 public static List filterFlag(List principals, int flag) { 131 132 Iterator it = principals.iterator(); 133 while (it.hasNext()) { 134 CmsPrincipal p = (CmsPrincipal)it.next(); 135 if ((p.getFlags() & flag) != flag) { 136 it.remove(); 137 } 138 } 139 return principals; 140 } 141 142 148 public static String getPrefixedGroup(String name) { 149 150 StringBuffer result = new StringBuffer (name.length() + 10); 151 result.append(I_CmsPrincipal.PRINCIPAL_GROUP); 152 result.append('.'); 153 result.append(name); 154 return result.toString(); 155 } 156 157 163 public static String getPrefixedUser(String name) { 164 165 StringBuffer result = new StringBuffer (name.length() + 10); 166 result.append(I_CmsPrincipal.PRINCIPAL_USER); 167 result.append('.'); 168 result.append(name); 169 return result.toString(); 170 } 171 172 186 public static I_CmsPrincipal readPrefixedPrincipal(CmsObject cms, String name) throws CmsException { 187 188 if (CmsStringUtil.isNotEmpty(name)) { 189 String upperCaseName = name.toUpperCase(); 190 if (upperCaseName.startsWith(I_CmsPrincipal.PRINCIPAL_GROUP)) { 191 String groupName = name.substring(I_CmsPrincipal.PRINCIPAL_GROUP.length() + 1); 193 return cms.readGroup(groupName); 194 } else if (upperCaseName.startsWith(I_CmsPrincipal.PRINCIPAL_USER)) { 195 String userName = name.substring(I_CmsPrincipal.PRINCIPAL_USER.length() + 1); 197 return cms.readUser(userName); 198 } 199 } 200 throw new CmsSecurityException(Messages.get().container(Messages.ERR_INVALID_PRINCIPAL_1, name)); 202 } 203 204 219 public static I_CmsPrincipal readPrincipal(CmsObject cms, String type, String name) throws CmsException { 220 221 if (CmsStringUtil.isNotEmpty(type)) { 222 String upperCaseType = type.toUpperCase(); 223 if (PRINCIPAL_GROUP.equals(upperCaseType)) { 224 return cms.readGroup(name); 226 } else if (PRINCIPAL_USER.equals(upperCaseType)) { 227 return cms.readUser(name); 229 } 230 } 231 throw new CmsSecurityException(Messages.get().container(Messages.ERR_INVALID_PRINCIPAL_TYPE_2, type, name)); 233 } 234 235 238 public boolean equals(Object obj) { 239 240 if (obj == this) { 241 return true; 242 } 243 if (obj instanceof I_CmsPrincipal) { 244 if (m_id != null) { 245 return m_id.equals(((I_CmsPrincipal)obj).getId()); 246 } 247 } 248 return false; 249 } 250 251 254 public String getDescription() { 255 256 return m_description; 257 } 258 259 262 public int getFlags() { 263 264 return m_flags; 265 } 266 267 270 public CmsUUID getId() { 271 272 return m_id; 273 } 274 275 278 public String getName() { 279 280 return m_name; 281 } 282 283 286 public String getPrefixedName() { 287 288 if (isUser()) { 289 return getPrefixedUser(getName()); 290 } else if (isGroup()) { 291 return getPrefixedGroup(getName()); 292 } 293 return getName(); 294 } 295 296 299 public int hashCode() { 300 301 if (m_id != null) { 302 return m_id.hashCode(); 303 } 304 return CmsUUID.getNullUUID().hashCode(); 305 } 306 307 310 public boolean isEnabled() { 311 312 return (getFlags() & I_CmsPrincipal.FLAG_DISABLED) == 0; 313 } 314 315 318 public boolean isGroup() { 319 320 return this instanceof CmsGroup; 321 } 322 323 326 public boolean isUser() { 327 328 return this instanceof CmsUser; 329 } 330 331 334 public void setDescription(String description) { 335 336 m_description = description; 337 } 338 339 342 public void setEnabled(boolean enabled) { 343 344 if (enabled != isEnabled()) { 345 setFlags(getFlags() ^ I_CmsPrincipal.FLAG_DISABLED); 347 } 348 } 349 350 353 public void setFlags(int value) { 354 355 m_flags = value; 356 } 357 358 361 public void setName(String name) { 362 363 checkName(name); 364 m_name = name; 365 } 366 } | Popular Tags |