当前位置:网站首页>Permission management

Permission management

2022-06-21 07:44:00 Dot ink

For front-end pages and function points , Authority management done .

Role definition :

const ROLE = {
    MANAGER: 1,
    USER: 2,
}

Product definition :

const PRODUCT_TYPE = {
    TESTA: 0,
    TESTB: 1
}

Function point definition :( Specific to the buttons on the page , Display and hide some functions )

const FUNCTION_POINT = {
    SEARCH: " Inquire about ",
    MODIFY: " modify ",
}

Permission definition :( Page path / The function node corresponds to the permission )

const AUTH = {
    "/user": `ROLE${ROLE.MANAGER}`,
    "/testA": `ROLE${ROLE.USER} && PRODUCT_TYPE${PRODUCT_TYPE.TESTA}`,
   "/testB": `ROLE${ROLE.USER} && PRODUCT_TYPE${PRODUCT_TYPE.TESTB}`,
    [FUNCTION_POINT.SEARCH]: `ROLE${ROLE.MANAGER}`,
}

Rights management :

let AuthManager = {
    role: null,
    productType: null,
    // Initialize user roles 
    initial(role, productType) {
        this.role = role;
        this.productType = productType;
    },
    check(prop) {
        if (!AUTH[prop]) return true;
        let string = "return " + AUTH[prop];
        string = string.replaceAll("ROLE", `${this.role}===`);
        string = string.replaceAll("PRODUCT_TYPE", `${this.productType}===`);
        let result;
        try {
            // eslint-disable-next-line no-new-func
            result = new Function(string)();
        } catch {
            result = true;
        }
        return result;
    },
    getDefaultPage() {
        let result;
        switch (this.role) {
            case ROLE.MANAGER: result = "/"; break;
            case ROLE.USER:
                if (this.productType === PRODUCT_TYPE.TESTA) result = "/testA";
                if (this.productType === PRODUCT_TYPE.TESTB) result = "/testB";
                break;
            default:
                break;
        }
        return result;
    }
}

export default AuthManager;
原网站

版权声明
本文为[Dot ink]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202221500000102.html