01: /*
02: * This program is free software; you can redistribute it and/or modify
03: * it under the terms of the GNU General Public License as published by
04: * the Free Software Foundation; either version 2 of the License, or
05: * (at your option) any later version.
06: *
07: * This program is distributed in the hope that it will be useful,
08: * but WITHOUT ANY WARRANTY; without even the implied warranty of
09: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10: * GNU Library General Public License for more details.
11: *
12: * You should have received a copy of the GNU General Public License
13: * along with this program; if not, write to the Free Software
14: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15: */
16: package dlog4j.security;
17:
18: import javax.servlet.http.HttpServletRequest;
19: import javax.servlet.jsp.JspException;
20: import javax.servlet.jsp.tagext.TagSupport;
21:
22: import dlog4j.formbean.UserForm;
23: import web.security.Operation;
24: import web.security.Privilege;
25: import web.security.Range;
26: import web.security.Resource;
27: import web.security.impl.PrivilegeImpl;
28:
29: /**
30: * 用来在页面上进行判断用户时候有访问的权限
31: * @author Winter Lau
32: */
33: public class CanAccessTag extends TagSupport {
34:
35: protected String resource;
36: protected String operation;
37: protected String range;
38:
39: public int doStartTag() throws JspException {
40: UserForm user = UserForm
41: .getLoginUser((HttpServletRequest) pageContext
42: .getRequest());
43: if (user == null)
44: return SKIP_BODY;
45: DlogRole role = user.getRole();
46: if (role == null)
47: return SKIP_BODY;
48: return role.canDo(getPrivilege()) ? EVAL_BODY_INCLUDE
49: : SKIP_BODY;
50: }
51:
52: protected Privilege getPrivilege() {
53: try {
54: SecurityConfig sc = SecurityConfig.getConfig();
55: Resource res = sc.getResourceByName(resource);
56: Operation opt = sc.getOperationByName(operation);
57: Range rng = sc.getRangeByName(range);
58: return new PrivilegeImpl(res, opt, rng);
59: } catch (Exception e) {
60: pageContext.getServletContext().log("", e);
61: }
62: return null;
63: }
64:
65: public String getOperation() {
66: return operation;
67: }
68:
69: public void setOperation(String operation) {
70: this .operation = operation;
71: }
72:
73: public String getRange() {
74: return range;
75: }
76:
77: public void setRange(String range) {
78: this .range = range;
79: }
80:
81: public String getResource() {
82: return resource;
83: }
84:
85: public void setResource(String resource) {
86: this.resource = resource;
87: }
88: }
|