Commit e226f8b0 authored by Jason Song's avatar Jason Song Committed by GitHub

Merge pull request #1773 from JaredTan95/refactoring-rolepermissionService

refactoring namespace service a little.
parents dcc49834 43495f99
package com.ctrip.framework.apollo.portal.controller;
import com.ctrip.framework.apollo.common.dto.AppNamespaceDTO;
import com.ctrip.framework.apollo.common.http.MultiResponseEntity;
import com.ctrip.framework.apollo.common.http.RichResponseEntity;
import com.ctrip.framework.apollo.common.utils.BeanUtils;
import com.ctrip.framework.apollo.portal.api.AdminServiceAPI;
import com.ctrip.framework.apollo.portal.component.PermissionValidator;
import com.ctrip.framework.apollo.portal.listener.AppNamespaceDeletionEvent;
import com.google.common.collect.Sets;
import static com.ctrip.framework.apollo.common.utils.RequestPrecondition.checkModel;
import com.ctrip.framework.apollo.common.dto.AppNamespaceDTO;
import com.ctrip.framework.apollo.common.dto.NamespaceDTO;
import com.ctrip.framework.apollo.common.entity.AppNamespace;
import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.common.http.MultiResponseEntity;
import com.ctrip.framework.apollo.common.http.RichResponseEntity;
import com.ctrip.framework.apollo.common.utils.BeanUtils;
import com.ctrip.framework.apollo.common.utils.InputValidator;
import com.ctrip.framework.apollo.common.utils.RequestPrecondition;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.api.AdminServiceAPI;
import com.ctrip.framework.apollo.portal.component.PermissionValidator;
import com.ctrip.framework.apollo.portal.component.config.PortalConfig;
import com.ctrip.framework.apollo.portal.constant.RoleType;
import com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO;
import com.ctrip.framework.apollo.portal.entity.model.NamespaceCreationModel;
import com.ctrip.framework.apollo.portal.listener.AppNamespaceCreationEvent;
import com.ctrip.framework.apollo.portal.listener.AppNamespaceDeletionEvent;
import com.ctrip.framework.apollo.portal.service.AppNamespaceService;
import com.ctrip.framework.apollo.portal.service.NamespaceService;
import com.ctrip.framework.apollo.portal.service.RoleInitializationService;
import com.ctrip.framework.apollo.portal.service.RolePermissionService;
import com.ctrip.framework.apollo.portal.spi.UserInfoHolder;
import com.ctrip.framework.apollo.portal.util.RoleUtils;
import com.ctrip.framework.apollo.tracer.Tracer;
import com.google.common.collect.Sets;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.slf4j.Logger;
......@@ -44,11 +43,6 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
import static com.ctrip.framework.apollo.common.utils.RequestPrecondition.checkModel;
@RestController
public class NamespaceController {
......@@ -65,8 +59,6 @@ public class NamespaceController {
@Autowired
private RoleInitializationService roleInitializationService;
@Autowired
private RolePermissionService rolePermissionService;
@Autowired
private PortalConfig portalConfig;
@Autowired
private PermissionValidator permissionValidator;
......@@ -145,7 +137,7 @@ public class NamespaceController {
}
}
assignNamespaceRoleToOperator(appId, namespaceName);
namespaceService.assignNamespaceRoleToOperator(appId, namespaceName,userInfoHolder.getUser().getUserId());
return ResponseEntity.ok().build();
}
......@@ -199,7 +191,8 @@ public class NamespaceController {
AppNamespace createdAppNamespace = appNamespaceService.createAppNamespaceInLocal(appNamespace, appendNamespacePrefix);
if (portalConfig.canAppAdminCreatePrivateNamespace() || createdAppNamespace.isPublic()) {
assignNamespaceRoleToOperator(appId, appNamespace.getName());
namespaceService.assignNamespaceRoleToOperator(appId, appNamespace.getName(),
userInfoHolder.getUser().getUserId());
}
publisher.publishEvent(new AppNamespaceCreationEvent(createdAppNamespace));
......@@ -282,17 +275,4 @@ public class NamespaceController {
return Sets.union(missingAppNamespaceNames, missingNamespaceNames);
}
private void assignNamespaceRoleToOperator(String appId, String namespaceName) {
//default assign modify、release namespace role to namespace creator
String operator = userInfoHolder.getUser().getUserId();
rolePermissionService
.assignRoleToUsers(RoleUtils.buildNamespaceRoleName(appId, namespaceName, RoleType.MODIFY_NAMESPACE),
Sets.newHashSet(operator), operator);
rolePermissionService
.assignRoleToUsers(RoleUtils.buildNamespaceRoleName(appId, namespaceName, RoleType.RELEASE_NAMESPACE),
Sets.newHashSet(operator), operator);
}
}
package com.ctrip.framework.apollo.portal.service;
import com.ctrip.framework.apollo.portal.component.config.PortalConfig;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.ctrip.framework.apollo.common.constants.GsonType;
import com.ctrip.framework.apollo.common.dto.ItemDTO;
import com.ctrip.framework.apollo.common.dto.NamespaceDTO;
......@@ -17,20 +12,28 @@ import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.core.utils.StringUtils;
import com.ctrip.framework.apollo.portal.api.AdminServiceAPI;
import com.ctrip.framework.apollo.portal.component.PortalSettings;
import com.ctrip.framework.apollo.portal.component.config.PortalConfig;
import com.ctrip.framework.apollo.portal.constant.RoleType;
import com.ctrip.framework.apollo.portal.constant.TracerEventType;
import com.ctrip.framework.apollo.portal.entity.bo.ItemBO;
import com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO;
import com.ctrip.framework.apollo.portal.spi.UserInfoHolder;
import com.ctrip.framework.apollo.portal.util.RoleUtils;
import com.ctrip.framework.apollo.tracer.Tracer;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
@Service
public class NamespaceService {
......@@ -55,6 +58,8 @@ public class NamespaceService {
private InstanceService instanceService;
@Autowired
private NamespaceBranchService branchService;
@Autowired
private RolePermissionService rolePermissionService;
public NamespaceDTO createNamespace(Env env, NamespaceDTO namespace) {
......@@ -65,8 +70,8 @@ public class NamespaceService {
NamespaceDTO createdNamespace = namespaceAPI.createNamespace(env, namespace);
Tracer.logEvent(TracerEventType.CREATE_NAMESPACE,
String.format("%s+%s+%s+%s", namespace.getAppId(), env, namespace.getClusterName(),
namespace.getNamespaceName()));
String.format("%s+%s+%s+%s", namespace.getAppId(), env, namespace.getClusterName(),
namespace.getNamespaceName()));
return createdNamespace;
}
......@@ -78,19 +83,24 @@ public class NamespaceService {
//1. check parent namespace has not instances
if (namespaceHasInstances(appId, env, clusterName, namespaceName)) {
throw new BadRequestException("Can not delete namespace because namespace has active instances");
throw new BadRequestException(
"Can not delete namespace because namespace has active instances");
}
//2. check child namespace has not instances
NamespaceDTO childNamespace = branchService.findBranchBaseInfo(appId, env, clusterName, namespaceName);
NamespaceDTO childNamespace = branchService
.findBranchBaseInfo(appId, env, clusterName, namespaceName);
if (childNamespace != null &&
namespaceHasInstances(appId, env, childNamespace.getClusterName(), namespaceName)) {
throw new BadRequestException("Can not delete namespace because namespace's branch has active instances");
throw new BadRequestException(
"Can not delete namespace because namespace's branch has active instances");
}
//3. check public namespace has not associated namespace
if (appNamespace != null && appNamespace.isPublic() && publicAppNamespaceHasAssociatedNamespace(namespaceName, env)) {
throw new BadRequestException("Can not delete public namespace which has associated namespaces");
if (appNamespace != null && appNamespace.isPublic() && publicAppNamespaceHasAssociatedNamespace(
namespaceName, env)) {
throw new BadRequestException(
"Can not delete public namespace which has associated namespaces");
}
String operator = userInfoHolder.getUser().getUserId();
......@@ -98,7 +108,8 @@ public class NamespaceService {
namespaceAPI.deleteNamespace(env, appId, clusterName, namespaceName, operator);
}
public NamespaceDTO loadNamespaceBaseInfo(String appId, Env env, String clusterName, String namespaceName) {
public NamespaceDTO loadNamespaceBaseInfo(String appId, Env env, String clusterName,
String namespaceName) {
NamespaceDTO namespace = namespaceAPI.loadNamespace(appId, env, clusterName, namespaceName);
if (namespace == null) {
throw new BadRequestException("namespaces not exist");
......@@ -125,7 +136,7 @@ public class NamespaceService {
namespaceBOs.add(namespaceBO);
} catch (Exception e) {
logger.error("parse namespace error. app id:{}, env:{}, clusterName:{}, namespace:{}",
appId, env, clusterName, namespace.getNamespaceName(), e);
appId, env, clusterName, namespace.getNamespaceName(), e);
throw e;
}
}
......@@ -137,12 +148,14 @@ public class NamespaceService {
return namespaceAPI.findNamespaceByCluster(appId, env, clusterName);
}
public List<NamespaceDTO> getPublicAppNamespaceAllNamespaces(Env env, String publicNamespaceName, int page,
int size) {
public List<NamespaceDTO> getPublicAppNamespaceAllNamespaces(Env env, String publicNamespaceName,
int page,
int size) {
return namespaceAPI.getPublicAppNamespaceAllNamespaces(env, publicNamespaceName, page, size);
}
public NamespaceBO loadNamespaceBO(String appId, Env env, String clusterName, String namespaceName) {
public NamespaceBO loadNamespaceBO(String appId, Env env, String clusterName,
String namespaceName) {
NamespaceDTO namespace = namespaceAPI.loadNamespace(appId, env, clusterName, namespaceName);
if (namespace == null) {
throw new BadRequestException("namespaces not exist");
......@@ -150,7 +163,8 @@ public class NamespaceService {
return transformNamespace2BO(env, namespace);
}
public boolean namespaceHasInstances(String appId, Env env, String clusterName, String namespaceName) {
public boolean namespaceHasInstances(String appId, Env env, String clusterName,
String namespaceName) {
return instanceService.getInstanceCountByNamepsace(appId, env, clusterName, namespaceName) > 0;
}
......@@ -159,9 +173,10 @@ public class NamespaceService {
}
public NamespaceBO findPublicNamespaceForAssociatedNamespace(Env env, String appId,
String clusterName, String namespaceName) {
String clusterName, String namespaceName) {
NamespaceDTO namespace =
namespaceAPI.findPublicNamespaceForAssociatedNamespace(env, appId, clusterName, namespaceName);
namespaceAPI
.findPublicNamespaceForAssociatedNamespace(env, appId, clusterName, namespaceName);
return transformNamespace2BO(env, namespace);
}
......@@ -229,7 +244,8 @@ public class NamespaceService {
NamespaceDTO namespaceDTO = namespace.getBaseInfo();
//先从当前appId下面找,包含私有的和公共的
AppNamespace appNamespace =
appNamespaceService.findByAppIdAndName(namespaceDTO.getAppId(), namespaceDTO.getNamespaceName());
appNamespaceService
.findByAppIdAndName(namespaceDTO.getAppId(), namespaceDTO.getNamespaceName());
//再从公共的app namespace里面找
if (appNamespace == null) {
appNamespace = appNamespaceService.findPublicAppNamespace(namespaceDTO.getNamespaceName());
......@@ -291,4 +307,16 @@ public class NamespaceService {
return itemBO;
}
public void assignNamespaceRoleToOperator(String appId, String namespaceName, String operator) {
//default assign modify、release namespace role to namespace creator
rolePermissionService
.assignRoleToUsers(
RoleUtils.buildNamespaceRoleName(appId, namespaceName, RoleType.MODIFY_NAMESPACE),
Sets.newHashSet(operator), operator);
rolePermissionService
.assignRoleToUsers(
RoleUtils.buildNamespaceRoleName(appId, namespaceName, RoleType.RELEASE_NAMESPACE),
Sets.newHashSet(operator), operator);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment