Search in sources :

Example 31 with User

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

the class AccountController method createUserAccount.

@PostMapping(consumes = "application/json", value = "/rest/accounts")
@PreAuthorize("hasAuthority('sysadmin') or #user.name == authentication.name")
public ResponseEntity<Boolean> createUserAccount(Principal user) {
    OAuth2Authentication oauth2User = (OAuth2Authentication) user;
    if (accountService.getUser(oauth2User.getName()) != null) {
        return new ResponseEntity<>(false, HttpStatus.CREATED);
    }
    User createdUser = null;
    try {
        createdUser = accountService.createNonTechnicalUser(oauth2User.getName(), getAuthenticationProvider(oauth2User), null);
    } catch (InvalidUserException iue) {
        return new ResponseEntity<>(false, HttpStatus.BAD_REQUEST);
    }
    SpringUserUtils.refreshSpringSecurityUser(createdUser, userNamespaceRoleService);
    return new ResponseEntity<>(true, HttpStatus.CREATED);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) User(org.eclipse.vorto.repository.domain.User) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) InvalidUserException(org.eclipse.vorto.repository.services.exceptions.InvalidUserException) PostMapping(org.springframework.web.bind.annotation.PostMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 32 with User

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

the class AccountController method updateAccount.

@PutMapping("/rest/accounts/{username:.+}")
@PreAuthorize("hasAuthority('sysadmin') or #username == authentication.name")
public ResponseEntity<UserDto> updateAccount(@PathVariable("username") final String username, HttpEntity<String> httpEntity) {
    User account = accountService.getUser(username);
    if (account == null) {
        return new ResponseEntity<>((UserDto) null, HttpStatus.NOT_FOUND);
    }
    account.setEmailAddress(httpEntity.getBody());
    accountService.updateUser(account);
    return new ResponseEntity<>(UserDto.fromUser(account), HttpStatus.OK);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) User(org.eclipse.vorto.repository.domain.User) PutMapping(org.springframework.web.bind.annotation.PutMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 33 with User

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

the class AccountController method getUser.

@GetMapping("/rest/accounts/{username:.+}")
@PreAuthorize("isAuthenticated()")
public ResponseEntity<UserDto> getUser(@ApiParam(value = "Username", required = true) @PathVariable String username) {
    IUserContext userContext = UserContext.user(SecurityContextHolder.getContext().getAuthentication());
    User user = accountService.getUser(ControllerUtils.sanitize(username));
    if (user != null) {
        // logged-on user's name
        return new ResponseEntity<>(UserDto.fromUser(user, !userContext.getUsername().equals(username)), HttpStatus.OK);
    } else {
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
}
Also used : IUserContext(org.eclipse.vorto.repository.core.IUserContext) ResponseEntity(org.springframework.http.ResponseEntity) User(org.eclipse.vorto.repository.domain.User) GetMapping(org.springframework.web.bind.annotation.GetMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 34 with User

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

the class HomeController method getUser.

@ApiOperation(value = "Returns the currently logged in User")
@ApiResponses(value = { @ApiResponse(code = 401, message = "Unauthorized"), @ApiResponse(code = 200, message = "OK") })
@RequestMapping(value = { "/user", "/me" }, method = RequestMethod.GET)
public ResponseEntity<Map<String, Object>> getUser(Principal user, final HttpServletRequest request) throws ParseException {
    Map<String, Object> map = new LinkedHashMap<>();
    if (user == null)
        return new ResponseEntity<>(map, HttpStatus.UNAUTHORIZED);
    IOAuthProvider provider = registry.getByPrincipal(user);
    OAuthUser oauthUser = provider.createUser((OAuth2Authentication) user);
    User userAccount = accountService.getUser(oauthUser.getUserId());
    Date updateCutoff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(updateDate);
    map.put("name", oauthUser.getUserId());
    if (Objects.nonNull(userAccount)) {
        map.put("subject", userAccount.getSubject());
    }
    map.put("displayName", oauthUser.getDisplayName());
    map.put("isRegistered", Boolean.toString(userAccount != null));
    map.put("roles", oauthUser.getRoles());
    map.put("needUpdate", Boolean.toString(needUpdate(userAccount, updateCutoff)));
    map.put("logOutUrl", provider.getWebflowConfiguration().get().getLogoutUrl(request));
    map.put("provider", new OAuthProvider(provider.getId(), provider.getLabel(), provider.getWebflowConfiguration().get()));
    map.put("sysadmin", userAccount == null ? false : userRepositoryRoleService.isSysadmin(userAccount));
    return new ResponseEntity<>(map, HttpStatus.OK);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) OAuthUser(org.eclipse.vorto.repository.oauth.OAuthUser) User(org.eclipse.vorto.repository.domain.User) OAuthUser(org.eclipse.vorto.repository.oauth.OAuthUser) OAuthProvider(org.eclipse.vorto.repository.web.oauth.OAuthProvider) IOAuthProvider(org.eclipse.vorto.repository.oauth.IOAuthProvider) SimpleDateFormat(java.text.SimpleDateFormat) IOAuthProvider(org.eclipse.vorto.repository.oauth.IOAuthProvider) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 35 with User

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

the class UserService method delete.

@Transactional
public void delete(final String userId) {
    User userToDelete = cache.withUser(userId).getUser();
    if (userToDelete != null) {
        eventPublisher.publishEvent(new AppEvent(this, userId, EventType.USER_DELETED));
        userRepository.delete(userToDelete);
        if (userToDelete.hasEmailAddress()) {
            notificationService.sendNotification(new DeleteAccountMessage(userToDelete));
        }
    }
}
Also used : AppEvent(org.eclipse.vorto.repository.core.events.AppEvent) User(org.eclipse.vorto.repository.domain.User) DeleteAccountMessage(org.eclipse.vorto.repository.notification.message.DeleteAccountMessage) Transactional(org.springframework.transaction.annotation.Transactional)

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