Search in sources :

Example 1 with Permission

use of com.objectcomputing.checkins.services.permissions.Permission in project check-ins by objectcomputing.

the class CurrentUserController method currentUser.

/**
 * Get user details from Google authentication
 *
 * @param authentication {@link Authentication} or null
 * @return {@link HttpResponse<CurrentUserDTO>}
 */
@Get
public HttpResponse<CurrentUserDTO> currentUser(@Nullable Authentication authentication) {
    if (authentication == null) {
        return HttpResponse.unauthorized();
    }
    String workEmail = authentication.getAttributes().get("email").toString();
    String imageUrl = authentication.getAttributes().get("picture") != null ? authentication.getAttributes().get("picture").toString() : "";
    String name = authentication.getAttributes().get("name").toString().trim();
    String firstName = name.substring(0, name.indexOf(' '));
    String lastName = name.substring(name.indexOf(' ') + 1).trim();
    MemberProfile user = currentUserServices.findOrSaveUser(firstName, lastName, workEmail);
    List<Permission> permissions = permissionServices.findUserPermissions(user.getId());
    Set<Role> roles = roleServices.findUserRoles(user.getId());
    List<String> rolesAsString = roles.stream().map(o -> o.getRole()).collect(Collectors.toList());
    return HttpResponse.ok().headers(headers -> headers.location(location(user.getId()))).body(fromEntity(user, imageUrl, permissions, rolesAsString));
}
Also used : Role(com.objectcomputing.checkins.services.role.Role) Role(com.objectcomputing.checkins.services.role.Role) Controller(io.micronaut.http.annotation.Controller) Permission(com.objectcomputing.checkins.services.permissions.Permission) Secured(io.micronaut.security.annotation.Secured) PermissionServices(com.objectcomputing.checkins.services.permissions.PermissionServices) Authentication(io.micronaut.security.authentication.Authentication) Set(java.util.Set) MemberProfileUtils(com.objectcomputing.checkins.services.memberprofile.MemberProfileUtils) SecurityRule(io.micronaut.security.rules.SecurityRule) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) List(java.util.List) Tag(io.swagger.v3.oas.annotations.tags.Tag) Nullable(io.micronaut.core.annotation.Nullable) PermissionRepository(com.objectcomputing.checkins.services.permissions.PermissionRepository) HttpResponse(io.micronaut.http.HttpResponse) MemberProfile(com.objectcomputing.checkins.services.memberprofile.MemberProfile) RoleRepository(com.objectcomputing.checkins.services.role.RoleRepository) URI(java.net.URI) RoleServices(com.objectcomputing.checkins.services.role.RoleServices) Get(io.micronaut.http.annotation.Get) MemberProfile(com.objectcomputing.checkins.services.memberprofile.MemberProfile) Permission(com.objectcomputing.checkins.services.permissions.Permission) Get(io.micronaut.http.annotation.Get)

Example 2 with Permission

use of com.objectcomputing.checkins.services.permissions.Permission in project check-ins by objectcomputing.

the class PermissionFixture method setPermissionsForAdmin.

default void setPermissionsForAdmin(UUID roleID) {
    List<Permission> permissions = getPermissionRepository().findAll();
    for (Permissions adminPermission : adminPermissions) {
        Optional<Permission> permission = permissions.stream().filter(s -> s.getPermission().equals(adminPermission.name())).findFirst();
        permission.ifPresent(value -> setRolePermission(roleID, value.getId()));
    }
}
Also used : List(java.util.List) Permission(com.objectcomputing.checkins.services.permissions.Permission) Optional(java.util.Optional) UUID(java.util.UUID) Permissions(com.objectcomputing.checkins.security.permissions.Permissions) Permission(com.objectcomputing.checkins.services.permissions.Permission) Permissions(com.objectcomputing.checkins.security.permissions.Permissions)

Example 3 with Permission

use of com.objectcomputing.checkins.services.permissions.Permission in project check-ins by objectcomputing.

the class PermissionFixture method setPermissionsForMember.

