001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/trunk/sakai/admin-tools/su/src/java/org/sakaiproject/tool/su/SuTool.java $
003: * $Id: SuTool.java 5970 2006-02-15 03:07:19Z ggolden@umich.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.site.impl;
021:
022: import java.util.Set;
023: import java.util.Stack;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.sakaiproject.authz.api.AuthzGroup;
028: import org.sakaiproject.authz.api.GroupNotDefinedException;
029: import org.sakaiproject.authz.api.Member;
030: import org.sakaiproject.authz.api.Role;
031: import org.sakaiproject.authz.api.RoleAlreadyDefinedException;
032: import org.sakaiproject.authz.cover.AuthzGroupService;
033: import org.sakaiproject.entity.api.ResourceProperties;
034: import org.sakaiproject.entity.api.ResourcePropertiesEdit;
035: import org.sakaiproject.id.cover.IdManager;
036: import org.sakaiproject.site.api.Group;
037: import org.sakaiproject.site.api.Site;
038: import org.sakaiproject.site.cover.SiteService;
039: import org.sakaiproject.time.api.Time;
040: import org.sakaiproject.user.api.User;
041: import org.sakaiproject.util.BaseResourceProperties;
042: import org.sakaiproject.util.BaseResourcePropertiesEdit;
043: import org.w3c.dom.Document;
044: import org.w3c.dom.Element;
045:
046: /**
047: * <p>
048: * BaseGroup is an implementation of the Site API Group.
049: * </p>
050: */
051: public class BaseGroup implements Group, Identifiable {
052: /** Our log (commons). */
053: private static Log M_log = LogFactory.getLog(BaseGroup.class);
054:
055: /** A fixed class serian number. */
056: private static final long serialVersionUID = 1L;
057:
058: /** The title. */
059: protected String m_title = null;
060:
061: /** The description. */
062: protected String m_description = null;
063:
064: /** The site id. */
065: protected String m_id = null;
066:
067: /** The properties. */
068: protected ResourcePropertiesEdit m_properties = null;
069:
070: /** The site I belong to. */
071: protected Site m_site = null;
072:
073: /** The azg from the AuthzGroupService that is my AuthzGroup impl. */
074: protected AuthzGroup m_azg = null;
075:
076: /** Set to true if we have changed our azg, so it need to be written back on save. */
077: protected boolean m_azgChanged = false;
078:
079: /**
080: * Construct. Auto-generate the id.
081: *
082: * @param site
083: * The site in which this page lives.
084: */
085: protected BaseGroup(Site site) {
086: if (site == null)
087: M_log.warn("BaseGroup(site) created with null site");
088:
089: m_site = site;
090: m_id = IdManager.createUuid();
091: m_properties = new BaseResourcePropertiesEdit();
092: }
093:
094: protected BaseGroup(String id, String title, String description,
095: Site site) {
096: if (site == null)
097: M_log.warn("BaseGroup(..., site) created with null site");
098:
099: m_id = id;
100: m_title = title;
101: m_description = description;
102: m_site = site;
103: m_properties = new BaseResourcePropertiesEdit();
104: }
105:
106: /**
107: * Construct as a copy of another.
108: *
109: * @param other
110: * The other to copy.
111: * @param site
112: * The site in which this group lives.
113: * @param exact
114: * If true, we copy id - else we generate a new one.
115: */
116: protected BaseGroup(Group other, Site site, boolean exact) {
117: if (site == null)
118: M_log
119: .warn("BaseGroup(other, site...) created with null site");
120:
121: BaseGroup bOther = (BaseGroup) other;
122:
123: m_site = (Site) site;
124:
125: if (exact) {
126: m_id = bOther.m_id;
127: } else {
128: m_id = IdManager.createUuid();
129: }
130:
131: m_title = bOther.m_title;
132: m_description = bOther.m_description;
133:
134: m_properties = new BaseResourcePropertiesEdit();
135: m_properties.addAll(other.getProperties());
136: ((BaseResourcePropertiesEdit) m_properties)
137: .setLazy(((BaseResourceProperties) other
138: .getProperties()).isLazy());
139: }
140:
141: /**
142: * @inheritDoc
143: */
144: public String getTitle() {
145: return m_title;
146: }
147:
148: /**
149: * @inheritDoc
150: */
151: public String getDescription() {
152: return m_description;
153: }
154:
155: /**
156: * @inheritDoc
157: */
158: public Site getContainingSite() {
159: return m_site;
160: }
161:
162: /**
163: * @inheritDoc
164: */
165: public void setTitle(String title) {
166: m_title = title;
167: }
168:
169: /**
170: * @inheritDoc
171: */
172: public void setDescription(String description) {
173: m_description = description;
174: }
175:
176: /**
177: * @inheritDoc
178: */
179: public String getUrl() {
180: return null;
181: }
182:
183: /**
184: * @inheritDoc
185: */
186: public String getReference() {
187: return ((BaseSiteService) (SiteService.getInstance()))
188: .siteGroupReference(m_site.getId(), getId());
189: }
190:
191: /**
192: * @inheritDoc
193: */
194: public String getReference(String rootProperty) {
195: return getReference();
196: }
197:
198: /**
199: * @inheritDoc
200: */
201: public String getUrl(String rootProperty) {
202: return getUrl();
203: }
204:
205: /**
206: * @inheritDoc
207: */
208: public String getId() {
209: return m_id;
210: }
211:
212: /**
213: * @inheritDoc
214: */
215: public ResourceProperties getProperties() {
216: return m_properties;
217: }
218:
219: /**
220: * @inheritDoc
221: */
222: public Element toXml(Document doc, Stack stack) {
223: return null;
224: }
225:
226: /**
227: * @inheritDoc
228: */
229: public boolean isActiveEdit() {
230: return true;
231: }
232:
233: /**
234: * @inheritDoc
235: */
236: public ResourcePropertiesEdit getPropertiesEdit() {
237: return m_properties;
238: }
239:
240: /**
241: * @inheritDoc
242: */
243: public String toString() {
244: return m_title + " (" + m_id + ")";
245: }
246:
247: /**
248: * @inheritDoc
249: */
250: public boolean equals(Object obj) {
251: if (obj instanceof Group) {
252: return ((Group) obj).getId().equals(getId());
253: }
254:
255: // compare to strings as id
256: if (obj instanceof String) {
257: return ((String) obj).equals(getId());
258: }
259:
260: return false;
261: }
262:
263: /**
264: * @inheritDoc
265: */
266: public int hashCode() {
267: return getId().hashCode();
268: }
269:
270: /**
271: * @inheritDoc
272: */
273: public int compareTo(Object obj) {
274: if (!(obj instanceof Group))
275: throw new ClassCastException();
276:
277: // if the object are the same, say so
278: if (obj == this )
279: return 0;
280:
281: // start the compare by comparing their title
282: int compare = getTitle().compareTo(((Site) obj).getTitle());
283:
284: // if these are the same
285: if (compare == 0) {
286: // sort based on (unique) id
287: compare = getId().compareTo(((Group) obj).getId());
288: }
289:
290: return compare;
291: }
292:
293: /**
294: * Access (find if needed) the azg from the AuthzGroupService that implements my grouping.
295: *
296: * @return My azg.
297: */
298: protected AuthzGroup getAzg() {
299: if (m_azg == null) {
300: try {
301: m_azg = AuthzGroupService.getAuthzGroup(getReference());
302: } catch (GroupNotDefinedException e) {
303: try {
304: // create the group's azg, but don't store it yet (that happens if save is called)
305: // use a template, but assign no user any maintain role
306:
307: // find the template for the new azg
308: String groupAzgTemplate = ((BaseSiteService) (SiteService
309: .getInstance())).groupAzgTemplate(m_site);
310: AuthzGroup template = null;
311: try {
312: template = AuthzGroupService
313: .getAuthzGroup(groupAzgTemplate);
314: } catch (Exception e1) {
315: try {
316: // if the template is not defined, try the fall back template
317: template = AuthzGroupService
318: .getAuthzGroup("!group.template");
319: } catch (Exception e2) {
320: }
321: }
322:
323: m_azg = AuthzGroupService.newAuthzGroup(
324: getReference(), template, null);
325: m_azgChanged = true;
326: } catch (Throwable t) {
327: M_log.warn("getAzg: " + t);
328: }
329: }
330: }
331:
332: return m_azg;
333: }
334:
335: public void addMember(String userId, String roleId, boolean active,
336: boolean provided) {
337: m_azgChanged = true;
338: getAzg().addMember(userId, roleId, active, provided);
339: }
340:
341: public Role addRole(String id) throws RoleAlreadyDefinedException {
342: m_azgChanged = true;
343: return getAzg().addRole(id);
344: }
345:
346: public Role addRole(String id, Role other)
347: throws RoleAlreadyDefinedException {
348: m_azgChanged = true;
349: return getAzg().addRole(id, other);
350: }
351:
352: public User getCreatedBy() {
353: return getAzg().getCreatedBy();
354: }
355:
356: public Time getCreatedTime() {
357: return getAzg().getCreatedTime();
358: }
359:
360: public String getMaintainRole() {
361: return getAzg().getMaintainRole();
362: }
363:
364: public Member getMember(String userId) {
365: return getAzg().getMember(userId);
366: }
367:
368: public Set getMembers() {
369: return getAzg().getMembers();
370: }
371:
372: public User getModifiedBy() {
373: return getAzg().getModifiedBy();
374: }
375:
376: public Time getModifiedTime() {
377: return getAzg().getModifiedTime();
378: }
379:
380: public String getProviderGroupId() {
381: return getAzg().getProviderGroupId();
382: }
383:
384: public Role getRole(String id) {
385: return getAzg().getRole(id);
386: }
387:
388: public Set getRoles() {
389: return getAzg().getRoles();
390: }
391:
392: public Set getRolesIsAllowed(String function) {
393: return getAzg().getRolesIsAllowed(function);
394: }
395:
396: public Role getUserRole(String userId) {
397: return getAzg().getUserRole(userId);
398: }
399:
400: public Set getUsers() {
401: return getAzg().getUsers();
402: }
403:
404: public Set getUsersHasRole(String role) {
405: return getAzg().getUsersHasRole(role);
406: }
407:
408: public Set getUsersIsAllowed(String function) {
409: return getAzg().getUsersIsAllowed(function);
410: }
411:
412: public boolean hasRole(String userId, String role) {
413: return getAzg().hasRole(userId, role);
414: }
415:
416: public boolean isAllowed(String userId, String function) {
417: return getAzg().isAllowed(userId, function);
418: }
419:
420: public boolean isEmpty() {
421: return getAzg().isEmpty();
422: }
423:
424: public void removeMember(String userId) {
425: m_azgChanged = true;
426: getAzg().removeMember(userId);
427: }
428:
429: public void removeMembers() {
430: m_azgChanged = true;
431: getAzg().removeMembers();
432: }
433:
434: public void removeRole(String role) {
435: m_azgChanged = true;
436: getAzg().removeRole(role);
437: }
438:
439: public void removeRoles() {
440: m_azgChanged = true;
441: getAzg().removeRoles();
442: }
443:
444: public void setMaintainRole(String role) {
445: m_azgChanged = true;
446: getAzg().setMaintainRole(role);
447: }
448:
449: public void setProviderGroupId(String id) {
450: m_azgChanged = true;
451: getAzg().setProviderGroupId(id);
452: }
453:
454: public boolean keepIntersection(AuthzGroup other) {
455: boolean changed = getAzg().keepIntersection(other);
456: if (changed)
457: m_azgChanged = true;
458: return changed;
459: }
460: }
|