001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software GmbH & Co. KG, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.file; 029 030import org.opencms.main.OpenCms; 031import org.opencms.security.CmsPrincipal; 032import org.opencms.security.I_CmsPrincipal; 033import org.opencms.util.CmsMacroResolver; 034import org.opencms.util.CmsStringUtil; 035import org.opencms.util.CmsUUID; 036 037import java.util.Locale; 038 039/** 040 * A group principal in the OpenCms permission system.<p> 041 * 042 * @since 6.0.0 043 * 044 * @see CmsUser 045 */ 046public class CmsGroup extends CmsPrincipal { 047 048 /** The serial version id. */ 049 private static final long serialVersionUID = 468957888179396857L; 050 051 /** The parent id of the group. */ 052 private CmsUUID m_parentId; 053 054 /** 055 * Creates a new, empty OpenCms group principal. 056 */ 057 public CmsGroup() { 058 059 // noop 060 } 061 062 /** 063 * Creates a new OpenCms group principal. 064 * 065 * @param id the unique id of the group 066 * @param parentId the is of the parent group 067 * @param name the fully qualified name of the name of the group 068 * @param description the description of the group 069 * @param flags the flags of the group 070 */ 071 public CmsGroup(CmsUUID id, CmsUUID parentId, String name, String description, int flags) { 072 073 m_id = id; 074 m_name = name; 075 m_description = description; 076 m_flags = flags; 077 m_parentId = parentId; 078 } 079 080 /** 081 * Checks if the given String starts with {@link I_CmsPrincipal#PRINCIPAL_GROUP} followed by a dot.<p> 082 * 083 * <ul> 084 * <li>Works if the given String is <code>null</code>. 085 * <li>Removes white spaces around the String before the check. 086 * <li>Also works with prefixes not being in upper case. 087 * <li>Does not check if the group after the prefix actually exists. 088 * </ul> 089 * 090 * @param principalName the group name to check 091 * 092 * @return <code>true</code> in case the String starts with {@link I_CmsPrincipal#PRINCIPAL_GROUP} 093 */ 094 public static boolean hasPrefix(String principalName) { 095 096 return CmsStringUtil.isNotEmptyOrWhitespaceOnly(principalName) 097 && (principalName.trim().toUpperCase().startsWith(I_CmsPrincipal.PRINCIPAL_GROUP + ".")); 098 } 099 100 /** 101 * Removes the prefix if the given String starts with {@link I_CmsPrincipal#PRINCIPAL_GROUP} followed by a dot.<p> 102 * 103 * <ul> 104 * <li>Works if the given String is <code>null</code>. 105 * <li>If the given String does not start with {@link I_CmsPrincipal#PRINCIPAL_GROUP} followed by a dot it is returned unchanged. 106 * <li>Removes white spaces around the group name. 107 * <li>Also works with prefixes not being in upper case. 108 * <li>Does not check if the group after the prefix actually exists. 109 * </ul> 110 * 111 * @param principalName the group name to remove the prefix from 112 * 113 * @return the given String with the prefix {@link I_CmsPrincipal#PRINCIPAL_GROUP} with the following dot removed 114 */ 115 public static String removePrefix(String principalName) { 116 117 String result = principalName; 118 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(principalName)) { 119 if (hasPrefix(principalName)) { 120 result = principalName.trim().substring(I_CmsPrincipal.PRINCIPAL_GROUP.length() + 1); 121 } 122 } 123 return result; 124 } 125 126 /** 127 * Checks if the provided group name is valid and can be used as an argument value 128 * for {@link #setName(String)}.<p> 129 * 130 * A group name must not be empty or whitespace only.<p> 131 * 132 * @param name the group name to check 133 * 134 * @see org.opencms.security.I_CmsValidationHandler#checkGroupName(String) 135 */ 136 public void checkName(String name) { 137 138 OpenCms.getValidationHandler().checkGroupName(name); 139 } 140 141 /** 142 * @see java.lang.Object#clone() 143 */ 144 @Override 145 public Object clone() { 146 147 return new CmsGroup(m_id, m_parentId, m_name, m_description, m_flags); 148 } 149 150 /** 151 * Returns the description of this organizational unit.<p> 152 * 153 * @param locale the locale 154 * 155 * @return the description of this organizational unit 156 */ 157 public String getDescription(Locale locale) { 158 159 CmsMacroResolver macroResolver = new CmsMacroResolver(); 160 macroResolver.setMessages(org.opencms.db.generic.Messages.get().getBundle(locale)); 161 return macroResolver.resolveMacros(m_description); 162 } 163 164 /** 165 * Returns the parent group id of this group.<p> 166 * 167 * @return the parent group id of this group 168 */ 169 public CmsUUID getParentId() { 170 171 return m_parentId; 172 } 173 174 /** 175 * @see org.opencms.security.I_CmsPrincipal#isGroup() 176 */ 177 @Override 178 public boolean isGroup() { 179 180 return true; 181 } 182 183 /** 184 * Checks if this group is a role group.<p> 185 * 186 * @return <code>true</code> if this group is a role group 187 */ 188 public boolean isRole() { 189 190 return (getFlags() & I_CmsPrincipal.FLAG_GROUP_ROLE) == I_CmsPrincipal.FLAG_GROUP_ROLE; 191 } 192 193 /** 194 * @see org.opencms.security.I_CmsPrincipal#isUser() 195 */ 196 @Override 197 public boolean isUser() { 198 199 return false; 200 } 201 202 /** 203 * Checks if this group is a virtual group, emulating a role.<p> 204 * 205 * @return if this group is a virtual group 206 */ 207 public boolean isVirtual() { 208 209 return (getFlags() & I_CmsPrincipal.FLAG_GROUP_VIRTUAL) == I_CmsPrincipal.FLAG_GROUP_VIRTUAL; 210 } 211 212 /** 213 * Sets the parent group id of this group.<p> 214 * 215 * @param parentId the parent group id to set 216 */ 217 public void setParentId(CmsUUID parentId) { 218 219 m_parentId = parentId; 220 } 221 222 /** 223 * @see java.lang.Object#toString() 224 */ 225 @Override 226 public String toString() { 227 228 StringBuffer result = new StringBuffer(); 229 result.append("[Group]"); 230 result.append(" name:"); 231 result.append(getName()); 232 result.append(" id:"); 233 result.append(m_id); 234 result.append(" description:"); 235 result.append(m_description); 236 return result.toString(); 237 } 238}