Skip to content

Commit 5091e26

Browse files
committed
优化角色管理
1 parent 15e0514 commit 5091e26

File tree

5 files changed

+110
-5
lines changed

5 files changed

+110
-5
lines changed

hsweb-system/hsweb-system-authorization/hsweb-system-authorization-controller/src/main/java/org/hswebframework/web/controller/authorization/AuthorizationController.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ public void getVerifyCode(HttpServletResponse response, HttpSession session) thr
9797

9898
@RequestMapping("/login-out")
9999
@AccessLogger("退出登录")
100-
public ResponseMessage loginOut() {
101-
100+
public ResponseMessage loginOut(Authorization authorization) {
101+
listenerAdapter.onLoginOut(authorization);
102102
return ok();
103103
}
104104

@@ -178,6 +178,12 @@ public void onAuthorizeFail(String username) {
178178
userAuthorizationListeners.forEach(listener -> listener.onAuthorizeFail(username));
179179
}
180180

181+
@Override
182+
public void onLoginOut(Authorization authorization) {
183+
if (userAuthorizationListeners != null)
184+
userAuthorizationListeners.forEach(listener -> listener.onLoginOut(authorization));
185+
}
186+
181187
@Override
182188
public void onAuthorizeSuccess(boolean isRemembered, Authorization authorization) {
183189
if (userAuthorizationListeners != null)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2016 http://www.hswebframework.org
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, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*
17+
*/
18+
19+
package org.hswebframework.web.controller.authorization;
20+
21+
import org.hswebframework.web.authorization.Permission;
22+
import org.hswebframework.web.authorization.annotation.Authorize;
23+
import org.hswebframework.web.commons.entity.param.QueryParamEntity;
24+
import org.hswebframework.web.controller.QueryController;
25+
import org.hswebframework.web.controller.message.ResponseMessage;
26+
import org.hswebframework.web.entity.authorization.PermissionRoleEntity;
27+
import org.hswebframework.web.entity.authorization.RoleEntity;
28+
import org.hswebframework.web.entity.authorization.bind.BindPermissionRoleEntity;
29+
import org.hswebframework.web.logging.AccessLogger;
30+
import org.hswebframework.web.service.authorization.RoleService;
31+
import org.springframework.beans.factory.annotation.Autowired;
32+
import org.springframework.web.bind.annotation.*;
33+
34+
import static org.hswebframework.web.controller.message.ResponseMessage.ok;
35+
36+
/**
37+
* 角色控制器
38+
*
39+
* @author zhouhao
40+
*/
41+
@RestController
42+
@RequestMapping("${hsweb.web.mappings.role:role}")
43+
@AccessLogger("{role_manager}")
44+
@Authorize(permission = "role")
45+
public class RoleController implements QueryController<RoleEntity, String, QueryParamEntity> {
46+
47+
@Autowired
48+
private RoleService roleService;
49+
50+
@Override
51+
public RoleService getService() {
52+
return roleService;
53+
}
54+
55+
@PostMapping
56+
@Authorize(action = Permission.ACTION_ADD)
57+
@AccessLogger("{add}")
58+
public ResponseMessage addRole(@RequestBody BindPermissionRoleEntity<PermissionRoleEntity> permissionRoleEntity) {
59+
return ok(roleService.insert(permissionRoleEntity));
60+
}
61+
62+
@PutMapping("/{id}")
63+
@Authorize(action = Permission.ACTION_UPDATE)
64+
@AccessLogger("{update}")
65+
public ResponseMessage updateRole(@PathVariable String id, @RequestBody BindPermissionRoleEntity<PermissionRoleEntity> permissionRoleEntity) {
66+
permissionRoleEntity.setId(id);
67+
roleService.updateByPrimaryKey(permissionRoleEntity);
68+
return ok();
69+
}
70+
71+
@PutMapping("/disable/{id}")
72+
@Authorize(action = Permission.ACTION_DISABLE)
73+
@AccessLogger("{disable}")
74+
public ResponseMessage disable(@PathVariable String id) {
75+
roleService.disable(id);
76+
return ok();
77+
}
78+
79+
@PutMapping("/enable/{id}")
80+
@Authorize(action = Permission.ACTION_ENABLE)
81+
@AccessLogger("{disable}")
82+
public ResponseMessage enable(@PathVariable String id) {
83+
roleService.enable(id);
84+
return ok();
85+
}
86+
}

hsweb-system/hsweb-system-authorization/hsweb-system-authorization-controller/src/main/java/org/hswebframework/web/controller/authorization/UserController.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@
2727
import org.hswebframework.web.logging.AccessLogger;
2828
import org.hswebframework.web.service.authorization.UserService;
2929
import org.springframework.beans.factory.annotation.Autowired;
30-
import org.springframework.transaction.annotation.Transactional;
3130
import org.springframework.web.bind.annotation.*;
3231

33-
import static org.hswebframework.web.controller.message.ResponseMessage.*;
32+
import static org.hswebframework.web.controller.message.ResponseMessage.ok;
3433

3534
/**
3635
* TODO 完成注释
@@ -41,7 +40,6 @@
4140
@RequestMapping("${hsweb.web.mappings.user:user}")
4241
@Authorize(permission = "user")
4342
@AccessLogger("用户管理")
44-
@Transactional
4543
public class UserController implements QueryController<UserEntity, String, QueryParamEntity>, CreateController<UserEntity, String> {
4644

4745
private UserService userService;

hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-api/src/main/java/org/hswebframework/web/service/authorization/RoleService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414
public interface RoleService extends
1515
CreateEntityService<RoleEntity>,
1616
QueryByEntityService<RoleEntity> {
17+
1718
<T extends PermissionRoleEntity> String insert(BindPermissionRoleEntity<T> roleEntity);
1819

20+
<T extends PermissionRoleEntity> void updateByPrimaryKey(BindPermissionRoleEntity<T> roleEntity);
21+
1922
boolean enable(String roleId);
2023

2124
boolean disable(String roleId);

hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/SimpleRoleService.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.hswebframework.web.service.authorization.simple;
1919

20+
import org.hswebframework.web.commons.entity.GenericEntity;
2021
import org.hswebframework.web.dao.authorization.PermissionRoleDao;
2122
import org.hswebframework.web.dao.authorization.RoleDao;
2223
import org.hswebframework.web.entity.authorization.PermissionRoleEntity;
@@ -73,6 +74,17 @@ public <T extends PermissionRoleEntity> String insert(BindPermissionRoleEntity<T
7374
return roleEntity.getId();
7475
}
7576

77+
@Override
78+
public <T extends PermissionRoleEntity> void updateByPrimaryKey(BindPermissionRoleEntity<T> roleEntity) {
79+
tryValidateProperty(StringUtils.hasLength(roleEntity.getId()), RoleEntity.id, "id {not_be_null}");
80+
tryValidateProperty(null == selectByPk(roleEntity.getId()), RoleEntity.id, "{role_exists}");
81+
roleEntity.setEnabled(null);
82+
tryValidate(roleEntity);
83+
DefaultDSLUpdateService
84+
.createUpdate(roleDao, roleEntity)
85+
.where(GenericEntity.id, roleEntity.getId());
86+
}
87+
7688
@Override
7789
public boolean enable(String roleId) {
7890
return DefaultDSLUpdateService.createUpdate(getDao()).set("enabled", true).where(RoleEntity.id, roleId).exec() > 0;

0 commit comments

Comments
 (0)