mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 23:45:44 +01:00
#9208 add group permission prefix
This commit is contained in:
@@ -1,4 +1,11 @@
|
|||||||
package sonia.scm;
|
package sonia.scm;
|
||||||
|
|
||||||
public class AlreadyExistsException extends Exception {
|
public class AlreadyExistsException extends Exception {
|
||||||
|
|
||||||
|
public AlreadyExistsException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public AlreadyExistsException() {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,4 +7,9 @@ public class NotFoundException extends Exception {
|
|||||||
|
|
||||||
public NotFoundException() {
|
public NotFoundException() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public NotFoundException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ import javax.ws.rs.ext.Provider;
|
|||||||
public class AlreadyExistsExceptionMapper implements ExceptionMapper<AlreadyExistsException> {
|
public class AlreadyExistsExceptionMapper implements ExceptionMapper<AlreadyExistsException> {
|
||||||
@Override
|
@Override
|
||||||
public Response toResponse(AlreadyExistsException exception) {
|
public Response toResponse(AlreadyExistsException exception) {
|
||||||
return Response.status(Status.CONFLICT).build();
|
return Response.status(Status.CONFLICT)
|
||||||
|
.entity(exception.getMessage())
|
||||||
|
.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -90,6 +90,8 @@ public class StatusExceptionMapper<E extends Throwable>
|
|||||||
logger.debug(msg.toString());
|
logger.debug(msg.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
return Response.status(status).build();
|
return Response.status(status)
|
||||||
|
.entity(exception.getMessage())
|
||||||
|
.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,14 @@ public class PermissionDto extends HalRepresentation {
|
|||||||
|
|
||||||
private boolean groupPermission = false;
|
private boolean groupPermission = false;
|
||||||
|
|
||||||
|
public PermissionDto() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public PermissionDto(String permissionName, boolean groupPermission) {
|
||||||
|
name = permissionName;
|
||||||
|
this.groupPermission = groupPermission;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("squid:S1185") // We want to have this method available in this package
|
@SuppressWarnings("squid:S1185") // We want to have this method available in this package
|
||||||
|
|||||||
@@ -136,6 +136,7 @@ public class PermissionRootResource {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Update a permission to the user or group managed by the repository
|
* Update a permission to the user or group managed by the repository
|
||||||
|
* ignore the user input for groupPermission and take it from the path parameter (if the group prefix (@) exists it is a group permission)
|
||||||
*
|
*
|
||||||
* @param permission permission to modify
|
* @param permission permission to modify
|
||||||
* @param permissionName permission to modify
|
* @param permissionName permission to modify
|
||||||
@@ -153,10 +154,18 @@ public class PermissionRootResource {
|
|||||||
public Response update(@PathParam("namespace") String namespace,
|
public Response update(@PathParam("namespace") String namespace,
|
||||||
@PathParam("name") String name,
|
@PathParam("name") String name,
|
||||||
@PathParam("permission-name") String permissionName,
|
@PathParam("permission-name") String permissionName,
|
||||||
PermissionDto permission) throws NotFoundException {
|
PermissionDto permission) throws NotFoundException, AlreadyExistsException {
|
||||||
log.info("try to update the permission with name: {}. the modified permission is: {}", permissionName, permission);
|
log.info("try to update the permission with name: {}. the modified permission is: {}", permissionName, permission);
|
||||||
Repository repository = load(namespace, name);
|
Repository repository = load(namespace, name);
|
||||||
RepositoryPermissions.permissionWrite(repository).check();
|
RepositoryPermissions.permissionWrite(repository).check();
|
||||||
|
String extractedPermissionName = getPermissionName(permissionName);
|
||||||
|
if (!isPermissionExist(new PermissionDto(extractedPermissionName, isGroupPermission(permissionName)), repository)) {
|
||||||
|
throw new NotFoundException("the permission " + extractedPermissionName + " does not exist");
|
||||||
|
}
|
||||||
|
permission.setGroupPermission(isGroupPermission(permissionName));
|
||||||
|
if (!extractedPermissionName.equals(permission.getName())) {
|
||||||
|
checkPermissionAlreadyExists(permission, repository, "target permission " + permission.getName() + " already exists");
|
||||||
|
}
|
||||||
Permission existingPermission = repository.getPermissions()
|
Permission existingPermission = repository.getPermissions()
|
||||||
.stream()
|
.stream()
|
||||||
.filter(filterPermission(permissionName))
|
.filter(filterPermission(permissionName))
|
||||||
@@ -201,13 +210,19 @@ public class PermissionRootResource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Predicate<Permission> filterPermission(String permissionName) {
|
Predicate<Permission> filterPermission(String permissionName) {
|
||||||
boolean isGroupPermission = permissionName.startsWith(GROUP_PREFIX);
|
return permission -> getPermissionName(permissionName).equals(permission.getName())
|
||||||
return permission -> Optional.of(permissionName)
|
|
||||||
.filter(p -> !isGroupPermission)
|
|
||||||
.orElse(permissionName.substring(1))
|
|
||||||
.equals(permission.getName())
|
|
||||||
&&
|
&&
|
||||||
permission.isGroupPermission() == isGroupPermission;
|
permission.isGroupPermission() == isGroupPermission(permissionName);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getPermissionName(String permissionName) {
|
||||||
|
return Optional.of(permissionName)
|
||||||
|
.filter(p -> !isGroupPermission(permissionName))
|
||||||
|
.orElse(permissionName.substring(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isGroupPermission(String permissionName) {
|
||||||
|
return permissionName.startsWith(GROUP_PREFIX);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -230,15 +245,23 @@ public class PermissionRootResource {
|
|||||||
*
|
*
|
||||||
* @param permission the searched permission
|
* @param permission the searched permission
|
||||||
* @param repository the repository to be inspected
|
* @param repository the repository to be inspected
|
||||||
|
* @param errorMessage error message
|
||||||
* @throws AlreadyExistsException if the permission already exists in the repository
|
* @throws AlreadyExistsException if the permission already exists in the repository
|
||||||
*/
|
*/
|
||||||
private void checkPermissionAlreadyExists(PermissionDto permission, Repository repository) throws AlreadyExistsException {
|
private void checkPermissionAlreadyExists(PermissionDto permission, Repository repository, String errorMessage) throws AlreadyExistsException {
|
||||||
boolean isPermissionAlreadyExist = repository.getPermissions()
|
if (isPermissionExist(permission, repository)) {
|
||||||
|
throw new AlreadyExistsException(errorMessage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isPermissionExist(PermissionDto permission, Repository repository) {
|
||||||
|
return repository.getPermissions()
|
||||||
.stream()
|
.stream()
|
||||||
.anyMatch(p -> p.getName().equals(permission.getName()) && p.isGroupPermission() == permission.isGroupPermission());
|
.anyMatch(p -> p.getName().equals(permission.getName()) && p.isGroupPermission() == permission.isGroupPermission());
|
||||||
if (isPermissionAlreadyExist) {
|
}
|
||||||
throw new AlreadyExistsException();
|
|
||||||
}
|
private void checkPermissionAlreadyExists(PermissionDto permission, Repository repository) throws AlreadyExistsException {
|
||||||
|
checkPermissionAlreadyExists(permission, repository, "the permission " + permission.getName() + " already exist.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user