Search in sources :

Example 6 with Role

use of org.candlepin.model.Role in project candlepin by candlepin.

the class RoleResource method addUser.

@ApiOperation(notes = "Adds a User to a Role", value = "addUser")
@ApiResponses({ @ApiResponse(code = 404, message = "") })
@POST
@Path("/{role_id}/users/{username}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.WILDCARD)
public Role addUser(@PathParam("role_id") String roleId, @PathParam("username") String username) {
    Role role = lookupRole(roleId);
    User user = lookupUser(username);
    userService.addUserToRole(role, user);
    return role;
}
Also used : Role(org.candlepin.model.Role) User(org.candlepin.model.User) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 7 with Role

use of org.candlepin.model.Role in project candlepin by candlepin.

the class RoleResource method createRole.

@ApiOperation(notes = "Creates a Role", value = "createRole")
@ApiResponses({ @ApiResponse(code = 404, message = "") })
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Role createRole(@ApiParam(name = "role", required = true) Role role) {
    // Attach actual owner objects to each incoming permission:
    for (PermissionBlueprint p : role.getPermissions()) {
        Owner temp = p.getOwner();
        Owner actual = ownerCurator.lookupByKey(temp.getKey());
        if (actual == null) {
            throw new NotFoundException(i18n.tr("No such owner: {0}", temp.getKey()));
        }
        p.setOwner(actual);
    }
    Role r = this.userService.createRole(role);
    return r;
}
Also used : Role(org.candlepin.model.Role) Owner(org.candlepin.model.Owner) PermissionBlueprint(org.candlepin.model.PermissionBlueprint) NotFoundException(org.candlepin.common.exceptions.NotFoundException) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 8 with Role

use of org.candlepin.model.Role in project candlepin by candlepin.

the class RoleResource method updateRole.

@ApiOperation(notes = "Updates a Role.  To avoid race conditions, we do not support " + "updating the user or permission collections. Currently this call will only update " + "the role name. See the specific nested POST/DELETE calls for modifying users and" + " permissions.", value = "updateRole")
@ApiResponses({ @ApiResponse(code = 404, message = "") })
@PUT
@Path("{role_id}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Role updateRole(@PathParam("role_id") String roleId, @ApiParam(name = "role", required = true) Role role) {
    // and that ID is NOT equal to what the ID in the URL is, then throw an error
    if (role.getId() != null && !roleId.equals(role.getId())) {
        throw new BadRequestException(i18n.tr("Role ID does not match path."));
    }
    Role existingRole = lookupRole(roleId);
    existingRole.setName(role.getName());
    return this.userService.updateRole(existingRole);
}
Also used : Role(org.candlepin.model.Role) BadRequestException(org.jboss.resteasy.spi.BadRequestException) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) PUT(javax.ws.rs.PUT)

Example 9 with Role

use of org.candlepin.model.Role in project candlepin by candlepin.

the class RoleResource method addRolePermission.

@ApiOperation(notes = "Adds a Permission to a Role. Returns the updated Role.", value = "addRolePermission")
@ApiResponses({ @ApiResponse(code = 404, message = ""), @ApiResponse(code = 400, message = "") })
@POST
@Path("{role_id}/permissions")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Role addRolePermission(@PathParam("role_id") String roleId, @ApiParam(name = "permissionBlueprint", required = true) PermissionBlueprint permission) {
    Role existingRole = lookupRole(roleId);
    // internal use:
    if (permission.getAccess().equals(Access.NONE)) {
        throw new BadRequestException(i18n.tr("Access type NONE not supported."));
    }
    // Attach actual owner objects to each incoming permission:
    Owner temp = permission.getOwner();
    Owner real = ownerCurator.lookupByKey(temp.getKey());
    permission.setOwner(real);
    existingRole.addPermission(permission);
    Role r = this.userService.updateRole(existingRole);
    return r;
}
Also used : Role(org.candlepin.model.Role) Owner(org.candlepin.model.Owner) BadRequestException(org.jboss.resteasy.spi.BadRequestException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 10 with Role

use of org.candlepin.model.Role in project candlepin by candlepin.

the class RoleResource method removeRolePermission.

@ApiOperation(notes = "Removes a Permission from a Role. Returns the updated Role.", value = "removeRolePermission")
@ApiResponses({ @ApiResponse(code = 404, message = "") })
@DELETE
@Path("{role_id}/permissions/{perm_id}")
@Produces(MediaType.APPLICATION_JSON)
public Role removeRolePermission(@PathParam("role_id") String roleId, @PathParam("perm_id") String permissionId) {
    Role existingRole = lookupRole(roleId);
    Set<PermissionBlueprint> picks = new HashSet<>();
    boolean found = false;
    PermissionBlueprint toRemove = null;
    for (PermissionBlueprint op : existingRole.getPermissions()) {
        if (!op.getId().equals(permissionId)) {
            picks.add(op);
        } else {
            found = true;
            toRemove = op;
        }
    }
    if (!found) {
        throw new NotFoundException(i18n.tr("No such permission: {0} in role: {1}", permissionId, roleId));
    }
    existingRole.setPermissions(picks);
    Role r = this.userService.updateRole(existingRole);
    toRemove.setOwner(null);
    permissionCurator.delete(toRemove);
    return r;
}
Also used : Role(org.candlepin.model.Role) PermissionBlueprint(org.candlepin.model.PermissionBlueprint) NotFoundException(org.candlepin.common.exceptions.NotFoundException) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

Role (org.candlepin.model.Role)23 User (org.candlepin.model.User)14 Test (org.junit.Test)10 PermissionBlueprint (org.candlepin.model.PermissionBlueprint)9 Owner (org.candlepin.model.Owner)8 ApiOperation (io.swagger.annotations.ApiOperation)7 Produces (javax.ws.rs.Produces)7 ApiResponses (io.swagger.annotations.ApiResponses)6 Path (javax.ws.rs.Path)6 HashSet (java.util.HashSet)4 Consumes (javax.ws.rs.Consumes)4 LinkedList (java.util.LinkedList)3 POST (javax.ws.rs.POST)3 UserPrincipal (org.candlepin.auth.UserPrincipal)3 DELETE (javax.ws.rs.DELETE)2 Principal (org.candlepin.auth.Principal)2 OwnerPermission (org.candlepin.auth.permissions.OwnerPermission)2 Permission (org.candlepin.auth.permissions.Permission)2 UsernameConsumersPermission (org.candlepin.auth.permissions.UsernameConsumersPermission)2 NotFoundException (org.candlepin.common.exceptions.NotFoundException)2