001: /**********************************************************************************
002: *
003: * $Id: AuthzSectionsImpl.java 19624 2006-12-15 23:58:34Z ray@media.berkeley.edu $
004: *
005: ***********************************************************************************
006: *
007: * Copyright (c) 2005 The Regents of the University of California, The MIT Corporation
008: *
009: * Licensed under the Educational Community License, Version 1.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.opensource.org/licenses/ecl1.php
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: *
021: **********************************************************************************/package org.sakaiproject.tool.gradebook.facades.sections;
022:
023: import java.util.*;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027:
028: import org.sakaiproject.section.api.SectionAwareness;
029: import org.sakaiproject.section.api.coursemanagement.CourseSection;
030: import org.sakaiproject.section.api.coursemanagement.EnrollmentRecord;
031: import org.sakaiproject.section.api.facade.Role;
032:
033: import org.sakaiproject.tool.gradebook.facades.Authn;
034: import org.sakaiproject.tool.gradebook.facades.Authz;
035:
036: /**
037: * An implementation of Gradebook-specific authorization needs based
038: * on the shared Section Awareness API.
039: */
040: public class AuthzSectionsImpl implements Authz {
041: private static final Log log = LogFactory
042: .getLog(AuthzSectionsImpl.class);
043:
044: private Authn authn;
045: private SectionAwareness sectionAwareness;
046:
047: public boolean isUserAbleToGrade(String gradebookUid) {
048: String userUid = authn.getUserUid();
049: return (getSectionAwareness().isSiteMemberInRole(gradebookUid,
050: userUid, Role.INSTRUCTOR) || getSectionAwareness()
051: .isSiteMemberInRole(gradebookUid, userUid, Role.TA));
052: }
053:
054: public boolean isUserAbleToGradeAll(String gradebookUid) {
055: String userUid = authn.getUserUid();
056: return getSectionAwareness().isSiteMemberInRole(gradebookUid,
057: userUid, Role.INSTRUCTOR);
058: }
059:
060: public boolean isUserAbleToGradeSection(String sectionUid) {
061: String userUid = authn.getUserUid();
062: return getSectionAwareness().isSectionMemberInRole(sectionUid,
063: userUid, Role.TA);
064: }
065:
066: public boolean isUserAbleToEditAssessments(String gradebookUid) {
067: String userUid = authn.getUserUid();
068: return getSectionAwareness().isSiteMemberInRole(gradebookUid,
069: userUid, Role.INSTRUCTOR);
070: }
071:
072: public boolean isUserAbleToViewOwnGrades(String gradebookUid) {
073: String userUid = authn.getUserUid();
074: return getSectionAwareness().isSiteMemberInRole(gradebookUid,
075: userUid, Role.STUDENT);
076: }
077:
078: /**
079: * Note that this is not a particularly efficient implementation.
080: * If the method becomes more heavily used, it should be optimized.
081: */
082: public boolean isUserAbleToGradeStudent(String gradebookUid,
083: String studentUid) {
084: if (isUserAbleToGradeAll(gradebookUid)) {
085: return true;
086: }
087:
088: List sections = getAvailableSections(gradebookUid);
089: for (Iterator iter = sections.iterator(); iter.hasNext();) {
090: CourseSection section = (CourseSection) iter.next();
091: if (getSectionAwareness().isSectionMemberInRole(
092: section.getUuid(), studentUid, Role.STUDENT)) {
093: return true;
094: }
095: }
096:
097: return false;
098: }
099:
100: /**
101: */
102: public List getAvailableEnrollments(String gradebookUid) {
103: List enrollments;
104: if (isUserAbleToGradeAll(gradebookUid)) {
105: enrollments = getSectionAwareness().getSiteMembersInRole(
106: gradebookUid, Role.STUDENT);
107: } else {
108: // We use a map because we may have duplicate students among the section
109: // participation records.
110: Map enrollmentMap = new HashMap();
111: List sections = getAvailableSections(gradebookUid);
112: for (Iterator iter = sections.iterator(); iter.hasNext();) {
113: CourseSection section = (CourseSection) iter.next();
114: List sectionEnrollments = getSectionEnrollmentsTrusted(section
115: .getUuid());
116: for (Iterator eIter = sectionEnrollments.iterator(); eIter
117: .hasNext();) {
118: EnrollmentRecord enr = (EnrollmentRecord) eIter
119: .next();
120: enrollmentMap.put(enr.getUser().getUserUid(), enr);
121: }
122: }
123: enrollments = new ArrayList(enrollmentMap.values());
124: }
125: return enrollments;
126: }
127:
128: public List getAvailableSections(String gradebookUid) {
129: SectionAwareness sectionAwareness = getSectionAwareness();
130: List availableSections = new ArrayList();
131: boolean userAbleToGradeAll = isUserAbleToGradeAll(gradebookUid);
132:
133: // Get the list of sections. For now, just use whatever default
134: // sorting we get from the Section Awareness component.
135: List sections = sectionAwareness.getSections(gradebookUid);
136: for (Iterator iter = sections.iterator(); iter.hasNext();) {
137: CourseSection section = (CourseSection) iter.next();
138: if (userAbleToGradeAll
139: || isUserAbleToGradeSection(section.getUuid())) {
140: availableSections.add(section);
141: }
142: }
143:
144: return availableSections;
145: }
146:
147: private List getSectionEnrollmentsTrusted(String sectionUid) {
148: return getSectionAwareness().getSectionMembersInRole(
149: sectionUid, Role.STUDENT);
150: }
151:
152: public List getSectionEnrollments(String gradebookUid,
153: String sectionUid) {
154: String userUid = authn.getUserUid();
155: List enrollments;
156: if (isUserAbleToGradeAll(gradebookUid)
157: || isUserAbleToGradeSection(sectionUid)) {
158: enrollments = getSectionEnrollmentsTrusted(sectionUid);
159: } else {
160: enrollments = new ArrayList();
161: log.warn("getSectionEnrollments for sectionUid="
162: + sectionUid + " called by unauthorized userUid="
163: + userUid);
164: }
165: return enrollments;
166: }
167:
168: public List findMatchingEnrollments(String gradebookUid,
169: String searchString, String optionalSectionUid) {
170: List enrollments;
171: List allEnrollmentsFilteredBySearch = getSectionAwareness()
172: .findSiteMembersInRole(gradebookUid, Role.STUDENT,
173: searchString);
174:
175: if (allEnrollmentsFilteredBySearch.isEmpty()
176: || ((optionalSectionUid == null) && isUserAbleToGradeAll(gradebookUid))) {
177: enrollments = allEnrollmentsFilteredBySearch;
178: } else {
179: if (optionalSectionUid == null) {
180: enrollments = getAvailableEnrollments(gradebookUid);
181: } else {
182: // The user has selected a particular section.
183: enrollments = getSectionEnrollments(gradebookUid,
184: optionalSectionUid);
185: }
186: Set availableStudentUids = new HashSet();
187: for (Iterator iter = enrollments.iterator(); iter.hasNext();) {
188: EnrollmentRecord enr = (EnrollmentRecord) iter.next();
189: availableStudentUids.add(enr.getUser().getUserUid());
190: }
191:
192: enrollments = new ArrayList();
193: for (Iterator iter = allEnrollmentsFilteredBySearch
194: .iterator(); iter.hasNext();) {
195: EnrollmentRecord enr = (EnrollmentRecord) iter.next();
196: if (availableStudentUids.contains(enr.getUser()
197: .getUserUid())) {
198: enrollments.add(enr);
199: }
200: }
201: }
202:
203: return enrollments;
204: }
205:
206: public Authn getAuthn() {
207: return authn;
208: }
209:
210: public void setAuthn(Authn authn) {
211: this .authn = authn;
212: }
213:
214: public SectionAwareness getSectionAwareness() {
215: return sectionAwareness;
216: }
217:
218: public void setSectionAwareness(SectionAwareness sectionAwareness) {
219: this.sectionAwareness = sectionAwareness;
220: }
221:
222: }
|