001: /**********************************************************************************
002: * $URL:https://source.sakaiproject.org/svn/osp/trunk/common/api-impl/src/java/org/theospi/portfolio/security/model/SakaiDefaultPermsManager.java $
003: * $Id:SakaiDefaultPermsManager.java 9134 2006-05-08 20:28:42Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 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.theospi.portfolio.security.model;
021:
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Map;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.sakaiproject.authz.api.AuthzGroup;
029: import org.sakaiproject.authz.api.AuthzGroupService;
030: import org.sakaiproject.authz.api.AuthzPermissionException;
031: import org.sakaiproject.authz.api.FunctionManager;
032: import org.sakaiproject.authz.api.GroupNotDefinedException;
033: import org.sakaiproject.authz.api.Role;
034: import org.sakaiproject.tool.api.Session;
035: import org.sakaiproject.tool.cover.SessionManager;
036: import org.theospi.portfolio.security.DefaultRealmManager;
037:
038: /**
039: * Created by IntelliJ IDEA.
040: * User: John Ellis
041: * Date: Feb 8, 2006
042: * Time: 4:04:47 PM
043: * To change this template use File | Settings | File Templates.
044: */
045: public class SakaiDefaultPermsManager {
046:
047: private Map defaultPermissions;
048: private List functions;
049: private FunctionManager functionManager;
050: private AuthzGroupService authzGroupService;
051: private String prefix;
052: private List realmManagers;
053: private boolean autoDdl = true;
054:
055: protected final transient Log logger = LogFactory
056: .getLog(getClass());
057:
058: public void init() {
059: logger.info("init()");
060: // need to register functions... set defaults on the ones that are not there
061: Session sakaiSession = SessionManager.getCurrentSession();
062: String userId = sakaiSession.getUserId();
063:
064: try {
065: sakaiSession.setUserId("admin");
066: sakaiSession.setUserEid("admin");
067:
068: if (getPrefix() != null) {
069: List currentFunctions = getFunctionManager()
070: .getRegisteredFunctions(getPrefix());
071:
072: for (Iterator i = getFunctions().iterator(); i
073: .hasNext();) {
074: String function = (String) i.next();
075: if (currentFunctions.contains(function)) {
076: i.remove();
077: } else {
078: getFunctionManager().registerFunction(function);
079: }
080: }
081: }
082:
083: if (isAutoDdl()) {
084: // set the defaults for anything in functions
085: for (Iterator i = getDefaultPermissions().entrySet()
086: .iterator(); i.hasNext();) {
087: Map.Entry entry = (Map.Entry) i.next();
088: processRealm((String) entry.getKey(), (Map) entry
089: .getValue());
090: }
091: }
092: } finally {
093: sakaiSession.setUserEid(userId);
094: sakaiSession.setUserId(userId);
095: }
096:
097: }
098:
099: protected void processRealm(String realm, Map defaultPerms) {
100: try {
101: AuthzGroup group = getAuthzGroupService().getAuthzGroup(
102: realm);
103: boolean isNew = isRealmNew(group);
104: for (Iterator i = defaultPerms.entrySet().iterator(); i
105: .hasNext();) {
106: Map.Entry entry = (Map.Entry) i.next();
107: Role role = group.getRole((String) entry.getKey());
108: setupRole(role, (List) entry.getValue(), isNew);
109: }
110: getAuthzGroupService().save(group);
111: } catch (GroupNotDefinedException e) {
112: throw new RuntimeException(e);
113: } catch (AuthzPermissionException e) {
114: throw new RuntimeException(e);
115: }
116: }
117:
118: protected boolean isRealmNew(AuthzGroup group) {
119: for (Iterator i = getRealmManagers().iterator(); i.hasNext();) {
120: DefaultRealmManager manager = (DefaultRealmManager) i
121: .next();
122: if (manager.getNewRealmName().equals(group.getId())) {
123: return manager.isNewlyCreated();
124: }
125: }
126:
127: return false;
128: }
129:
130: protected void setupRole(Role role, List functions, boolean isNew) {
131: for (Iterator i = functions.iterator(); i.hasNext();) {
132: String func = (String) i.next();
133: if (isNew || getFunctions().contains(func)) {
134: role.allowFunction(func);
135: }
136: }
137: }
138:
139: public Map getDefaultPermissions() {
140: return defaultPermissions;
141: }
142:
143: public void setDefaultPermissions(Map defaultPermissions) {
144: this .defaultPermissions = defaultPermissions;
145: }
146:
147: public List getFunctions() {
148: return functions;
149: }
150:
151: public void setFunctions(List functions) {
152: this .functions = functions;
153: }
154:
155: public FunctionManager getFunctionManager() {
156: return functionManager;
157: }
158:
159: public void setFunctionManager(FunctionManager functionManager) {
160: this .functionManager = functionManager;
161: }
162:
163: public String getPrefix() {
164: return prefix;
165: }
166:
167: public void setPrefix(String prefix) {
168: this .prefix = prefix;
169: }
170:
171: public AuthzGroupService getAuthzGroupService() {
172: return authzGroupService;
173: }
174:
175: public void setAuthzGroupService(AuthzGroupService authzGroupService) {
176: this .authzGroupService = authzGroupService;
177: }
178:
179: public List getRealmManagers() {
180: return realmManagers;
181: }
182:
183: public void setRealmManagers(List realmManagers) {
184: this .realmManagers = realmManagers;
185: }
186:
187: public boolean isAutoDdl() {
188: return autoDdl;
189: }
190:
191: public void setAutoDdl(boolean autoDdl) {
192: this.autoDdl = autoDdl;
193: }
194: }
|