AuthenticationManager.ts 4.21 KB
import {DefaultTokenInterceptor} from '../http/DefaultTokenInterceptor';
import {Privilege} from './Privilege';
import Authenticity from "./Authenticity";

export abstract class AuthenticationManager {

    /**
     * TOKEN存储KEY
     */
    public static readonly TOKEN = 'token';

    /**
     *
     * @param token 登录认证token
     * @param authenticity 登录认证用户
     */
    constructor(protected token: string | null, protected authenticity: Authenticity | null) {
        this.loadStorageToken();
        this.loadStorageAuthentication();
    }

    /**
     * 更新TOKEN
     */
    updateToken(token: string) {
        this.token = token;
        localStorage.setItem(AuthenticationManager.TOKEN, token);
    }

    /**
     * 获取TOKEN
     */
    getToken() {
        return this.token;
    }


    /**
     * 删除TOKEN
     */
    removeToken() {
        this.token = null;
        localStorage.removeItem(AuthenticationManager.TOKEN);
    }

    /**
     * 删除登录信息
     */
    removeAuthentication() {
        this.authenticity = null;
        this.deleteAuthentication();
    }

    /**
     * 退出
     */
    logout() {
        this.removeToken();
        this.removeAuthentication();
    }

    getAuthentication(): Authenticity | null {
        return this.authenticity;
    }

    setAuthentication(authentication: Authenticity) {
        this.authenticity = authentication;
    }


    /**
     * 更新登录认证
     * @ param authentication
     */
    updateAuthentication(authentication: Authenticity) {
        this.updateToken(authentication.token);
        this.authenticity = authentication;
        this.saveAuthentication(authentication);
    }


    /**
     * 判断是否登录
     */
    isLogin(): boolean {
        return this.getAuthentication() != null;
    }

    /**
     * 判断是否有权限
     * @ param privilegeCode
     */
    hasPrivilege(privilegeCode: string): boolean {
        if (this.isLogin()) {
            return this.recursivePrivilegeCode(this.authenticity.privileges, privilegeCode);
        } else {
            return false;
        }
    }

    recursivePrivilegeCode(privileges: Privilege[], privilegeCode: string): boolean {
        for (const privilege of privileges) {
            if (privilege.code === privilegeCode) {
                return true;
            } else if (privilege.children != null && privilege.children.length > 0) {
                const hp = this.recursivePrivilegeCode(privilege.children, privilegeCode);
                if (hp) {
                    return true;
                }
            }
        }
        return false;
    }

    recursivePrivilegeValue(privileges: Privilege[], path: string): boolean {
        for (const privilege of privileges) {
            if (privilege.value === path) {
                return true;
            } else if (privilege.children != null && privilege.children.length > 0) {
                const hp = this.recursivePrivilegeValue(privilege.children, path);
                if (hp) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 获取TOKEN
     */
    getStorageToken(): string {
        return localStorage.getItem(AuthenticationManager.TOKEN);
    }

    deleteAuthentication(): void {
        localStorage.removeItem(DefaultTokenInterceptor.AUTHORIZATION);
    }


    getStorageAuthentication(): string {
        return localStorage.getItem(DefaultTokenInterceptor.AUTHORIZATION);
    }

    saveAuthentication(authentication: Authenticity): void {
        localStorage.setItem(DefaultTokenInterceptor.AUTHORIZATION, JSON.stringify(authentication));
    }

    loadStorageAuthentication(): Promise<any> {
        this.authenticity = JSON.parse(localStorage.getItem(DefaultTokenInterceptor.AUTHORIZATION));
        return Promise.resolve(this.authenticity);
    }

    loadStorageToken(): Promise<any> {
        this.token = localStorage.getItem(AuthenticationManager.TOKEN);
        return Promise.resolve(this.token);
    }

    hasRole(roleCode: any): boolean {
        if (this.authenticity && this.authenticity.authorities) {
            return this.authenticity.authorities.includes(roleCode);
        } else {
            return false;
        }
    }
}