All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jgcomptech.tools.authc.UserRoleManager Maven / Gradle / Ivy

There is a newer version: 1.5.1
Show newest version
package com.jgcomptech.tools.authc;

import com.jgcomptech.tools.authz.PermissionManager;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.stream.Collectors;

/**
 * Manages all user account roles.
 * @since 1.4.0
 */
public final class UserRoleManager {
    private static UserRoleManager instance;
    private final HashMap userRoles = new HashMap<>();

    /**
     * Returns the singleton instance of the UserRoleManager.
     * @return the singleton instance of the UserRoleManager
     */
    public static UserRoleManager getInstance() {
        if(instance == null) instance = new UserRoleManager();
        return instance;
    }

    /** Creates an instance of the UserRoleManager. */
    private UserRoleManager() {
        final var Admin = PermissionManager.SystemPermissions.Admin.getName();
        final var Edit = PermissionManager.SystemPermissions.Edit.getName();
        final var Create = PermissionManager.SystemPermissions.Create.getName();
        final var Read = PermissionManager.SystemPermissions.Read.getName();

        SystemUserRoles.ADMIN.getRole().modify().add(Admin, Edit, Create, Read);

        SystemUserRoles.EDITOR.getRole().modify().add(Edit, Create, Read);

        SystemUserRoles.AUTHOR.getRole().modify().add(Create, Read);

        SystemUserRoles.BASIC.getRole().modify().add(Read);

        addExistingUserRole(SystemUserRoles.ADMIN.getRole());
        addExistingUserRole(SystemUserRoles.EDITOR.role);
        addExistingUserRole(SystemUserRoles.AUTHOR.role);
        addExistingUserRole(SystemUserRoles.BASIC.role);
        addExistingUserRole(SystemUserRoles.NONE.role);
    }

    /** A List of the implemented system user roles. */
    public enum SystemUserRoles {
        /** Has Admin, Edit, Create and Read Permissions. */
        ADMIN(new UserRole("admin")),
        /** Has Edit, Create and Read Permissions. */
        EDITOR(new UserRole("editor")),
        /** Has Create and Read Permissions. */
        AUTHOR(new UserRole("author")),
        /** Has Read Permissions. */
        BASIC(new UserRole("basic")),
        /** Does not have any Permissions. */
        NONE(new UserRole("none"));
        private final UserRole role;

        SystemUserRoles(final UserRole role) {
            this.role = role;
            this.role.enable();
        }

        public String getName() { return role.getName(); }

        public UserRole getRole() { return role; }

        public static HashSet getRoles() {
            return Arrays.stream(values()).map(p -> p.role.getName()).collect(Collectors.toCollection(HashSet::new));
        }
    }

    /**
     * Creates a new role and adds it to the list.
     * @param name the name of the new role
     * @return the new role as a UserRole object
     */
    public UserRole createUserRole(final String name) {
        if(name == null || name.trim().trim().isEmpty()) {
            throw new IllegalArgumentException("Name Cannot Be Empty!");
        }

        final UserRole role;
        if(!userRoles.containsKey(name)) {
            role = new UserRole(name);
            userRoles.put(name, role);
        } else role = userRoles.get(name);
        return role;
    }

    /**
     * Adds an existing role to the list.
     * @param role the role to be added
     */
    public void addExistingUserRole(final UserRole role) {
        if(role == null) {
            throw new IllegalArgumentException("Role Cannot Be Null!");
        }
        if(!userRoles.containsKey(role.getName())) {
            userRoles.put(role.getName(), role);
        }
    }

    /**
     * Returns a list of the current existing user roles.
     * @return a list of the current existing user roles
     */
    public HashMap getUserRoles() { return userRoles; }

    /**
     * Returns the specified user role.
     * @param name the name of the user role to return
     * @return the specified user role
     */
    public UserRole getUserRole(final String name) { return userRoles.get(name); }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy