001: /*
002: * JBoss, Home of Professional Open Source
003: * Copyright 2005, JBoss Inc., and individual contributors as indicated
004: * by the @authors tag. See the copyright.txt in the distribution for a
005: * full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.portal.cms.security;
023:
024: import java.security.BasicPermission;
025: import java.util.HashSet;
026: import java.util.Iterator;
027: import java.util.Set;
028:
029: /** @author Sohil Shah - sohil.shah@jboss.com - Nov 28, 2006 */
030: public class Permission extends BasicPermission {
031: /**
032: *
033: */
034: private long id = 0; //unique id for this permission object in the storage (typically database)
035: private String service = null; //portal service that this permission applies to like cms etc
036: private String action = null; //action on the service that needs to be protected
037: private boolean isNegated = false;
038:
039: /**
040: *
041: */
042: private Set criteria = null;
043: private Set roleAssoc = null;
044: private Set userAssoc = null;
045:
046: /**
047: *
048: */
049: private Set roles = null; //these are information carrying fields, they are not persisted in the database
050: private Set users = null; //these are information carrying fields, they are not persisted in the database
051:
052: /**
053: *
054: *
055: */
056: public Permission() {
057: this ("null");
058: }
059:
060: /** @param name */
061: public Permission(String service) {
062: super (service);
063: this .service = service;
064: }
065:
066: /**
067: * @param name
068: * @param actions
069: */
070: public Permission(String service, String actions) {
071: super (service, actions);
072: this .service = service;
073: this .action = actions;
074: }
075:
076: /** @return */
077: public long getId() {
078: return this .id;
079: }
080:
081: /** @param id */
082: public void setId(long id) {
083: this .id = id;
084: }
085:
086: /** @return */
087: public Set getCriteria() {
088: return this .criteria;
089: }
090:
091: /** @param criteria */
092: public void setCriteria(Set criteria) {
093: this .criteria = criteria;
094: }
095:
096: /** @param criteria */
097: public void addCriteria(Criteria criteria) {
098: if (this .criteria == null) {
099: this .criteria = new HashSet();
100: }
101: this .criteria.add(criteria);
102: }
103:
104: /** @return */
105: public String getService() {
106: return this .service;
107: }
108:
109: /** @param name */
110: public void setService(String service) {
111: this .service = service;
112: }
113:
114: /** @return */
115: public String getAction() {
116: return this .action;
117: }
118:
119: /** @param action */
120: public void setAction(String action) {
121: this .action = action;
122: }
123:
124: /**
125: * Finds the specified criteria value for the criteriaId
126: *
127: * @param criteriaId
128: * @return value of the criteria
129: */
130: public String findCriteriaValue(String criteriaId) {
131: String value = null;
132: if (this .criteria != null) {
133: for (Iterator itr = this .criteria.iterator(); itr.hasNext();) {
134: Criteria cour = (Criteria) itr.next();
135: if (criteriaId.equals(cour.getName())) {
136: value = cour.getValue();
137: }
138: }
139: }
140: return value;
141: }
142:
143: /** @return */
144: public boolean isNegated() {
145: return isNegated;
146: }
147:
148: /** @param isNegated */
149: public void setNegated(boolean isNegated) {
150: this .isNegated = isNegated;
151: }
152:
153: /** @return */
154: public Set getRoleAssoc() {
155: return roleAssoc;
156: }
157:
158: /** @param roleAssoc */
159: public void setRoleAssoc(Set roleAssoc) {
160: this .roleAssoc = roleAssoc;
161: }
162:
163: /** @param roleAssoc */
164: public void addRoleAssoc(PermRoleAssoc roleAssoc) {
165: if (this .roleAssoc == null) {
166: this .roleAssoc = new HashSet();
167: }
168: this .roleAssoc.add(roleAssoc);
169: }
170:
171: /** @return */
172: public Set getRoleAssocIds() {
173: Set ids = new HashSet();
174: if (this .roleAssoc != null) {
175: for (Iterator itr = this .roleAssoc.iterator(); itr
176: .hasNext();) {
177: PermRoleAssoc cour = (PermRoleAssoc) itr.next();
178: ids.add(cour.getRoleId());
179: }
180: }
181: return ids;
182: }
183:
184: /** @return */
185: public Set getUserAssoc() {
186: return userAssoc;
187: }
188:
189: /** @param userAssoc */
190: public void setUserAssoc(Set userAssoc) {
191: this .userAssoc = userAssoc;
192: }
193:
194: /** @param userAssoc */
195: public void addUserAssoc(PermUserAssoc userAssoc) {
196: if (this .userAssoc == null) {
197: this .userAssoc = new HashSet();
198: }
199: this .userAssoc.add(userAssoc);
200: }
201:
202: /** @return */
203: public Set getUserAssocIds() {
204: Set ids = new HashSet();
205: if (this .userAssoc != null) {
206: for (Iterator itr = this .userAssoc.iterator(); itr
207: .hasNext();) {
208: PermUserAssoc cour = (PermUserAssoc) itr.next();
209: ids.add(cour.getUserId());
210: }
211: }
212: return ids;
213: }
214:
215: /** @return */
216: public Set getRoles() {
217: return roles;
218: }
219:
220: /** @param roles */
221: public void setRoles(Set roles) {
222: this .roles = roles;
223: }
224:
225: /** @return */
226: public Set getUsers() {
227: return users;
228: }
229:
230: /** @param users */
231: public void setUsers(Set users) {
232: this .users = users;
233: }
234:
235: /**
236: *
237: */
238: public String toString() {
239: StringBuffer buffer = new StringBuffer();
240:
241: buffer.append("-----------------------------\n");
242: buffer.append("ID=" + this .id + "\n");
243: buffer.append("Service=" + this .service + "\n");
244: buffer.append("Action=" + this .action + "\n");
245: buffer.append("Negated=" + this .isNegated + "\n");
246:
247: //print role association
248: if (this .roleAssoc != null) {
249: for (Iterator itr = this .roleAssoc.iterator(); itr
250: .hasNext();) {
251: PermRoleAssoc cour = (PermRoleAssoc) itr.next();
252: buffer.append("Role =" + cour.getRoleId() + "\n");
253: }
254: }
255:
256: //print user association
257: if (this .userAssoc != null) {
258: for (Iterator itr = this .userAssoc.iterator(); itr
259: .hasNext();) {
260: PermUserAssoc cour = (PermUserAssoc) itr.next();
261: buffer.append("User =" + cour.getUserId() + "\n");
262: }
263: }
264:
265: //print criteria
266: if (this .criteria != null) {
267: for (Iterator itr = this .criteria.iterator(); itr.hasNext();) {
268: Criteria cour = (Criteria) itr.next();
269: buffer.append("Criteria =" + cour.getName() + ","
270: + cour.getValue() + "\n");
271: }
272: }
273:
274: buffer.append("-----------------------------\n");
275:
276: return buffer.toString();
277: }
278:
279: /**
280: *
281: */
282: public boolean equals(Object obj) {
283: boolean equals = false;
284: if (obj instanceof Permission) {
285: Permission input = (Permission) obj;
286: if (input.id == this .id) {
287: equals = true;
288: }
289: }
290: return equals;
291: }
292: }
|