001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/presence/trunk/presence-api/api/src/java/org/sakaiproject/presence/api/PresenceService.java $
003: * $Id: PresenceService.java 7844 2006-04-17 13:06:02Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 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.component.app.roster;
021:
022: import java.util.ArrayList;
023: import java.util.Collection;
024: import java.util.Collections;
025: import java.util.Comparator;
026: import java.util.Iterator;
027: import java.util.List;
028: import java.util.Map;
029: import java.util.Set;
030: import java.util.HashSet;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.sakaiproject.api.app.profile.Profile;
035: import org.sakaiproject.api.app.profile.ProfileManager;
036: import org.sakaiproject.api.app.roster.Participant;
037: import org.sakaiproject.api.app.roster.RosterManager;
038: import org.sakaiproject.api.app.roster.RosterFunctions;
039: import org.sakaiproject.api.privacy.PrivacyManager;
040: import org.sakaiproject.authz.api.AuthzGroup;
041: import org.sakaiproject.authz.api.GroupNotDefinedException;
042: import org.sakaiproject.authz.api.Role;
043: import org.sakaiproject.authz.cover.AuthzGroupService;
044: import org.sakaiproject.authz.cover.FunctionManager;
045: import org.sakaiproject.authz.cover.SecurityService;
046: import org.sakaiproject.site.api.Group;
047: import org.sakaiproject.site.api.Site;
048: import org.sakaiproject.site.cover.SiteService;
049: import org.sakaiproject.tool.cover.ToolManager;
050: import org.sakaiproject.user.api.User;
051: import org.sakaiproject.user.api.UserNotDefinedException;
052: import org.sakaiproject.user.cover.UserDirectoryService;
053: import org.sakaiproject.component.cover.ServerConfigurationService;
054:
055: /**
056: * @author rshastri
057: * @version $Id: RosterManagerImpl.java 1244 2005-08-17 16:06:54Z rshastri@iupui.edu $
058: */
059: public class RosterManagerImpl implements RosterManager {
060: private static final Log LOG = LogFactory
061: .getLog(RosterManagerImpl.class);
062:
063: /** Dependency: ProfileManager */
064: private ProfileManager profileManager;
065: private PrivacyManager privacyManager;
066:
067: private static final String SITE_UPD_PERM = "site.upd";
068:
069: /**
070: * @param sakaiProfileManager
071: */
072: public void setProfileManager(ProfileManager newProfileManager) {
073: if (LOG.isDebugEnabled()) {
074: LOG.debug("setProfileManager(ProfileManager "
075: + newProfileManager + ")");
076: }
077:
078: this .profileManager = newProfileManager;
079: }
080:
081: /**
082: * @param PrivacyManager
083: */
084: public void setPrivacyManager(PrivacyManager privacyManager) {
085: if (LOG.isDebugEnabled()) {
086: LOG.debug("setPrivacyManager(PrivacyManager "
087: + privacyManager + ")");
088: }
089:
090: this .privacyManager = privacyManager;
091: }
092:
093: public void init() {
094: LOG.debug("init()");
095:
096: Collection registered = FunctionManager.getInstance()
097: .getRegisteredFunctions(
098: RosterFunctions.ROSTER_FUNCTION_PREFIX);
099: if (!registered
100: .contains(RosterFunctions.ROSTER_FUNCTION_EXPORT)) {
101: FunctionManager
102: .registerFunction(RosterFunctions.ROSTER_FUNCTION_EXPORT);
103: }
104:
105: if (!registered
106: .contains(RosterFunctions.ROSTER_FUNCTION_VIEWALL)) {
107: FunctionManager
108: .registerFunction(RosterFunctions.ROSTER_FUNCTION_VIEWALL);
109: }
110:
111: if (!registered
112: .contains(RosterFunctions.ROSTER_FUNCTION_VIEWHIDDEN)) {
113: FunctionManager
114: .registerFunction(RosterFunctions.ROSTER_FUNCTION_VIEWHIDDEN);
115: }
116:
117: if (!registered
118: .contains(RosterFunctions.ROSTER_FUNCTION_VIEWOFFICIALID)) {
119: FunctionManager
120: .registerFunction(RosterFunctions.ROSTER_FUNCTION_VIEWOFFICIALID);
121: }
122:
123: if (!registered
124: .contains(RosterFunctions.ROSTER_FUNCTION_VIEWSECTION)) {
125: FunctionManager
126: .registerFunction(RosterFunctions.ROSTER_FUNCTION_VIEWSECTION);
127: }
128: }
129:
130: public void destroy() {
131: LOG.debug("destroy()");
132: ; // do nothing (for now)
133: }
134:
135: private Participant createParticipantByUser(User user,
136: Profile profile) {
137: if (LOG.isDebugEnabled()) {
138: LOG.debug("createParticipantByUser(User " + user + ")");
139: }
140: return new ParticipantImpl(user.getId(), user.getDisplayId(),
141: user.getEid(), user.getFirstName(), user.getLastName(),
142: profile, getUserRoleTitle(user), getUserSections(user));
143: }
144:
145: /* (non-Javadoc)
146: * @see org.sakaiproject.api.app.roster.RosterManager#getParticipantById(java.lang.String)
147: */
148: public Participant getParticipantById(String participantId) {
149: if (LOG.isDebugEnabled()) {
150: LOG
151: .debug("getParticipantById(String" + participantId
152: + ")");
153: }
154: if (participantId != null) {
155: try {
156: User user = UserDirectoryService.getUser(participantId);
157: Profile profile = profileManager
158: .getUserProfileById(participantId);
159: //TODO: individual ferpa checks
160: return createParticipantByUser(user, profile);
161: } catch (UserNotDefinedException e) {
162: LOG.error("getParticipantById: " + e.getMessage(), e);
163: }
164: }
165: return null;
166: }
167:
168: /**
169: * retrieve the list of participants filtered by the passed filter
170: * (such as Show All Sections) that are viewable by the current user
171: * @param filter
172: * @return List
173: */
174: public List getRoster(String filter) {
175: LOG.debug("getRoster");
176:
177: /* we have several possibilities to return:
178: 1. All users
179: 2. All users in current user's section(s)
180: 3. All users in the selected section
181: - note: privacy is considered for each option
182: */
183:
184: if (filter == null)
185: return null;
186:
187: User currentUser = UserDirectoryService.getCurrentUser();
188: boolean userHasViewAllPerm = userHasPermission(currentUser,
189: RosterFunctions.ROSTER_FUNCTION_VIEWALL);
190: boolean userHasViewSectionPerm = userHasPermission(currentUser,
191: RosterFunctions.ROSTER_FUNCTION_VIEWSECTION);
192:
193: List roster = new ArrayList();
194: Set userIds = new HashSet();
195:
196: // view all sections
197: if (filter.equalsIgnoreCase(VIEW_ALL_SECT)) {
198: // first check the current user's authorization for this option - must have roster.viewall
199: if (!userHasViewAllPerm)
200: return null;
201:
202: userIds = getUsersInAllSections();
203: } else if (filter.equalsIgnoreCase(VIEW_MY_SECT)) {
204: // first check that current user has roster.viewsection permission
205: if (!userHasViewSectionPerm)
206: return null;
207:
208: userIds = getUsersInCurrentUsersSections(currentUser);
209: } else if (filter.equalsIgnoreCase(VIEW_NO_SECT)) {
210: if (userHasViewAllPerm
211: || (!siteHasSections() && (userHasViewAllPerm || userHasViewSectionPerm))) {
212: userIds = getUsersInAllSections();
213: } else if (userHasViewSectionPerm) {
214: userIds = getUsersInCurrentUsersSections(currentUser);
215: }
216: } else // we will assume the filter is a specific section
217: {
218:
219: // first check that current user has roster.viewsection or roster.viewall permission
220: if (!userHasViewAllPerm && !userHasViewSectionPerm)
221: return null;
222:
223: Set usersInSection = getUsersInSection(filter);
224: if (usersInSection == null || usersInSection.isEmpty())
225: return null;
226:
227: // if user does not have viewall perm, must check to see if member of section
228: if (!userHasViewAllPerm) {
229: if (!usersInSection.contains(currentUser.getId()))
230: return null;
231: }
232:
233: userIds = usersInSection;
234: }
235:
236: if (userIds == null || userIds.isEmpty())
237: return null;
238:
239: // Check for privacy restrictions
240: if (!userHasPermission(currentUser,
241: RosterFunctions.ROSTER_FUNCTION_VIEWHIDDEN)) {
242: userIds = privacyManager.findViewable(getContextSiteId(),
243: userIds);
244: }
245:
246: if (userIds != null && userIds.size() > 0) {
247: Map<String, Profile> profiles = profileManager
248: .getProfiles(userIds);
249:
250: Iterator iter = userIds.iterator();
251: while (iter.hasNext()) {
252: try {
253: User user = UserDirectoryService
254: .getUser((String) iter.next());
255: if (user != null) {
256: roster.add(createParticipantByUser(user,
257: profiles.get(user.getId())));
258: }
259: } catch (UserNotDefinedException e) {
260: LOG.info("getRoster: " + e.getMessage(), e);
261: }
262: }
263: }
264:
265: return roster;
266: }
267:
268: /**
269: * Get the userids of all the users in the site
270: * @return
271: */
272: private Set getUsersInAllSections() {
273: List users = new ArrayList();
274: Set userIds = new HashSet();
275:
276: AuthzGroup realm;
277: try {
278: realm = AuthzGroupService.getAuthzGroup(getContextSiteId());
279: users.addAll(UserDirectoryService
280: .getUsers(realm.getUsers()));
281:
282: if (users == null)
283: return null;
284:
285: Iterator userIterator = users.iterator();
286: while (userIterator.hasNext()) {
287: userIds.add(((User) userIterator.next()).getId());
288: }
289: } catch (GroupNotDefinedException e) {
290: LOG.error("getUsersInAllSections: " + e.getMessage(), e);
291: }
292:
293: return userIds;
294: }
295:
296: /**
297: * Returns the userids of the users who are in the current user's section(s)
298: * @param currentUser
299: * @return
300: */
301: private Set getUsersInCurrentUsersSections(User currentUser) {
302: Set userIds = new HashSet();
303: List userSections = getUserSections(currentUser);
304: Iterator sectionIter = userSections.iterator();
305: while (sectionIter.hasNext()) {
306: String section = (String) sectionIter.next();
307: Set usersInSection = getUsersInSection(section);
308: if (usersInSection != null && !usersInSection.isEmpty()) {
309: Iterator userIter = usersInSection.iterator();
310: while (userIter.hasNext()) {
311: String userInSection = (String) userIter.next();
312: if (!userIds.contains(userInSection))
313: userIds.add(userInSection);
314: }
315: }
316: }
317:
318: return userIds;
319: }
320:
321: /*
322: * (non-Javadoc)
323: * @see org.sakaiproject.api.app.roster.RosterManager#currentUserHasExportPerm()
324: */
325: public boolean currentUserHasExportPerm() {
326: return userHasPermission(UserDirectoryService.getCurrentUser(),
327: RosterFunctions.ROSTER_FUNCTION_EXPORT);
328: }
329:
330: /*
331: * (non-Javadoc)
332: * @see org.sakaiproject.api.app.roster.RosterManager#currentUserHasViewOfficialIdPerm()
333: */
334: public boolean currentUserHasViewOfficialIdPerm() {
335: return userHasPermission(UserDirectoryService.getCurrentUser(),
336: RosterFunctions.ROSTER_FUNCTION_VIEWOFFICIALID);
337: }
338:
339: /*
340: * (non-Javadoc)
341: * @see org.sakaiproject.api.app.roster.RosterManager#currentUserHasViewHiddenPerm()
342: */
343: public boolean currentUserHasViewHiddenPerm() {
344: return userHasPermission(UserDirectoryService.getCurrentUser(),
345: RosterFunctions.ROSTER_FUNCTION_VIEWHIDDEN);
346: }
347:
348: /*
349: * (non-Javadoc)
350: * @see org.sakaiproject.api.app.roster.RosterManager#currentUserHasViewSectionPerm()
351: */
352: public boolean currentUserHasViewSectionPerm() {
353: return userHasPermission(UserDirectoryService.getCurrentUser(),
354: RosterFunctions.ROSTER_FUNCTION_VIEWSECTION);
355: }
356:
357: /*
358: * (non-Javadoc)
359: * @see org.sakaiproject.api.app.roster.RosterManager#currentUserHasViewAllPerm()
360: */
361: public boolean currentUserHasViewAllPerm() {
362: return userHasPermission(UserDirectoryService.getCurrentUser(),
363: RosterFunctions.ROSTER_FUNCTION_VIEWALL);
364: }
365:
366: /*
367: * (non-Javadoc)
368: * @see org.sakaiproject.api.app.roster.RosterManager#currentUserHasSiteUpdatePerm()
369: */
370: public boolean currentUserHasSiteUpdatePerm() {
371: return userHasPermission(UserDirectoryService.getCurrentUser(),
372: SITE_UPD_PERM);
373: }
374:
375: /* (non-Javadoc)
376: * @see org.sakaiproject.api.app.roster.RosterManager#sortParticipants(java.util.List, java.lang.String, boolean)
377: */
378: public void sortParticipants(List participants,
379: String sortByColumn, boolean ascending) {
380: if (participants == null || participants.size() <= 1)
381: return;
382:
383: Comparator comparator;
384: if (Participant.SORT_BY_LAST_NAME.equals(sortByColumn)) {
385: comparator = ParticipantImpl.LastNameComparator;
386: } else if (Participant.SORT_BY_FIRST_NAME.equals(sortByColumn)) {
387: comparator = ParticipantImpl.FirstNameComparator;
388: } else if (Participant.SORT_BY_SECTIONS.equals(sortByColumn)) {
389: comparator = ParticipantImpl.SectionsComparator;
390: } else if (Participant.SORT_BY_ID.equals(sortByColumn)) {
391: comparator = ParticipantImpl.UserIdComparator;
392: } else {
393: comparator = ParticipantImpl.RoleComparator;
394: }
395:
396: Collections.sort(participants, comparator);
397: if (!ascending) {
398: Collections.reverse(participants);
399: }
400: }
401:
402: /**
403: * Check if given user has the given permission
404: * @param user
405: * @param permissionName
406: * @return boolean
407: */
408: private boolean userHasPermission(User user, String permissionName) {
409: if (user != null)
410: return SecurityService.unlock(user, permissionName,
411: getContextSiteId());
412: else
413: return false;
414: }
415:
416: /**
417: * @return siteId
418: */
419: private String getContextSiteId() {
420: LOG.debug("getContextSiteId()");
421: return ("/site/" + ToolManager.getCurrentPlacement()
422: .getContext());
423: }
424:
425: /**
426: *
427: * @param user
428: * @return
429: */
430: private String getUserRoleTitle(User user) {
431: try {
432: return AuthzGroupService.getUserRole(user.getId(),
433: getContextSiteId());
434: } catch (Exception e) {
435: LOG.error("Exception", e);
436: }
437: return "";
438: }
439:
440: /**
441: * Determine if sectioning exists in this site
442: * @return
443: */
444: public boolean siteHasSections() {
445: List sections = getAllSections();
446: if (sections != null && !sections.isEmpty())
447: return true;
448:
449: return false;
450: }
451:
452: /**
453: * returns sections that user has permission to view
454: * @return
455: */
456: public List getViewableSectionsForCurrentUser() {
457: if (currentUserHasViewAllPerm()) {
458: return getAllSections();
459: } else if (currentUserHasViewSectionPerm()) {
460: return getUserSections(UserDirectoryService
461: .getCurrentUser());
462: }
463:
464: return null;
465: }
466:
467: /**
468: * Returns the userids of the users in the given section
469: * @param sectionName
470: * @return
471: */
472: private Set getUsersInSection(String sectionName) {
473: Set usersInSection = new HashSet();
474: try {
475: Site currentSite = SiteService.getSite(ToolManager
476: .getCurrentPlacement().getContext());
477:
478: Collection groups = currentSite.getGroups();
479: for (Iterator groupIterator = groups.iterator(); groupIterator
480: .hasNext();) {
481: Group currentGroup = (Group) groupIterator.next();
482: if (currentGroup.getTitle().equalsIgnoreCase(
483: sectionName)) {
484: Set userSet = currentGroup.getUsers();
485: for (Iterator setIter = userSet.iterator(); setIter
486: .hasNext();) {
487: String userId = (String) setIter.next();
488: usersInSection.add(userId);
489: }
490:
491: return usersInSection;
492: }
493: }
494: } catch (Exception e) {
495: LOG.error("getUsersInSection" + e.getMessage(), e);
496: }
497:
498: return usersInSection;
499: }
500:
501: /**
502: *
503: * @param user
504: * @return
505: */
506: private List getUserSections(User user) {
507: List sections = new ArrayList();
508: try {
509: Site currentSite = SiteService.getSite(ToolManager
510: .getCurrentPlacement().getContext());
511:
512: Collection groups = currentSite.getGroupsWithMember(user
513: .getId());
514: for (Iterator groupIterator = groups.iterator(); groupIterator
515: .hasNext();) {
516: Group currentGroup = (Group) groupIterator.next();
517: sections.add(currentGroup.getTitle());
518: }
519:
520: if (sections.size() > 1) {
521: Collections.sort(sections);
522: }
523: } catch (Exception e) {
524: LOG.error("getUserSections: " + e.getMessage(), e);
525: }
526:
527: return sections;
528: }
529:
530: private List getAllSections() {
531: List sections = new ArrayList();
532: try {
533: Site currentSite = SiteService.getSite(ToolManager
534: .getCurrentPlacement().getContext());
535: Collection groups = currentSite.getGroups();
536: for (Iterator groupIterator = groups.iterator(); groupIterator
537: .hasNext();) {
538: Group currentGroup = (Group) groupIterator.next();
539: sections.add(currentGroup.getTitle());
540: }
541: } catch (Exception e) {
542: LOG.error("getAllSections: " + e.getMessage(), e);
543: }
544:
545: return sections;
546: }
547:
548: }
|