View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.tuigwaa.controller;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Map;
23  
24  import javax.servlet.http.HttpServletRequest;
25  
26  import org.apache.struts.action.ActionForm;
27  import org.apache.struts.action.ActionMapping;
28  import org.seasar.tuigwaa.security.Action;
29  import org.seasar.tuigwaa.security.SecurityService;
30  import org.seasar.tuigwaa.system.SiteConfig;
31  
32  
33  public class SiteSecurityForm extends ActionForm {
34  
35  	private static final long serialVersionUID = -7198572236685453907L;
36  
37  	private SecurityService securityService;
38  
39  	private SiteConfig siteConfig;
40  
41  	private List securityList = new ArrayList();
42  
43  	public SiteSecurityForm(SecurityService securityService,
44  			SiteConfig siteConfig) {
45  		this.securityService = securityService;
46  		this.siteConfig = siteConfig;
47  
48  		List roleListSet = new ArrayList();
49  
50  		for (int i = 0; i < Action.ACTIONS.length; i++) {
51  			roleListSet.add(createRoleList(Action.ACTIONS[i]));
52  		}
53  		setSecurityList(roleListSet);
54  	}
55  
56  	private RoleList createRoleList(Action action) {
57  		String[] roleNames = siteConfig.getRoleNames();
58  		String siteName = siteConfig.getName();
59  		RoleList roleList = new RoleList(action);
60  		boolean[] flags = new boolean[roleNames.length];
61  		for (int i = 0; i < roleNames.length; i++) {
62  			flags[i] = securityService.hasPermission(siteName, roleNames[i],
63  					action);
64  		}
65  		roleList.setFlags(flags);
66  		return roleList;
67  	}
68  
69  	public void reset(ActionMapping mapping, HttpServletRequest request) {
70  		super.reset(mapping, request);
71  	}
72  
73  	public List getSecurityList() {
74  		return securityList;
75  	}
76  
77  	public void setSecurityList(List securityList) {
78  		this.securityList = securityList;
79  	}
80  
81  	public Object getItm(int index) {
82  		return securityList.get(index);
83  	}
84  
85  	public Map getSecurityMap() {
86  		Map map = new HashMap();
87  
88  		Iterator roleListItr = getSecurityList().iterator();
89  		while (roleListItr.hasNext()) {
90  			RoleList roleInfo = (RoleList) roleListItr.next();
91  			Action action = roleInfo.getAction();
92  			String[] roleNames = roleInfo.getRoles();
93  			map.put(action, roleNames);
94  		}
95  		return map;
96  	}
97  
98  	public class RoleList {
99  
100 		private boolean[] flags;
101 
102 		private Action action;
103 
104 		private String[] roles;
105 
106 		public RoleList(Action action) {
107 			this.action = action;
108 		}
109 
110 		public Action getAction() {
111 			return action;
112 		}
113 
114 		public String getActionName() {
115 			return action.getActionName();
116 		}
117 
118 		public String[] getRoles() {
119 			return roles;
120 		}
121 
122 		public void setRoles(String[] roles) {
123 			this.roles = roles;
124 		}
125 
126 		public boolean[] getFlags() {
127 			return flags;
128 		}
129 
130 		public void setFlags(boolean[] flags) {
131 			this.flags = flags;
132 		}
133 	}
134 }