|
| 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