01: /*
02: jGuard is a security framework based on top of jaas (java authentication and authorization security).
03: it is written for web applications, to resolve simply, access control problems.
04: version $Name: $
05: http://sourceforge.net/projects/jguard/
06:
07: Copyright (C) 2004 Charles GAY
08:
09: This library is free software; you can redistribute it and/or
10: modify it under the terms of the GNU Lesser General Public
11: License as published by the Free Software Foundation; either
12: version 2.1 of the License, or (at your option) any later version.
13:
14: This library is distributed in the hope that it will be useful,
15: but WITHOUT ANY WARRANTY; without even the implied warranty of
16: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17: Lesser General Public License for more details.
18:
19: You should have received a copy of the GNU Lesser General Public
20: License along with this library; if not, write to the Free Software
21: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22:
23:
24: jGuard project home page:
25: http://sourceforge.net/projects/jguard/
26:
27: */
28: package net.sf.jguard.core.authorization.permissions;
29:
30: import java.security.Permission;
31: import java.util.Date;
32:
33: /**
34: * decorate java.security.Permission subclasses by defining a duration
35: * for their validity.
36: * @author <a href="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
37: */
38: public class DurationDecorator extends Permission {
39:
40: private static final long serialVersionUID = 3085444057980849140L;
41: private Permission permission;
42: private Date begin;
43: private Date end;
44:
45: /**
46: *
47: * @param p permission to decorate
48: * @param begin begin date of the duration: can be <i>null</i> if there is no begin
49: * @param end end date of the duration : can be <i>null</i> if there is no end
50: */
51: public DurationDecorator(Permission p, Date start, Date end) {
52: super (p.getName());
53: this .permission = p;
54: this .begin = start;
55: this .end = end;
56: }
57:
58: public boolean equals(Object obj) {
59: if (obj instanceof DurationDecorator) {
60: DurationDecorator duration = (DurationDecorator) obj;
61: if (this .permission.getName().equals(duration.getName())
62: && this .permission.getActions().equals(
63: duration.getActions())) {
64: return true;
65: }
66: }
67: return false;
68: }
69:
70: public String getActions() {
71: return permission.getActions();
72: }
73:
74: public int hashCode() {
75: int hashCode = permission.hashCode();
76: if (begin != null) {
77: hashCode += begin.hashCode();
78: }
79: if (end != null) {
80: hashCode += end.hashCode();
81: }
82: return hashCode;
83:
84: }
85:
86: public boolean implies(Permission permission) {
87: Date now = new Date();
88: if (begin != null && now.before(begin)) {
89: return false;
90: }
91: if (end != null && now.after(end)) {
92: return false;
93: }
94: //duration check succeed,
95: //so we check in a classic way the permission
96: return this.permission.implies(permission);
97: }
98:
99: }
|