001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH (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, 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 parent id of the group. */
049    private CmsUUID m_parentId;
050
051    /**
052     * Creates a new, empty OpenCms group principal.
053     */
054    public CmsGroup() {
055
056        // noop
057    }
058
059    /**
060     * Creates a new OpenCms group principal.
061     *
062     * @param id the unique id of the group
063     * @param parentId the is of the parent group
064     * @param name the fully qualified name of the name of the group
065     * @param description the description of the group
066     * @param flags the flags of the group
067     */
068    public CmsGroup(CmsUUID id, CmsUUID parentId, String name, String description, int flags) {
069
070        m_id = id;
071        m_name = name;
072        m_description = description;
073        m_flags = flags;
074        m_parentId = parentId;
075    }
076
077    /**
078     * Checks if the given String starts with {@link I_CmsPrincipal#PRINCIPAL_GROUP} followed by a dot.<p>
079     *
080     * <ul>
081     * <li>Works if the given String is <code>null</code>.
082     * <li>Removes white spaces around the String before the check.
083     * <li>Also works with prefixes not being in upper case.
084     * <li>Does not check if the group after the prefix actually exists.
085     * </ul>
086     *
087     * @param principalName the group name to check
088     *
089     * @return <code>true</code> in case the String starts with {@link I_CmsPrincipal#PRINCIPAL_GROUP}
090     */
091    public static boolean hasPrefix(String principalName) {
092
093        return CmsStringUtil.isNotEmptyOrWhitespaceOnly(principalName)
094            && (principalName.trim().toUpperCase().startsWith(I_CmsPrincipal.PRINCIPAL_GROUP + "."));
095    }
096
097    /**
098     * Removes the prefix if the given String starts with {@link I_CmsPrincipal#PRINCIPAL_GROUP} followed by a dot.<p>
099     *
100     * <ul>
101     * <li>Works if the given String is <code>null</code>.
102     * <li>If the given String does not start with {@link I_CmsPrincipal#PRINCIPAL_GROUP} followed by a dot it is returned unchanged.
103     * <li>Removes white spaces around the group name.
104     * <li>Also works with prefixes not being in upper case.
105     * <li>Does not check if the group after the prefix actually exists.
106     * </ul>
107     *
108     * @param principalName the group name to remove the prefix from
109     *
110     * @return the given String with the prefix {@link I_CmsPrincipal#PRINCIPAL_GROUP} with the following dot removed
111     */
112    public static String removePrefix(String principalName) {
113
114        String result = principalName;
115        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(principalName)) {
116            if (hasPrefix(principalName)) {
117                result = principalName.trim().substring(I_CmsPrincipal.PRINCIPAL_GROUP.length() + 1);
118            }
119        }
120        return result;
121    }
122
123    /**
124     * Checks if the provided group name is valid and can be used as an argument value
125     * for {@link #setName(String)}.<p>
126     *
127     * A group name must not be empty or whitespace only.<p>
128     *
129     * @param name the group name to check
130     *
131     * @see org.opencms.security.I_CmsValidationHandler#checkGroupName(String)
132     */
133    public void checkName(String name) {
134
135        OpenCms.getValidationHandler().checkGroupName(name);
136    }
137
138    /**
139     * @see java.lang.Object#clone()
140     */
141    @Override
142    public Object clone() {
143
144        return new CmsGroup(m_id, m_parentId, m_name, m_description, m_flags);
145    }
146
147    /**
148     * Returns the description of this organizational unit.<p>
149     *
150     * @param locale the locale
151     *
152     * @return the description of this organizational unit
153     */
154    public String getDescription(Locale locale) {
155
156        CmsMacroResolver macroResolver = new CmsMacroResolver();
157        macroResolver.setMessages(org.opencms.db.generic.Messages.get().getBundle(locale));
158        return macroResolver.resolveMacros(m_description);
159    }
160
161    /**
162     * Returns the parent group id of this group.<p>
163     *
164     * @return the parent group id of this group
165     */
166    public CmsUUID getParentId() {
167
168        return m_parentId;
169    }
170
171    /**
172     * @see org.opencms.security.I_CmsPrincipal#isGroup()
173     */
174    @Override
175    public boolean isGroup() {
176
177        return true;
178    }
179
180    /**
181     * Checks if this group is a role group.<p>
182     *
183     * @return <code>true</code> if this group is a role group
184     */
185    public boolean isRole() {
186
187        return (getFlags() & I_CmsPrincipal.FLAG_GROUP_ROLE) == I_CmsPrincipal.FLAG_GROUP_ROLE;
188    }
189
190    /**
191     * @see org.opencms.security.I_CmsPrincipal#isUser()
192     */
193    @Override
194    public boolean isUser() {
195
196        return false;
197    }
198
199    /**
200     * Checks if this group is a virtual group, emulating a role.<p>
201     *
202     * @return if this group is a virtual group
203     */
204    public boolean isVirtual() {
205
206        return (getFlags() & I_CmsPrincipal.FLAG_GROUP_VIRTUAL) == I_CmsPrincipal.FLAG_GROUP_VIRTUAL;
207    }
208
209    /**
210     * Sets the parent group id of this group.<p>
211     *
212     * @param parentId the parent group id to set
213     */
214    public void setParentId(CmsUUID parentId) {
215
216        m_parentId = parentId;
217    }
218
219    /**
220     * @see java.lang.Object#toString()
221     */
222    @Override
223    public String toString() {
224
225        StringBuffer result = new StringBuffer();
226        result.append("[Group]");
227        result.append(" name:");
228        result.append(getName());
229        result.append(" id:");
230        result.append(m_id);
231        result.append(" description:");
232        result.append(m_description);
233        return result.toString();
234    }
235}