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  
29  package net.sf.jguard.jee.extras.dwr2;
30  
31  import java.security.BasicPermission;
32  import java.security.Permission;
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 2.x.
39   * @author <a href="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
40   */
41  public class DWR2Permission extends BasicPermission{
42  
43      private static final long serialVersionUID = -5380977242062636050L;
44      
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 DWR2Permission(String name, String actions) {
64  		super(name);
65  		this.name = name;
66  		this.actions = actions;
67  		if(name==null){
68  			throw new IllegalArgumentException(" 'name' must not be null ");
69  		}
70  		String[] actionsArray = (String[])actions.split(",");
71  		if(actionsArray.length!=3){
72  			throw new IllegalArgumentException(" DWR1Permission must have 3 actions : creatorClassName,className and methodName ");
73  		}
74  		creatorClassName = actionsArray[0];
75  		className = actionsArray[1];
76  		methodName = actionsArray[2];
77  		if(creatorClassName==null ||className==null || methodName==null){
78  			throw new IllegalArgumentException(" one or more of these arguments are 'null' : \n name="+name+ "creatorClassName ="+creatorClassName+" className= "+className);
79  		}
80  	}
81  	
82  	public int hashCode(){
83  		return name.hashCode()+creatorClassName.hashCode()+className.hashCode()+methodName.hashCode();
84  	}
85  	
86  	public boolean equals(Object object){
87  		if (object instanceof DWR2Permission == false) {
88  			 return false;
89  		 }else{
90  			DWR2Permission dwrPerm = (DWR2Permission) object;
91  			if(name.equals(dwrPerm.getName())||!creatorClassName.equals(dwrPerm.getCreatorClassName())||!className.equals(dwrPerm.getClassName())||!methodName.equals(dwrPerm.getMethodName())){
92  				return false;
93  			}else{
94  				//case where creatorClassName, AND className AND methodName aer equals.
95  				return true;
96  			}
97  		 }
98  	}
99  
100 	
101 	 public boolean implies(Permission p) {
102 		 if (p instanceof DWR2Permission == false) {
103 			 return false;
104 		 }else{
105 			DWR2Permission dwrPerm = (DWR2Permission) p;
106 			if(!creatorClassName.equals(dwrPerm.getCreatorClassName())||!className.equals(dwrPerm.getClassName())||!methodName.equals(dwrPerm.getMethodName())){
107 				return false;
108 			}else{
109 				//case where creatorClassName, AND className AND methodName aer equals.
110 				return true;
111 			}
112 		 }
113 		
114 	 }
115 
116 	public String getClassName() {
117 		return className;
118 	}
119 
120 	public String getMethodName() {
121 		return methodName;
122 	}
123 
124 	public String getCreatorClassName() {
125 		return creatorClassName;
126 	}
127 
128 	public String toString(){
129 		StringBuffer buffer = new StringBuffer();
130 		buffer.append("name=");
131 		buffer.append(name);
132 		buffer.append(",");
133 		buffer.append("creatorClassName=");
134 		buffer.append(creatorClassName);
135 		buffer.append(",");
136 		buffer.append("className=");
137 		buffer.append(className);
138 		buffer.append(",");
139 		buffer.append("methodName=");
140 		buffer.append(methodName);
141 		return buffer.toString();
142 		
143 	}
144 	
145 	/**
146 	 * return an enmpy JGPermissionCollection.
147 	 *
148 	 * @return empty JGPermissionCollection
149 	 */
150 	public java.security.PermissionCollection newPermissionCollection() {
151 		return new JGPositivePermissionCollection();
152 	}
153 
154 	public String getActions() {
155 		return actions;
156 	}
157 
158 }