001: package org.theospi.portfolio.security.model;
002:
003: import java.util.Iterator;
004: import java.util.List;
005:
006: import org.sakaiproject.authz.api.AuthzGroup;
007: import org.sakaiproject.authz.api.AuthzGroupService;
008: import org.sakaiproject.authz.api.AuthzPermissionException;
009: import org.sakaiproject.authz.api.GroupAlreadyDefinedException;
010: import org.sakaiproject.authz.api.GroupIdInvalidException;
011: import org.sakaiproject.authz.api.GroupNotDefinedException;
012: import org.sakaiproject.authz.api.Role;
013: import org.sakaiproject.authz.api.RoleAlreadyDefinedException;
014: import org.sakaiproject.tool.api.Session;
015: import org.sakaiproject.tool.cover.SessionManager;
016: import org.theospi.portfolio.security.DefaultRealmManager;
017: import org.apache.commons.logging.Log;
018: import org.apache.commons.logging.LogFactory;
019:
020: /**
021: * Created by IntelliJ IDEA.
022: * User: John Ellis
023: * Date: Feb 27, 2006
024: * Time: 2:47:41 PM
025: * To change this template use File | Settings | File Templates.
026: */
027: public class DefaultRealmManagerImpl implements DefaultRealmManager {
028:
029: protected final transient Log logger = LogFactory
030: .getLog(getClass());
031:
032: private AuthzGroupService authzGroupService;
033: private String newRealmName;
034: private List roles;
035: private boolean newlyCreated;
036: private boolean recreate = false;
037: private boolean autoDdl = true;
038:
039: public void init() {
040: logger.info("init()");
041:
042: if (isAutoDdl()) {
043: Session sakaiSession = SessionManager.getCurrentSession();
044: String userId = sakaiSession.getUserId();
045: try {
046: sakaiSession.setUserId("admin");
047: sakaiSession.setUserEid("admin");
048: try {
049: AuthzGroup group = getAuthzGroupService()
050: .getAuthzGroup(newRealmName);
051: if (group != null) {
052: if (recreate) {
053: getAuthzGroupService().removeAuthzGroup(
054: group);
055: } else {
056: newlyCreated = false;
057: return;
058: }
059: }
060: } catch (GroupNotDefinedException e) {
061: // no worries... must not be created yet.
062: } catch (AuthzPermissionException e) {
063: logger.error("Failed to recreate realm.", e);
064: newlyCreated = false;
065: return;
066: }
067:
068: newlyCreated = true;
069:
070: try {
071: AuthzGroup newRealm = getAuthzGroupService()
072: .addAuthzGroup(newRealmName);
073: addRoles(newRealm);
074: getAuthzGroupService().save(newRealm);
075: } catch (GroupNotDefinedException e) {
076: throw new RuntimeException(e);
077: } catch (AuthzPermissionException e) {
078: throw new RuntimeException(e);
079: } catch (GroupAlreadyDefinedException e) {
080: throw new RuntimeException(e);
081: } catch (GroupIdInvalidException e) {
082: throw new RuntimeException(e);
083: } catch (RoleAlreadyDefinedException e) {
084: throw new RuntimeException(e);
085: }
086: } finally {
087: sakaiSession.setUserId(userId);
088: sakaiSession.setUserEid(userId);
089: }
090: }
091: }
092:
093: protected void addRoles(AuthzGroup newRealm)
094: throws RoleAlreadyDefinedException {
095: for (Iterator i = getRoles().iterator(); i.hasNext();) {
096: Object roleInfo = i.next();
097: if (roleInfo instanceof String) {
098: newRealm.addRole((String) roleInfo);
099: } else {
100: RealmRole role = (RealmRole) roleInfo;
101: Role newRole = newRealm.addRole(role.getRole());
102: if (role.isMaintain()) {
103: newRealm.setMaintainRole(newRole.getId());
104: }
105: }
106: }
107: }
108:
109: public AuthzGroupService getAuthzGroupService() {
110: return authzGroupService;
111: }
112:
113: public void setAuthzGroupService(AuthzGroupService authzGroupService) {
114: this .authzGroupService = authzGroupService;
115: }
116:
117: public String getNewRealmName() {
118: return newRealmName;
119: }
120:
121: public void setNewRealmName(String newRealmName) {
122: this .newRealmName = newRealmName;
123: }
124:
125: public List getRoles() {
126: return roles;
127: }
128:
129: public void setRoles(List roles) {
130: this .roles = roles;
131: }
132:
133: public boolean isNewlyCreated() {
134: return newlyCreated;
135: }
136:
137: public void setNewlyCreated(boolean newlyCreated) {
138: this .newlyCreated = newlyCreated;
139: }
140:
141: public boolean isRecreate() {
142: return recreate;
143: }
144:
145: public void setRecreate(boolean recreate) {
146: this .recreate = recreate;
147: }
148:
149: public boolean isAutoDdl() {
150: return autoDdl;
151: }
152:
153: public void setAutoDdl(boolean autoDdl) {
154: this.autoDdl = autoDdl;
155: }
156: }
|