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.CmsUUID;
035
036import java.util.Locale;
037
038/**
039 * A group principal in the OpenCms permission system.<p>
040 * 
041 * @since 6.0.0 
042 * 
043 * @see CmsUser
044 */
045public class CmsGroup extends CmsPrincipal {
046
047    /** The parent id of the group. */
048    private CmsUUID m_parentId;
049
050    /**
051     * Creates a new, empty OpenCms group principal.
052     */
053    public CmsGroup() {
054
055        // noop
056    }
057
058    /**
059     * Creates a new OpenCms group principal.
060     * 
061     * @param id the unique id of the group
062     * @param parentId the is of the parent group
063     * @param name the fully qualified name of the name of the group
064     * @param description the description of the group
065     * @param flags the flags of the group    
066     */
067    public CmsGroup(CmsUUID id, CmsUUID parentId, String name, String description, int flags) {
068
069        m_id = id;
070        m_name = name;
071        m_description = description;
072        m_flags = flags;
073        m_parentId = parentId;
074    }
075
076    /**
077     * Checks if the provided group name is valid and can be used as an argument value 
078     * for {@link #setName(String)}.<p> 
079     * 
080     * A group name must not be empty or whitespace only.<p>
081     * 
082     * @param name the group name to check
083     * 
084     * @see org.opencms.security.I_CmsValidationHandler#checkGroupName(String)
085     */
086    public void checkName(String name) {
087
088        OpenCms.getValidationHandler().checkGroupName(name);
089    }
090
091    /**
092     * @see java.lang.Object#clone()
093     */
094    @Override
095    public Object clone() {
096
097        return new CmsGroup(m_id, m_parentId, m_name, m_description, m_flags);
098    }
099
100    /**
101     * Returns the description of this organizational unit.<p>
102     *
103     * @param locale the locale
104     *
105     * @return the description of this organizational unit
106     */
107    public String getDescription(Locale locale) {
108
109        CmsMacroResolver macroResolver = new CmsMacroResolver();
110        macroResolver.setMessages(org.opencms.db.generic.Messages.get().getBundle(locale));
111        return macroResolver.resolveMacros(m_description);
112    }
113
114    /**
115     * Returns the parent group id of this group.<p>
116     * 
117     * @return the parent group id of this group
118     */
119    public CmsUUID getParentId() {
120
121        return m_parentId;
122    }
123
124    /**
125     * @see org.opencms.security.I_CmsPrincipal#isGroup()
126     */
127    @Override
128    public boolean isGroup() {
129
130        return true;
131    }
132
133    /**
134     * Returns <code>true</code> if this group is enabled as a project user group.<p> 
135     * 
136     * @return <code>true</code> if this group is enabled as a project user group 
137     */
138    public boolean isProjectCoWorker() {
139
140        return (getFlags() & I_CmsPrincipal.FLAG_GROUP_PROJECT_USER) == I_CmsPrincipal.FLAG_GROUP_PROJECT_USER;
141    }
142
143    /**
144     * Returns <code>true</code> if this group is enabled as a project manager group.<p> 
145     * 
146     * @return <code>true</code> if this group is enabled as a project manager group
147     */
148    public boolean isProjectManager() {
149
150        return (getFlags() & I_CmsPrincipal.FLAG_GROUP_PROJECT_MANAGER) == I_CmsPrincipal.FLAG_GROUP_PROJECT_MANAGER;
151    }
152
153    /**
154     * Checks if this group is a role group.<p>
155     * 
156     * @return <code>true</code> if this group is a role group
157     */
158    public boolean isRole() {
159
160        return (getFlags() & I_CmsPrincipal.FLAG_GROUP_ROLE) == I_CmsPrincipal.FLAG_GROUP_ROLE;
161    }
162
163    /**
164     * @see org.opencms.security.I_CmsPrincipal#isUser()
165     */
166    @Override
167    public boolean isUser() {
168
169        return false;
170    }
171
172    /**
173     * Checks if this group is a virtual group, emulating a role.<p>
174     * 
175     * @return if this group is a virtual group
176     */
177    public boolean isVirtual() {
178
179        return (getFlags() & I_CmsPrincipal.FLAG_GROUP_VIRTUAL) == I_CmsPrincipal.FLAG_GROUP_VIRTUAL;
180    }
181
182    /**
183     * Sets the parent group id of this group.<p>
184     * 
185     * @param parentId the parent group id to set
186     */
187    public void setParentId(CmsUUID parentId) {
188
189        m_parentId = parentId;
190    }
191
192    /**
193     * Sets the project user flag for this group to the given value.<p>
194     * 
195     * @param value the value to set
196     */
197    public void setProjectCoWorker(boolean value) {
198
199        if (isProjectCoWorker() != value) {
200            setFlags(getFlags() ^ I_CmsPrincipal.FLAG_GROUP_PROJECT_USER);
201        }
202    }
203
204    /**
205     * Sets the project manager flag for this group to the given value.<p>
206     * 
207     * @param value the value to set
208     */
209    public void setProjectManager(boolean value) {
210
211        if (isProjectManager() != value) {
212            setFlags(getFlags() ^ I_CmsPrincipal.FLAG_GROUP_PROJECT_MANAGER);
213        }
214    }
215
216    /**
217     * @see java.lang.Object#toString()
218     */
219    @Override
220    public String toString() {
221
222        StringBuffer result = new StringBuffer();
223        result.append("[Group]");
224        result.append(" name:");
225        result.append(getName());
226        result.append(" id:");
227        result.append(m_id);
228        result.append(" description:");
229        result.append(m_description);
230        return result.toString();
231    }
232}