001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/providers/tags/sakai_2-4-1/sample/src/java/org/sakaiproject/provider/authzGroup/SampleGroupProvider.java $
003: * $Id: SampleGroupProvider.java 20456 2007-01-19 00:58:21Z jholtzman@berkeley.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 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.provider.authzGroup;
021:
022: import java.io.FileInputStream;
023: import java.util.HashMap;
024: import java.util.HashSet;
025: import java.util.Iterator;
026: import java.util.Map;
027: import java.util.Properties;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.sakaiproject.authz.api.GroupProvider;
032: import org.sakaiproject.util.StringUtil;
033:
034: /**
035: * <p>
036: * Sample of a GroupProvider. Shows how to handle compound ids (connected with a '+').
037: * </p>
038: * <p>
039: * To use, set an authzGroup's external id to one of the following:
040: * <ul>
041: * <li>sakai.access</li>
042: * <li>sakai.maintain</li>
043: * <li>sakai.access+sakai.maintain</li>
044: * </ul>
045: * </p>
046: * <p>
047: * You should also have a user directory provider that recognizes the users:
048: * <ul>
049: * <li>user1</li>
050: * <li>user</li>
051: * <li>user3</li>
052: * </ul>
053: * The SampleUserDirectoryProvider does this.
054: * </p>
055: */
056: public class SampleGroupProvider implements GroupProvider {
057: /** Our log (commons). */
058: private static Log M_log = LogFactory
059: .getLog(SampleGroupProvider.class);
060:
061: /**********************************************************************************************************************************************************************************************************************************************************
062: * Dependencies and their setter methods
063: *********************************************************************************************************************************************************************************************************************************************************/
064:
065: /** Configuration: the full file path to the external user grants file (optional). */
066: protected String m_xFile = null;
067:
068: /**
069: * Dependency - set the full file path to the optional external user grants file.
070: *
071: * @param value
072: * The full file path to the external user grants file.
073: */
074: public void setXFile(String value) {
075: m_xFile = value;
076: }
077:
078: /** The external id for the "course" group. */
079: protected String m_courseExternalId = "2006,FALL,SMPL,001,001";
080:
081: /**
082: * Dependency - set the external id for the "course" group.
083: *
084: * @param value
085: * The external id for the "course" site.
086: */
087: public void setCourseExternalId(String value) {
088: m_courseExternalId = value;
089: }
090:
091: /** how many students in the "course" group. */
092: protected int m_courseStudents = 22;
093:
094: /**
095: * Set how many students in the "course" group
096: *
097: * @param count
098: * How many students in the "course" group
099: */
100: public void setCourseStudents(String count) {
101: m_courseStudents = Integer.parseInt(count);
102: }
103:
104: /**********************************************************************************************************************************************************************************************************************************************************
105: * Init and Destroy
106: *********************************************************************************************************************************************************************************************************************************************************/
107:
108: /**
109: * Final initialization, once all dependencies are set.
110: */
111: public void init() {
112: try {
113: // users: 1 is 'a', 2 is both, 3 is 'm'
114: m_usersa = new HashSet();
115: m_usersa.add("user1");
116: m_usersa.add("user2");
117:
118: m_usersm = new HashSet();
119: m_usersm.add("user2");
120: m_usersm.add("user3");
121:
122: // the instructor and ta and some students
123: m_usersc = new HashSet();
124: if ((m_courseStudents > 0) && (m_courseExternalId != null)) {
125: m_usersc.add("instructor");
126: m_usersc.add("ta");
127:
128: for (int i = 1; i <= m_courseStudents; i++) {
129: m_usersc.add("student" + i);
130: }
131: }
132:
133: M_log.info("init()");
134: } catch (Throwable t) {
135: M_log.warn("init(): ", t);
136: }
137: }
138:
139: /**
140: * Cleanup before shutting down.
141: */
142: public void destroy() {
143: M_log.info("destroy()");
144: }
145:
146: /**********************************************************************************************************************************************************************************************************************************************************
147: * GroupProvider implementation
148: *********************************************************************************************************************************************************************************************************************************************************/
149:
150: /** A collection of user ids. */
151: protected HashSet m_usersa = null;
152:
153: protected HashSet m_usersm = null;
154:
155: protected Properties m_usersx = null;
156:
157: protected HashSet m_usersc = null;
158:
159: /**
160: * Construct.
161: */
162: public SampleGroupProvider() {
163: }
164:
165: /**
166: * {@inheritDoc}
167: */
168: public String getRole(String id, String user) {
169: update();
170:
171: String rv = null;
172:
173: // process compound id, and prefer "maintain" to "access" if there's a choice
174: String[] ids = unpackId(id);
175: for (int i = 0; i < ids.length; i++) {
176: // if the user is in the list for the 'a' group
177: if ((m_usersa.contains(user))
178: && ("sakai.access".equals(ids[i]))) {
179: // give the "access" role, if "maintain" not already found
180: if (!("maintain".equals(rv))) {
181: rv = "access";
182: }
183: }
184:
185: // if the user is in the list for the 'm' group
186: if ((m_usersm.contains(user))
187: && ("sakai.maintain".equals(ids[i]))) {
188: // give the "maintain" role
189: rv = "maintain";
190: }
191:
192: if ((m_usersx.contains(user)) && ("sakai.x".equals(ids[i]))) {
193: // give whatever's in the x role, if "maintain" not already found
194: if (!("maintain".equals(rv))) {
195: rv = m_usersx.getProperty(user);
196: }
197: }
198:
199: if ((m_usersc.contains(user))
200: && (m_courseExternalId.equals(ids[i]))) {
201: if ("instructor".equals(user)) {
202: rv = "Instructor";
203: } else if ("ta".equals(user)) {
204: rv = "Teaching Assistant";
205: } else {
206: rv = "Student";
207: }
208: }
209: }
210:
211: return rv;
212: }
213:
214: /**
215: * {@inheritDoc}
216: */
217: public Map getUserRolesForGroup(String id) {
218: update();
219:
220: Map rv = new HashMap();
221:
222: // responds properly to a null id - with an empty map
223: if (id == null)
224: return rv;
225:
226: // process compound id, and prefer "maintain" to "access" if there's a choice
227: String[] ids = unpackId(id);
228: for (int i = 0; i < ids.length; i++) {
229: if ("sakai.access".equals(ids[i])) {
230: // put each user in the map as "access", unless they are already there (as maintain perhaps)
231: for (Iterator it = m_usersa.iterator(); it.hasNext();) {
232: String userId = (String) it.next();
233: if (!("maintain".equals(rv.get(userId)))) {
234: rv.put(userId, "access");
235: }
236: }
237: }
238:
239: if ("sakai.maintain".equals(ids[i])) {
240: // put each user in the map as "maintain", even if they are there (as access perhaps)
241: for (Iterator it = m_usersm.iterator(); it.hasNext();) {
242: String userId = (String) it.next();
243: rv.put(userId, "maintain");
244: }
245: }
246:
247: if ("sakai.x".equals(ids[i])) {
248: // put each user in the map as whatever they are in x, unless they are already there (as maintain perhaps)
249: for (Iterator it = m_usersx.keySet().iterator(); it
250: .hasNext();) {
251: String userId = (String) it.next();
252: if (!("maintain".equals(rv.get(userId)))) {
253: rv.put(userId, m_usersx.getProperty(userId));
254: }
255: }
256: }
257:
258: if ((m_courseExternalId != null)
259: && (m_courseExternalId.equals(ids[i]))) {
260: for (Iterator it = m_usersc.iterator(); it.hasNext();) {
261: String userId = (String) it.next();
262: if ("instructor".equals(userId)) {
263: rv.put(userId, "Instructor");
264: } else if ("ta".equals(userId)) {
265: rv.put(userId, "Teaching Assistant");
266: } else {
267: rv.put(userId, "Student");
268: }
269: }
270: }
271: }
272:
273: return rv;
274: }
275:
276: /**
277: * {@inheritDoc}
278: */
279: public Map getGroupRolesForUser(String userId) {
280: update();
281:
282: Map rv = new HashMap();
283:
284: if (m_usersa.contains(userId)) {
285: rv.put("sakai.access", "access");
286: }
287:
288: if (m_usersm.contains(userId)) {
289: rv.put("sakai.maintain", "maintain");
290: }
291:
292: if (m_usersx.keySet().contains(userId)) {
293: rv.put("sakai.x", m_usersx.getProperty(userId));
294: }
295:
296: if (m_usersc.contains(userId)) {
297: if ("instructor".equals(userId)) {
298: rv.put(m_courseExternalId, "Instructor");
299: } else if ("ta".equals(userId)) {
300: rv.put(m_courseExternalId, "Teaching Assistant");
301: } else {
302: rv.put(m_courseExternalId, "Student");
303: }
304: }
305:
306: return rv;
307: }
308:
309: /**
310: * {@inheritDoc}
311: */
312: public String packId(String[] ids) {
313: if (ids == null || ids.length == 0) {
314: return null;
315: }
316:
317: if (ids.length == 1) {
318: return ids[0];
319: }
320:
321: StringBuffer sb = new StringBuffer();
322: for (int i = 0; i < ids.length; i++) {
323: sb.append(ids[i]);
324: if (i < ids.length - 1) {
325: sb.append("+");
326: }
327: }
328: return sb.toString();
329: }
330:
331: /**
332: * {@inheritDoc}
333: */
334: public String[] unpackId(String id) {
335: String[] rv = null;
336:
337: // if there is not a '+' return just the id
338: int pos = id.indexOf('+');
339: if (pos == -1) {
340: rv = new String[1];
341: rv[0] = id;
342: }
343:
344: // otherwise split by the '+'
345: else {
346: rv = StringUtil.split(id, "+");
347: }
348:
349: return rv;
350: }
351:
352: /**
353: * {@inheritDoc}
354: */
355: public String preferredRole(String one, String other) {
356: // maintain and Instructor first
357: if ("maintain".equals(one) || ("maintain".equals(other)))
358: return "maintain";
359:
360: if ("Instructor".equals(one) || ("Instructor".equals(other)))
361: return "Instructor";
362:
363: // ta next
364: if ("Teaching Assistant".equals(one)
365: || ("Teaching Assistant".equals(other)))
366: return "Teaching Assistant";
367:
368: // access and Student next
369: if ("access".equals(one) || ("access".equals(other)))
370: return "access";
371:
372: if ("Student".equals(one) || ("Student".equals(other)))
373: return "Student";
374:
375: // something we don't know, so we just return the latest role found
376: return one;
377: }
378:
379: protected void update() {
380: m_usersx = new Properties();
381:
382: if (m_xFile != null) {
383: try {
384: m_usersx.load(new FileInputStream(m_xFile));
385: } catch (Exception e) {
386: M_log.warn("update: reading users.properties file: "
387: + m_xFile + " : " + e);
388: }
389: }
390: }
391: }
|