001: /**********************************************************************************
002: * $URL: $
003: * $Id: $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2006,2007 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.poll.tool.params;
021:
022: import java.util.Map;
023: import java.util.Iterator;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027:
028: import org.sakaiproject.authz.api.AuthzGroup;
029: import org.sakaiproject.authz.cover.AuthzGroupService;
030: import org.sakaiproject.authz.api.Role;
031: import org.sakaiproject.authz.api.AuthzPermissionException;
032: import org.sakaiproject.authz.api.GroupNotDefinedException;
033: import org.sakaiproject.tool.cover.ToolManager;
034:
035: import org.sakaiproject.poll.logic.PollListManager;
036:
037: public class PermissionAction {
038:
039: private static Log m_log = LogFactory
040: .getLog(PermissionAction.class);
041: public Map perms = null;
042: public String submissionStatus;
043:
044: public void setRoleperms(Map perms) {
045: this .perms = perms;
046: }
047:
048: public String setPermissions() {
049:
050: if (submissionStatus.equals("cancel"))
051: return "cancel";
052:
053: m_log.info("Seting permissions");
054: if (perms == null)
055: m_log.error("My perms Map is null");
056: else {
057: AuthzGroup authz = null;
058: try {
059: authz = AuthzGroupService.getAuthzGroup("/site/"
060: + ToolManager.getCurrentPlacement()
061: .getContext());
062: } catch (GroupNotDefinedException e) {
063: // TODO Auto-generated catch block
064: e.printStackTrace();
065: return "error";
066: }
067: for (Iterator i = perms.keySet().iterator(); i.hasNext();) {
068: String key = (String) i.next();
069: Role role = authz.getRole(key);
070: try {
071: PollRolePerms rp = (PollRolePerms) perms.get(key);
072: if (rp.add != null)
073: setFunc(role, PollListManager.PERMISSION_ADD,
074: rp.add);
075: if (rp.deleteAny != null)
076: setFunc(role,
077: PollListManager.PERMISSION_DELETE_ANY,
078: rp.deleteAny);
079: if (rp.deleteOwn != null)
080: setFunc(role,
081: PollListManager.PERMISSION_DELETE_OWN,
082: rp.deleteOwn);
083: if (rp.editAny != null)
084: setFunc(role,
085: PollListManager.PERMISSION_EDIT_ANY,
086: rp.editAny);
087: if (rp.editOwn != null)
088: setFunc(role,
089: PollListManager.PERMISSION_EDIT_OWN,
090: rp.editOwn);
091: if (rp.vote != null)
092: setFunc(role, PollListManager.PERMISSION_VOTE,
093: rp.vote);
094:
095: m_log.info(" Key: " + key + " Vote: " + rp.vote
096: + " New: " + rp.add);
097: } catch (Exception e) {
098: m_log.error(" ClassCast Ex PermKey: " + key);
099: e.printStackTrace();
100: return "error";
101: }
102: }
103: try {
104: AuthzGroupService.save(authz);
105: } catch (GroupNotDefinedException e) {
106: // TODO Auto-generated catch block
107: e.printStackTrace();
108: return "error";
109: } catch (AuthzPermissionException e) {
110: // TODO Auto-generated catch block
111: e.printStackTrace();
112: return "error";
113: }
114:
115: }
116: return "Success";
117: }
118:
119: private void setFunc(Role role, String function, Boolean allow) {
120:
121: //m_log.debug("Setting " + function + " to " + allow.toString() + " for " + rolename + " in /site/" + ToolManager.getCurrentPlacement().getContext());
122: if (allow.booleanValue())
123: role.allowFunction(function);
124: else
125: role.disallowFunction(function);
126:
127: }
128:
129: public String cancel() {
130: return "cancel";
131: }
132:
133: }
|