Search in sources :

Example 1 with UserStatus

use of alien4cloud.rest.model.UserStatus in project alien4cloud by alien4cloud.

the class AuthController method getLoginStatus.

/**
 * Get the current user's status (login, roles etc.).
 *
 * @return The current user's status wrapped in a {@link RestResponse} object.
 */
@ApiOperation(value = "Get the current authentication status and user's roles.", notes = "Return the current user's status and it's roles.")
@RequestMapping(value = "/status", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public RestResponse<UserStatus> getLoginStatus() {
    final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    final UserStatus userStatus = new UserStatus();
    if (auth == null) {
        userStatus.setIsLogged(false);
    } else {
        userStatus.setIsLogged(auth.isAuthenticated() && !(auth instanceof AnonymousAuthenticationToken));
        userStatus.setUsername(auth.getName());
        if (auth.getPrincipal() instanceof User) {
            userStatus.setGithubUsername(((User) auth.getPrincipal()).getFirstName());
            userStatus.setGroups(((User) auth.getPrincipal()).getGroups());
        }
        for (GrantedAuthority role : auth.getAuthorities()) {
            userStatus.getRoles().add(role.getAuthority());
        }
    }
    if (env.acceptsProfiles("github-auth")) {
        userStatus.setAuthSystem("github");
    } else if (samlEnabled) {
        userStatus.setAuthSystem("saml");
    } else {
        userStatus.setAuthSystem("alien");
    }
    return RestResponseBuilder.<UserStatus>builder().data(userStatus).build();
}
Also used : User(alien4cloud.security.model.User) Authentication(org.springframework.security.core.Authentication) GrantedAuthority(org.springframework.security.core.GrantedAuthority) AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken) UserStatus(alien4cloud.rest.model.UserStatus) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

UserStatus (alien4cloud.rest.model.UserStatus)1 User (alien4cloud.security.model.User)1 ApiOperation (io.swagger.annotations.ApiOperation)1 AnonymousAuthenticationToken (org.springframework.security.authentication.AnonymousAuthenticationToken)1 Authentication (org.springframework.security.core.Authentication)1 GrantedAuthority (org.springframework.security.core.GrantedAuthority)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1