longevity: ruoli cliente e trainer con le rispettive rotte
This commit is contained in:
+8
-1
@@ -5,7 +5,7 @@ import { randomBytes } from 'node:crypto';
|
|||||||
export const SESSION_COOKIE = 'session';
|
export const SESSION_COOKIE = 'session';
|
||||||
const SESSION_DAYS = 7;
|
const SESSION_DAYS = 7;
|
||||||
|
|
||||||
export type Role = 'admin' | 'superuser' | 'user' | 'piattaforme';
|
export type Role = 'admin' | 'superuser' | 'user' | 'piattaforme' | 'cliente' | 'trainer';
|
||||||
|
|
||||||
export function hashPassword(plain: string): string {
|
export function hashPassword(plain: string): string {
|
||||||
return bcrypt.hashSync(plain, 12);
|
return bcrypt.hashSync(plain, 12);
|
||||||
@@ -62,6 +62,11 @@ const RULES: [RegExp, Role[]][] = [
|
|||||||
// sotto /piattaforme. Stesso ruolo per entrambe.
|
// sotto /piattaforme. Stesso ruolo per entrambe.
|
||||||
[/^\/campus(\/|$)/, ['admin', 'piattaforme']],
|
[/^\/campus(\/|$)/, ['admin', 'piattaforme']],
|
||||||
[/^\/piattaforme(\/|$)/, ['admin', 'piattaforme']],
|
[/^\/piattaforme(\/|$)/, ['admin', 'piattaforme']],
|
||||||
|
// Longevity: il cliente vede solo il proprio spazio, il gestionale è del trainer.
|
||||||
|
[/^\/longevity\/gestionale(\/|$)/, ['admin', 'trainer']],
|
||||||
|
[/^\/api\/longevity\/gestionale(\/|$)/, ['admin', 'trainer']],
|
||||||
|
[/^\/longevity(\/|$)/, ['admin', 'trainer', 'cliente']],
|
||||||
|
[/^\/api\/longevity(\/|$)/, ['admin', 'trainer', 'cliente']],
|
||||||
];
|
];
|
||||||
|
|
||||||
export function canAccessAdminPath(role: string, pathname: string): boolean {
|
export function canAccessAdminPath(role: string, pathname: string): boolean {
|
||||||
@@ -71,6 +76,8 @@ export function canAccessAdminPath(role: string, pathname: string): boolean {
|
|||||||
}
|
}
|
||||||
// piattaforme vede solo le sue sezioni (coperte da RULES) e il logout
|
// piattaforme vede solo le sue sezioni (coperte da RULES) e il logout
|
||||||
if (role === 'piattaforme') return pathname === '/admin/logout';
|
if (role === 'piattaforme') return pathname === '/admin/logout';
|
||||||
|
// cliente e trainer non sono utenti del sito: fuori dalle rotte elencate non passano.
|
||||||
|
if (role === 'cliente' || role === 'trainer') return pathname === '/admin/logout';
|
||||||
return true; // blog, upload, logout, showtags: tutti i loggati
|
return true; // blog, upload, logout, showtags: tutti i loggati
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
import { describe, it, expect } from 'vitest';
|
||||||
|
import { canAccessAdminPath } from '../../src/lib/auth';
|
||||||
|
|
||||||
|
describe('accesso alle rotte longevity', () => {
|
||||||
|
it('il cliente entra nel proprio spazio', () => {
|
||||||
|
expect(canAccessAdminPath('cliente', '/longevity/io')).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('il cliente NON entra nel gestionale', () => {
|
||||||
|
expect(canAccessAdminPath('cliente', '/longevity/gestionale')).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('il trainer entra in entrambi', () => {
|
||||||
|
expect(canAccessAdminPath('trainer', '/longevity/io')).toBe(true);
|
||||||
|
expect(canAccessAdminPath('trainer', '/longevity/gestionale')).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('il cliente non entra nelle altre piattaforme ne nel blog', () => {
|
||||||
|
expect(canAccessAdminPath('cliente', '/campus')).toBe(false);
|
||||||
|
expect(canAccessAdminPath('cliente', '/admin/content')).toBe(false);
|
||||||
|
expect(canAccessAdminPath('cliente', '/admin/posts')).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('il ruolo piattaforme non eredita longevity', () => {
|
||||||
|
expect(canAccessAdminPath('piattaforme', '/longevity/gestionale')).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('admin passa sempre', () => {
|
||||||
|
expect(canAccessAdminPath('admin', '/longevity/gestionale')).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user