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;
}
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;
}
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);
}
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;
}
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;
}
Aggregations