001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/metaobj/tags/sakai_2-4-1/metaobj-util/tool-lib/src/java/org/sakaiproject/metaobj/security/model/SimpleToolPermissionManager.java $
003: * $Id: SimpleToolPermissionManager.java 14230 2006-09-05 18:02:51Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.metaobj.security.model;
021:
022: import java.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.sakaiproject.metaobj.security.mgt.PermissionManager;
030: import org.sakaiproject.metaobj.security.mgt.ToolPermissionManager;
031: import org.sakaiproject.metaobj.shared.mgt.AgentManager;
032: import org.sakaiproject.metaobj.shared.mgt.IdManager;
033: import org.sakaiproject.metaobj.shared.model.Agent;
034: import org.sakaiproject.metaobj.shared.model.Id;
035: import org.sakaiproject.metaobj.worksite.intf.ToolEventListener;
036: import org.sakaiproject.metaobj.worksite.model.SiteTool;
037: import org.sakaiproject.site.api.ToolConfiguration;
038:
039: public class SimpleToolPermissionManager implements ToolEventListener,
040: ToolPermissionManager {
041: protected final transient Log logger = LogFactory
042: .getLog(getClass());
043:
044: private Map defaultPermissions;
045: private AgentManager agentManager;
046: private PermissionManager permissionManager;
047: private String permissionEditName;
048: private IdManager idManager;
049: private List functions = new ArrayList();
050:
051: /**
052: * sets up the default perms for a tool. Use's the tool id as the qualifier.
053: * Assumes that if no perms exist for the tool, the perms should be set to the defaults.
054: *
055: * @param toolConfig
056: */
057: public void toolSiteChanged(ToolConfiguration toolConfig) {
058: Id toolId = getIdManager().getId(toolConfig.getId());
059: PermissionsEdit edit = new PermissionsEdit();
060: edit.setQualifier(toolId);
061: edit.setName(getPermissionEditName());
062: edit.setSiteId(toolConfig.getContainingPage()
063: .getContainingSite().getId());
064: getPermissionManager().fillPermissions(edit);
065: if (edit.getPermissions() == null
066: || edit.getPermissions().size() == 0) {
067: createDefaultPermissions(edit.getSiteId(), toolId);
068: }
069: }
070:
071: public void toolRemoved(SiteTool siteTool) {
072: // todo remove all authz
073: }
074:
075: protected void createDefaultPermissions(String worksiteId,
076: Id qualifier) {
077: PermissionsEdit edit = setupPermissions(worksiteId, qualifier);
078: edit.setName(getPermissionEditName());
079: getPermissionManager().updatePermissions(edit);
080: }
081:
082: protected PermissionsEdit setupPermissions(String worksiteId,
083: Id qualifier) {
084:
085: List permissions = new ArrayList();
086: PermissionsEdit edit = new PermissionsEdit();
087: edit.setQualifier(qualifier);
088: edit.setSiteId(worksiteId);
089: for (Iterator i = getDefaultPermissions().entrySet().iterator(); i
090: .hasNext();) {
091: Map.Entry entry = (Map.Entry) i.next();
092: String agentName = (String) entry.getKey();
093: List functions = (List) entry.getValue();
094: processFunctions(permissions, agentName, functions,
095: worksiteId);
096: }
097:
098: edit.setPermissions(permissions);
099: return edit;
100: }
101:
102: protected void processFunctions(List permissions, String roleName,
103: List functions, String worksiteId) {
104: Agent agent = getAgentManager().getWorksiteRole(roleName,
105: worksiteId);
106:
107: for (Iterator i = functions.iterator(); i.hasNext();) {
108: Permission permission = new Permission();
109: permission.setAgent(agent);
110: permission.setFunction((String) i.next());
111: permissions.add(permission);
112: }
113: }
114:
115: public Map getDefaultPermissions() {
116: return defaultPermissions;
117: }
118:
119: public void setDefaultPermissions(Map defaultPermissions) {
120: this .defaultPermissions = defaultPermissions;
121: }
122:
123: public AgentManager getAgentManager() {
124: return agentManager;
125: }
126:
127: public void setAgentManager(AgentManager agentManager) {
128: this .agentManager = agentManager;
129: }
130:
131: public PermissionManager getPermissionManager() {
132: return permissionManager;
133: }
134:
135: public void setPermissionManager(PermissionManager permissionManager) {
136: this .permissionManager = permissionManager;
137: }
138:
139: public String getPermissionEditName() {
140: return permissionEditName;
141: }
142:
143: public void setPermissionEditName(String permissionEditName) {
144: this .permissionEditName = permissionEditName;
145: }
146:
147: public IdManager getIdManager() {
148: return idManager;
149: }
150:
151: public void setIdManager(IdManager idManager) {
152: this .idManager = idManager;
153: }
154:
155: public List getFunctions(PermissionsEdit edit) {
156: return functions;
157: }
158:
159: public List getReadOnlyQualifiers(PermissionsEdit edit) {
160: return new ArrayList();
161: }
162:
163: public void duplicatePermissions(ToolConfiguration fromTool,
164: ToolConfiguration toTool) {
165: getPermissionManager().duplicatePermissions(
166: getIdManager().getId(fromTool.getId()),
167: getIdManager().getId(toTool.getId()),
168: toTool.getContainingPage().getContainingSite());
169: }
170:
171: public void setFunctions(List functions) {
172: this.functions = functions;
173: }
174:
175: }
|