This is an automated email from the ASF dual-hosted git repository. guangning pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/pulsar-manager.git
The following commit(s) were added to refs/heads/master by this push: new dfbb19b Allow user to assign tenant to role (#436) dfbb19b is described below commit dfbb19ba44a5f65e43a79969b3d2a724bacac267 Author: Sourabh Agrawal <sourabh.agraw...@nutanix.com> AuthorDate: Thu Jan 13 20:07:12 2022 +0530 Allow user to assign tenant to role (#436) ### Motivation Improve Tenant/Namespace resource assignment workflow. * When assigning the resource to a role, it only let you choose from Namespace,SCHEMA,Functions but does not allow the user to select TENANT. A user should have ability to choose tenant for a role and assign the role to a user. Based on which the user should be able to see the tenant it is assigned with. ### Modifications * changes in this PR include below fixes. 1. Add TENANT as ResourceType along with Namepsace,Schema etc for Create/Edit Role. 2. Include only tenants as resource while preparing the response from api /tenants in TenantsController. 3. In Success LoginResponse, add the tenant as header which is assigned to user's role instead of tenant with user's name. ### Verifying this change - [ ] Make sure that the change passes the `./gradlew build` checks. --- .../pulsar/manager/controller/LoginController.java | 12 ++++++++- .../pulsar/manager/controller/RolesController.java | 31 +++++++++++++++++----- .../manager/controller/TenantsController.java | 4 ++- .../pulsar/manager/dao/TenantsRepositoryImpl.java | 5 ++++ .../pulsar/manager/entity/TenantsRepository.java | 2 ++ .../pulsar/manager/mapper/TenantsMapper.java | 4 +++ .../manager/dao/TenantsRepositoryImplTest.java | 18 ++++++++++++- 7 files changed, 67 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/apache/pulsar/manager/controller/LoginController.java b/src/main/java/org/apache/pulsar/manager/controller/LoginController.java index 4973a32..7e199ee 100644 --- a/src/main/java/org/apache/pulsar/manager/controller/LoginController.java +++ b/src/main/java/org/apache/pulsar/manager/controller/LoginController.java @@ -20,6 +20,8 @@ import org.apache.pulsar.manager.entity.RoleBindingEntity; import org.apache.pulsar.manager.entity.RoleBindingRepository; import org.apache.pulsar.manager.entity.RoleInfoEntity; import org.apache.pulsar.manager.entity.RolesRepository; +import org.apache.pulsar.manager.entity.TenantEntity; +import org.apache.pulsar.manager.entity.TenantsRepository; import org.apache.pulsar.manager.entity.UserInfoEntity; import org.apache.pulsar.manager.entity.UsersRepository; import org.apache.pulsar.manager.service.JwtService; @@ -27,6 +29,7 @@ import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; +import org.apache.pulsar.manager.utils.ResourceType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpHeaders; @@ -81,6 +84,9 @@ public class LoginController { @Autowired private RoleBindingRepository roleBindingRepository; + @Autowired + private TenantsRepository tenantsRepository; + @ApiOperation(value = "Login pulsar manager") @ApiResponses({ @ApiResponse(code = 200, message = "ok"), @@ -112,7 +118,7 @@ public class LoginController { result.put("login", "success"); headers.add("token", token); headers.add("username", userAccount); - headers.add("tenant", userAccount); + jwtService.setToken(request.getSession().getId(), token); List<RoleBindingEntity> roleBindingEntities = roleBindingRepository. findByUserId(userInfoEntity.getUserId()); @@ -123,6 +129,10 @@ public class LoginController { if (!roleIdList.isEmpty()) { List<RoleInfoEntity> roleInfoEntities = rolesRepository.findAllRolesByMultiId(roleIdList); for (RoleInfoEntity roleInfoEntity : roleInfoEntities) { + if(roleInfoEntity.getResourceType().equals(ResourceType.TENANTS.name())){ + Optional<TenantEntity> tenantEntity = tenantsRepository.findByTenantId(roleInfoEntity.getResourceId()); + headers.add("tenant",tenantEntity.get().getTenant()); + } if (roleInfoEntity.getFlag() == 0) { // Super users can access all types return new ResponseEntity<>(result, headers, HttpStatus.OK); diff --git a/src/main/java/org/apache/pulsar/manager/controller/RolesController.java b/src/main/java/org/apache/pulsar/manager/controller/RolesController.java index 0f94ddb..bd04493 100644 --- a/src/main/java/org/apache/pulsar/manager/controller/RolesController.java +++ b/src/main/java/org/apache/pulsar/manager/controller/RolesController.java @@ -24,6 +24,8 @@ import org.apache.pulsar.manager.entity.NamespaceEntity; import org.apache.pulsar.manager.entity.NamespacesRepository; import org.apache.pulsar.manager.entity.RoleInfoEntity; import org.apache.pulsar.manager.entity.RolesRepository; +import org.apache.pulsar.manager.entity.TenantEntity; +import org.apache.pulsar.manager.entity.TenantsRepository; import org.apache.pulsar.manager.service.RolesService; import org.apache.pulsar.manager.utils.ResourceType; import org.hibernate.validator.constraints.Range; @@ -59,15 +61,19 @@ public class RolesController { private final HttpServletRequest request; + private TenantsRepository tenantsRepository; + public RolesController( RolesRepository rolesRepository, RolesService rolesService, NamespacesRepository namespacesRepository, + TenantsRepository tenantsRepository, HttpServletRequest request) { this.rolesRepository = rolesRepository; this.rolesService = rolesService; this.namespacesRepository = namespacesRepository; this.request = request; + this.tenantsRepository = tenantsRepository; } @ApiOperation(value = "Get the list of existing roles, support paging, the default is 10 per page") @@ -228,6 +234,7 @@ public class RolesController { public ResponseEntity<Map<String, Object>> getResourceType() { Map<String, Object> result = Maps.newHashMap(); Set<String> resourceTypeList = Sets.newHashSet(); + resourceTypeList.add(ResourceType.TENANTS.name()); resourceTypeList.add(ResourceType.NAMESPACES.name()); resourceTypeList.add(ResourceType.TOPICS.name()); resourceTypeList.add(ResourceType.SCHEMAS.name()); @@ -247,18 +254,30 @@ public class RolesController { Map<String, Object> result = Maps.newHashMap(); String token = request.getHeader("token"); String tenant = request.getHeader("tenant"); + String environment = request.getHeader("environment"); Map<String, String> validateResult = rolesService.validateCurrentTenant(token, tenant); if (validateResult.get("error") != null) { result.put("error", validateResult.get("error")); return ResponseEntity.ok(result); } - List<NamespaceEntity> namespaceEntities = namespacesRepository.findByTenant(tenant); Set<Map<String, Object>> nameSet = Sets.newHashSet(); - for (NamespaceEntity namespaceEntity : namespaceEntities) { - Map<String, Object> namespace = Maps.newHashMap(); - namespace.put("name", namespaceEntity.getNamespace()); - namespace.put("id", namespaceEntity.getNamespaceId()); - nameSet.add(namespace); + if(ResourceType.TENANTS.name().equals(resourceType)){ + List<TenantEntity> tenantEntities = tenantsRepository.findByEnvironment(environment); + tenantEntities.forEach(tenantEntity -> { + Map<String, Object> tenantMap = Maps.newHashMap(); + tenantMap.put("name", tenantEntity.getTenant()); + tenantMap.put("id", tenantEntity.getTenantId()); + nameSet.add(tenantMap); + }); + + } else if(ResourceType.NAMESPACES.name().equals(resourceType)) { + List<NamespaceEntity> namespaceEntities = namespacesRepository.findByTenant(tenant); + for (NamespaceEntity namespaceEntity : namespaceEntities) { + Map<String, Object> namespace = Maps.newHashMap(); + namespace.put("name", namespaceEntity.getNamespace()); + namespace.put("id", namespaceEntity.getNamespaceId()); + nameSet.add(namespace); + } } result.put("data", nameSet); return ResponseEntity.ok(result); diff --git a/src/main/java/org/apache/pulsar/manager/controller/TenantsController.java b/src/main/java/org/apache/pulsar/manager/controller/TenantsController.java index 1319048..01505a1 100644 --- a/src/main/java/org/apache/pulsar/manager/controller/TenantsController.java +++ b/src/main/java/org/apache/pulsar/manager/controller/TenantsController.java @@ -124,7 +124,9 @@ public class TenantsController { List<RoleInfoEntity> roleInfoEntities = rolesRepository.findAllRolesByMultiId(roleIdList); List<Long> tenantsIdList = new ArrayList<>(); for (RoleInfoEntity roleInfoEntity : roleInfoEntities) { - tenantsIdList.add(roleInfoEntity.getResourceId()); + if(roleInfoEntity.getResourceType().equals(ResourceType.TENANTS.name())) { + tenantsIdList.add(roleInfoEntity.getResourceId()); + } } if (!tenantsIdList.isEmpty()) { tenantEntities = tenantsRepository.findByMultiId(tenantsIdList); diff --git a/src/main/java/org/apache/pulsar/manager/dao/TenantsRepositoryImpl.java b/src/main/java/org/apache/pulsar/manager/dao/TenantsRepositoryImpl.java index 40a5576..c072756 100644 --- a/src/main/java/org/apache/pulsar/manager/dao/TenantsRepositoryImpl.java +++ b/src/main/java/org/apache/pulsar/manager/dao/TenantsRepositoryImpl.java @@ -79,4 +79,9 @@ public class TenantsRepositoryImpl implements TenantsRepository { tenantsMapper.delete(tenant); } + @Override + public List<TenantEntity> findByEnvironment(String environment) { + return tenantsMapper.findAll(environment); + } + } diff --git a/src/main/java/org/apache/pulsar/manager/entity/TenantsRepository.java b/src/main/java/org/apache/pulsar/manager/entity/TenantsRepository.java index ced4f24..e27af49 100644 --- a/src/main/java/org/apache/pulsar/manager/entity/TenantsRepository.java +++ b/src/main/java/org/apache/pulsar/manager/entity/TenantsRepository.java @@ -38,5 +38,7 @@ public interface TenantsRepository { void remove(String tenant); + List<TenantEntity> findByEnvironment(String environment); + } diff --git a/src/main/java/org/apache/pulsar/manager/mapper/TenantsMapper.java b/src/main/java/org/apache/pulsar/manager/mapper/TenantsMapper.java index 82b288a..78cffdb 100644 --- a/src/main/java/org/apache/pulsar/manager/mapper/TenantsMapper.java +++ b/src/main/java/org/apache/pulsar/manager/mapper/TenantsMapper.java @@ -77,4 +77,8 @@ public interface TenantsMapper { @Delete("DELETE FROM tenants WHERE tenant = #{tenant}") void delete(String tenant); + @Select("SELECT tenant, tenant_id as tenantId, admin_roles as adminRoles,allowed_clusters as allowedClusters," + + "environment_name as environmentName " + + "FROM tenants WHERE environment_name = #{environment}") + List<TenantEntity> findAll(String environment); } diff --git a/src/test/java/org/apache/pulsar/manager/dao/TenantsRepositoryImplTest.java b/src/test/java/org/apache/pulsar/manager/dao/TenantsRepositoryImplTest.java index 57c559b..a31765f 100644 --- a/src/test/java/org/apache/pulsar/manager/dao/TenantsRepositoryImplTest.java +++ b/src/test/java/org/apache/pulsar/manager/dao/TenantsRepositoryImplTest.java @@ -129,4 +129,20 @@ public class TenantsRepositoryImplTest { Assert.assertEquals("test-cluster", getTenantEntity.getAllowedClusters()); Assert.assertEquals("test-environment", getTenantEntity.getEnvironmentName()); } -} \ No newline at end of file + + @Test + public void findByEnvironment() { + TenantEntity tenantEntity = new TenantEntity(); + tenantEntity.setTenant("test"); + tenantEntity.setAdminRoles("test-role"); + tenantEntity.setAllowedClusters("test-cluster"); + tenantEntity.setEnvironmentName("test-environment"); + long tenantId = tenantsRepository.save(tenantEntity); + List<TenantEntity> result = tenantsRepository.findByEnvironment("test-environment"); + TenantEntity getTenantEntity = result.get(0); + Assert.assertEquals("test", getTenantEntity.getTenant()); + Assert.assertEquals("test-role", getTenantEntity.getAdminRoles()); + Assert.assertEquals("test-cluster", getTenantEntity.getAllowedClusters()); + Assert.assertEquals("test-environment", getTenantEntity.getEnvironmentName()); + } +}