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