import { Injectable } from "@angular/core";
import { User } from "./model/user.model";

@Injectable({
    providedIn: 'root'
})
export class RoleUtil{

    hasUserRights(roleNameOrUser: String | User): boolean{ 
        let role: String = this.getRole(roleNameOrUser);
        return role === 'ROLE_ADMIN' || role === 'ROLE_ANALYST' || role === 'ROLE_USER';
    }

    hasAnalystRights(roleOrUser: String | User): boolean{
        let role: String = this.getRole(roleOrUser);
        return role === 'ROLE_ADMIN' || role === 'ROLE_ANALYST';
    }

    hasAdminRights(roleOrUser: String | User): boolean{
        let role: String = this.getRole(roleOrUser);
        return role === 'ROLE_ADMIN';
    }

    private getRole(roleNameOrUser: any): String{
        if (roleNameOrUser === null || roleNameOrUser === undefined)
            return null;
        return typeof roleNameOrUser === "string" ? roleNameOrUser : roleNameOrUser.role.name;
    }

}