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.dialogs.permissions;
029
030import org.opencms.file.CmsObject;
031import org.opencms.main.CmsException;
032import org.opencms.main.CmsLog;
033import org.opencms.security.CmsAccessControlEntry;
034import org.opencms.security.CmsPrincipal;
035import org.opencms.security.CmsRole;
036import org.opencms.security.I_CmsPrincipal;
037import org.opencms.util.CmsUUID;
038
039import java.util.Set;
040
041import org.apache.commons.lang.builder.HashCodeBuilder;
042import org.apache.commons.logging.Log;
043
044/**
045 * Bean for permissions which have changed.<p>
046 */
047public class CmsPermissionBean {
048    /** Logger for this class. */ 
049    private static final Log LOG = CmsLog.getLog(CmsPermissionBean.class);
050
051    /**Principal Type. */
052    private String m_principalType;
053
054    /**Principal Name. */
055    private String m_principalName;
056
057    /**Allowed state. */
058    private int m_allowed;
059
060    /**Denied state. */
061    private int m_denied;
062
063    /**Flags. */
064    private int m_flags;
065
066    /**Permission string. */
067    private String m_permissionString;
068
069    /**Should the permission be deleted? */
070    private boolean m_delete;
071
072    /**
073     * Constructor for delete permission.<p>
074     *
075     * @param principalType principal type
076     * @param principalName principal name
077     */
078    public CmsPermissionBean(String principalType, String principalName) {
079
080        m_principalName = principalName;
081        m_principalType = principalType;
082        m_delete = true;
083    }
084
085    /**
086     * Constructor for new or edited permission.<p>
087     *
088     * @param principalType principal type
089     * @param principalName principal name
090     * @param allowed int
091     * @param denied int
092     * @param flags int
093     */
094    public CmsPermissionBean(String principalType, String principalName, int allowed, int denied, int flags) {
095
096        m_principalName = principalName;
097        m_principalType = principalType;
098        m_allowed = allowed;
099        m_denied = denied;
100        m_flags = flags;
101        m_delete = false;
102    }
103
104    /**
105     * Constructor with permission string.<p>
106     *
107     * @param principalType type
108     * @param principalName name
109     * @param permissionString permission string
110     */
111    public CmsPermissionBean(String principalType, String principalName, String permissionString) {
112
113        m_principalName = principalName;
114        m_principalType = principalType;
115        m_permissionString = permissionString;
116    }
117
118    /**
119     * Gets the bean for principal from list of beans.<p>
120     *
121     * @param beans to look principal up
122     * @param principalName name of principal to get bean of
123     * @return CmsPermissionBean
124     */
125    public static CmsPermissionBean getBeanForPrincipal(Set<CmsPermissionBean> beans, String principalName) {
126
127        for (CmsPermissionBean bean : beans) {
128            if (bean.getPrincipalName().equals(principalName)) {
129                return bean;
130            }
131        }
132        return null;
133    }
134
135    /**
136     * Get name of principal from ACE.<p>
137     *
138     * @param cms CmsObject
139     * @param entry ACE
140     * @return principal name
141     */
142    public static String getPrincipalNameFromACE(CmsObject cms, CmsAccessControlEntry entry) {
143
144        if (entry.isAllOthers()) {
145            return CmsAccessControlEntry.PRINCIPAL_ALL_OTHERS_NAME;
146        }
147        if (entry.isOverwriteAll()) {
148            return CmsAccessControlEntry.PRINCIPAL_OVERWRITE_ALL_NAME;
149        }
150        CmsRole role = CmsRole.valueOfId(entry.getPrincipal());
151        if (role != null) {
152            return role.getRoleName();
153        } else {
154            try {
155                return CmsPrincipal.readPrincipal(cms, entry.getPrincipal()).getName();
156            } catch (CmsException e) {
157                //
158            }
159        }
160        return "";
161    }
162
163    /**
164     * @see java.lang.Object#equals(java.lang.Object)
165     */
166    @Override
167    public boolean equals(Object o) {
168
169        if (!((o instanceof CmsPermissionBean) || (o instanceof String))) {
170            return false;
171        }
172        if (o instanceof String) {
173            return m_principalName.equals(o);
174        }
175        CmsPermissionBean bean = (CmsPermissionBean)o;
176        return bean.getPrincipalName().equals(m_principalName) && bean.getPrincipalType().equals(m_principalType);
177    }
178
179    /**
180     * Gets the allowed flag.<p>
181     *
182     * @return int
183     */
184    public int getAllowed() {
185
186        return m_allowed;
187    }
188
189    /**
190     * Gets the denied flag.<p>
191     *
192     * @return int
193     */
194    public int getDenied() {
195
196        return m_denied;
197    }
198
199    /**
200     * Gets the flag.<p>
201     *
202     * @return int
203     */
204    public int getFlags() {
205
206        return m_flags;
207    }
208
209    /**
210     * Returns the permission string.<p>
211     *
212     * @return the permission string or null if not set
213     */
214    public String getPermissionString() {
215
216        return m_permissionString;
217    }
218
219    /**
220     * Gets the principal name.<p>
221     *
222     * @return the name of the principal
223     */
224    public String getPrincipalName() {
225
226        return m_principalName;
227    }
228
229    /**
230     * Gets the type of the principal.<p>
231     *
232     * @return String
233     */
234    public String getPrincipalType() {
235
236        return m_principalType;
237    }
238
239    /**
240     * @see java.lang.Object#hashCode()
241     */
242    @Override
243    public int hashCode() {
244
245        return new HashCodeBuilder(17, 31).append(m_principalName).toHashCode();
246    }
247
248    /**
249     * Returns whether the permission should be removed.<p>
250     *
251     * @return true-> permission will be removed
252     */
253    public boolean isDeleted() {
254
255        return m_delete;
256    }
257
258    /**
259     * Checks if principal is real.<p>
260     *
261     * @return true if principal is real
262     */
263    public boolean isRealPrinciple() {
264
265        return !(CmsAccessControlEntry.PRINCIPAL_ALL_OTHERS_NAME.equals(m_principalName)
266            | CmsAccessControlEntry.PRINCIPAL_OVERWRITE_ALL_NAME.equals(m_principalName));
267    }
268
269    /**
270     * Sets the flag of the ACE.<p>
271     *
272     * @param flags to be set
273     */
274    public void setFlags(int flags) {
275
276        m_flags |= flags;
277
278    }
279
280    /**
281     * Creates ACE from bean.<p>
282     *
283     * @param cms CmsObject
284     * @param resID id of resource
285     * @return CmsAccessControlEntry
286     */
287    public CmsAccessControlEntry toAccessControlEntry(CmsObject cms, CmsUUID resID) {
288
289        CmsUUID id = null;
290        if (isRealPrinciple()) {
291            if (CmsRole.PRINCIPAL_ROLE.equals(m_principalType)) {
292                CmsRole role = CmsRole.valueOfRoleName(m_principalName);
293                if (role != null) {
294                    id = role.getId();
295                }
296            } else {
297                try {
298                    if (I_CmsPrincipal.PRINCIPAL_GROUP.equals(m_principalType)) {
299                        id = cms.readGroup(m_principalName).getId();
300                    } else if (I_CmsPrincipal.PRINCIPAL_USER.equals(m_principalType)) {
301                        id = cms.readUser(m_principalName).getId();
302                    }
303                } catch (CmsException e) {
304                    LOG.error(e.getLocalizedMessage(), e);
305                }
306            }
307        } else {
308            if (m_principalName.equals(CmsAccessControlEntry.PRINCIPAL_ALL_OTHERS_NAME)) {
309                id = CmsAccessControlEntry.PRINCIPAL_ALL_OTHERS_ID;
310            }
311            if (m_principalName.equals(CmsAccessControlEntry.PRINCIPAL_OVERWRITE_ALL_NAME)) {
312                id = CmsAccessControlEntry.PRINCIPAL_OVERWRITE_ALL_ID;
313            }
314        }
315
316        if (id == null) {
317            return null;
318        }
319
320        if (m_permissionString == null) {
321            return new CmsAccessControlEntry(resID, id, m_allowed, m_denied, m_flags);
322        }
323        CmsAccessControlEntry entry = new CmsAccessControlEntry(resID, id, m_permissionString);
324        return entry;
325    }
326
327}