diff --git a/packages/app/src/cores/index.js b/packages/app/src/cores/index.js index a3d4a9e2..8ada4e59 100644 --- a/packages/app/src/cores/index.js +++ b/packages/app/src/cores/index.js @@ -1,6 +1,7 @@ import SettingsCore from "./settings" import APICore from "./api" import StyleCore from "./style" +import PermissionsCore from "./permissions" import I18nCore from "./i18n" import NotificationsCore from "./notifications" @@ -13,6 +14,7 @@ import mediaPlayer from "./mediaPlayer" export default [ SettingsCore, APICore, + PermissionsCore, StyleCore, I18nCore, SoundCore, diff --git a/packages/app/src/cores/permissions/index.js b/packages/app/src/cores/permissions/index.js new file mode 100644 index 00000000..4122712f --- /dev/null +++ b/packages/app/src/cores/permissions/index.js @@ -0,0 +1,43 @@ +import Core from "evite/src/core" +import UserModel from "models/user" + +export default class PermissionsCore extends Core { + publicMethods = { + permissions: this + } + + isUserAdmin = "unchecked" + + // this will works with a newer version of evite + async initializeBeforeRuntimeInit() { + this.isUserAdmin = await UserModel.hasAdmin() + } + + hasAdmin = async () => { + return await UserModel.hasAdmin() + } + + hasPermission = async (permission) => { + let query = [] + + if (Array.isArray(permission)) { + query = permission + } else { + query = [permission] + } + + // create a promise and check if the user has all the permission in the query + const result = await Promise.all(query.map(async (permission) => { + const hasPermission = await UserModel.hasRole(permission) + + return hasPermission + })) + + // if the user has all the permission in the query, return true + if (result.every((hasPermission) => hasPermission)) { + return true + } + + return false + } +} \ No newline at end of file