All files / common/session user.ts

54.16% Statements 26/48
66.66% Branches 4/6
50% Functions 4/8
54.16% Lines 26/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 481x 1x 1x 1x 1x 1x 1x 1x 1x 1x             1x 1x 6x 6x 7x 1x 1x       1x 1x 6x 6x 6x 6x 6x                           6x 6x
import { Staff, MenuAuthority } from './../models';
import {SessionStorage} from 'quasar'
 
export interface User {
    readOnly?: boolean;
    role?: string | undefined; 
    token?: string;
    dataUser?: Staff
}
 
export async function setUserLogin(token: string, readOnly: boolean, dataUser: Staff): Promise<void> {
    const user: User = {
        token,readOnly, dataUser, role: dataUser.roles.length ? dataUser.roles[0].code : undefined
    }
    SessionStorage.set("userLogin", user)
}
 
export function readOnly(): boolean|undefined {
    const user = SessionStorage.getItem("userLogin") as User
    return user?.readOnly
}
 
export function me(): string|undefined {
    const user = SessionStorage.getItem("userLogin") as User
    return user.role
}
 
export function userRoles(): Array<string> {
    let authority :Array<string> = []
    const user = SessionStorage.getItem("userLogin") as User || null;
    let authMenu :Array<any> = []
    authMenu = user ? user.dataUser?.roles[0].menuAuthority : [] as any
    authMenu.forEach(el => {
        if (el.create) {
            authority.push(el.menuAuthority.code + ':' + 'create')
        }
        if (el.view) {
            authority.push(el.menuAuthority.code + ':' + 'view')
        }
        if (el.update) {
            authority.push(el.menuAuthority.code + ':' + 'update')
        }
        if (el.delete) {
            authority.push(el.menuAuthority.code + ':' + 'delete')
        }
    });
    return authority
}