This commit is contained in:
2026-02-11 17:46:22 +03:00
commit eacfacb13b
266 changed files with 51337 additions and 0 deletions

View File

@@ -0,0 +1,182 @@
import { defineStore } from 'pinia'
import api from 'src/services/api'
import { useAuthStore } from 'stores/authStore'
export const usePermissionStore = defineStore('permission', {
state: () => ({
// API route yetkileri
routes: [],
// module+action matrix
matrix: [],
loaded: false
}),
getters: {
/* ================= ADMIN ================= */
isAdmin () {
const auth = useAuthStore()
return auth.isAdmin === true
},
/* ================= API ROUTE ================= */
hasApiPermission: (state) => (apiPathOrPerm) => {
const auth = useAuthStore()
if (auth.isAdmin) return true
if (!state.loaded) return false
if (!apiPathOrPerm) return true
// ============================
// 1⃣ MODULE:ACTION GELDİYSE
// ============================
if (apiPathOrPerm.includes(':')) {
const [module, action] = apiPathOrPerm.split(':')
return state.matrix.some(p =>
p.module === module &&
p.action === action &&
p.allowed === true
)
}
// ============================
// 2⃣ API PATH GELDİYSE
// ===========================
const apiPath = apiPathOrPerm
// exact match
if (state.routes.some(p =>
p.route === apiPath && p.can_access
)) {
return true
}
// /{id} normalize
const normalized = apiPath
.replace(/\/\d+/g, '/{id}')
if (state.routes.some(p =>
p.route === normalized && p.can_access
)) {
return true
}
// prefix
return state.routes.some(p =>
p.can_access && apiPath.startsWith(p.route)
)
},
/* ================= MODULE ================= */
hasModule: (state) => (module) => {
const auth = useAuthStore()
if (auth.isAdmin) return true
if (!state.loaded) return false
return state.matrix.some(p =>
p.module === module &&
p.allowed === true
)
},
/* ================= ACTION ================= */
hasPermission: (state) => (module, action) => {
const auth = useAuthStore()
if (auth.isAdmin) return true
if (!state.loaded) return false
return state.matrix.some(p =>
p.module === module &&
p.action === action &&
p.allowed === true
)
}
},
actions: {
async fetchPermissions () {
const auth = useAuthStore()
if (auth.isAdmin) {
this.routes = []
this.matrix = []
this.loaded = true
return
}
try {
// API ROUTES
const routesRes = await api.get('/permissions/routes')
this.routes = routesRes.data || []
// EFFECTIVE MATRIX
const effRes = await api.get('/permissions/effective')
this.matrix = effRes.data || []
console.group('🔐 PERMISSION DEBUG')
console.log('API ROUTES:', this.routes)
console.log('EFFECTIVE MATRIX:', this.matrix)
console.groupEnd()
} catch (err) {
console.error('❌ Permission load failed', err)
this.routes = []
this.matrix = []
} finally {
this.loaded = true
}
},
clear () {
this.routes = []
this.matrix = []
this.loaded = false
}
}
})