default void setPermissionsForMember(UUID roleID) {
    List<Permission> permissions = getPermissionRepository().findAll();
    for (Permissions memberPermission : memberPermissions) {
        Optional<Permission> permission = permissions.stream().filter(s -> s.getPermission().equals(memberPermission.name())).findFirst();
        permission.ifPresent(value -> setRolePermission(roleID, value.getId()));
    }
}
Also used : List(java.util.List) Permission(com.objectcomputing.checkins.services.permissions.Permission) Optional(java.util.Optional) UUID(java.util.UUID) Permissions(com.objectcomputing.checkins.security.permissions.Permissions) Permission(com.objectcomputing.checkins.services.permissions.Permission) Permissions(com.objectcomputing.checkins.security.permissions.Permissions)

Example 4 with Permission

use of com.objectcomputing.checkins.services.permissions.Permission in project check-ins by objectcomputing.

the class RolePermissionServicesImpl method findAll.

public List<RolePermissionResponseDTO> findAll() {
    List<RolePermissionResponseDTO> roleInfo = new ArrayList<>();
    List<RolePermission> records = rolePermissionRepository.findAll();
    List<Role> roles = roleServices.findAllRoles();
    List<Permission> permissions = permissionServices.findAll();
    for (Role role : roles) {
        List<Permission> permissionsAssociatedWithRole = new ArrayList<>();
        for (RolePermission rolePermission : records) {
            if (role.getId().equals(rolePermission.getRoleId())) {
                Optional<Permission> permission = permissions.stream().filter(s -> s.getId().equals(rolePermission.getPermissionId())).findFirst();
                permission.ifPresent(permissionsAssociatedWithRole::add);
            }
        }
        RolePermissionResponseDTO rolePermissionResponseDTO = new RolePermissionResponseDTO();
        rolePermissionResponseDTO.setRoleId(role.getId());
        rolePermissionResponseDTO.setRole(role.getRole());
        rolePermissionResponseDTO.setDescription(role.getDescription());
        rolePermissionResponseDTO.setPermissions(permissionsAssociatedWithRole);
        roleInfo.add(rolePermissionResponseDTO);
    }
    return roleInfo;
}
Also used : Role(com.objectcomputing.checkins.services.role.Role) Role(com.objectcomputing.checkins.services.role.Role) List(java.util.List) Permission(com.objectcomputing.checkins.services.permissions.Permission) PermissionServices(com.objectcomputing.checkins.services.permissions.PermissionServices) Optional(java.util.Optional) Singleton(javax.inject.Singleton) RoleServices(com.objectcomputing.checkins.services.role.RoleServices) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) Permission(com.objectcomputing.checkins.services.permissions.Permission)

Example 5 with Permission

use of com.objectcomputing.checkins.services.permissions.Permission in project check-ins by objectcomputing.

the class LocalUserPasswordAuthProvider method authenticate.

