You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.1 KiB
83 lines
2.1 KiB
import { defineStore } from "pinia"
|
|
import { isEmpty } from "lodash"
|
|
import { computed, ref } from "vue"
|
|
import securityService from "../services/securityService"
|
|
|
|
export const useSecurityStore = defineStore("security", () => {
|
|
const user = ref(null)
|
|
const isLoading = ref(true)
|
|
const isAuthenticated = computed(() => !isEmpty(user.value))
|
|
|
|
const hasRole = computed(() => (role) => {
|
|
if (user.value && user.value.roles) {
|
|
return user.value.roles.indexOf(role) !== -1
|
|
}
|
|
|
|
return false
|
|
})
|
|
|
|
/**
|
|
* @param {string} role
|
|
*/
|
|
const removeRole = (role) => {
|
|
const index = user.value.roles.indexOf(role)
|
|
|
|
if (index > -1) {
|
|
user.value.roles.splice(index, 1)
|
|
}
|
|
}
|
|
|
|
const isStudent = computed(() => hasRole.value("ROLE_STUDENT"))
|
|
|
|
const isStudentBoss = computed(() => hasRole.value("ROLE_STUDENT_BOSS"))
|
|
|
|
const isHRM = computed(() => hasRole.value("ROLE_HR"))
|
|
|
|
const isTeacher = computed(() => (isAdmin.value ? true : hasRole.value("ROLE_TEACHER")))
|
|
|
|
const isCurrentTeacher = computed(() => (isAdmin.value ? true : hasRole.value("ROLE_CURRENT_COURSE_TEACHER")))
|
|
|
|
const isCourseAdmin = computed(() =>
|
|
isAdmin.value
|
|
? true
|
|
: hasRole.value("ROLE_CURRENT_COURSE_SESSION_TEACHER") || hasRole.value("ROLE_CURRENT_COURSE_TEACHER"),
|
|
)
|
|
|
|
const isSessionAdmin = computed(() => hasRole.value("ROLE_SESSION_MANAGER"))
|
|
|
|
const isAdmin = computed(() => hasRole.value("ROLE_SUPER_ADMIN") || hasRole.value("ROLE_ADMIN"))
|
|
|
|
async function checkSession() {
|
|
isLoading.value = true
|
|
try {
|
|
const response = await securityService.checkSession()
|
|
if (response.isAuthenticated) {
|
|
user.value = response.user
|
|
} else {
|
|
user.value = null
|
|
}
|
|
} catch (error) {
|
|
console.error("Error checking session:", error)
|
|
user.value = null
|
|
} finally {
|
|
isLoading.value = false
|
|
}
|
|
}
|
|
|
|
return {
|
|
user,
|
|
isLoading,
|
|
isAuthenticated,
|
|
hasRole,
|
|
removeRole,
|
|
isStudent,
|
|
isStudentBoss,
|
|
isHRM,
|
|
isTeacher,
|
|
isCurrentTeacher,
|
|
isCourseAdmin,
|
|
isSessionAdmin,
|
|
isAdmin,
|
|
checkSession,
|
|
}
|
|
})
|
|
|