Commit 9b6a6985 by lepdou

bugfix public appnamespace unique & repeat create namespace permission

parent 38f02745
......@@ -44,12 +44,17 @@ public class GlobalDefaultExceptionHandler {
return handleError(request, INTERNAL_SERVER_ERROR, ex);
}
@ExceptionHandler({HttpRequestMethodNotSupportedException.class, HttpMediaTypeException.class, BadRequestException.class})
@ExceptionHandler({HttpRequestMethodNotSupportedException.class, HttpMediaTypeException.class})
public ResponseEntity<Map<String, Object>> badRequest(HttpServletRequest request,
ServletException ex) {
return handleError(request, BAD_REQUEST, ex);
}
@ExceptionHandler({BadRequestException.class})
public ResponseEntity<Map<String, Object>> badRequest(HttpServletRequest request, BadRequestException ex) {
return handleError(request, BAD_REQUEST, ex);
}
@ExceptionHandler(NotFoundException.class)
public ResponseEntity<Map<String, Object>> notFound(HttpServletRequest request,
NotFoundException ex) {
......
......@@ -5,13 +5,18 @@ import com.ctrip.framework.apollo.core.dto.NamespaceDTO;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.core.utils.StringUtils;
import com.ctrip.framework.apollo.portal.auth.UserInfoHolder;
import com.ctrip.framework.apollo.portal.entity.form.NamespaceCreationModel;
import com.ctrip.framework.apollo.portal.entity.vo.NamespaceVO;
import com.ctrip.framework.apollo.portal.listener.AppNamespaceCreationEvent;
import com.ctrip.framework.apollo.portal.service.NamespaceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
......@@ -21,10 +26,13 @@ import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import static com.ctrip.framework.apollo.portal.util.RequestPrecondition.checkArgument;
import static com.ctrip.framework.apollo.portal.util.RequestPrecondition.checkModel;
@RestController
public class NamespaceController {
Logger logger = LoggerFactory.getLogger(NamespaceController.class);
@Autowired
private ApplicationEventPublisher publisher;
@Autowired
......@@ -37,13 +45,24 @@ public class NamespaceController {
return namespaceService.findPublicAppNamespaces();
}
@PreAuthorize(value = "@permissionValidator.hasCreateNamespacePermission(#namespace.appId)")
@RequestMapping(value = "/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces", method = RequestMethod.POST)
public NamespaceDTO createNamespace(@PathVariable String env, @RequestBody NamespaceDTO namespace) {
@PreAuthorize(value = "@permissionValidator.hasCreateNamespacePermission(#appId)")
@RequestMapping(value = "/apps/{appId}/namespaces", method = RequestMethod.POST)
public ResponseEntity<Void> createNamespace(@PathVariable String appId, @RequestBody List<NamespaceCreationModel> models) {
checkModel(!CollectionUtils.isEmpty(models));
for (NamespaceCreationModel model : models) {
NamespaceDTO namespace = model.getNamespace();
checkArgument(namespace.getAppId(), namespace.getClusterName(), namespace.getNamespaceName());
checkArgument(model.getEnv(), namespace.getAppId(), namespace.getClusterName(), namespace.getNamespaceName());
return namespaceService.createNamespace(Env.valueOf(env), namespace);
try {
namespaceService.createNamespace(Env.valueOf(model.getEnv()), namespace);
} catch (Exception e) {
logger.error("create namespace error.", e);
}
}
return ResponseEntity.ok().build();
}
@RequestMapping(value = "/apps/{appId}/appnamespaces", method = RequestMethod.POST)
......
package com.ctrip.framework.apollo.portal.cumsomize;
package com.ctrip.framework.apollo.portal.customize;
import com.ctrip.framework.apollo.common.customize.LoggingCustomizer;
import com.ctrip.framework.apollo.portal.repository.ServerConfigRepository;
......
/**
* 携程内部的日志系统,第三方公司可删除
*/
package com.ctrip.framework.apollo.portal.cumsomize;
package com.ctrip.framework.apollo.portal.customize;
package com.ctrip.framework.apollo.portal.entity.form;
import com.ctrip.framework.apollo.core.dto.NamespaceDTO;
public class NamespaceCreationModel {
private String env;
private NamespaceDTO namespace;
public String getEnv() {
return env;
}
public void setEnv(String env) {
this.env = env;
}
public NamespaceDTO getNamespace() {
return namespace;
}
public void setNamespace(NamespaceDTO namespace) {
this.namespace = namespace;
}
}
......@@ -11,6 +11,7 @@ import com.ctrip.framework.apollo.core.dto.ItemDTO;
import com.ctrip.framework.apollo.core.dto.NamespaceDTO;
import com.ctrip.framework.apollo.core.dto.ReleaseDTO;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.core.exception.BadRequestException;
import com.ctrip.framework.apollo.core.exception.ServiceException;
import com.ctrip.framework.apollo.core.utils.StringUtils;
import com.ctrip.framework.apollo.portal.PortalSettings;
......@@ -97,6 +98,10 @@ public class NamespaceService {
@Transactional
public AppNamespace createAppNamespaceInLocal(AppNamespace appNamespace) {
//not unique
if (appNamespaceRepository.findByName(appNamespace.getName()) != null){
throw new BadRequestException(appNamespace.getName() + "已存在");
}
AppNamespace managedAppNamespace = appNamespaceRepository.findByAppIdAndName(appNamespace.getAppId(), appNamespace.getName());
//update
if (managedAppNamespace != null){
......
......@@ -47,22 +47,23 @@ namespace_module.controller("LinkNamespaceController",
}
var hasCreatedClusterCnt = 0;
var namespaceCreationModels = [];
selectedClusters.forEach(function (cluster) {
NamespaceService.createNamespace($scope.appId, cluster.env, cluster.clusterName,
$scope.namespaceName).then(function (result) {
toastr.success(
cluster.env + "_" + result.clusterName + "_" + result.namespaceName
+ "创建成功");
hasCreatedClusterCnt ++;
if (hasCreatedClusterCnt == selectedClustersSize){
$scope.step = 2;
namespaceCreationModels.push({
env: cluster.env,
namespace: {
appId: $scope.appId,
clusterName: cluster.clusterName,
namespaceName: $scope.namespaceName
}
}, function (result) {
toastr.error(AppUtil.errorMsg(result),
cluster.env + "_" + cluster.clusterName + "_"
+ $scope.namespaceName + "创建失败");
});
});
NamespaceService.createNamespace($scope.appId, namespaceCreationModels)
.then(function (result) {
toastr.success("创建成功");
}, function (result) {
toastr.error(AppUtil.errorMsg(result));
});
}else {
NamespaceService.createAppNamespace($scope.appId, $scope.appNamespace).then(function (result) {
$scope.step = 2;
......
......@@ -7,7 +7,7 @@ appService.service("NamespaceService", ['$resource', '$q', function ($resource,
},
createNamespace: {
method: 'POST',
url: '/apps/:appId/envs/:env/clusters/:clusterName/namespaces',
url: '/apps/:appId/namespaces',
isArray: false
},
createAppNamespace: {
......@@ -27,17 +27,11 @@ appService.service("NamespaceService", ['$resource', '$q', function ($resource,
});
return d.promise;
},
createNamespace: function (appId, env, clusterName, namespaceName) {
createNamespace: function (appId, namespaceCreationModel) {
var d = $q.defer();
namespace_source.createNamespace({
appId: appId,
env: env,
clusterName: clusterName
}, {
appId: appId,
clusterName: clusterName,
namespaceName: namespaceName
}, function (result) {
appId: appId
}, namespaceCreationModel, function (result) {
d.resolve(result);
}, function (result) {
d.reject(result);
......
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