@Override
public Publisher<AuthenticationResponse> authenticate(@Nullable HttpRequest<?> httpRequest, AuthenticationRequest<?, ?> authReq) {
    String email = authReq.getIdentity().toString();
    MemberProfile memberProfile = currentUserServices.findOrSaveUser(email, email, email);
    String role;
    // if empty get default roles, otherwise create role on the fly
    if (StringUtils.isNotEmpty(role = authReq.getSecret().toString())) {
        List<String> roles = usersStore.getUserRole(role);
        if (roles == null) {
            return Flowable.just(new AuthenticationFailed(String.format("Invalid role selected %s", role)));
        }
        // remove a user from the roles they currently have (as assigned in test data)
        memberRoleServices.removeMemberFromRoles(memberProfile.getId());
        // add the roles based on role override / configuration properties
        for (String curRole : roles) {
            // if no role is found then create and save it
            Role currentRole = roleServices.findByRole(curRole).orElse(null);
            if (currentRole == null) {
                currentRole = roleServices.save(new Role(null, curRole, "description"));
            }
            memberRoleServices.saveByIds(memberProfile.getId(), currentRole.getId());
        }
    }
    List<Permission> permissions = permissionServices.findUserPermissions(memberProfile.getId());
    List<String> permissionsAsString = permissions.stream().map(o -> o.getPermission()).collect(Collectors.toList());
    Set<Role> userRoles = roleServices.findUserRoles(memberProfile.getId());
    List<String> rolesAsString = userRoles.stream().map(o -> o.getRole()).collect(Collectors.toList());
    Map<String, Object> attributes = new HashMap<>();
    attributes.put("permissions", permissionsAsString);
    attributes.put("email", memberProfile.getWorkEmail());
    return Flowable.just(new ExtendedUserDetails(email, rolesAsString, attributes));
}
Also used : Environment(io.micronaut.context.env.Environment) Permission(com.objectcomputing.checkins.services.permissions.Permission) PermissionServices(com.objectcomputing.checkins.services.permissions.PermissionServices) HashMap(java.util.HashMap) AuthenticationRequest(io.micronaut.security.authentication.AuthenticationRequest) Singleton(javax.inject.Singleton) Environments(com.objectcomputing.checkins.Environments) Flowable(io.reactivex.Flowable) Nullable(io.micronaut.core.annotation.Nullable) Map(java.util.Map) Requires(io.micronaut.context.annotation.Requires) HttpRequest(io.micronaut.http.HttpRequest) AuthenticationFailed(io.micronaut.security.authentication.AuthenticationFailed) AuthenticationResponse(io.micronaut.security.authentication.AuthenticationResponse) Role(com.objectcomputing.checkins.services.role.Role) Publisher(org.reactivestreams.Publisher) Set(java.util.Set) Collectors(java.util.stream.Collectors) StringUtils(io.micronaut.core.util.StringUtils) List(java.util.List) ExtendedUserDetails(com.objectcomputing.checkins.security.permissions.ExtendedUserDetails) CurrentUserServices(com.objectcomputing.checkins.services.memberprofile.currentuser.CurrentUserServices) MemberProfile(com.objectcomputing.checkins.services.memberprofile.MemberProfile) RoleServices(com.objectcomputing.checkins.services.role.RoleServices) AuthenticationProvider(io.micronaut.security.authentication.AuthenticationProvider) MemberRoleServices(com.objectcomputing.checkins.services.role.member_roles.MemberRoleServices) HashMap(java.util.HashMap) MemberProfile(com.objectcomputing.checkins.services.memberprofile.MemberProfile) ExtendedUserDetails(com.objectcomputing.checkins.security.permissions.ExtendedUserDetails) Role(com.objectcomputing.checkins.services.role.Role) AuthenticationFailed(io.micronaut.security.authentication.AuthenticationFailed) Permission(com.objectcomputing.checkins.services.permissions.Permission)

Aggregations

Permission (com.objectcomputing.checkins.services.permissions.Permission)7 List (java.util.List)7 Role (com.objectcomputing.checkins.services.role.Role)4 Optional (java.util.Optional)4 UUID (java.util.UUID)4 Permissions (com.objectcomputing.checkins.security.permissions.Permissions)3 MemberProfile (com.objectcomputing.checkins.services.memberprofile.MemberProfile)3 PermissionServices (com.objectcomputing.checkins.services.permissions.PermissionServices)3 RoleServices (com.objectcomputing.checkins.services.role.RoleServices)3 Nullable (io.micronaut.core.annotation.Nullable)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 Singleton (javax.inject.Singleton)2 Environments (com.objectcomputing.checkins.Environments)1 ExtendedUserDetails (com.objectcomputing.checkins.security.permissions.ExtendedUserDetails)1 MemberProfileUtils (com.objectcomputing.checkins.services.memberprofile.MemberProfileUtils)1 CurrentUserServices (com.objectcomputing.checkins.services.memberprofile.currentuser.CurrentUserServices)1 PermissionRepository (com.objectcomputing.checkins.services.permissions.PermissionRepository)1 RoleRepository (com.objectcomputing.checkins.services.role.RoleRepository)1 MemberRoleServices (com.objectcomputing.checkins.services.role.member_roles.MemberRoleServices)1