use of ca.corefacility.bioinformatics.irida.model.user.User in project irida by phac-nml.
the class RESTProjectUsersController method addUserToProject.
/**
* Add a relationship between a {@link Project} and a {@link User}.
*
* @param projectId
* the project ID to add the user to.
* @param representation
* the JSON key-value pair that contains the identifier for the
* project and the identifier for the user.
* @param response
* a reference to the servlet response.
* @return a response indicating that the collection was modified.
* @throws ProjectWithoutOwnerException
* this cannot actually be thrown, it's an artifact of using
* spring HATEOAS {@code linkTo} and {@code methodOn}.
*/
@RequestMapping(value = "/api/projects/{projectId}/users", method = RequestMethod.POST)
public ModelMap addUserToProject(@PathVariable Long projectId, @RequestBody Map<String, String> representation, HttpServletResponse response) throws ProjectWithoutOwnerException {
// first, get the project
Project p = projectService.read(projectId);
String username = representation.get(USER_ID_KEY);
// then, get the user
User u = userService.getUserByUsername(username);
ProjectRole r = ProjectRole.PROJECT_USER;
// then add the user to the project with the specified role.
Join<Project, User> joined = projectService.addUserToProject(p, u, r);
LabelledRelationshipResource<Project, User> lrr = new LabelledRelationshipResource<Project, User>(joined.getLabel(), joined);
// prepare a link to the user
lrr.add(linkTo(RESTUsersController.class).slash(u.getUsername()).withSelfRel());
// prepare a link to the user as added to the project
lrr.add(linkTo(methodOn(RESTProjectUsersController.class).removeUserFromProject(projectId, u.getUsername())).withRel(RESTGenericController.REL_RELATIONSHIP));
// prepare a link back to the user collection of the project
lrr.add(linkTo(methodOn(RESTProjectUsersController.class).getUsersForProject(projectId)).withRel(REL_PROJECT_USERS));
// prepare a link back to the project
lrr.add(linkTo(RESTProjectsController.class).slash(projectId).withRel(RESTProjectsController.REL_PROJECT));
String location = linkTo(RESTProjectsController.class).slash(projectId).slash("users").slash(username).withSelfRel().getHref();
// add a location header and set the response status.
response.addHeader(HttpHeaders.LOCATION, location);
response.setStatus(HttpStatus.CREATED.value());
// prepare the response for the client
ModelMap modelMap = new ModelMap();
modelMap.addAttribute(RESTGenericController.RESOURCE_NAME, lrr);
return modelMap;
}
use of ca.corefacility.bioinformatics.irida.model.user.User in project irida by phac-nml.
the class RESTProjectUsersController method removeUserFromProject.
/**
* Remove the relationship between a {@link Project} and a {@link User}.
*
* @param projectId the {@link Project} identifier to remove the {@link User} from.
* @param userId the {@link User} identifier to remove from the {@link Project}.
* @return a response including links back to the {@link Project} and the {@link User} collection for
* the {@link Project}.
* @throws ProjectWithoutOwnerException if removing this user will leave the project without an owner
*/
@RequestMapping(value = "/api/projects/{projectId}/users/{userId}", method = RequestMethod.DELETE)
public ModelMap removeUserFromProject(@PathVariable Long projectId, @PathVariable String userId) throws ProjectWithoutOwnerException {
// Read the project and user from the database
Project p = projectService.read(projectId);
User u = userService.getUserByUsername(userId);
// ask the project service to remove the user from the project
projectService.removeUserFromProject(p, u);
// prepare a link back to the user collection of the project
RootResource response = new RootResource();
response.add(linkTo(methodOn(RESTProjectUsersController.class).getUsersForProject(projectId)).withRel(REL_PROJECT_USERS));
// prepare a link back to the project
response.add(linkTo(RESTProjectsController.class).slash(projectId).withRel(RESTProjectsController.REL_PROJECT));
// respond to the client
ModelMap modelMap = new ModelMap();
modelMap.addAttribute(RESTGenericController.RESOURCE_NAME, response);
return modelMap;
}
use of ca.corefacility.bioinformatics.irida.model.user.User in project irida by phac-nml.
the class ProjectEventHandlerTest method testDelegateUserRemoved.
@Test
public void testDelegateUserRemoved() {
Class<? extends ProjectEvent> clazz = UserRemovedProjectEvent.class;
Project project = new Project();
User user = new User();
Object[] args = { project, user };
MethodEvent methodEvent = new MethodEvent(clazz, null, args);
when(eventRepository.save(any(ProjectEvent.class))).thenReturn(new UserRemovedProjectEvent(project, user));
handler.delegate(methodEvent);
ArgumentCaptor<ProjectEvent> captor = ArgumentCaptor.forClass(ProjectEvent.class);
verify(eventRepository).save(captor.capture());
ProjectEvent event = captor.getValue();
assertTrue(event instanceof UserRemovedProjectEvent);
verify(projectRepository).save(any(Project.class));
}
use of ca.corefacility.bioinformatics.irida.model.user.User in project irida by phac-nml.
the class ProjectEventHandlerTest method testOtherEvent.
@Test
public void testOtherEvent() {
Class<? extends ProjectEvent> clazz = ProjectEvent.class;
Project project = new Project();
User user = new User();
Object[] args = { project, user };
MethodEvent methodEvent = new MethodEvent(clazz, null, args);
handler.delegate(methodEvent);
verifyZeroInteractions(eventRepository);
}
use of ca.corefacility.bioinformatics.irida.model.user.User in project irida by phac-nml.
the class UserTest method testShortPassword.
@Test
public void testShortPassword() {
User u = new User();
u.setPassword("Sma1!");
Set<ConstraintViolation<User>> constraintViolations = validator.validateProperty(u, "password");
assertEquals(1, constraintViolations.size());
assertEquals(b.getString("user.password.size"), constraintViolations.iterator().next().getMessage());
}
Aggregations