1 package org.apache.turbine.util.template;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.turbine.Turbine;
23 import org.apache.turbine.TurbineConstants;
24 import org.apache.turbine.om.security.Permission;
25 import org.apache.turbine.om.security.Role;
26 import org.apache.turbine.services.security.TurbineSecurity;
27 import org.apache.turbine.services.template.TurbineTemplate;
28 import org.apache.turbine.util.RunData;
29
30 /***
31 * Utility class to help check for proper authorization when using
32 * template screens. Sample usages:
33 *
34 * <p><pre><code>
35 * TemplateSecurityCheck secCheck = new TemplateSecurityCheck( data );
36 * secCheck.setMessage( "Sorry, you do not have permission to " +
37 * "access this area." );
38 * secCheck.setFailTemplate("login.wm");
39 * if ( !secCheck.hasRole("ADMIN") )
40 * return;
41 * </pre></code>
42 *
43 * @author <a href="mbryson@mont.mindspring.com">Dave Bryson</a>
44 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
45 * @version $Id: TemplateSecurityCheck.java 534527 2007-05-02 16:10:59Z tv $
46 */
47 public class TemplateSecurityCheck
48 {
49 private String message =
50 "Sorry, you do not have permission to access this area.";
51 private String failScreen = TurbineTemplate.getDefaultScreen();
52 private String failTemplate;
53 private RunData data = null;
54
55 /***
56 * Constructor.
57 *
58 * @param data A Turbine RunData object.
59 * @param message A String with the message to display upon
60 * failure.
61 */
62 public TemplateSecurityCheck(RunData data, String message)
63 {
64 this.data = data;
65 this.message = message;
66 }
67
68 /***
69 * Generic Constructor.
70 *
71 * @param data A Turbine RunData object.
72 */
73 public TemplateSecurityCheck(RunData data)
74 {
75 this.data = data;
76 }
77
78 /***
79 * Does the User have this role?
80 *
81 * @param role The role to be checked.
82 * @return Whether the user has the role.
83 * @exception Exception Trouble validating.
84 */
85 public boolean hasRole(Role role)
86 throws Exception
87 {
88 if (!checkLogin())
89 {
90 return false;
91 }
92
93 if (data.getACL() == null || !data.getACL().hasRole(role))
94 {
95 data.setScreen(getFailScreen());
96 data.getTemplateInfo().setScreenTemplate(getFailTemplate());
97 data.setMessage(getMessage());
98 return false;
99 }
100
101 return true;
102 }
103
104 /***
105 * Does the User have this permission?
106 *
107 * @param permission The permission to be checked.
108 * @return Whether the user has the permission.
109 * @exception Exception Trouble validating.
110 */
111 public boolean hasPermission(Permission permission)
112 throws Exception
113 {
114 boolean value = true;
115 if (data.getACL() == null || !data.getACL().hasPermission(permission))
116 {
117 data.setScreen(getFailScreen());
118 data.getTemplateInfo().setScreenTemplate(getFailTemplate());
119 data.setMessage(getMessage());
120 value = false;
121 }
122
123 return value;
124 }
125
126 /***
127 * Check that the user has logged in.
128 *
129 * @return True if user has logged in.
130 * @exception Exception, a generic exception.
131 */
132 public boolean checkLogin()
133 throws Exception
134 {
135 boolean value = true;
136
137
138 if (!TurbineSecurity.isAnonymousUser(data.getUser())
139 && !data.getUser().hasLoggedIn())
140 {
141 data.setMessage(Turbine.getConfiguration()
142 .getString(TurbineConstants.LOGIN_MESSAGE));
143
144 data.getTemplateInfo().setScreenTemplate(getFailTemplate());
145 value = false;
146 }
147
148 return value;
149 }
150
151 /***
152 * Set the message that should be displayed. This is initialized
153 * in the constructor.
154 *
155 * @param v A String with the message that should be displayed.
156 */
157 public void setMessage(String v)
158 {
159 this.message = v;
160 }
161
162 /***
163 * Get the message that should be displayed. This is initialized
164 * in the constructor.
165 *
166 * @return A String with the message that should be displayed.
167 */
168 public String getMessage()
169 {
170 return message;
171 }
172
173 /***
174 * Get the value of failScreen.
175 *
176 * @return A String with the value of failScreen.
177 */
178 public String getFailScreen()
179 {
180 return failScreen;
181 }
182
183 /***
184 * Set the value of failScreen.
185 *
186 * @param v A String with the value of failScreen.
187 */
188 public void setFailScreen(String v)
189 {
190 this.failScreen = v;
191 }
192
193 /***
194 * Get the value of failTemplate.
195 *
196 * @return A String with the value of failTemplate.
197 */
198 public String getFailTemplate()
199 {
200 return failTemplate;
201 }
202
203 /***
204 * Set the value of failTemplate.
205 *
206 * @param v A String with the value of failTemplate.
207 */
208 public void setFailTemplate(String v)
209 {
210 this.failTemplate = v;
211 }
212 }