Search in sources :

Example 6 with User

use of org.eclipse.vorto.repository.domain.User in project vorto by eclipse.

the class AccountController method upgradeUserAccount.

@PostMapping("/rest/accounts/{username:.+}/updateTask")
@PreAuthorize("hasAuthority('sysadmin') or #username == authentication.name")
public ResponseEntity<Boolean> upgradeUserAccount(Principal user, @ApiParam(value = "Username", required = true) @PathVariable String username) {
    User userAccount = accountService.getUser(username);
    if (userAccount == null) {
        return new ResponseEntity<>(true, HttpStatus.NOT_FOUND);
    }
    updateService.installUserUpgrade(userAccount, () -> user);
    return new ResponseEntity<>(true, HttpStatus.CREATED);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) User(org.eclipse.vorto.repository.domain.User) PostMapping(org.springframework.web.bind.annotation.PostMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 7 with User

use of org.eclipse.vorto.repository.domain.User in project vorto by eclipse.

the class AccountController method createTechnicalUser.

/**
 * This endpoint is added for suite integration. <br/>
 * It allows any authenticated user to create a technical user <b>if</b> that user does not exist
 * already, without the need to immediately associate that user to an existing namespace owned
 * by the requestor. <br/>
 * This is currently used by the "request access to namespace" form in its standalone version,
 * when the form is also parametrized with at least a {@code userId} whose value is the name of
 * the technical user to create (or not). <br/>
 * Returns HTTP {@literal 200} if the technical user already exists, {@literal 201} if created
 * successfully or {@literal 400} if the user cannot be created due to bad parameter values.<br/>
 * Parameter sanitization is mostly done through Spring security, and at service level.<br/>
 * Note that in the current implementation, the parametrized standalone form will first ask the
 * back-end whether the given technical use exists anyway. <br/>
 * While doubling the amount of networking, this is conductive to a workflow where the end user
 * opening the parametrized form has to click a button explicitly when creating the technical
 * user, instead of the form doing so automatically when loading parametrized. <br/>
 * In other words, the duplicate networking call (first ask if user exists, then if not allow
 * creating it) restricts possible automated abuse of technical user creation, by means of
 * a forced UI interaction.
 *
 * @param technicalUser
 * @return
 * @see AccountController#getUser(String)
 */
@PostMapping(consumes = "application/json", value = "/rest/accounts/createTechnicalUser")
@PreAuthorize("isAuthenticated()")
public ResponseEntity<OperationResult> createTechnicalUser(@RequestBody @ApiParam(value = "The technical user to be created", required = true) final UserDto technicalUser) {
    // user exists - do nothing and return false / 200
    User existingUser = accountService.getUser(technicalUser.getUsername());
    if (existingUser != null) {
        return new ResponseEntity<>(OperationResult.success("User already exists"), HttpStatus.OK);
    }
    // user does not exist
    // getting calling user
    User actor = accountService.getUser(UserContext.user(SecurityContextHolder.getContext().getAuthentication()).getUsername());
    try {
        // adding date fields
        technicalUser.setDateCreated(Timestamp.from(Instant.now()));
        technicalUser.setLastUpdated(Timestamp.from(Instant.now()));
        // UI will inform end-user that by creating the technical user, the terms and conditions are
        // considered to be approved
        userService.createOrUpdateTechnicalUser(actor, technicalUser.toUser());
        return new ResponseEntity<>(OperationResult.success(), HttpStatus.CREATED);
    } catch (InvalidUserException iue) {
        LOGGER.warn("Invalid technical user creation request.", iue);
        return new ResponseEntity<>(OperationResult.failure(iue.getMessage()), HttpStatus.BAD_REQUEST);
    }
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) User(org.eclipse.vorto.repository.domain.User) InvalidUserException(org.eclipse.vorto.repository.services.exceptions.InvalidUserException) PostMapping(org.springframework.web.bind.annotation.PostMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 8 with User

use of org.eclipse.vorto.repository.domain.User in project vorto by eclipse.

the class AccountControllerTest method verifyTechnicalUserCreatedBy.

@Test
public void verifyTechnicalUserCreatedBy() throws Exception {
    UserDto payload = UserDto.fromUser(new UserBuilder().withName("theTechnicalUser").withAuthenticationProviderID("GITHUB").withAuthenticationSubject("theSubject").build());
    repositoryServer.perform(post("/rest/accounts/createTechnicalUser").content(objectMapper.writeValueAsString(payload)).contentType("application/json").with(userSysadmin)).andExpect(status().isCreated());
    // fetch sysadmin id
    User sysadmin = userRepository.findByUsername(USER_SYSADMIN_NAME);
    assertNotNull(sysadmin);
    // compare with the tech user created by
    User theTechnicalUser = userRepository.findByUsername("theTechnicalUser");
    assertNotNull(theTechnicalUser);
    assertEquals(sysadmin.getId(), theTechnicalUser.getCreatedBy());
}
Also used : User(org.eclipse.vorto.repository.domain.User) UserDto(org.eclipse.vorto.repository.web.account.dto.UserDto) UserBuilder(org.eclipse.vorto.repository.services.UserBuilder) Test(org.junit.Test)

Example 9 with User

use of org.eclipse.vorto.repository.domain.User in project vorto by eclipse.

the class RepositoryInitializer method createAdminUser.

private void createAdminUser(String username, long id) {
    if (userRepository.findByUsername(username) == null) {
        logger.info("Creating admin user: {}", username);
        User user = null;
        try {
            user = new UserBuilder().withName(username).build();
        } catch (InvalidUserException iue) {
            logger.warn("Unable to create admin user - skipping.");
            return;
        }
        // TODO : set to be configurable from configuration file
        user.setEmailAddress("vorto-dev@bosch-si.com");
        user.setAuthenticationProviderId("GITHUB");
        user.setTechnicalUser(false);
        userRepository.save(user);
    }
    User user = userRepository.findByUsername(username);
    UserRepositoryRoles roles = userRepositoryRoleRepository.findByUser(user.getId()).orElse(new UserRepositoryRoles());
    if (roles.getUser() == null) {
        roles.setUser(user);
    }
    if (roles.getId() == null) {
        roles.setId(id);
    }
    roles.setRoles(RepositoryRole.SYS_ADMIN.getRole());
    userRepositoryRoleRepository.save(roles);
}
Also used : User(org.eclipse.vorto.repository.domain.User) UserRepositoryRoles(org.eclipse.vorto.repository.domain.UserRepositoryRoles) InvalidUserException(org.eclipse.vorto.repository.services.exceptions.InvalidUserException) UserBuilder(org.eclipse.vorto.repository.services.UserBuilder)

Example 10 with User

use of org.eclipse.vorto.repository.domain.User in project vorto by eclipse.

the class UserAccountServiceTest method testCreateUserAlreadyExists.

@Test(expected = IllegalArgumentException.class)
public void testCreateUserAlreadyExists() throws Exception {
    User user = setupUser("alex");
    when(userRepository.findByUsername("alex")).thenReturn(user);
    accountService.createNonTechnicalUser(user.getUsername(), "GITHUB", null);
}
Also used : User(org.eclipse.vorto.repository.domain.User) Test(org.junit.Test)

Aggregations

User (org.eclipse.vorto.repository.domain.User)36 ResponseEntity (org.springframework.http.ResponseEntity)13 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)12 IUserContext (org.eclipse.vorto.repository.core.IUserContext)8 DoesNotExistException (org.eclipse.vorto.repository.services.exceptions.DoesNotExistException)8 PostMapping (org.springframework.web.bind.annotation.PostMapping)8 OperationForbiddenException (org.eclipse.vorto.repository.services.exceptions.OperationForbiddenException)7 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)7 Optional (java.util.Optional)6 InvalidUserException (org.eclipse.vorto.repository.services.exceptions.InvalidUserException)6 ApiParam (io.swagger.annotations.ApiParam)5 Collection (java.util.Collection)5 Map (java.util.Map)5 Collectors (java.util.stream.Collectors)5 DefaultUserAccountService (org.eclipse.vorto.repository.account.impl.DefaultUserAccountService)5 Namespace (org.eclipse.vorto.repository.domain.Namespace)5 NamespaceService (org.eclipse.vorto.repository.services.NamespaceService)5 UserNamespaceRoleService (org.eclipse.vorto.repository.services.UserNamespaceRoleService)5 Test (org.junit.Test)5 Autowired (org.springframework.beans.factory.annotation.Autowired)5