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