added permissions core

This commit is contained in:
srgooglo 2022-09-03 04:53:29 +02:00
parent 7f144e8053
commit e1ca4f5b31
2 changed files with 45 additions and 0 deletions

View File

@ -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,

View File

@ -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
}
}