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.authz.impl;
021:
022: import java.util.Collection;
023: import java.util.HashSet;
024: import java.util.Iterator;
025: import java.util.Set;
026: import java.util.Stack;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.sakaiproject.authz.api.AuthzGroup;
031: import org.sakaiproject.authz.api.Role;
032: import org.sakaiproject.util.StringUtil;
033: import org.sakaiproject.util.Xml;
034: import org.w3c.dom.Document;
035: import org.w3c.dom.Element;
036: import org.w3c.dom.Node;
037: import org.w3c.dom.NodeList;
038:
039: /**
040: * <p>
041: * BaseRole is an implementation of the AuthzGroup API Role.
042: * </p>
043: */
044: public class BaseRole implements Role {
045: /** Our log (commons). */
046: private static Log M_log = LogFactory.getLog(BaseRole.class);
047:
048: /** A fixed class serian number. */
049: private static final long serialVersionUID = 1L;
050:
051: /** The role id. */
052: protected String m_id = null;
053:
054: /** The locks that make up this. */
055: protected Set m_locks = null;
056:
057: /** The role description. */
058: protected String m_description = null;
059:
060: /** Whether this is a provider-only role */
061: protected boolean m_providerOnly;
062:
063: /** Active flag. */
064: protected boolean m_active = false;
065:
066: /**
067: * Construct.
068: *
069: * @param id
070: * The role id.
071: */
072: public BaseRole(String id) {
073: m_id = id;
074: m_locks = new HashSet();
075: }
076:
077: /**
078: * Construct as a copy
079: *
080: * @param id
081: * The role id.
082: * @param other
083: * The role to copy.
084: */
085: public BaseRole(String id, Role other) {
086: m_id = id;
087: m_description = ((BaseRole) other).m_description;
088: m_providerOnly = ((BaseRole) other).m_providerOnly;
089: m_locks = new HashSet();
090: m_locks.addAll(((BaseRole) other).m_locks);
091: }
092:
093: /**
094: * Construct from information in XML.
095: *
096: * @param el
097: * The XML DOM Element definining the role.
098: */
099: public BaseRole(Element el, AuthzGroup azGroup) {
100: m_locks = new HashSet();
101: m_id = StringUtil.trimToNull(el.getAttribute("id"));
102:
103: m_description = StringUtil.trimToNull(el
104: .getAttribute("description"));
105: if (m_description == null) {
106: m_description = StringUtil.trimToNull(Xml.decodeAttribute(
107: el, "description-enc"));
108: }
109:
110: if ("true".equalsIgnoreCase(el.getAttribute("provider-only")))
111: m_providerOnly = true;
112:
113: // the children (abilities)
114: NodeList children = el.getChildNodes();
115: final int length = children.getLength();
116: for (int i = 0; i < length; i++) {
117: Node child = children.item(i);
118: if (child.getNodeType() != Node.ELEMENT_NODE)
119: continue;
120: Element element = (Element) child;
121:
122: // look for role | lock ability
123: if (element.getTagName().equals("ability")) {
124: String roleId = StringUtil.trimToNull(element
125: .getAttribute("role"));
126: String lock = StringUtil.trimToNull(element
127: .getAttribute("lock"));
128:
129: if (roleId != null) {
130: M_log.warn("(el): nested role: " + m_id + " "
131: + roleId);
132: }
133:
134: m_locks.add(lock);
135: }
136: }
137: }
138:
139: /**
140: * {@inheritDoc}
141: */
142: public Element toXml(Document doc, Stack stack) {
143: Element role = doc.createElement("role");
144:
145: if (stack.isEmpty()) {
146: doc.appendChild(role);
147: } else {
148: ((Element) stack.peek()).appendChild(role);
149: }
150:
151: stack.push(role);
152:
153: role.setAttribute("id", getId());
154:
155: // encode the description
156: if (m_description != null)
157: Xml.encodeAttribute(role, "description-enc", m_description);
158:
159: // encode the provider only flag
160: if (m_providerOnly)
161: Xml.encodeAttribute(role, "provider-only", "true");
162:
163: // locks
164: for (Iterator a = m_locks.iterator(); a.hasNext();) {
165: String lock = (String) a.next();
166:
167: Element element = doc.createElement("ability");
168: role.appendChild(element);
169: element.setAttribute("lock", lock);
170: }
171:
172: stack.pop();
173:
174: return role;
175: }
176:
177: /**
178: * Enable editing.
179: */
180: protected void activate() {
181: m_active = true;
182: }
183:
184: /**
185: * Check to see if the azGroup is still active, or has already been closed.
186: *
187: * @return true if the azGroup is active, false if it's been closed.
188: */
189: public boolean isActiveEdit() {
190: return m_active;
191: }
192:
193: /**
194: * Close the azGroup object - it cannot be used after this.
195: */
196: protected void closeEdit() {
197: m_active = false;
198: }
199:
200: /**
201: * {@inheritDoc}
202: */
203: public String getId() {
204: return m_id;
205: }
206:
207: /**
208: * {@inheritDoc}
209: */
210: public String getDescription() {
211: return m_description;
212: }
213:
214: /**
215: * {@inheritDoc}
216: */
217: public boolean isProviderOnly() {
218: return m_providerOnly;
219: }
220:
221: /**
222: * {@inheritDoc}
223: */
224: public boolean isAllowed(String lock) {
225: return m_locks.contains(lock);
226: }
227:
228: /**
229: * {@inheritDoc}
230: */
231: public Set getAllowedFunctions() {
232: Set rv = new HashSet();
233: rv.addAll(m_locks);
234: return rv;
235: }
236:
237: /**
238: * {@inheritDoc}
239: */
240: public void setDescription(String description) {
241: m_description = StringUtil.trimToNull(description);
242: }
243:
244: /**
245: * {@inheritDoc}
246: */
247: public void setProviderOnly(boolean providerOnly) {
248: m_providerOnly = providerOnly;
249: }
250:
251: /**
252: * {@inheritDoc}
253: */
254: public void allowFunction(String lock) {
255: m_locks.add(lock);
256: }
257:
258: /**
259: * {@inheritDoc}
260: */
261: public void allowFunctions(Collection locks) {
262: m_locks.addAll(locks);
263: }
264:
265: /**
266: * {@inheritDoc}
267: */
268: public void disallowFunction(String lock) {
269: m_locks.remove(lock);
270: }
271:
272: /**
273: * {@inheritDoc}
274: */
275: public void disallowFunctions(Collection locks) {
276: m_locks.removeAll(locks);
277: }
278:
279: /**
280: * {@inheritDoc}
281: */
282: public boolean allowsNoFunctions() {
283: return m_locks.isEmpty();
284: }
285:
286: /**
287: * {@inheritDoc}
288: */
289: public void disallowAll() {
290: m_locks.clear();
291: }
292:
293: /**
294: * {@inheritDoc}
295: */
296: public int compareTo(Object obj) {
297: if (!(obj instanceof Role))
298: throw new ClassCastException();
299:
300: // if the object are the same, say so
301: if (obj == this )
302: return 0;
303:
304: // sort based on (unique) id
305: int compare = getId().compareTo(((Role) obj).getId());
306:
307: return compare;
308: }
309:
310: /**
311: * {@inheritDoc}
312: */
313: public boolean equals(Object obj) {
314: if (!(obj instanceof Role))
315: return false;
316:
317: return ((Role) obj).getId().equals(getId());
318: }
319:
320: /**
321: * {@inheritDoc}
322: */
323: public int hashCode() {
324: return getId().hashCode();
325: }
326: }
|