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, 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.ui.apps.user;
029
030import org.opencms.file.CmsObject;
031import org.opencms.main.CmsException;
032import org.opencms.main.OpenCms;
033import org.opencms.security.CmsRole;
034
035import java.util.ArrayList;
036import java.util.HashMap;
037import java.util.List;
038import java.util.Map;
039
040/**
041 * Ou Handler.
042 */
043public class CmsOUHandler {
044
045    /**List of all managable OUs for current user. */
046    private List<String> m_managableOU;
047
048    /**CmsObject. */
049    private CmsObject m_cms;
050
051    /**Is Root account manager (or higher). */
052    private boolean m_isRootAccountManager;
053
054    /**Maps ou names to boolean if they are parents of managable ou. */
055    private Map<String, Boolean> m_isParentOfOU = new HashMap<String, Boolean>();
056
057    /**
058     * Public constructor.<p>
059     *
060     * @param cms CmsObject
061     */
062    public CmsOUHandler(CmsObject cms) {
063
064        m_cms = cms;
065        m_isRootAccountManager = OpenCms.getRoleManager().hasRole(m_cms, CmsRole.ACCOUNT_MANAGER.forOrgUnit(""));
066        if (!m_isRootAccountManager) {
067            m_managableOU = getManagableOUs(cms);
068        }
069    }
070
071    /**
072     * Gets List of managable OU names for the current user.<p>
073     *
074     * @param cms CmsObject
075     * @return List of String
076     */
077    public static List<String> getManagableOUs(CmsObject cms) {
078
079        List<String> ous = new ArrayList<String>();
080        try {
081            for (CmsRole role : OpenCms.getRoleManager().getRolesOfUser(
082                cms,
083                cms.getRequestContext().getCurrentUser().getName(),
084                "",
085                true,
086                false,
087                true)) {
088                if (role.getRoleName().equals(CmsRole.ACCOUNT_MANAGER.getRoleName())) {
089                    if (role.getOuFqn().equals("")) {
090                        ous.add(0, role.getOuFqn());
091                    } else {
092                        ous.add(role.getOuFqn());
093                    }
094                }
095            }
096        } catch (CmsException e) {
097            //
098        }
099        return ous;
100    }
101
102    /**
103     * Get base ou for given manageable ous.<p>
104     *
105     * @return Base ou (may be outside of given ou)
106     */
107    public String getBaseOU() {
108
109        if (m_isRootAccountManager) {
110            return "";
111        }
112
113        if (m_managableOU.contains("")) {
114            return "";
115        }
116        String base = m_managableOU.get(0);
117        for (String ou : m_managableOU) {
118            while (!ou.startsWith(base)) {
119                base = base.substring(0, base.length() - 1);
120                if (base.lastIndexOf("/") > -1) {
121                    base = base.substring(0, base.lastIndexOf("/"));
122                } else {
123                    return "";
124                }
125            }
126        }
127        return base;
128    }
129
130    /**
131     * Checks if given ou is manageable.<p>
132     *
133     * @param ou to check
134     * @return true is it is manageable
135     */
136    public boolean isOUManagable(String ou) {
137
138        if (m_isRootAccountManager) {
139            return true;
140        }
141
142        if (OpenCms.getRoleManager().hasRole(m_cms, CmsRole.ACCOUNT_MANAGER.forOrgUnit(""))) {
143            return true;
144        }
145
146        return m_managableOU.contains(assureOUString(ou));
147    }
148
149    /**
150     * Checks if given ou is parent of a managable ou.<p>
151     *
152     * @param name to check
153     * @return boolean
154     */
155    public boolean isParentOfManagableOU(String name) {
156
157        if (m_isRootAccountManager) {
158            return true;
159        }
160
161        if (m_isParentOfOU.containsKey(name)) {
162            return m_isParentOfOU.get(name).booleanValue();
163        }
164        for (String ou : m_managableOU) {
165            if (ou.startsWith(name)) {
166                m_isParentOfOU.put(name, Boolean.valueOf(true));
167                return true;
168            }
169        }
170        m_isParentOfOU.put(name, Boolean.valueOf(false));
171        return false;
172    }
173
174    /**
175     *
176     * Returns valid ou name.<p>
177     *
178     * @param ou name
179     * @return valid ou name with "/" at the end
180     */
181    private String assureOUString(String ou) {
182
183        if (ou.equals("") | ou.endsWith("/")) {
184            return ou;
185        }
186        return ou + "/";
187    }
188
189}