Search in sources :

Example 1 with OAuthUser

use of org.eclipse.vorto.repository.oauth.OAuthUser 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)

Aggregations

ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 SimpleDateFormat (java.text.SimpleDateFormat)1 User (org.eclipse.vorto.repository.domain.User)1 IOAuthProvider (org.eclipse.vorto.repository.oauth.IOAuthProvider)1 OAuthUser (org.eclipse.vorto.repository.oauth.OAuthUser)1 OAuthProvider (org.eclipse.vorto.repository.web.oauth.OAuthProvider)1 ResponseEntity (org.springframework.http.ResponseEntity)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1