Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "elo",
"version": "1.37.0",
"version": "1.38.0",
"private": true,
"type": "module",
"scripts": {
Expand Down
33 changes: 33 additions & 0 deletions scripts/sql/backfill-emotion-ruler-view-all.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
-- Backfill (OPCIONAL): marca role_config.can_view_emotion_ruler = true
-- para todos os usuários ativos e não-TOTEM.
--
-- Contexto:
-- * A Régua de Emoções foi liberada para todos os usuários (exceto TOTEM/
-- desativado) via política centralizada em src/lib/access-control.ts
-- (canViewEmotionRuler). A visualização NÃO depende mais deste campo.
-- * Este script é apenas COSMÉTICO: mantém os toggles do editor de permissões
-- (admin/users) coerentes com o novo comportamento público. Não é necessário
-- para que a liberação funcione, e cadastros futuros são cobertos pela política,
-- não por este campo.
--
-- Observações de segurança:
-- * Não toca em usuários TOTEM nem desativados (is_active = false).
-- * "updatedAt" é setado manualmente porque o @updatedAt do Prisma é aplicado
-- na camada da aplicação, não em UPDATEs via SQL puro.
-- * Idempotente: só atualiza quem ainda não tem o campo = true.

-- 1) (Opcional) Conferência antes de aplicar — quantos serão afetados:
-- SELECT COUNT(*) AS users_a_atualizar
-- FROM "users"
-- WHERE "is_active" = true
-- AND COALESCE(("role_config" ->> 'isTotem')::boolean, false) = false
-- AND COALESCE(("role_config" ->> 'can_view_emotion_ruler')::boolean, false) = false;

-- 2) Aplicação do backfill:
UPDATE "users"
SET "role_config" = COALESCE("role_config", '{}'::jsonb)
|| jsonb_build_object('can_view_emotion_ruler', true),
"updatedAt" = NOW()
WHERE "is_active" = true
AND COALESCE(("role_config" ->> 'isTotem')::boolean, false) = false
AND COALESCE(("role_config" ->> 'can_view_emotion_ruler')::boolean, false) = false;
5 changes: 3 additions & 2 deletions src/hooks/use-access-control.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,10 @@ export function useAccessControl() {
};

const canViewEmotionRuler = (): boolean => {
// Liberado para todos os usuários, exceto TOTEM/desativado
// (alinhado ao helper canViewEmotionRuler em @/lib/access-control).
if (!db_user?.role_config) return false;
if (db_user.role_config.sudo) return true;
return db_user.role_config.can_view_emotion_ruler ?? false;
return !db_user.role_config.isTotem;
};

const canManageEmotionRules = (): boolean => {
Expand Down
9 changes: 4 additions & 5 deletions src/lib/access-control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,11 @@ export function canViewCars(roleConfig: RolesConfig | null): boolean {
}

export function canViewEmotionRuler(roleConfig: RolesConfig | null): boolean {
// Régua de Emoções é um módulo restrito (não liberado para todos).
// Acesso somente para sudo ou quem tem a permissão explícita; nunca TOTEM/desativado.
// Régua de Emoções liberada para todos os usuários, exceto TOTEM/desativado
// (alinhado aos demais módulos de visualização). O campo can_view_emotion_ruler
// permanece no schema por compatibilidade, mas não restringe mais a visualização.
if (!roleConfig) return false;
if (roleConfig.isTotem) return false;
if (roleConfig.sudo) return true;
return roleConfig.can_view_emotion_ruler === true;
return !roleConfig.isTotem;
}

/**
Expand Down
7 changes: 2 additions & 5 deletions src/server/api/routers/emotion-ruler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,8 @@ export const emotionRulerRouter = createTRPCRouter({

const roleConfig = getEffectiveRoleConfig(user);

if (roleConfig.isTotem === true) {
return { shouldShow: false, ruler: null };
}

if (!roleConfig.can_view_emotion_ruler && !roleConfig.sudo) {
// Liberado para todos (exceto TOTEM/desativado) via política centralizada.
if (!canViewEmotionRuler(roleConfig)) {
return { shouldShow: false, ruler: null };
}

Expand Down
Loading