refactor: update permission management and role structure
- Enhanced the permission management system by introducing a new base permissions structure, allowing for better organization and clarity. - Updated the role creation process to normalize role levels, limiting them to two levels: 0 (maximum access) and 1 (administrative access). - Improved the UI for permission displays, ensuring users are informed when no permissions are available. - Added alerts and messages to guide users in creating permissions when none exist. - Streamlined backend queries for permissions and roles to improve performance and maintainability.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { v } from 'convex/values';
|
||||
import { query, mutation } from './_generated/server';
|
||||
import { internalMutation, query, mutation } from './_generated/server';
|
||||
import type { Id } from './_generated/dataModel';
|
||||
import { getCurrentUserFunction } from './auth';
|
||||
|
||||
@@ -98,13 +98,16 @@ export const criar = mutation({
|
||||
permissoesParaCopiar = permissoesOrigem.map((item) => item.permissaoId);
|
||||
}
|
||||
|
||||
const nivelAjustado = Math.min(Math.max(Math.round(args.nivel), 0), 10);
|
||||
// Agora só existem níveis 0 e 1.
|
||||
// 0 = máximo (acesso total), 1 = administrativo (também com acesso total).
|
||||
// Qualquer valor informado diferente de 0 é normalizado para 1.
|
||||
const nivelNormalizado = Math.round(args.nivel) <= 0 ? 0 : 1;
|
||||
const setor = args.setor?.trim();
|
||||
|
||||
const roleId = await ctx.db.insert('roles', {
|
||||
nome: nomeNormalizado,
|
||||
descricao: args.descricao.trim() || args.nome.trim(),
|
||||
nivel: nivelAjustado,
|
||||
nivel: nivelNormalizado,
|
||||
setor: setor && setor.length > 0 ? setor : undefined,
|
||||
customizado: true,
|
||||
criadoPor: usuarioAtual._id,
|
||||
@@ -123,3 +126,26 @@ export const criar = mutation({
|
||||
return { sucesso: true as const, roleId };
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Migração de níveis de roles para o novo modelo (apenas 0 e 1).
|
||||
* - Mantém níveis 0 e 1 como estão.
|
||||
* - Converte qualquer nível > 1 para 1.
|
||||
*/
|
||||
export const migrarNiveisRoles = internalMutation({
|
||||
args: {},
|
||||
returns: v.null(),
|
||||
handler: async (ctx) => {
|
||||
const roles = await ctx.db.query('roles').collect();
|
||||
|
||||
for (const role of roles) {
|
||||
if (role.nivel <= 1) continue;
|
||||
|
||||
await ctx.db.patch(role._id, {
|
||||
nivel: 1
|
||||
});
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user