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
87 lines
2.6 KiB
TypeScript
87 lines
2.6 KiB
TypeScript
import { defineStore } from 'pinia';
|
|
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';
|
|
|
|
const { NotifyResponse } = useNotify();
|
|
|
|
export const useUserStore = defineStore('user', {
|
|
state: (): UserState => ({
|
|
user: null,
|
|
}),
|
|
getters: {
|
|
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: {
|
|
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();
|
|
|
|
if (!this.user) return;
|
|
if ($q) {
|
|
$q?.notify({
|
|
message: "user '" + this.user?.username + "' logged out",
|
|
color: 'orange',
|
|
position: 'bottom-right',
|
|
icon: 'warning',
|
|
timeout: 5000,
|
|
});
|
|
} else {
|
|
console.error("user '" + this.user?.username + "' logged out");
|
|
}
|
|
|
|
this.user = null;
|
|
|
|
const router = useGlobalRouter();
|
|
|
|
router?.push('/').catch((err) => {
|
|
if ($q) {
|
|
$q?.notify({
|
|
message: err,
|
|
color: 'orange',
|
|
position: 'bottom-right',
|
|
icon: 'warning',
|
|
timeout: 5000,
|
|
});
|
|
} else {
|
|
console.error("user '" + this.user?.username + "' logged out");
|
|
}
|
|
});
|
|
},
|
|
},
|
|
});
|