- Add source maps and license files for compiled assets - Add debug and test files for development - Complete Git repository with all necessary files for MoneyFlix project
402 lines
22 KiB
PHP
402 lines
22 KiB
PHP
<?php
|
|
use App\Core\AssetPath;
|
|
use App\Core\OutputSanitize;
|
|
?>
|
|
|
|
<div class="row" x-data="rolesList">
|
|
<div class="col-12">
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<div class="d-flex align-items-center flex-wrap gap-2">
|
|
<div class="flex-grow-1">
|
|
<h4 class="card-title mb-0">Gestão de Perfis de Utilizador</h4>
|
|
</div>
|
|
<div class="flex-shrink-0">
|
|
<button class="btn btn-info add-btn" data-bs-toggle="modal" data-bs-target="#addRoleModal">
|
|
<i class="ri-add-fill me-1 align-bottom"></i> Adicionar Perfil
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="card-body">
|
|
<div class="row g-3 mb-3">
|
|
<div class="col-md-4">
|
|
<div class="search-box">
|
|
<input type="text"
|
|
class="form-control search"
|
|
placeholder="Pesquisar perfil..."
|
|
x-model="searchQuery"
|
|
@input="loadRoles()">
|
|
<i class="ri-search-line search-icon"></i>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-auto ms-auto">
|
|
<div class="d-flex align-items-center gap-2">
|
|
<span class="text-muted">Ordenar por: </span>
|
|
<select class="form-control mb-0" x-model="sortBy" @change="loadRoles()">
|
|
<option value="name">Nome</option>
|
|
<option value="users_count">Utilizadores</option>
|
|
<option value="created_at">Data de Criação</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="table-responsive table-card mb-3">
|
|
<table class="table align-middle table-nowrap mb-0">
|
|
<thead class="table-light">
|
|
<tr>
|
|
<th scope="col" style="width: 50px;">
|
|
<div class="form-check">
|
|
<input class="form-check-input" type="checkbox" id="checkAll">
|
|
</div>
|
|
</th>
|
|
<th class="sort" data-sort="name">Nome do Perfil</th>
|
|
<th class="sort" data-sort="description">Descrição</th>
|
|
<th class="sort" data-sort="permissions">Permissões</th>
|
|
<th class="sort" data-sort="users_count">Utilizadores</th>
|
|
<th class="sort" data-sort="status">Estado</th>
|
|
<th scope="col">Ações</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<template x-if="loading">
|
|
<tr>
|
|
<td colspan="7" class="text-center py-4">
|
|
<div class="spinner-border text-primary" role="status">
|
|
<span class="visually-hidden">A carregar...</span>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
</template>
|
|
|
|
<template x-for="role in roles" :key="role.id">
|
|
<tr>
|
|
<th scope="row">
|
|
<div class="form-check">
|
|
<input class="form-check-input" type="checkbox" :value="role.id">
|
|
</div>
|
|
</th>
|
|
<td>
|
|
<div class="d-flex align-items-center">
|
|
<div class="avatar-xs me-3">
|
|
<div class="avatar-title" :class="getRoleIconClass(role.type)">
|
|
<i :class="getRoleIcon(role.type)"></i>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<h6 class="mb-0" x-text="role.name"></h6>
|
|
<small class="text-muted" x-text="role.key"></small>
|
|
</div>
|
|
</div>
|
|
</td>
|
|
<td x-text="role.description"></td>
|
|
<td>
|
|
<template x-for="permission in role.permissions" :key="permission">
|
|
<span class="badge bg-info-subtle text-info me-1" x-text="permission"></span>
|
|
</template>
|
|
</td>
|
|
<td>
|
|
<div class="d-flex align-items-center">
|
|
<div class="avatar-group me-2">
|
|
<template x-for="user in role.users.slice(0, 3)" :key="user.id">
|
|
<div class="avatar-group-item">
|
|
<div class="avatar-xs">
|
|
<img :src="user.avatar || '/assets/images/users/user-dummy-img.jpg'"
|
|
:alt="user.name"
|
|
class="rounded-circle img-fluid">
|
|
</div>
|
|
</div>
|
|
</template>
|
|
<template x-if="role.users.length > 3">
|
|
<div class="avatar-group-item">
|
|
<div class="avatar-xs">
|
|
<div class="avatar-title rounded-circle bg-light text-primary">
|
|
+<span x-text="role.users.length - 3"></span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</template>
|
|
</div>
|
|
<span class="text-muted" x-text="role.users_count + ' utilizadores'"></span>
|
|
</div>
|
|
</td>
|
|
<td>
|
|
<span class="badge" :class="role.status === 'active' ? 'bg-success-subtle text-success' : 'bg-secondary-subtle text-secondary'" x-text="role.status === 'active' ? 'Ativo' : 'Inativo'"></span>
|
|
</td>
|
|
<td>
|
|
<div class="dropdown">
|
|
<button class="btn btn-soft-secondary btn-sm dropdown" type="button" data-bs-toggle="dropdown" aria-expanded="false">
|
|
<i class="ri-more-fill align-middle"></i>
|
|
</button>
|
|
<ul class="dropdown-menu dropdown-menu-end">
|
|
<li><a class="dropdown-item" href="javascript:void(0);" @click="viewRole(role.id)"><i class="ri-eye-fill align-bottom me-2 text-muted"></i> Ver</a></li>
|
|
<li><a class="dropdown-item" href="javascript:void(0);" @click="editRole(role.id)"><i class="ri-pencil-fill align-bottom me-2 text-muted"></i> Editar</a></li>
|
|
<li><a class="dropdown-item" href="javascript:void(0);" @click="duplicateRole(role.id)"><i class="ri-file-copy-fill align-bottom me-2 text-muted"></i> Duplicar</a></li>
|
|
<li><hr class="dropdown-divider"></li>
|
|
<li><a class="dropdown-item text-danger" href="javascript:void(0);" @click="deleteRole(role.id)"><i class="ri-delete-bin-fill align-bottom me-2 text-muted"></i> Eliminar</a></li>
|
|
</ul>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
</template>
|
|
|
|
<template x-if="!loading && roles.length === 0">
|
|
<tr>
|
|
<td colspan="7" class="text-center py-4">
|
|
<div class="text-muted">
|
|
<i class="ri-shield-user-line fs-1"></i>
|
|
<p class="mt-2">Nenhum perfil encontrado</p>
|
|
<button class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#addRoleModal">
|
|
Criar Primeiro Perfil
|
|
</button>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
</template>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Modal para adicionar perfil -->
|
|
<div class="modal fade" id="addRoleModal" tabindex="-1" aria-labelledby="addRoleModalLabel" aria-hidden="true">
|
|
<div class="modal-dialog modal-dialog-centered modal-lg">
|
|
<div class="modal-content border-0">
|
|
<div class="modal-header bg-info-subtle p-3">
|
|
<h5 class="modal-title" id="addRoleModalLabel">Adicionar Novo Perfil</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<form id="addRoleForm">
|
|
<div class="modal-body">
|
|
<div class="row g-3">
|
|
<div class="col-lg-6">
|
|
<div>
|
|
<label for="role-name" class="form-label">Nome do Perfil *</label>
|
|
<input type="text" class="form-control" id="role-name" name="name" placeholder="Ex: Editor, Gestor, etc." required>
|
|
</div>
|
|
</div>
|
|
<div class="col-lg-6">
|
|
<div>
|
|
<label for="role-key" class="form-label">Chave Única *</label>
|
|
<input type="text" class="form-control" id="role-key" name="key" placeholder="Ex: editor, manager, etc." required>
|
|
<small class="text-muted">Usado internamente pelo sistema</small>
|
|
</div>
|
|
</div>
|
|
<div class="col-lg-12">
|
|
<div>
|
|
<label for="role-description" class="form-label">Descrição</label>
|
|
<textarea class="form-control" id="role-description" name="description" rows="3" placeholder="Descreva as responsabilidades deste perfil"></textarea>
|
|
</div>
|
|
</div>
|
|
<div class="col-lg-12">
|
|
<div>
|
|
<label class="form-label">Permissões</label>
|
|
<div class="row">
|
|
<div class="col-md-6">
|
|
<h6 class="text-muted mb-2">Gestão de Utilizadores</h6>
|
|
<div class="form-check">
|
|
<input class="form-check-input" type="checkbox" id="perm-users-view" name="permissions[]" value="users.view">
|
|
<label class="form-check-label" for="perm-users-view">Ver utilizadores</label>
|
|
</div>
|
|
<div class="form-check">
|
|
<input class="form-check-input" type="checkbox" id="perm-users-create" name="permissions[]" value="users.create">
|
|
<label class="form-check-label" for="perm-users-create">Criar utilizadores</label>
|
|
</div>
|
|
<div class="form-check">
|
|
<input class="form-check-input" type="checkbox" id="perm-users-edit" name="permissions[]" value="users.edit">
|
|
<label class="form-check-label" for="perm-users-edit">Editar utilizadores</label>
|
|
</div>
|
|
<div class="form-check">
|
|
<input class="form-check-input" type="checkbox" id="perm-users-delete" name="permissions[]" value="users.delete">
|
|
<label class="form-check-label" for="perm-users-delete">Eliminar utilizadores</label>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-6">
|
|
<h6 class="text-muted mb-2">Gestão de Conteúdo</h6>
|
|
<div class="form-check">
|
|
<input class="form-check-input" type="checkbox" id="perm-content-view" name="permissions[]" value="content.view">
|
|
<label class="form-check-label" for="perm-content-view">Ver conteúdo</label>
|
|
</div>
|
|
<div class="form-check">
|
|
<input class="form-check-input" type="checkbox" id="perm-content-create" name="permissions[]" value="content.create">
|
|
<label class="form-check-label" for="perm-content-create">Criar conteúdo</label>
|
|
</div>
|
|
<div class="form-check">
|
|
<input class="form-check-input" type="checkbox" id="perm-content-edit" name="permissions[]" value="content.edit">
|
|
<label class="form-check-label" for="perm-content-edit">Editar conteúdo</label>
|
|
</div>
|
|
<div class="form-check">
|
|
<input class="form-check-input" type="checkbox" id="perm-content-delete" name="permissions[]" value="content.delete">
|
|
<label class="form-check-label" for="perm-content-delete">Eliminar conteúdo</label>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="row mt-3">
|
|
<div class="col-md-6">
|
|
<h6 class="text-muted mb-2">Relatórios</h6>
|
|
<div class="form-check">
|
|
<input class="form-check-input" type="checkbox" id="perm-reports-view" name="permissions[]" value="reports.view">
|
|
<label class="form-check-label" for="perm-reports-view">Ver relatórios</label>
|
|
</div>
|
|
<div class="form-check">
|
|
<input class="form-check-input" type="checkbox" id="perm-reports-create" name="permissions[]" value="reports.create">
|
|
<label class="form-check-label" for="perm-reports-create">Criar relatórios</label>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-6">
|
|
<h6 class="text-muted mb-2">Sistema</h6>
|
|
<div class="form-check">
|
|
<input class="form-check-input" type="checkbox" id="perm-system-settings" name="permissions[]" value="system.settings">
|
|
<label class="form-check-label" for="perm-system-settings">Configurações do sistema</label>
|
|
</div>
|
|
<div class="form-check">
|
|
<input class="form-check-input" type="checkbox" id="perm-system-admin" name="permissions[]" value="system.admin">
|
|
<label class="form-check-label" for="perm-system-admin">Acesso administrativo</label>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<div class="hstack gap-2 justify-content-end">
|
|
<button type="button" class="btn btn-light" data-bs-dismiss="modal">Cancelar</button>
|
|
<button type="submit" class="btn btn-success">Criar Perfil</button>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
// Alpine.js data
|
|
window.rolesList = {
|
|
roles: [],
|
|
loading: false,
|
|
searchQuery: '',
|
|
sortBy: 'name',
|
|
|
|
init() {
|
|
this.loadRoles();
|
|
},
|
|
|
|
async loadRoles() {
|
|
this.loading = true;
|
|
try {
|
|
const response = await fetch('/api/users/roles?' + new URLSearchParams({
|
|
search: this.searchQuery,
|
|
sort: this.sortBy
|
|
}));
|
|
const data = await response.json();
|
|
|
|
if (data.success) {
|
|
this.roles = data.data.roles || [];
|
|
} else {
|
|
console.error('Erro na API:', data.message);
|
|
alert('Erro ao carregar perfis: ' + data.message);
|
|
this.roles = [];
|
|
}
|
|
} catch (error) {
|
|
console.error('Erro ao carregar perfis:', error);
|
|
alert('Erro ao carregar perfis. Verifique a consola para mais detalhes.');
|
|
this.roles = [];
|
|
} finally {
|
|
this.loading = false;
|
|
}
|
|
},
|
|
|
|
getRoleIconClass(type) {
|
|
const classes = {
|
|
'admin': 'bg-danger-subtle text-danger',
|
|
'editor': 'bg-info-subtle text-info',
|
|
'user': 'bg-warning-subtle text-warning'
|
|
};
|
|
return classes[type] || 'bg-secondary-subtle text-secondary';
|
|
},
|
|
|
|
getRoleIcon(type) {
|
|
const icons = {
|
|
'admin': 'ri-shield-user-line',
|
|
'editor': 'ri-edit-line',
|
|
'user': 'ri-user-line'
|
|
};
|
|
return icons[type] || 'ri-user-line';
|
|
},
|
|
|
|
viewRole(id) {
|
|
window.location.href = `/admin/roles/${id}`;
|
|
},
|
|
|
|
editRole(id) {
|
|
window.location.href = `/admin/roles/${id}/edit`;
|
|
},
|
|
|
|
duplicateRole(id) {
|
|
// Implementar duplicação
|
|
alert('Funcionalidade de duplicação em desenvolvimento');
|
|
},
|
|
|
|
async deleteRole(id) {
|
|
if (confirm('Tem certeza que deseja eliminar este perfil?')) {
|
|
try {
|
|
const response = await fetch(`/api/users/roles/${id}`, {
|
|
method: 'DELETE'
|
|
});
|
|
const data = await response.json();
|
|
if (data.success) {
|
|
this.loadRoles();
|
|
} else {
|
|
alert('Erro ao eliminar perfil: ' + data.message);
|
|
}
|
|
} catch (error) {
|
|
console.error('Erro:', error);
|
|
alert('Erro ao eliminar perfil');
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
// Form submission
|
|
document.getElementById('addRoleForm').addEventListener('submit', async function(e) {
|
|
e.preventDefault();
|
|
|
|
const formData = new FormData(this);
|
|
const submitBtn = this.querySelector('button[type="submit"]');
|
|
const originalText = submitBtn.textContent;
|
|
|
|
submitBtn.textContent = 'A criar...';
|
|
submitBtn.disabled = true;
|
|
|
|
try {
|
|
const response = await fetch('/api/users/roles', {
|
|
method: 'POST',
|
|
body: formData
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
if (data.success) {
|
|
alert('Perfil criado com sucesso!');
|
|
bootstrap.Modal.getInstance(document.getElementById('addRoleModal')).hide();
|
|
this.reset();
|
|
window.rolesList.loadRoles();
|
|
} else {
|
|
alert('Erro ao criar perfil: ' + data.message);
|
|
}
|
|
} catch (error) {
|
|
console.error('Erro:', error);
|
|
alert('Erro ao criar perfil');
|
|
} finally {
|
|
submitBtn.textContent = originalText;
|
|
submitBtn.disabled = false;
|
|
}
|
|
});
|
|
});
|
|
</script>
|