new package dbHandler accesscontrol memeberdb and login with rights
All checks were successful
Build Quasar SPA and Go Backend for memberApp / build-spa (push) Successful in 2m20s
Build Quasar SPA and Go Backend for memberApp / build-backend (amd64, .exe, windows) (push) Successful in 5m27s
Build Quasar SPA and Go Backend for memberApp / build-backend (amd64, , linux) (push) Successful in 5m32s
Build Quasar SPA and Go Backend for memberApp / build-backend (arm, 6, , linux) (push) Successful in 5m28s
Build Quasar SPA and Go Backend for memberApp / build-backend (arm64, , linux) (push) Successful in 5m29s
All checks were successful
Build Quasar SPA and Go Backend for memberApp / build-spa (push) Successful in 2m20s
Build Quasar SPA and Go Backend for memberApp / build-backend (amd64, .exe, windows) (push) Successful in 5m27s
Build Quasar SPA and Go Backend for memberApp / build-backend (amd64, , linux) (push) Successful in 5m32s
Build Quasar SPA and Go Backend for memberApp / build-backend (arm, 6, , linux) (push) Successful in 5m28s
Build Quasar SPA and Go Backend for memberApp / build-backend (arm64, , linux) (push) Successful in 5m29s
This commit is contained in:
@@ -1,30 +1,52 @@
|
||||
import { defineStore } from 'pinia';
|
||||
import { useGlobalRouter } from 'src/utils/globalRouter';
|
||||
import { useGlobalQ } from 'src/utils/globalQ';
|
||||
import { useGlobalRouter } from 'src/vueLib/utils/globalRouter';
|
||||
import { useGlobalQ } from 'src/vueLib/utils/globalQ';
|
||||
import { appApi } from 'src/boot/axios';
|
||||
import { useNotify } from '../general/useNotify';
|
||||
import type { Role } from '../models/roles';
|
||||
import type { UserState, User } from '../models/user';
|
||||
import type { Permission } from '../checkboxes/permissions';
|
||||
|
||||
interface User {
|
||||
id: number;
|
||||
username: string;
|
||||
role: string;
|
||||
}
|
||||
|
||||
interface UserState {
|
||||
user: User | null;
|
||||
}
|
||||
const { NotifyResponse } = useNotify();
|
||||
|
||||
export const useUserStore = defineStore('user', {
|
||||
state: (): UserState => ({
|
||||
user: null,
|
||||
}),
|
||||
getters: {
|
||||
isAuthenticated: (state): boolean => !!state.user,
|
||||
isAuthenticated: (state: UserState): boolean => {
|
||||
return !!state.user;
|
||||
},
|
||||
|
||||
isAuthorizedAs: (state: UserState) => {
|
||||
return (roles: string[]) => {
|
||||
return state.user !== null && roles.includes(state.user.role);
|
||||
};
|
||||
},
|
||||
isPermittedTo: (state: UserState) => {
|
||||
return (name: string, type: 'read' | 'write' | 'delete'): boolean => {
|
||||
const permission = state.user?.permissions?.find((r: Permission) => r.name === name);
|
||||
switch (type) {
|
||||
case 'read':
|
||||
return permission?.permission ? (permission.permission & (1 << 0)) === 1 : false;
|
||||
case 'write':
|
||||
return permission?.permission ? (permission.permission & (1 << 1)) === 2 : false;
|
||||
case 'delete':
|
||||
return permission?.permission ? (permission.permission & (1 << 2)) === 4 : false;
|
||||
}
|
||||
};
|
||||
},
|
||||
},
|
||||
actions: {
|
||||
setUser(user: User) {
|
||||
this.user = user;
|
||||
},
|
||||
getUser() {
|
||||
return this.user;
|
||||
async setUser(user: User) {
|
||||
await appApi
|
||||
.get('roles?role=' + user.role)
|
||||
.then((resp) => {
|
||||
const roleData = resp.data.find((role: Role) => role.role === user.role);
|
||||
user.permissions = roleData?.permissions || [];
|
||||
this.user = user;
|
||||
})
|
||||
.catch((err) => NotifyResponse(err, 'error'));
|
||||
},
|
||||
clearUser() {
|
||||
const $q = useGlobalQ();
|
||||
@@ -60,9 +82,5 @@ export const useUserStore = defineStore('user', {
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
isAuthorizedAs(roles: string[]) {
|
||||
return this.user !== null && roles.includes(this.user.role);
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user