Search in sources :

Example 46 with User

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;
}
Also used : Project(ca.corefacility.bioinformatics.irida.model.project.Project) LabelledRelationshipResource(ca.corefacility.bioinformatics.irida.web.assembler.resource.LabelledRelationshipResource) User(ca.corefacility.bioinformatics.irida.model.user.User) ModelMap(org.springframework.ui.ModelMap) RESTUsersController(ca.corefacility.bioinformatics.irida.web.controller.api.RESTUsersController) ProjectRole(ca.corefacility.bioinformatics.irida.model.enums.ProjectRole) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 47 with User

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;
}
Also used : RootResource(ca.corefacility.bioinformatics.irida.web.assembler.resource.RootResource) Project(ca.corefacility.bioinformatics.irida.model.project.Project) User(ca.corefacility.bioinformatics.irida.model.user.User) ModelMap(org.springframework.ui.ModelMap) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 48 with User

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));
}
Also used : Project(ca.corefacility.bioinformatics.irida.model.project.Project) User(ca.corefacility.bioinformatics.irida.model.user.User) UserRemovedProjectEvent(ca.corefacility.bioinformatics.irida.model.event.UserRemovedProjectEvent) UserRoleSetProjectEvent(ca.corefacility.bioinformatics.irida.model.event.UserRoleSetProjectEvent) UserRemovedProjectEvent(ca.corefacility.bioinformatics.irida.model.event.UserRemovedProjectEvent) DataAddedToSampleProjectEvent(ca.corefacility.bioinformatics.irida.model.event.DataAddedToSampleProjectEvent) ProjectEvent(ca.corefacility.bioinformatics.irida.model.event.ProjectEvent) SampleAddedProjectEvent(ca.corefacility.bioinformatics.irida.model.event.SampleAddedProjectEvent) Test(org.junit.Test)

Example 49 with User

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);
}
Also used : Project(ca.corefacility.bioinformatics.irida.model.project.Project) User(ca.corefacility.bioinformatics.irida.model.user.User) UserRoleSetProjectEvent(ca.corefacility.bioinformatics.irida.model.event.UserRoleSetProjectEvent) UserRemovedProjectEvent(ca.corefacility.bioinformatics.irida.model.event.UserRemovedProjectEvent) DataAddedToSampleProjectEvent(ca.corefacility.bioinformatics.irida.model.event.DataAddedToSampleProjectEvent) ProjectEvent(ca.corefacility.bioinformatics.irida.model.event.ProjectEvent) SampleAddedProjectEvent(ca.corefacility.bioinformatics.irida.model.event.SampleAddedProjectEvent) Test(org.junit.Test)

Example 50 with User

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());
}
Also used : User(ca.corefacility.bioinformatics.irida.model.user.User) Test(org.junit.Test)

Aggregations

User (ca.corefacility.bioinformatics.irida.model.user.User)252 Test (org.junit.Test)153 Project (ca.corefacility.bioinformatics.irida.model.project.Project)84 WithMockUser (org.springframework.security.test.context.support.WithMockUser)57 Authentication (org.springframework.security.core.Authentication)45 Join (ca.corefacility.bioinformatics.irida.model.joins.Join)34 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)27 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)27 PageRequest (org.springframework.data.domain.PageRequest)26 UserGroup (ca.corefacility.bioinformatics.irida.model.user.group.UserGroup)25 ProjectRole (ca.corefacility.bioinformatics.irida.model.enums.ProjectRole)24 ProjectUserJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectUserJoin)24 AnalysisSubmission (ca.corefacility.bioinformatics.irida.model.workflow.submission.AnalysisSubmission)19 Principal (java.security.Principal)19 ProjectSampleJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin)18 RelatedProjectJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.RelatedProjectJoin)18 List (java.util.List)18 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)17 EntityNotFoundException (ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException)16 ArrayList (java.util.ArrayList)16