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.security;
029
030import org.opencms.db.CmsSecurityManager;
031import org.opencms.file.CmsGroup;
032import org.opencms.file.CmsObject;
033import org.opencms.file.CmsProject;
034import org.opencms.file.CmsResource;
035import org.opencms.file.CmsResourceFilter;
036import org.opencms.file.CmsUser;
037import org.opencms.file.CmsUserSearchParameters;
038import org.opencms.main.CmsException;
039
040import java.util.List;
041
042/**
043 * This manager provide access to the organizational unit related operations.<p>
044 * 
045 * @since 6.5.6
046 */
047public class CmsOrgUnitManager {
048
049    /** The security manager. */
050    private final CmsSecurityManager m_securityManager;
051
052    /**
053     * Default constructor.<p>
054     * 
055     * @param securityManager the security manager
056     */
057    public CmsOrgUnitManager(CmsSecurityManager securityManager) {
058
059        m_securityManager = securityManager;
060    }
061
062    /**
063     * Adds a resource to the given organizational unit.<p>
064     * 
065     * @param cms the opencms context
066     * @param ouFqn the full qualified name of the organizational unit to add the resource to
067     * @param resourceName the name of the resource that is to be added to the organizational unit
068     * 
069     * @throws CmsException if something goes wrong
070     */
071    public void addResourceToOrgUnit(CmsObject cms, String ouFqn, String resourceName) throws CmsException {
072
073        CmsOrganizationalUnit orgUnit = readOrganizationalUnit(cms, ouFqn);
074        CmsResource resource = cms.readResource(resourceName);
075        m_securityManager.addResourceToOrgUnit(cms.getRequestContext(), orgUnit, resource);
076    }
077
078    /**
079     * Counts the users which fit the given search criteria.<p>
080     * 
081     * @param cms the current CMS context 
082     * @param params the user search parameters 
083     * 
084     * @return the total number of users which fit the given search parameters 
085     * 
086     * @throws CmsException if something goes wrong 
087     */
088    public long countUsers(CmsObject cms, CmsUserSearchParameters params) throws CmsException {
089
090        return m_securityManager.countUsers(cms.getRequestContext(), params);
091    }
092
093    /**
094     * Creates a new organizational unit.<p>
095     * 
096     * The parent structure must exist.<p>
097     * 
098     * @param cms the opencms context
099     * @param ouFqn the fully qualified name of the new organizational unit
100     * @param description the description of the new organizational unit
101     * @param flags the flags for the new organizational unit
102     * @param resourceName the first associated resource
103     *
104     * @return a <code>{@link CmsOrganizationalUnit}</code> object representing 
105     *          the newly created organizational unit
106     *
107     * @throws CmsException if operation was not successful
108     */
109    public CmsOrganizationalUnit createOrganizationalUnit(
110        CmsObject cms,
111        String ouFqn,
112        String description,
113        int flags,
114        String resourceName) throws CmsException {
115
116        CmsResource resource = null;
117        if (((flags & CmsOrganizationalUnit.FLAG_WEBUSERS) == 0) || (resourceName != null)) {
118            // only normal OUs have to have at least one resource
119            resource = cms.readResource(resourceName);
120        }
121        return m_securityManager.createOrganizationalUnit(cms.getRequestContext(), ouFqn, description, flags, resource);
122    }
123
124    /**
125     * Deletes an organizational unit.<p>
126     *
127     * Only organizational units that contain no sub units can be deleted.<p>
128     * 
129     * The organizational unit can not be delete if it is used in the request context, 
130     * or if the current user belongs to it.<p>
131     * 
132     * All users and groups in the given organizational unit will be deleted.<p>
133     * 
134     * @param cms the opencms context
135     * @param ouFqn the fully qualified name of the organizational unit to delete
136     * 
137     * @throws CmsException if operation was not successful
138     */
139    public void deleteOrganizationalUnit(CmsObject cms, String ouFqn) throws CmsException {
140
141        CmsOrganizationalUnit orgUnit = readOrganizationalUnit(cms, ouFqn);
142        m_securityManager.deleteOrganizationalUnit(cms.getRequestContext(), orgUnit);
143    }
144
145    /**
146     * Returns all accessible projects of the given organizational unit. 
147     * 
148     * That is all projects which are owned by the current user or which are 
149     * accessible for the group of the user.<p>
150     *
151     * @param cms the opencms context
152     * @param ouFqn the fully qualified name of the organizational unit to get projects for
153     * @param includeSubOus if all projects of sub-organizational units should be retrieved too
154     * 
155     * @return all <code>{@link org.opencms.file.CmsProject}</code> objects in the organizational unit
156     *
157     * @throws CmsException if operation was not successful
158     */
159    public List<CmsProject> getAllAccessibleProjects(CmsObject cms, String ouFqn, boolean includeSubOus)
160    throws CmsException {
161
162        CmsOrganizationalUnit orgUnit = readOrganizationalUnit(cms, ouFqn);
163        return (m_securityManager.getAllAccessibleProjects(cms.getRequestContext(), orgUnit, includeSubOus));
164    }
165
166    /**
167     * Returns all manageable projects of the given organizational unit.<p>
168     *
169     * That is all projects which are owned by the current user or which are manageable
170     * for the group of the user.<p>
171     *
172     * @param cms the opencms context
173     * @param ouFqn the fully qualified name of the organizational unit to get projects for
174     * @param includeSubOus if all projects of sub-organizational units should be retrieved too
175     * 
176     * @return all <code>{@link org.opencms.file.CmsProject}</code> objects in the organizational unit
177     *
178     * @throws CmsException if operation was not successful
179     */
180    public List<CmsProject> getAllManageableProjects(CmsObject cms, String ouFqn, boolean includeSubOus)
181    throws CmsException {
182
183        CmsOrganizationalUnit orgUnit = readOrganizationalUnit(cms, ouFqn);
184        return (m_securityManager.getAllManageableProjects(cms.getRequestContext(), orgUnit, includeSubOus));
185    }
186
187    /**
188     * Returns all groups of the given organizational unit.<p>
189     *
190     * @param cms the opencms context
191     * @param ouFqn the fully qualified name of the organizational unit to get all principals for
192     * @param includeSubOus if all groups of sub-organizational units should be retrieved too
193     * 
194     * @return all <code>{@link org.opencms.file.CmsGroup}</code> objects in the organizational unit
195     *
196     * @throws CmsException if operation was not successful
197     */
198    public List<CmsGroup> getGroups(CmsObject cms, String ouFqn, boolean includeSubOus) throws CmsException {
199
200        CmsOrganizationalUnit orgUnit = readOrganizationalUnit(cms, ouFqn);
201        return (m_securityManager.getGroups(cms.getRequestContext(), orgUnit, includeSubOus, false));
202    }
203
204    /**
205     * Returns all child organizational units of the given parent organizational unit including 
206     * hierarchical deeper organization units if needed.<p>
207     *
208     * @param cms the opencms context
209     * @param ouFqn the fully qualified name of the parent organizational unit
210     * @param includeChildren if hierarchical deeper organization units should also be returned
211     * 
212     * @return a list of <code>{@link CmsOrganizationalUnit}</code> objects
213     * 
214     * @throws CmsException if operation was not successful
215     */
216    public List<CmsOrganizationalUnit> getOrganizationalUnits(CmsObject cms, String ouFqn, boolean includeChildren)
217    throws CmsException {
218
219        CmsOrganizationalUnit parent = readOrganizationalUnit(cms, ouFqn);
220        return m_securityManager.getOrganizationalUnits(cms.getRequestContext(), parent, includeChildren);
221    }
222
223    /**
224     * Returns all resources of the given organizational unit.<p>
225     *
226     * @param cms the opencms context
227     * @param ouFqn the fully qualified name of the organizational unit to get all resources for
228     * 
229     * @return all <code>{@link CmsResource}</code> objects in the organizational unit
230     *
231     * @throws CmsException if operation was not successful
232     */
233    public List<CmsResource> getResourcesForOrganizationalUnit(CmsObject cms, String ouFqn) throws CmsException {
234
235        CmsOrganizationalUnit orgUnit = readOrganizationalUnit(cms, ouFqn);
236        return m_securityManager.getResourcesForOrganizationalUnit(cms.getRequestContext(), orgUnit);
237    }
238
239    /**
240     * Returns all users of the given organizational unit.<p>
241     *
242     * @param cms the opencms context
243     * @param ouFqn the fully qualified name of the organizational unit to get all principals for
244     * @param recursive if all users of sub-organizational units should be retrieved too
245     * 
246     * @return all <code>{@link org.opencms.file.CmsUser}</code> objects in the organizational unit
247     *
248     * @throws CmsException if operation was not successful
249     */
250    public List<CmsUser> getUsers(CmsObject cms, String ouFqn, boolean recursive) throws CmsException {
251
252        CmsOrganizationalUnit orgUnit = readOrganizationalUnit(cms, ouFqn);
253        return m_securityManager.getUsers(cms.getRequestContext(), orgUnit, recursive);
254    }
255
256    /**
257     * Returns all users of the given organizational unit, without their additional info.<p>
258     *
259     * @param cms the opencms context
260     * @param ouFqn the fully qualified name of the organizational unit to get all principals for
261     * @param recursive if all users of sub-organizational units should be retrieved too
262     * 
263     * @return all <code>{@link org.opencms.file.CmsUser}</code> objects in the organizational unit
264     *
265     * @throws CmsException if operation was not successful
266     */
267    public List<CmsUser> getUsersWithoutAdditionalInfo(CmsObject cms, String ouFqn, boolean recursive)
268    throws CmsException {
269
270        CmsOrganizationalUnit orgUnit = readOrganizationalUnit(cms, ouFqn);
271        return m_securityManager.getUsersWithoutAdditionalInfo(cms.getRequestContext(), orgUnit, recursive);
272    }
273
274    /**
275     * Reads an organizational Unit based on its fully qualified name.<p>
276     *
277     * @param cms the opencms context
278     * @param ouFqn the fully qualified name of the organizational Unit to be read
279     * 
280     * @return the organizational Unit with the provided fully qualified name
281     * 
282     * @throws CmsException if something goes wrong
283     */
284    public CmsOrganizationalUnit readOrganizationalUnit(CmsObject cms, String ouFqn) throws CmsException {
285
286        return m_securityManager.readOrganizationalUnit(cms.getRequestContext(), ouFqn);
287    }
288
289    /**
290     * Removes a resource from the given organizational unit.<p>
291     * 
292     * @param cms the opencms context
293     * @param ouFqn the fully qualified name of the organizational unit to remove the resource from
294     * @param resourceName the name of the resource that is to be removed from the organizational unit
295     * 
296     * @throws CmsException if something goes wrong
297     */
298    public void removeResourceFromOrgUnit(CmsObject cms, String ouFqn, String resourceName) throws CmsException {
299
300        CmsOrganizationalUnit orgUnit = readOrganizationalUnit(cms, ouFqn);
301        CmsResource resource = cms.readResource(resourceName, CmsResourceFilter.ALL);
302
303        m_securityManager.removeResourceFromOrgUnit(cms.getRequestContext(), orgUnit, resource);
304    }
305
306    /**
307     * Searches users which fit the given search parameters.<p>
308     * 
309     * @param cms the current CMS context 
310     * @param params the user search parameters 
311     * 
312     * @return the users which fit the given search criteria 
313     * 
314     * @throws CmsException if something goes wrong 
315     */
316    public List<CmsUser> searchUsers(CmsObject cms, CmsUserSearchParameters params) throws CmsException {
317
318        return m_securityManager.searchUsers(cms.getRequestContext(), params);
319    }
320
321    /**
322     * Moves an user to the given organizational unit.<p>
323     * 
324     * @param cms the opencms context
325     * @param ouFqn the full qualified name of the organizational unit to add the user to
326     * @param userName the name of the user that is to be added to the organizational unit
327     * 
328     * @throws CmsException if something goes wrong
329     */
330    public void setUsersOrganizationalUnit(CmsObject cms, String ouFqn, String userName) throws CmsException {
331
332        CmsOrganizationalUnit orgUnit = readOrganizationalUnit(cms, ouFqn);
333        CmsUser user = cms.readUser(userName);
334        m_securityManager.setUsersOrganizationalUnit(cms.getRequestContext(), orgUnit, user);
335    }
336
337    /**
338     * Writes an already existing organizational unit.<p>
339     *
340     * The organizational unit has to be a valid OpenCms organizational unit.<br>
341     * 
342     * The organizational unit will be completely overridden by the given data.<p>
343     *
344     * @param cms the opencms context
345     * @param organizationalUnit the organizational unit that should be written
346     * 
347     * @throws CmsException if operation was not successful
348     */
349    public void writeOrganizationalUnit(CmsObject cms, CmsOrganizationalUnit organizationalUnit) throws CmsException {
350
351        m_securityManager.writeOrganizationalUnit(cms.getRequestContext(), organizationalUnit);
352    }
353}