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.db;
029
030import org.opencms.main.CmsRuntimeException;
031import org.opencms.security.CmsOrganizationalUnit;
032import org.opencms.util.CmsStringUtil;
033
034/**
035 * Provides access to the names of the OpenCms default users and groups.<p>
036 *
037 * @since 6.0.0
038 */
039public class CmsDefaultUsers {
040
041    /** Default name for the "Administrators" group. */
042    protected static final String DEFAULT_GROUP_ADMINISTRATORS = "Administrators";
043
044    /** Default name for the "Guests" group. */
045    protected static final String DEFAULT_GROUP_GUESTS = "Guests";
046
047    /** Default name for the "Users" group. */
048    protected static final String DEFAULT_GROUP_USERS = "Users";
049
050    /** Default name for the "Admin" user. */
051    protected static final String DEFAULT_USER_ADMIN = "Admin";
052
053    /** Default name for the "Deleted Resource" user. */
054    protected static final String DEFAULT_USER_DELETED_RESOURCE = "Admin";
055
056    /** Default name for the "Export" user. */
057    protected static final String DEFAULT_USER_EXPORT = "Export";
058
059    /** Default name for the "Guest" user. */
060    protected static final String DEFAULT_USER_GUEST = "Guest";
061
062    /** Administrators group name. */
063    private String m_groupAdministrators;
064
065    /** Guests group name. */
066    private String m_groupGuests;
067
068    /** System Users group name. */
069    private String m_groupUsers;
070
071    /** Administrator user name. */
072    private String m_userAdmin;
073
074    /** Deleted resource user name. */
075    private String m_userDeletedResource;
076
077    /** Export user name. */
078    private String m_userExport;
079
080    /** Guest user name. */
081    private String m_userGuest;
082
083    /**
084     * Constructor that initializes all names with default values.<p>
085     *
086     * See the constants of this class for the defaule values that are uses.<p>
087     */
088    public CmsDefaultUsers() {
089
090        m_userAdmin = DEFAULT_USER_ADMIN;
091        m_userGuest = DEFAULT_USER_GUEST;
092        m_userExport = DEFAULT_USER_EXPORT;
093        m_userDeletedResource = DEFAULT_USER_DELETED_RESOURCE;
094        m_groupAdministrators = DEFAULT_GROUP_ADMINISTRATORS;
095        m_groupUsers = DEFAULT_GROUP_USERS;
096        m_groupGuests = DEFAULT_GROUP_GUESTS;
097    }
098
099    /**
100     * Public constructor. <p>
101     *
102     * @param userAdmin the name of the default admin user
103     * @param userGuest the name of the guest user
104     * @param userExport the name of the export user
105     * @param userDeletedResource the name of the deleted resource user, can be <code>null</code>
106     * @param groupAdministrators the name of the administrators group
107     * @param groupUsers the name of the users group
108     * @param groupGuests the name of the guests group
109     */
110    public CmsDefaultUsers(
111        String userAdmin,
112        String userGuest,
113        String userExport,
114        String userDeletedResource,
115        String groupAdministrators,
116        String groupUsers,
117        String groupGuests) {
118
119        init(userAdmin, userGuest, userExport, userDeletedResource, groupAdministrators, groupUsers, groupGuests);
120    }
121
122    /**
123     * Returns the name of the administrators group.<p>
124     *
125     * @return the name of the administrators group
126     */
127    public String getGroupAdministrators() {
128
129        return m_groupAdministrators;
130    }
131
132    /**
133     * Returns the name of the guests group.<p>
134     *
135     * @return the name of the guests group
136     */
137    public String getGroupGuests() {
138
139        return m_groupGuests;
140    }
141
142    /**
143     * Returns the name of the project managers group.<p>
144     *
145     * @return the name of the project managers group
146     *
147     * @deprecated
148     *      As of OpenCms 9.5, there is no default group for project managers anymore.
149     *      Use the role {@link org.opencms.security.CmsRole#PROJECT_MANAGER} instead in case you want a user to be able to manage projects.
150     *      For backward compatibility in old installations, this returns the old default group name "Projectmanagers".
151     */
152    @Deprecated
153    public String getGroupProjectmanagers() {
154
155        return "Projectmanagers";
156    }
157
158    /**
159     * Returns the name of the users group.<p>
160     *
161     * @return the name of the users group
162     */
163    public String getGroupUsers() {
164
165        return m_groupUsers;
166    }
167
168    /**
169     * Returns the name of the default administrator user.<p>
170     *
171     * @return the name of the default administrator user
172     */
173    public String getUserAdmin() {
174
175        return m_userAdmin;
176    }
177
178    /**
179     * Returns the name of the default deleted resource user.<p>
180     *
181     * @return the name of the default deleted resource user
182     */
183    public String getUserDeletedResource() {
184
185        return m_userDeletedResource;
186    }
187
188    /**
189     * Returns the name of the user used to generate the static export.<p>
190     *
191     * @return the name of the user used to generate the static export
192     */
193    public String getUserExport() {
194
195        return m_userExport;
196    }
197
198    /**
199     * Returns the name of the default guest user.<p>
200     *
201     * @return the name of the default guest user
202     */
203    public String getUserGuest() {
204
205        return m_userGuest;
206    }
207
208    /**
209     * Checks if a given group name is the name of one of the OpenCms default groups.<p>
210     *
211     * @param groupName the group name to check
212     * @return <code>true</code> if group name is one of OpenCms default groups, <code>false</code> if it is not
213     * or if <code>groupName</code> is <code>null</code> or an empty string (no trim)
214     *
215     * @see #getGroupAdministrators()
216     * @see #getGroupUsers()
217     * @see #getGroupGuests()
218     */
219    public boolean isDefaultGroup(String groupName) {
220
221        if (CmsStringUtil.isEmptyOrWhitespaceOnly(groupName)) {
222            return false;
223        }
224
225        // first check without ou prefix, to stay backwards compatible
226        boolean isDefault = m_groupAdministrators.equals(groupName);
227        isDefault = isDefault || m_groupGuests.equals(groupName);
228        isDefault = isDefault || m_groupUsers.equals(groupName);
229
230        // Administrators and Guests groups have no special status in sub-OUs, but the Users group does
231        isDefault = isDefault || groupName.equals(CmsOrganizationalUnit.SEPARATOR + m_groupAdministrators);
232        isDefault = isDefault || groupName.equals(CmsOrganizationalUnit.SEPARATOR + m_groupGuests);
233        isDefault = isDefault || groupName.endsWith(CmsOrganizationalUnit.SEPARATOR + m_groupUsers);
234
235        return isDefault;
236    }
237
238    /**
239     * Checks if a given user name is the name of one of the OpenCms default users.<p>
240     *
241     * @param userName the group name to check
242     *
243     * @return <code>true</code> if user name is one of OpenCms default users, <code>false</code> if it is not
244     * or if <code>userName</code> is <code>null</code> or an empty string (no trim)
245     *
246     * @see #getUserAdmin()
247     * @see #getUserExport()
248     * @see #getUserGuest()
249     * @see #getUserDeletedResource()
250     */
251    public boolean isDefaultUser(String userName) {
252
253        if (CmsStringUtil.isEmptyOrWhitespaceOnly(userName)) {
254            return false;
255        }
256
257        // first check without ou prefix, to stay backwards compatible
258        boolean isDefault = m_userAdmin.equals(userName);
259        isDefault = isDefault || m_userGuest.equals(userName);
260        isDefault = isDefault || m_userExport.equals(userName);
261        isDefault = isDefault || m_userDeletedResource.equals(userName);
262
263        // now check with ou prefix
264        isDefault = isDefault || userName.equals(CmsOrganizationalUnit.SEPARATOR + m_userAdmin);
265        isDefault = isDefault || userName.equals(CmsOrganizationalUnit.SEPARATOR + m_userGuest);
266        isDefault = isDefault || userName.equals(CmsOrganizationalUnit.SEPARATOR + m_userExport);
267        isDefault = isDefault || userName.equals(CmsOrganizationalUnit.SEPARATOR + m_userDeletedResource);
268
269        return isDefault;
270    }
271
272    /**
273     * Checks if a given group name is the name of the guests group.<p>
274     *
275     * @param groupName the group name to check
276     *
277     * @return <code>true</code> if a given group name is the name of the guests group
278     */
279    public boolean isGroupGuests(String groupName) {
280
281        if (CmsStringUtil.isEmptyOrWhitespaceOnly(groupName)) {
282            return false;
283        }
284        return m_groupGuests.equals(groupName) || groupName.endsWith(CmsOrganizationalUnit.SEPARATOR + m_groupGuests);
285    }
286
287    /**
288     * Checks if a given user name is the name of the admin user.<p>
289     *
290     * @param userName the user name to check
291     *
292     * @return <code>true</code> if a given user name is the name of the admin user
293     */
294    public boolean isUserAdmin(String userName) {
295
296        if (CmsStringUtil.isEmptyOrWhitespaceOnly(userName)) {
297            return false;
298        }
299        return m_userAdmin.equals(userName);
300    }
301
302    /**
303     * Checks if a given user name is the name of the export user.<p>
304     *
305     * @param userName the user name to check
306     *
307     * @return <code>true</code> if a given user name is the name of the export user
308     */
309    public boolean isUserExport(String userName) {
310
311        if (CmsStringUtil.isEmptyOrWhitespaceOnly(userName)) {
312            return false;
313        }
314        return m_userExport.equals(userName);
315    }
316
317    /**
318     * Checks if a given user name is the name of the guest user.<p>
319     *
320     * @param userName the user name to check
321     *
322     * @return <code>true</code> if a given user name is the name of the guest user
323     */
324    public boolean isUserGuest(String userName) {
325
326        if (CmsStringUtil.isEmptyOrWhitespaceOnly(userName)) {
327            return false;
328        }
329        return m_userGuest.equals(userName);
330    }
331
332    /**
333     * Initializes this instance.<p>
334     *
335     * @param userAdmin the name of the default admin user
336     * @param userGuest the name of the guest user
337     * @param userExport the name of the export user
338     * @param userDeletedResource the name of the deleted resource user, can be <code>null</code>
339     * @param groupAdministrators the name of the administrators group
340     * @param groupUsers the name of the users group
341     * @param groupGuests the name of the guests group
342     */
343    protected void init(
344        String userAdmin,
345        String userGuest,
346        String userExport,
347        String userDeletedResource,
348        String groupAdministrators,
349        String groupUsers,
350        String groupGuests) {
351
352        // check if all required user and group names are not null or empty
353        if (CmsStringUtil.isEmptyOrWhitespaceOnly(userAdmin)
354            || CmsStringUtil.isEmptyOrWhitespaceOnly(userGuest)
355            || CmsStringUtil.isEmptyOrWhitespaceOnly(userExport)
356            || CmsStringUtil.isEmptyOrWhitespaceOnly(groupAdministrators)
357            || CmsStringUtil.isEmptyOrWhitespaceOnly(groupUsers)
358            || CmsStringUtil.isEmptyOrWhitespaceOnly(groupGuests)) {
359            throw new CmsRuntimeException(Messages.get().container(Messages.ERR_USER_GROUP_NAMES_EMPTY_0));
360        }
361        // set members
362        m_userAdmin = userAdmin.trim();
363        m_userGuest = userGuest.trim();
364        m_userExport = userExport.trim();
365        if (CmsStringUtil.isEmptyOrWhitespaceOnly(userDeletedResource)) {
366            m_userDeletedResource = userAdmin;
367        } else {
368            m_userDeletedResource = userDeletedResource.trim();
369        }
370        m_groupAdministrators = groupAdministrators.trim();
371        m_groupUsers = groupUsers.trim();
372        m_groupGuests = groupGuests.trim();
373    }
374}