001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/providers/tags/sakai_2-4-1/allhands/src/java/org/sakaiproject/provider/authzGroup/AllHandsGroupProvider.java $
003: * $Id: AllHandsGroupProvider.java 20456 2007-01-19 00:58:21Z jholtzman@berkeley.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.sakaiproject.provider.authzGroup;
021:
022: import java.util.HashMap;
023: import java.util.Map;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.sakaiproject.authz.api.GroupProvider;
028: import org.sakaiproject.util.StringUtil;
029:
030: /**
031: * <p>
032: * AllHands GroupProvider allows the creation of provided authzGroups that automatically include all users.
033: * </p>
034: * <p>
035: * To use, set an authzGroup's external id to one of the following:
036: * <ul>
037: * <li>sakai.allhands</li>
038: * </ul>
039: * </p>
040: */
041: public class AllHandsGroupProvider implements GroupProvider {
042: /** Our log (commons). */
043: private static Log M_log = LogFactory
044: .getLog(AllHandsGroupProvider.class);
045:
046: /**********************************************************************************************************************************************************************************************************************************************************
047: * Init and Destroy
048: *********************************************************************************************************************************************************************************************************************************************************/
049:
050: /**
051: * Final initialization, once all dependencies are set.
052: */
053: public void init() {
054: try {
055: M_log.info("init()");
056: } catch (Throwable t) {
057: M_log.warn("init(): ", t);
058: }
059: }
060:
061: /**
062: * Cleanup before shutting down.
063: */
064: public void destroy() {
065: M_log.info("destroy()");
066: }
067:
068: /**********************************************************************************************************************************************************************************************************************************************************
069: * GroupProvider implementation
070: *********************************************************************************************************************************************************************************************************************************************************/
071:
072: /**
073: * Construct.
074: */
075: public AllHandsGroupProvider() {
076: }
077:
078: /**
079: * {@inheritDoc}
080: */
081: public String getRole(String id, String user) {
082: System.out.println("getRole() id=" + id + " user=" + user);
083: // Apparently this is not called ???
084: return null;
085: }
086:
087: /**
088: * {@inheritDoc}
089: * This is not necessary - because the user has already been added because they are
090: * enrolled in the allhand.s
091: */
092: public Map getUserRolesForGroup(String id) {
093: Map rv = new HashMap();
094:
095: return rv;
096: }
097:
098: /**
099: * {@inheritDoc}
100: *
101: * If you can do some type of directory lookup to find out some
102: * Attribute of this user you could conditionally auto-add them
103: * to any number of these "virtual groups"
104: *
105: * You might try sakai.students, sakai.faculty,
106: * sakai.staff, sakai.teamleads
107: *
108: * There is nothing specific about the string "sakai." these could
109: * easily be something like course.eecs280, shib:yale.edu:faculty,
110: * or h23.groups.ruk.dk
111: *
112: * One caveat - this class is respnsbile for handling the unpacking
113: * when an admin wants to put in multiple external providers
114: * The code below uses the common convention of using "+" (homage
115: * to "or") to concatenate IDs. So the user above us could set the
116: * provider ID in the Sakai Realms Tool to be
117: *
118: * h23.groups.ruk.dk+course.eecs280
119: *
120: * to indicate thatr membership in either group is OK.
121: *
122: * To indicate membership in multiple groups in *this routine* add
123: * additional entries in the hash map (i.e. do not use the + notation
124: * in this routine).
125: */
126:
127: public Map getGroupRolesForUser(String userId) {
128: System.out.println("getGroupRolesForUser() user=" + userId);
129:
130: Map rv = new HashMap();
131:
132: rv.put("sakai.allhands", "access");
133:
134: return rv;
135: }
136:
137: public String[] unpackId(String id) {
138: String[] rv = null;
139:
140: // if there is not a '+' return just the id
141: int pos = id.indexOf('+');
142: if (pos == -1) {
143: rv = new String[1];
144: rv[0] = id;
145: }
146:
147: // otherwise split by the '+'
148: else {
149: rv = StringUtil.split(id, "+");
150: }
151:
152: return rv;
153: }
154:
155: /**
156: * {@inheritDoc}
157: */
158: public String packId(String[] ids) {
159: if (ids == null || ids.length == 0) {
160: return null;
161: }
162:
163: if (ids.length == 1) {
164: return ids[0];
165: }
166:
167: StringBuffer sb = new StringBuffer();
168: for (int i = 0; i < ids.length; i++) {
169: sb.append(ids[i]);
170: if (i < ids.length - 1) {
171: sb.append("+");
172: }
173: }
174: return sb.toString();
175: }
176:
177: /**
178: * {@inheritDoc}
179: */
180: public String preferredRole(String one, String other) {
181: // maintain is better than access
182: if ("maintain".equals(one) || ("maintain".equals(other)))
183: return "maintain";
184:
185: // access is better than nothing
186: if ("access".equals(one) || ("access".equals(other)))
187: return "access";
188:
189: // something we don't know, so we just return the latest role found
190: return one;
191: }
192: }
|