/* * Copyright (C) 2001-2004 Red Hat Inc. All Rights Reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ package com.arsdigita.cms; import com.arsdigita.cms.dispatcher.SimpleCache; import com.arsdigita.cms.publishToFile.LocalRequestPassword; import com.arsdigita.cms.util.SecurityConstants; import com.arsdigita.cms.workflow.CMSEngine; import com.arsdigita.cms.workflow.CMSTask; import com.arsdigita.cms.workflow.CMSTaskType; import com.arsdigita.kernel.Kernel; import com.arsdigita.kernel.Party; import com.arsdigita.kernel.User; import com.arsdigita.kernel.permissions.PermissionDescriptor; import com.arsdigita.kernel.permissions.PermissionService; import com.arsdigita.kernel.permissions.PrivilegeDescriptor; import com.arsdigita.kernel.security.UserContext; import com.arsdigita.toolbox.Security; import com.arsdigita.ui.login.LoginHelper; import com.arsdigita.util.Assert; import com.arsdigita.workflow.simple.Engine; import com.arsdigita.workflow.simple.Workflow; import java.io.IOException; import java.util.Iterator; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; /** *
Security class used for checking and granting privileges in CMS.
* * @author Michael Pih * @version $Id: SecurityManager.java 2280 2012-03-10 23:55:04Z pboy $ */ public class SecurityManager implements Security, SecurityConstants { private static final Logger s_log = Logger.getLogger (SecurityManager.class); public static final PrivilegeDescriptor CMS_PREVIEW_ITEM_DESCRIPTOR = new PrivilegeDescriptor(CMS_PREVIEW_ITEM); private ContentSection m_section; // MP: Use this. private SimpleCache m_cache; public SecurityManager(ContentSection section) { m_section = section; m_cache = new SimpleCache(); } public final boolean canAccess(final String action) { return canAccess(Kernel.getContext().getParty(), action); } /** * Determine whether a party has access to a particular action. * * @param party The party * @param action The action * @return true if the party has access, false otherwise * @pre (action != null) */ public boolean canAccess(final Party party, final String action) { if (s_log.isDebugEnabled()) { s_log.debug("Access check: party " + party + ", action " + action); } if (action.equals(WORKFLOW_ADMIN)) { return canAdministerWorkflow(party); } else if (action.equals(LIFECYCLE_ADMIN)) { return canAdministerLifecycles(party); } else if (action.equals(STAFF_ADMIN)) { return canAdministerRoles(party); } else if (action.equals(CONTENT_TYPE_ADMIN)) { return canAdministerContentTypes(party); } else if (action.equals(CATEGORY_ADMIN)) { return canAdministerCategories(party); } else if (action.equals(PUBLISH)) { return canPublishItems(party); } else if (action.equals(NEW_ITEM)) { return canCreateItems(party); } else if (action.equals(ADMIN_PAGES)) { return canViewAdminPages(party); } else if (action.equals(PUBLIC_PAGES)) { return canViewPublicPages(party); } else if (action.equals(PREVIEW_PAGES)) { return canViewPreviewPages(party); } else if (action.equals(DELETE_IMAGES)) { return canDeleteImages(party); } else if (action.equals(APPLY_ALTERNATE_WORKFLOWS)) { return canApplyAlternateWorkflows(party); } else { throw new IllegalArgumentException ("Unknown action for access check: " + action); } } /** * Determine whether the current user has access to a particular action. * * @param request The HTTP request * @param action The action * @return true if the logged-in user has access, false otherwise */ public boolean canAccess(final HttpServletRequest request, final String action) { final Party party = Kernel.getContext().getParty(); boolean canAccess = canAccess(party, action); if (!canAccess) { canAccess = LocalRequestPassword.validLocalRequest(request); } return canAccess; } public boolean canAccess(final User user, final String action, final ContentItem item) { if (s_log.isDebugEnabled()) { s_log.debug("Access check: user " + user + ", object " + item + ", action " + action); } if (action.equals(EDIT_ITEM)) { return canEditItem(user, item); } else if (action.equals(APPLY_WORKFLOW)) { return canApplyWorkflow(user, item); } else if (action.equals(DELETE_ITEM)) { return canDeleteItem(user, item); } else if (action.equals(SCHEDULE_PUBLICATION)) { return canSchedulePublication(user, item); } else if (action.equals(PUBLISH)) { return canPublishItems(user, item); } else if (action.equals(PUBLIC_PAGES)) { return canViewPublicPages(user, item); } else if (action.equals(PREVIEW_PAGES)) { return canViewPreviewPages(user, item); } else if (action.equals(NEW_ITEM)) { // this should really only be called if the ContentItem is // a folder... return canCreateItems(user, item); } else if (action.equals(APPLY_ALTERNATE_WORKFLOWS)) { return canApplyAlternateWorkflows(user, item); } else if (action.equals(STAFF_ADMIN)) { // this should really only be called if the ContentItem is // a folder... return canAdministerRoles(user, item); // section levels -- call non-item-specific version } else if (action.equals(WORKFLOW_ADMIN)) { return canAdministerWorkflow(user); } else if (action.equals(LIFECYCLE_ADMIN)) { return canAdministerLifecycles(user); } else if (action.equals(STAFF_ADMIN)) { return canAdministerRoles(user); } else if (action.equals(CONTENT_TYPE_ADMIN)) { return canAdministerContentTypes(user); } else if (action.equals(ADMIN_PAGES)) { return canViewAdminPages(user); } else { throw new IllegalArgumentException ("Unknown action for access check: " + action); } } public boolean canAccess(HttpServletRequest request, String action, ContentItem item) { User user = (User)Kernel.getContext().getParty(); boolean canAccess = canAccess(user, action, item); if (!canAccess) { canAccess = LocalRequestPassword.validLocalRequest(request); } return canAccess; } /** * Checking privileges. **/ protected boolean canAdministerLifecycles(Party party) { return (hasPermission(party, CMS_LIFECYCLE_ADMIN)); } protected boolean canAdministerWorkflow(Party party) { return (hasPermission(party, CMS_WORKFLOW_ADMIN)); } protected boolean canAdministerRoles(Party party) { return (hasPermission(party, CMS_STAFF_ADMIN)); } protected boolean canAdministerRoles(Party party, ContentItem item) { return (hasPermission(party, CMS_STAFF_ADMIN, item)); } protected boolean canAdministerContentTypes(Party party) { return (hasPermission(party, CMS_CONTENT_TYPE_ADMIN)); } protected boolean canAdministerCategories(Party party) { return (hasPermission(party, CMS_CATEGORY_ADMIN)); } protected boolean canPublishItems(Party party) { return (hasPermission(party, CMS_PUBLISH)); } protected boolean canPublishItems(Party party, ContentItem item) { return (hasPermission(party, CMS_PUBLISH, item)); } protected boolean canCreateItems(Party party) { return (hasPermission(party, CMS_NEW_ITEM)); } protected boolean canCreateItems(User user, ContentItem item) { return (hasPermission(user, CMS_NEW_ITEM, item)); } protected boolean canApplyAlternateWorkflows(Party party) { return (hasPermission(party, CMS_APPLY_ALTERNATE_WORKFLOWS)); } protected boolean canApplyAlternateWorkflows(User user, ContentItem item) { return (hasPermission(user, CMS_APPLY_ALTERNATE_WORKFLOWS, item)); } /** * Returns true if the specified user has the CMS_READ_ITEM permission on the * current content section. False otherwise. * * @pre m_section != null **/ protected boolean canViewPublicPages(Party party) { return (hasPermission(party, CMS_READ_ITEM)); // return true; } /** * Returns true if the specified user has the CMS_READ_ITEM permission on the * current content item. False otherwise. * * For now, just call the section-specific version. Must modify when we * implement folder-level permissions. * * @pre m_section != null **/ protected boolean canViewPublicPages(User user, ContentItem item) { return (hasPermission(user, CMS_READ_ITEM, item)); } /** * Returns true if the specified user has the CMS_PREVIEW_ITEM permission on the * current content section. False otherwise. * * @pre m_section != null **/ protected boolean canViewPreviewPages(Party party) { return (hasPermission(party, CMS_PREVIEW_ITEM) || hasPermission(party, CMS_EDIT_ITEM)); // return true; } /** * Returns true if the specified user has the CMS_PREVIEW_ITEM permission on the * current content item. False otherwise. * * For now, just call the section-specific version. Must modify when we * implement folder-level permissions. * * @pre m_section != null **/ protected boolean canViewPreviewPages(User user, ContentItem item) { return (hasPermission(user, CMS_PREVIEW_ITEM, item) || hasPermission(user, CMS_EDIT_ITEM, item)); } /** * Returns true if the specified party can access authoring UI in the * current content section. False otherwise. * * @pre m_section != null **/ protected boolean canViewAdminPages(Party party) { return (hasPermission(party, CMS_PREVIEW_ITEM)); } /** * Returns true if the specified user has the CMS_ITEM_ADMIN permission on the * current content item. False otherwise. * * * @pre m_section != null **/ protected boolean canDeleteImages(Party party) { return (hasPermission(party, CMS_ITEM_ADMIN)); } /** *Check if:
*Check if:
*Check if:
*