Search in sources :

Example 1 with UserAttributes

use of org.dcache.restful.providers.UserAttributes in project dcache by dCache.

the class UserResource method getUserAttributes.

@GET
@ApiOperation(value = "Provide information about the current user.", notes = "An introspection endpoint to allow the client to discover " + "information about the current user.")
@Produces(MediaType.APPLICATION_JSON)
public UserAttributes getUserAttributes(@Context HttpServletRequest request) {
    UserAttributes user = new UserAttributes();
    Subject subject = RequestUser.getSubject();
    if (Subjects.isNobody(subject)) {
        user.setStatus(UserAttributes.AuthenticationStatus.ANONYMOUS);
        user.setUid(null);
        user.setGids(null);
        user.setRoles(null);
    } else {
        user.setStatus(UserAttributes.AuthenticationStatus.AUTHENTICATED);
        user.setUid(Subjects.getUid(subject));
        user.setUsername(Subjects.getUserName(subject));
        List<Long> gids = Arrays.stream(Subjects.getGids(subject)).boxed().collect(Collectors.toList());
        user.setGids(gids);
        List<String> emails = Subjects.getEmailAddresses(subject);
        user.setEmail(emails.isEmpty() ? null : emails);
        for (LoginAttribute attribute : getLoginAttributes(request)) {
            if (attribute instanceof HomeDirectory) {
                user.setHomeDirectory(((HomeDirectory) attribute).getHome());
            } else if (attribute instanceof RootDirectory) {
                user.setRootDirectory(((RootDirectory) attribute).getRoot());
            } else if (attribute instanceof Role) {
                if (user.getRoles() == null) {
                    user.setRoles(new ArrayList<>());
                }
                user.getRoles().add(((Role) attribute).getRole());
            } else if (attribute instanceof UnassertedRole) {
                if (user.getUnassertedRoles() == null) {
                    user.setUnassertedRoles(new ArrayList<>());
                }
                user.getUnassertedRoles().add(((UnassertedRole) attribute).getRole());
            }
        }
    }
    return user;
}
Also used : UnassertedRole(org.dcache.auth.attributes.UnassertedRole) HomeDirectory(org.dcache.auth.attributes.HomeDirectory) LoginAttribute(org.dcache.auth.attributes.LoginAttribute) ArrayList(java.util.ArrayList) RootDirectory(org.dcache.auth.attributes.RootDirectory) Subject(javax.security.auth.Subject) UserAttributes(org.dcache.restful.providers.UserAttributes) Role(org.dcache.auth.attributes.Role) UnassertedRole(org.dcache.auth.attributes.UnassertedRole) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

ApiOperation (io.swagger.annotations.ApiOperation)1 ArrayList (java.util.ArrayList)1 Subject (javax.security.auth.Subject)1 GET (javax.ws.rs.GET)1 Produces (javax.ws.rs.Produces)1 HomeDirectory (org.dcache.auth.attributes.HomeDirectory)1 LoginAttribute (org.dcache.auth.attributes.LoginAttribute)1 Role (org.dcache.auth.attributes.Role)1 RootDirectory (org.dcache.auth.attributes.RootDirectory)1 UnassertedRole (org.dcache.auth.attributes.UnassertedRole)1 UserAttributes (org.dcache.restful.providers.UserAttributes)1