Hangar/frontend/plugins/perms.ts

152 lines
3.9 KiB
TypeScript
Raw Normal View History

2021-03-12 15:59:36 +08:00
import { Context } from '@nuxt/types';
import { Inject } from '@nuxt/types/app';
2021-04-10 09:24:06 +08:00
import { IPermission } from 'hangar-api';
2021-03-12 15:59:36 +08:00
import { NamedPermission } from '~/types/enums';
2021-04-10 09:24:06 +08:00
import { RootState } from '~/store';
2021-03-12 15:59:36 +08:00
2021-04-10 09:24:06 +08:00
const createUtil = ({ $util, store }: Context) => {
2021-03-12 15:59:36 +08:00
class Perms {
get canEditSubjectSettings() {
return $util.hasPerms(NamedPermission.EDIT_SUBJECT_SETTINGS);
}
// Subjects
get canManageSubjectMembers() {
return $util.hasPerms(NamedPermission.MANAGE_SUBJECT_MEMBERS);
}
get isSubjectOwner() {
return $util.hasPerms(NamedPermission.IS_SUBJECT_OWNER);
}
get isSubjectMember() {
return $util.hasPerms(NamedPermission.IS_SUBJECT_MEMBER);
}
// Projects
get canCreateProject() {
return $util.hasPerms(NamedPermission.CREATE_PROJECT);
}
get canEditPage() {
return $util.hasPerms(NamedPermission.EDIT_PAGE);
}
get canDeleteProject() {
return $util.hasPerms(NamedPermission.DELETE_PROJECT);
}
// Versions
get canCreateVersion() {
return $util.hasPerms(NamedPermission.CREATE_VERSION);
}
get canEditVersion() {
return $util.hasPerms(NamedPermission.EDIT_VERSION);
}
get canDeleteVersion() {
return $util.hasPerms(NamedPermission.DELETE_VERSION);
}
get canEditTags() {
return $util.hasPerms(NamedPermission.EDIT_TAGS);
}
// Organizations
get canCreateOrganization() {
return $util.hasPerms(NamedPermission.CREATE_ORGANIZATION);
}
get canPostAsOrganization() {
return $util.hasPerms(NamedPermission.POST_AS_ORGANIZATION);
}
// Moderators
get canAccessModNotesAndFlags() {
return $util.hasPerms(NamedPermission.MOD_NOTES_AND_FLAGS);
}
get canSeeHidden() {
return $util.hasPerms(NamedPermission.SEE_HIDDEN);
}
get isStaff() {
return $util.hasPerms(NamedPermission.IS_STAFF);
}
get isReviewer() {
return $util.hasPerms(NamedPermission.REVIEWER);
}
// Developers
get canViewHealth() {
return $util.hasPerms(NamedPermission.VIEW_HEALTH);
}
get canViewIp() {
return $util.hasPerms(NamedPermission.VIEW_IP);
}
get canViewStats() {
return $util.hasPerms(NamedPermission.VIEW_STATS);
}
get canViewLogs() {
return $util.hasPerms(NamedPermission.VIEW_LOGS);
}
// Admins
get canDoManualValueChanges() {
return $util.hasPerms(NamedPermission.MANUAL_VALUE_CHANGES);
}
get canHardDeleteProject() {
return $util.hasPerms(NamedPermission.HARD_DELETE_PROJECT);
}
get canHardDeleteVersion() {
return $util.hasPerms(NamedPermission.HARD_DELETE_VERSION);
}
get canEditAllUserSettings() {
return $util.hasPerms(NamedPermission.EDIT_ALL_USER_SETTINGS);
}
2021-04-10 09:24:06 +08:00
convert(value: NamedPermission): IPermission {
return (store.state as RootState).permissions.get(value)!;
}
2021-03-12 15:59:36 +08:00
}
return new Perms();
};
type permsType = ReturnType<typeof createUtil>;
declare module 'vue/types/vue' {
interface Vue {
$perms: permsType;
}
}
declare module '@nuxt/types' {
interface NuxtAppOptions {
$perms: permsType;
}
interface Context {
$perms: permsType;
}
}
declare module 'vuex/types/index' {
// eslint-disable-next-line no-unused-vars,@typescript-eslint/no-unused-vars
interface Store<S> {
$perms: permsType;
}
}
export default (ctx: Context, inject: Inject) => {
inject('perms', createUtil(ctx));
};