001: /*
002: * (C) Copyright 2000 - 2006 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019: package com.nabhinc.portal.model;
020:
021: import javax.servlet.ServletException;
022: import javax.servlet.http.HttpServletRequest;
023: import javax.xml.bind.Unmarshaller;
024: import javax.xml.bind.annotation.XmlElement;
025: import javax.xml.bind.annotation.XmlTransient;
026:
027: import com.nabhinc.condition.Condition;
028: import com.nabhinc.condition.ConditionFactory;
029: import com.nabhinc.condition.ConditionHolder;
030: import com.nabhinc.core.Group;
031: import com.nabhinc.core.Constants;
032: import com.nabhinc.util.EncryptionUtil;
033: import com.nabhinc.util.StringUtil;
034:
035: /**
036: *
037: *
038: * @author Padmanabh Dabke
039: * (c) 2006 Nabh Information Systems, Inc. All Rights Reserved.
040: */
041: public abstract class BaseProtectable implements Protectable, Group {
042:
043: @XmlElement(name="password")
044: private String bpPassword = null;
045:
046: @XmlTransient
047: private Condition bpViewPermission = null;
048:
049: @XmlTransient
050: private Condition bpEditPermission = ConditionFactory.OWNER_CONDITION;
051:
052: @XmlTransient
053: private Condition bpDeletePermission = ConditionFactory.OWNER_CONDITION;
054:
055: @XmlTransient
056: private Condition bpAdminPermission = ConditionFactory.OWNER_CONDITION;
057:
058: @XmlTransient
059: private Condition bpCreatePsitePermission = ConditionFactory.OWNER_CONDITION;
060:
061: @XmlElement(name="view-condition")
062: private ConditionHolder bpViewPermHolder = null;
063:
064: @XmlElement(name="edit-condition")
065: private ConditionHolder bpEditPermHolder = null;
066:
067: @XmlElement(name="delete-condition")
068: private ConditionHolder bpDeletePermHolder = null;
069:
070: @XmlElement(name="admin-condition")
071: private ConditionHolder bpAdminPermHolder = null;
072:
073: @XmlElement(name="create-psite-condition")
074: private ConditionHolder bpCreatePsitePermHolder = null;
075:
076: private transient boolean bpViewLoginRequired = false;
077:
078: private transient boolean bpEditLoginRequired = false;
079:
080: private transient boolean bpDeleteLoginRequired = false;
081:
082: private transient boolean bpAdminLoginRequired = false;
083:
084: private transient boolean bpCreatePsiteLoginRequired = false;
085:
086: public abstract String getOwnerName();
087:
088: @XmlTransient
089: public String getPassword() {
090: return bpPassword;
091: }
092:
093: public void setPassword(String p) {
094: if (StringUtil.isNotNullOrEmpty(p))
095: p = EncryptionUtil.encrypt(p, "MD5");
096: else
097: p = null;
098: this .bpPassword = p;
099: }
100:
101: public boolean checkPassword(String password) {
102: if (this .bpPassword == null) {
103: return true;
104: } else {
105: if (EncryptionUtil.encrypt(password, "MD5").equals(
106: this .bpPassword))
107: return true;
108: else
109: return false;
110: }
111: }
112:
113: public boolean isPasswordProtected() {
114: return this .bpPassword != null;
115: }
116:
117: @XmlTransient
118: public Condition getEditPermission() {
119: return bpEditPermission;
120: }
121:
122: public void setEditPermission(Condition bpEditPermission) {
123: this .bpEditPermission = bpEditPermission;
124: this .bpEditPermHolder = new ConditionHolder(bpEditPermission);
125: if (bpEditPermission == null)
126: bpEditLoginRequired = false;
127: else
128: bpEditLoginRequired = bpEditPermission.requiresLogin();
129: }
130:
131: @XmlTransient
132: public Condition getAdminPermission() {
133: return bpAdminPermission;
134: }
135:
136: public void setAdminPermission(Condition bpAdminPermission) {
137: this .bpAdminPermission = bpAdminPermission;
138: this .bpAdminPermHolder = new ConditionHolder(bpAdminPermission);
139: if (bpAdminPermission == null)
140: bpAdminLoginRequired = false;
141: else
142: bpAdminLoginRequired = bpAdminPermission.requiresLogin();
143: }
144:
145: @XmlTransient
146: public Condition getViewPermission() {
147: return bpViewPermission;
148: }
149:
150: public void setViewPermission(Condition bpViewPermission) {
151: this .bpViewPermission = bpViewPermission;
152: this .bpViewPermHolder = new ConditionHolder(bpViewPermission);
153: if (bpViewPermission == null)
154: bpViewLoginRequired = false;
155: else
156: bpViewLoginRequired = bpViewPermission.requiresLogin();
157: }
158:
159: @XmlTransient
160: public Condition getDeletePermission() {
161: return bpDeletePermission;
162: }
163:
164: public void setDeletePermission(Condition bpDeletePermission) {
165: this .bpDeletePermission = bpDeletePermission;
166: this .bpDeletePermHolder = new ConditionHolder(
167: bpDeletePermission);
168: }
169:
170: @XmlTransient
171: public Condition getCreatePsitePermission() {
172: return bpCreatePsitePermission;
173: }
174:
175: public void setCreatePsitePermission(
176: Condition bpCreatePsitePermission) {
177: this .bpCreatePsitePermission = bpCreatePsitePermission;
178: this .bpCreatePsitePermHolder = new ConditionHolder(
179: bpCreatePsitePermission);
180: if (bpCreatePsitePermission == null)
181: bpCreatePsiteLoginRequired = false;
182: else
183: bpCreatePsiteLoginRequired = bpCreatePsitePermission
184: .requiresLogin();
185: }
186:
187: public boolean isEditable(HttpServletRequest request)
188: throws ServletException {
189: if (request.isUserInRole(Constants.ADMIN_ROLE)
190: || getOwnerName().equals(request.getRemoteUser()))
191: return true;
192: if (this .bpEditPermission == null)
193: return true;
194: return this .bpEditPermission.isSatisfied(request, this );
195: }
196:
197: public boolean isAdminable(HttpServletRequest request)
198: throws ServletException {
199: if (request.isUserInRole(Constants.ADMIN_ROLE)
200: || getOwnerName().equals(request.getRemoteUser()))
201: return true;
202: if (this .bpAdminPermission == null)
203: return true;
204: return this .bpAdminPermission.isSatisfied(request, this );
205: }
206:
207: public boolean isViewable(HttpServletRequest request)
208: throws ServletException {
209: if (request.isUserInRole(Constants.ADMIN_ROLE)
210: || getOwnerName().equals(request.getRemoteUser()))
211: return true;
212: if (this .bpViewPermission == null)
213: return true;
214: return this .bpViewPermission.isSatisfied(request, this );
215: }
216:
217: public boolean isDeletable(HttpServletRequest request)
218: throws ServletException {
219: if (request.isUserInRole(Constants.ADMIN_ROLE)
220: || getOwnerName().equals(request.getRemoteUser()))
221: return true;
222: if (this .bpDeletePermission == null)
223: return true;
224: return this .bpDeletePermission.isSatisfied(request, this );
225: }
226:
227: public boolean isPsiteCreatable(HttpServletRequest request)
228: throws ServletException {
229: if (request.isUserInRole(Constants.ADMIN_ROLE))
230: return true;
231: if (this .bpCreatePsitePermission == null)
232: return true;
233: return this .bpCreatePsitePermission.isSatisfied(request, this );
234: }
235:
236: public void afterUnmarshal(Unmarshaller um, Object parent) {
237:
238: if (bpViewPermHolder != null)
239: bpViewPermission = bpViewPermHolder.condition;
240: if (bpEditPermHolder != null)
241: bpEditPermission = bpEditPermHolder.condition;
242: if (bpDeletePermHolder != null)
243: bpDeletePermission = bpDeletePermHolder.condition;
244: if (bpAdminPermHolder != null)
245: bpAdminPermission = bpAdminPermHolder.condition;
246: if (bpCreatePsitePermHolder != null)
247: bpCreatePsitePermission = bpCreatePsitePermHolder.condition;
248:
249: if (bpViewPermission != null)
250: bpViewLoginRequired = bpViewPermission.requiresLogin();
251: if (bpEditPermission != null)
252: bpEditLoginRequired = bpEditPermission.requiresLogin();
253: if (bpDeletePermission != null)
254: bpDeleteLoginRequired = bpDeletePermission.requiresLogin();
255: if (bpAdminPermission != null)
256: bpAdminLoginRequired = bpAdminPermission.requiresLogin();
257: if (bpCreatePsitePermission != null)
258: bpCreatePsiteLoginRequired = bpCreatePsitePermission
259: .requiresLogin();
260: }
261:
262: public boolean requiresLoginForView() {
263: return bpViewLoginRequired;
264: }
265:
266: public boolean requiresLoginForEdit() {
267: return bpEditLoginRequired;
268: }
269:
270: public boolean requiresLoginForDelete() {
271: return bpDeleteLoginRequired;
272: }
273:
274: public boolean requiresLoginForAdmin() {
275: return bpAdminLoginRequired;
276: }
277:
278: public boolean requiresLoginForCreatePsite() {
279: return bpCreatePsiteLoginRequired;
280: }
281:
282: }
|