Commit 0cae4838 authored by Zhuohao Li's avatar Zhuohao Li Committed by Jason Song

Add OpenAPI to rollback config. (#2351) (#2581)

parent fe281135
...@@ -6,6 +6,7 @@ import com.ctrip.framework.apollo.common.utils.BeanUtils; ...@@ -6,6 +6,7 @@ import com.ctrip.framework.apollo.common.utils.BeanUtils;
import com.ctrip.framework.apollo.common.utils.RequestPrecondition; import com.ctrip.framework.apollo.common.utils.RequestPrecondition;
import com.ctrip.framework.apollo.core.enums.Env; import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.core.utils.StringUtils; import com.ctrip.framework.apollo.core.utils.StringUtils;
import com.ctrip.framework.apollo.openapi.auth.ConsumerPermissionValidator;
import com.ctrip.framework.apollo.openapi.dto.NamespaceGrayDelReleaseDTO; import com.ctrip.framework.apollo.openapi.dto.NamespaceGrayDelReleaseDTO;
import com.ctrip.framework.apollo.openapi.dto.NamespaceReleaseDTO; import com.ctrip.framework.apollo.openapi.dto.NamespaceReleaseDTO;
import com.ctrip.framework.apollo.openapi.dto.OpenReleaseDTO; import com.ctrip.framework.apollo.openapi.dto.OpenReleaseDTO;
...@@ -15,19 +16,18 @@ import com.ctrip.framework.apollo.portal.entity.model.NamespaceReleaseModel; ...@@ -15,19 +16,18 @@ import com.ctrip.framework.apollo.portal.entity.model.NamespaceReleaseModel;
import com.ctrip.framework.apollo.portal.service.NamespaceBranchService; import com.ctrip.framework.apollo.portal.service.NamespaceBranchService;
import com.ctrip.framework.apollo.portal.service.ReleaseService; import com.ctrip.framework.apollo.portal.service.ReleaseService;
import com.ctrip.framework.apollo.portal.spi.UserService; import com.ctrip.framework.apollo.portal.spi.UserService;
import javax.servlet.http.HttpServletRequest;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import static com.ctrip.framework.apollo.common.utils.RequestPrecondition.checkModel;
@RestController("openapiReleaseController") @RestController("openapiReleaseController")
@RequestMapping("/openapi/v1/envs/{env}") @RequestMapping("/openapi/v1/envs/{env}")
public class ReleaseController { public class ReleaseController {
...@@ -35,14 +35,17 @@ public class ReleaseController { ...@@ -35,14 +35,17 @@ public class ReleaseController {
private final ReleaseService releaseService; private final ReleaseService releaseService;
private final UserService userService; private final UserService userService;
private final NamespaceBranchService namespaceBranchService; private final NamespaceBranchService namespaceBranchService;
private final ConsumerPermissionValidator consumerPermissionValidator;
public ReleaseController( public ReleaseController(
final ReleaseService releaseService, final ReleaseService releaseService,
final UserService userService, final UserService userService,
final NamespaceBranchService namespaceBranchService) { final NamespaceBranchService namespaceBranchService,
final ConsumerPermissionValidator consumerPermissionValidator) {
this.releaseService = releaseService; this.releaseService = releaseService;
this.userService = userService; this.userService = userService;
this.namespaceBranchService = namespaceBranchService; this.namespaceBranchService = namespaceBranchService;
this.consumerPermissionValidator = consumerPermissionValidator;
} }
@PreAuthorize(value = "@consumerPermissionValidator.hasReleaseNamespacePermission(#request, #appId, #namespaceName, #env)") @PreAuthorize(value = "@consumerPermissionValidator.hasReleaseNamespacePermission(#request, #appId, #namespaceName, #env)")
...@@ -155,4 +158,21 @@ public class ReleaseController { ...@@ -155,4 +158,21 @@ public class ReleaseController {
return OpenApiBeanUtils.transformFromReleaseDTO(releaseService.publish(releaseModel, releaseModel.getReleasedBy())); return OpenApiBeanUtils.transformFromReleaseDTO(releaseService.publish(releaseModel, releaseModel.getReleasedBy()));
} }
@PutMapping(path = "/releases/{releaseId}/rollback")
public void rollback(@PathVariable String env,
@PathVariable long releaseId, HttpServletRequest request) {
ReleaseDTO release = releaseService.findReleaseById(Env.valueOf(env), releaseId);
if (release == null) {
throw new BadRequestException("release not found");
}
if (!consumerPermissionValidator.hasReleaseNamespacePermission(request,release.getAppId(), release.getNamespaceName(), env)) {
throw new AccessDeniedException("Forbidden operation. you don't have release permission");
}
releaseService.rollback(Env.valueOf(env), releaseId);
}
} }
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