View Javadoc

1   /*
2   jGuard is a security framework based on top of jaas (java authentication and authorization security).
3   it is written for web applications, to resolve simply, access control problems.
4   version $Name:  $
5   http://sourceforge.net/projects/jguard/
6   
7   Copyright (C) 2004  Charles GAY
8   
9   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.jee.extras.dwr1;
29  
30  import java.security.BasicPermission;
31  import java.security.Permission;
32  
33  import net.sf.jguard.core.authorization.permissions.JGPositivePermissionCollection;
34  
35  /**
36   * represents the permission to instantiate some methods of beans from javascript
37   * via <a href="http://dwr.dev.java.net/">DWR</a>.
38   * this implementation works in DWR 1.x.
39   * @author <a href="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
40   *
41   */
42  public final class DWR1Permission extends BasicPermission {
43  
44  	private static final long serialVersionUID = -5380977262062636050L;
45  	private String name = null;
46          private String creatorClassName = null; 
47  	private String className = null;
48  	private String methodName = null;
49  	private String actions = null;
50      
51  	/**
52  	 * this permission requires a 'name' and an 'actions' string.
53  	 * this actions string must contains 3 actions divided by ','.
54  	 * first action is :'creatorClassName' : the class name of the creator.
55  	 * second action is 'className':the Class which will be instantiated by the Creator.
56  	 * third action is 'methodName': the Class which will be instantiated by the Creator.
57  	 * multiple method can have the same name, but it seems that DWR make no distinction between them.
58  	 * the method used when multiple methods ahve got thze same name is not clear (to me). 
59  	 * @param name
60  	 * @param actions 
61  	 * 
62  	 */
63  	public DWR1Permission(String name, String actions) {
64                  //TODO make distinction between permission at compile time and permission at execution
65  		super(name);
66  		this.name = name;
67  		this.actions = actions;
68  		if(name==null){
69  			throw new IllegalArgumentException(" 'name' must not be null ");
70  		}
71  		String[] actionsArray = (String[])actions.split(",");
72  		if(actionsArray.length!=3){
73  			throw new IllegalArgumentException(" DWR1Permission must have 3 actions : creatorClassName,className and methodName ");
74  		}
75  		creatorClassName = actionsArray[0];
76  		className = actionsArray[1];
77  		methodName = actionsArray[2];
78  		if(creatorClassName==null ||className==null || methodName==null){
79  			throw new IllegalArgumentException(" one or more of these arguments are 'null' : \n name="+name+ "creatorClassName ="+creatorClassName+" className= "+className);
80  		}
81  	}
82  	
83  	public int hashCode(){
84  		return name.hashCode()+creatorClassName.hashCode()+className.hashCode()+methodName.hashCode();
85  	}
86  	
87  	public boolean equals(Object object){
88  		if (object instanceof DWR1Permission == false) {
89  			 return false;
90  		 }else{
91  			DWR1Permission dwrPerm = (DWR1Permission) object;
92  			if(name.equals(dwrPerm.getName())||!creatorClassName.equals(dwrPerm.getCreatorClassName())||!className.equals(dwrPerm.getClassName())||!methodName.equals(dwrPerm.getMethodName())){
93  				return false;
94  			}else{
95  				//case where creatorClassName, AND className AND methodName aer equals.
96  				return true;
97  			}
98  		 }
99  	}
100 
101 	
102 	 public boolean implies(Permission p) {
103 		 if (p instanceof DWR1Permission == false) {
104 			 return false;
105 		 }else{
106 			DWR1Permission dwrPerm = (DWR1Permission) p;
107 			if(!creatorClassName.equals(dwrPerm.getCreatorClassName())||!className.equals(dwrPerm.getClassName())||!methodName.equals(dwrPerm.getMethodName())){
108 				return false;
109 			}else{
110 				//case where creatorClassName, AND className AND methodName aer equals.
111 				return true;
112 			}
113 		 }
114 		
115 	 }
116 
117 	public String getClassName() {
118 		return className;
119 	}
120 
121 	public String getMethodName() {
122 		return methodName;
123 	}
124 
125 	public String getCreatorClassName() {
126 		return creatorClassName;
127 	}
128 
129 	public String toString(){
130 		StringBuffer buffer = new StringBuffer();
131 		buffer.append("name=");
132 		buffer.append(name);
133 		buffer.append(",");
134 		buffer.append("creatorClassName=");
135 		buffer.append(creatorClassName);
136 		buffer.append(",");
137 		buffer.append("className=");
138 		buffer.append(className);
139 		buffer.append(",");
140 		buffer.append("methodName=");
141 		buffer.append(methodName);
142 		return buffer.toString();
143 		
144 	}
145 	
146 	/**
147 	 * return an enmpy JGPermissionCollection.
148 	 *
149 	 * @return empty JGPermissionCollection
150 	 */
151 	public java.security.PermissionCollection newPermissionCollection() {
152 		return new JGPositivePermissionCollection();
153 	}
154 
155 	public String getActions() {
156 		return actions;
157 	}
158 }