View Javadoc
1   /**
2    * Copyright By Grandsoft Company Limited. 
3    * @author wangsr
4    * @since jdk1.6 
5    * @date 2012-3-1 下午03:36:54
6    */
7   package gboat2.web.action;
8   
9   import gboat2.base.bridge.GboatAppContext;
10  import gboat2.base.bridge.model.UserSession;
11  import gboat2.base.core.annotation.Domain;
12  import gboat2.base.core.annotation.Module;
13  import gboat2.base.core.annotation.Operations;
14  import gboat2.base.core.logging.IBusinessLogService;
15  import gboat2.base.core.logging.Level;
16  import gboat2.base.core.web.BaseActionSupport;
17  import gboat2.base.core.web.JsonResult;
18  import gboat2.web.business.IAuthorityBusiness;
19  import gboat2.web.business.ICacheAssistBusiness;
20  import gboat2.web.business.IDataLevelAuthorityBusiness;
21  import gboat2.web.business.IGroupBusiness;
22  import gboat2.web.business.IGroupRoleBusiness;
23  import gboat2.web.business.IResourceBusiness;
24  import gboat2.web.business.IRoleBusiness;
25  import gboat2.web.business.ISystemConfigBusiness;
26  import gboat2.web.business.IUserBusiness;
27  import gboat2.web.model.AuthorityOperationVO;
28  import gboat2.web.model.AuthorityResourceVO;
29  import gboat2.web.model.Group;
30  import gboat2.web.model.Operation;
31  import gboat2.web.model.Resource;
32  import gboat2.web.model.Role;
33  import gboat2.web.model.SystemConfig;
34  import gboat2.web.model.UserRoleGroupOrganVO;
35  
36  import java.util.ArrayList;
37  import java.util.HashMap;
38  import java.util.List;
39  import java.util.Map;
40  
41  import org.apache.commons.lang3.StringUtils;
42  import org.apache.struts2.convention.annotation.ResultPath;
43  import org.springframework.beans.factory.annotation.Autowired;
44  
45  /**
46   * 角色管理
47   * @author wangsr
48   * @since jdk1.6
49   * @date 2012-3-13
50   *
51   */
52  @ResultPath("/content/role")
53  @Domain(value = Role.class)
54  @Module(name = "角色管理", desc = "管理各角色")
55  @Operations(value = {
56          @gboat2.base.core.annotation.Operation(name = "新增角色", code = "add"),
57          @gboat2.base.core.annotation.Operation(name = "编辑", code = "edit"),
58          @gboat2.base.core.annotation.Operation(name = "删除", code = "ajaxDelete")
59  })
60  public class RoleAction extends BaseActionSupport {
61  	
62  	private static final long serialVersionUID = 472119966630760254L;
63  	
64  	private String roleCode;
65  	
66  	private String roleId;
67  	
68  	private String resId;
69  	
70  	private String systemId;
71  	
72  	@Autowired
73  	private IRoleBusiness roleBusiness;
74  	
75  	@Autowired
76  	private IGroupBusiness groupBusi;
77  	
78  	@Autowired
79  	private IResourceBusiness resBusiness;
80  	
81  	@Autowired
82  	private IAuthorityBusiness authorityBusi;
83  	
84  	@Autowired
85  	private IGroupRoleBusiness groupRoleBusi;
86  	
87  	@Autowired
88  	private IDataLevelAuthorityBusiness dataLevelAuthorityBusi;
89  	
90  	@Autowired
91  	private IBusinessLogService loggingService;
92  	
93  	@Autowired
94  	private ICacheAssistBusiness cacheAssistBusi;
95  	
96  	@Autowired
97  	private ISystemConfigBusiness systemCfgBusi;
98  	
99  	/**
100 	 * 用户服务
101 	 */
102 	@Autowired
103 	private IUserBusiness userService;
104 	
105 	/**
106 	 * 取得用户的角色,为用户切换角色所用
107 	 */
108 	public void rolesOfUser() {
109 		UserSession session = GboatAppContext.getUserSession();
110 		if (null == session) {
111 		    GboatAppContext.output(JsonResult.createFailure("对不起,用户未登录或者登录超时!"));
112 			return;
113 		}
114 		List<UserRoleGroupOrganVO> userStatus = userService.getMapperVOByUserId(session.getUserId());
115 		GboatAppContext.output(userStatus);
116 	}
117 	
118 	//取得系统中,权限定制过的用户组(非继承)
119 	public void findAllGroupOfCustom() {
120 		List<String> list = this.authorityBusi.findAllAuthForIdBySysIdAndRoleId(systemId, roleId);
121 		List<Group> groups = new ArrayList<Group>();
122 		if (list != null && list.size() > 0) {
123 			for (String authForId : list) {
124 				String groupId = authForId.substring(0, authForId.indexOf("."));
125 				Group tmp = (Group) this.groupBusi.get(Group.class, groupId);
126 				groups.add(tmp);
127 			}
128 		}
129 		GboatAppContext.output(groups);
130 	}
131 	
132 	public void groupAuthority() {
133 		
134 	}
135 	
136 	public void initEdit() {
137 		Role role = (Role) getModel();
138 		if (StringUtils.isEmpty(role.getRoleId())) {
139 			loggingService.log(Level.INFO, "新增", "新增角色:" + role.getRoleName());
140 		} else {
141 			loggingService.log(Level.INFO, "编辑", "编辑角色:" + role.getRoleName());
142 		}
143 	}
144 	
145 	public void postDelete() {
146 		Role role = (Role) getModel();
147 		String roleId = role.getRoleId();
148 		// TODO 提到roleBusiness中
149 		// 删除授权给该角色的模块权限
150 		this.authorityBusi.deleteAuthorityByRoleId(roleId);
151 		// 删除分配给用户组的角色(mapper)
152 		this.groupRoleBusi.deleteByRoleId(roleId);
153 		// 删除该角色在用户组中定制的权限
154 		this.dataLevelAuthorityBusi.deleteDataLevelAuthorityByRoleId(roleId);
155 		
156 		// 所有系统的缓存
157 		List<SystemConfig> allSys = systemCfgBusi.getAllSysCfg();
158 		if (allSys != null && allSys.size() > 0) {
159 			for (SystemConfig cfg : allSys) {
160 				// 删除该角色的权限缓存
161 				this.cacheAssistBusi.removeCacheOfPriority(roleId, cfg.getSystemId());
162 				// 删除该角色的导航缓存
163 				this.cacheAssistBusi.removeCacheOfMenuTree(roleId, cfg.getSystemId());
164 			}
165 		}
166 		
167 		loggingService.log(Level.INFO, "删除", "删除角色:" + role.getRoleName());
168 	}
169 	
170 	/**
171 	 * 取得指定模块的操作(初始化多选按钮)
172 	 */
173 	public void findOperationsForResource() {
174 		List<Operation> allOperas = this.authorityBusi.findOperationsForResource(resId);
175 		List<Map<String, Object>> items = new ArrayList<Map<String,Object>>(); 
176 		//items : [{boxLabel : '显示文字',name : '提交后台的参数名称',inputValue : '提交值'}}
177 		for (Operation opera : allOperas) {
178 		    Map<String, Object> map = new HashMap<String, Object>();
179 			map.put("boxLabel", opera.getOperationName());
180 			map.put("name", "operations");
181 			map.put("inputValue", opera.getOperationId());
182 			map.put("operationId", opera.getOperationId());
183 			items.add(map);
184 		}
185 		GboatAppContext.output(items);
186 	}
187 	
188 	/**
189 	 * 取得当前角色的指定模块的操作(回填多选按钮)
190 	 */
191 	public void findOperationsForRole() {
192 		List<AuthorityOperationVO> allOperas = this.authorityBusi.findOperationsForRole(resId, roleId);
193 		// 角色拥有操作的ID
194 		List<String> arr = new ArrayList<String>();
195 		for (AuthorityOperationVO auop : allOperas) {
196 			arr.add(auop.getOperationId());
197 		}
198 		GboatAppContext.output(arr);
199 	}
200 	
201 	@gboat2.base.core.annotation.Operation(name = "权限管理", code = "findResourceTree", desc = "权限管理")
202 	public void authority() {
203 		
204 	}
205 	
206 	/**
207 	 * 得到所有的模块,初始化模块树(初始化角色树)
208 	 */
209 	public void findResourceTree() {
210 		Resource tree = this.resBusiness.getResourcesTree(systemId);
211 		GboatAppContext.output(tree.getChildren());
212 	}
213 	
214 	/**
215 	 * 取得当前角色的所有权限(回填角色树)
216 	 */
217 	public void findResourceForRole() {
218 		List<AuthorityResourceVO> allRes = this.authorityBusi.findResourceOfSystemByAuthForId(roleId, systemId);
219 		// 角色拥有模块的ID
220 		List<String> resIds = new ArrayList<String>();
221 		for (AuthorityResourceVO authRes : allRes) {
222 			resIds.add(authRes.getResId());
223 		}
224 		GboatAppContext.output(resIds);
225 	}
226 	
227 	/**
228 	 * 检查角色编码是否存在
229 	 */
230 	public void checkRoleCode() {
231 		Boolean exist = Boolean.TRUE;
232 		this.roleCode = ((Role) this.getModel()).getRoleCode();
233 		Role r = this.roleBusiness.findRoleByRoleCode(this.roleCode);
234 		if (r == null) {
235 			exist = Boolean.FALSE;
236 		}
237 		String reason = "该角色编码已经存在!!";
238 		if (!exist) {
239 			reason = "该角色编码可以使用!!";
240 		}
241 		Map<String, Object> result = new HashMap<String, Object>();
242 		result.put("valid", !exist);
243 		result.put("reason", reason);
244 		GboatAppContext.output(result);
245 	}
246 	
247 	public String getRoleCode() {
248 		return roleCode;
249 	}
250 	
251 	public void setRoleCode(String roleCode) {
252 		this.roleCode = roleCode;
253 	}
254 	
255 	public String getRoleId() {
256 		return roleId;
257 	}
258 	
259 	public void setRoleId(String roleId) {
260 		this.roleId = roleId;
261 	}
262 	
263 	public String getResId() {
264 		return resId;
265 	}
266 	
267 	public void setResId(String resId) {
268 		this.resId = resId;
269 	}
270 	
271 	public String getSystemId() {
272 		return systemId;
273 	}
274 	
275 	public void setSystemId(String systemId) {
276 		this.systemId = systemId;
277 	}
278 }