Skip to content

Commit 052112a

Browse files
[ISSUE alibaba#12017] Add the console backend API for auth section
* Add user handling module * Add role handling module * Add permission handling module
1 parent 0ad172f commit 052112a

12 files changed

+1442
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright 1999-2024 Alibaba Group Holding Ltd.
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+
package com.alibaba.nacos.console.controller.v3.auth;
19+
20+
import com.alibaba.nacos.api.model.v2.Result;
21+
import com.alibaba.nacos.auth.annotation.Secured;
22+
import com.alibaba.nacos.common.utils.StringUtils;
23+
import com.alibaba.nacos.config.server.paramcheck.ConfigDefaultHttpParamExtractor;
24+
import com.alibaba.nacos.console.proxy.auth.PermissionProxy;
25+
import com.alibaba.nacos.core.paramcheck.ExtractorManager;
26+
import com.alibaba.nacos.persistence.model.Page;
27+
import com.alibaba.nacos.plugin.auth.constant.ActionTypes;
28+
import com.alibaba.nacos.plugin.auth.impl.constant.AuthConstants;
29+
import com.alibaba.nacos.plugin.auth.impl.persistence.PermissionInfo;
30+
import org.springframework.beans.factory.annotation.Autowired;
31+
import org.springframework.web.bind.annotation.DeleteMapping;
32+
import org.springframework.web.bind.annotation.GetMapping;
33+
import org.springframework.web.bind.annotation.PostMapping;
34+
import org.springframework.web.bind.annotation.RequestMapping;
35+
import org.springframework.web.bind.annotation.RequestParam;
36+
import org.springframework.web.bind.annotation.RestController;
37+
38+
/**
39+
* Controller for handling HTTP requests related to permission operations.
40+
*
41+
* @author zhangyukun on:2024/8/16
42+
*/
43+
@RestController
44+
@RequestMapping("/v3/console/auth/permission")
45+
@ExtractorManager.Extractor(httpExtractor = ConfigDefaultHttpParamExtractor.class)
46+
public class ConsolePermissionController {
47+
48+
private final PermissionProxy permissionProxy;
49+
50+
/**
51+
* Constructs a new ConsolePermissionController with the provided PermissionProxy.
52+
*
53+
* @param permissionProxy the proxy used for handling permission-related operations
54+
*/
55+
@Autowired
56+
public ConsolePermissionController(PermissionProxy permissionProxy) {
57+
this.permissionProxy = permissionProxy;
58+
}
59+
60+
/**
61+
* Add a permission to a role.
62+
*
63+
* @param role the role
64+
* @param resource the related resource
65+
* @param action the related action
66+
* @return ok if succeed
67+
*/
68+
@PostMapping
69+
@Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX + "permissions", action = ActionTypes.WRITE)
70+
public Object createPermission(@RequestParam String role, @RequestParam String resource, @RequestParam String action) {
71+
permissionProxy.createPermission(role, resource, action);
72+
return Result.success("add permission ok!");
73+
}
74+
75+
76+
/**
77+
* Delete a permission from a role.
78+
*
79+
* @param role the role
80+
* @param resource the related resource
81+
* @param action the related action
82+
* @return ok if succeed
83+
*/
84+
@DeleteMapping
85+
@Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX + "permissions", action = ActionTypes.WRITE)
86+
public Object deletePermission(@RequestParam String role, @RequestParam String resource,
87+
@RequestParam String action) {
88+
permissionProxy.deletePermission(role, resource, action);
89+
return Result.success("delete permission ok!");
90+
}
91+
92+
/**
93+
* Query permissions of a role.
94+
*
95+
* @param role the role
96+
* @param pageNo page index
97+
* @param pageSize page size
98+
* @param search the type of search (accurate or blur)
99+
* @return permission of a role
100+
*/
101+
@GetMapping("/list")
102+
@Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX + "permissions", action = ActionTypes.READ)
103+
public Result<Page<PermissionInfo>> getPermissionList(@RequestParam int pageNo, @RequestParam int pageSize,
104+
@RequestParam(name = "role", defaultValue = StringUtils.EMPTY) String role,
105+
@RequestParam(name = "search", defaultValue = "accurate") String search) {
106+
Page<PermissionInfo> permissionPage = permissionProxy.getPermissionList(role, pageNo, pageSize, search);
107+
return Result.success(permissionPage);
108+
}
109+
110+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 1999-2024 Alibaba Group Holding Ltd.
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+
package com.alibaba.nacos.console.controller.v3.auth;
19+
20+
import com.alibaba.nacos.api.model.v2.Result;
21+
import com.alibaba.nacos.auth.annotation.Secured;
22+
import com.alibaba.nacos.common.utils.StringUtils;
23+
import com.alibaba.nacos.config.server.paramcheck.ConfigDefaultHttpParamExtractor;
24+
import com.alibaba.nacos.console.proxy.auth.RoleProxy;
25+
import com.alibaba.nacos.core.paramcheck.ExtractorManager;
26+
import com.alibaba.nacos.persistence.model.Page;
27+
import com.alibaba.nacos.plugin.auth.constant.ActionTypes;
28+
import com.alibaba.nacos.plugin.auth.impl.constant.AuthConstants;
29+
import com.alibaba.nacos.plugin.auth.impl.persistence.RoleInfo;
30+
import org.springframework.web.bind.annotation.DeleteMapping;
31+
import org.springframework.web.bind.annotation.GetMapping;
32+
import org.springframework.web.bind.annotation.PostMapping;
33+
import org.springframework.web.bind.annotation.RequestMapping;
34+
import org.springframework.web.bind.annotation.RequestParam;
35+
import org.springframework.web.bind.annotation.RestController;
36+
37+
import java.util.List;
38+
39+
/**
40+
* .
41+
*
42+
* @author zhangyukun on:2024/8/16
43+
*/
44+
@RestController
45+
@RequestMapping("/v3/console/auth/role")
46+
@ExtractorManager.Extractor(httpExtractor = ConfigDefaultHttpParamExtractor.class)
47+
public class ConsoleRoleController {
48+
49+
private final RoleProxy roleProxy;
50+
51+
public ConsoleRoleController(RoleProxy roleProxy) {
52+
this.roleProxy = roleProxy;
53+
}
54+
55+
/**
56+
* Add a role to a user
57+
*
58+
* <p>This method is used for 2 functions: 1. create a role and bind it to GLOBAL_ADMIN. 2. bind a role to an user.
59+
*
60+
* @param role role name
61+
* @param username username
62+
* @return Code 200 and message 'add role ok!'
63+
*/
64+
@PostMapping
65+
@Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX + "roles", action = ActionTypes.WRITE)
66+
public Object createRole(@RequestParam String role, @RequestParam String username) {
67+
roleProxy.createRole(role, username);
68+
return Result.success("add role ok!");
69+
}
70+
71+
/**
72+
* Delete a role. If no username is specified, all users under this role are deleted.
73+
*
74+
* @param role role
75+
* @param username username
76+
* @return ok if succeed
77+
*/
78+
@DeleteMapping
79+
@Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX + "roles", action = ActionTypes.WRITE)
80+
public Object deleteRole(@RequestParam String role,
81+
@RequestParam(name = "username", defaultValue = StringUtils.EMPTY) String username) {
82+
roleProxy.deleteRole(role, username);
83+
return Result.success("delete role of user " + username + " ok!");
84+
}
85+
86+
/**
87+
* Get roles list with the option for accurate or fuzzy search.
88+
*
89+
* @param pageNo number index of page
90+
* @param pageSize page size
91+
* @param username optional, username of user
92+
* @param role optional role
93+
* @param search the type of search: "accurate" for exact match, "blur" for fuzzy match
94+
* @return role list
95+
*/
96+
@GetMapping("/list")
97+
@Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX + "roles", action = ActionTypes.READ)
98+
public Result<Page<RoleInfo>> getRoleList(@RequestParam int pageNo, @RequestParam int pageSize,
99+
@RequestParam(name = "username", defaultValue = "") String username,
100+
@RequestParam(name = "role", defaultValue = "") String role,
101+
@RequestParam(name = "search", required = false, defaultValue = "accurate") String search) {
102+
Page<RoleInfo> rolePage = roleProxy.getRoleList(pageNo, pageSize, username, role, search);
103+
return Result.success(rolePage);
104+
}
105+
106+
/**
107+
* Fuzzy matching role name .
108+
*
109+
* @param role role id
110+
* @return role list
111+
*/
112+
@GetMapping("/search")
113+
@Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX + "roles", action = ActionTypes.READ)
114+
public Result<List<String>> getRoleListByRoleName(@RequestParam String role) {
115+
List<String> roles = roleProxy.getRoleListByRoleName(role);
116+
return Result.success(roles);
117+
}
118+
}

0 commit comments

Comments
 (0)