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