Added users page

Initial work on role separation
This commit is contained in:
Pinga 2023-11-09 17:28:17 +02:00
parent acd47fb181
commit 2cd764cd21
5 changed files with 235 additions and 5 deletions

View file

@ -0,0 +1,18 @@
<?php
namespace App\Controllers;
use App\Models\User;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Container\ContainerInterface;
class UsersController extends Controller
{
public function view(Request $request, Response $response)
{
$userModel = new User($this->container->get('db'));
$users = $userModel->getAllUsers();
return view($response,'admin/users/index.twig', compact('users'));
}
}

View file

@ -0,0 +1,77 @@
{% extends "layouts/app.twig" %}
{% block title %}{{ __('Users') }}{% endblock %}
{% block content %}
<div class="page-wrapper">
<!-- Page header -->
<div class="page-header d-print-none">
<div class="container-xl">
<div class="row g-2 align-items-center">
<div class="col">
<!-- Page pre-title -->
<div class="page-pretitle">
{{ __('Overview') }}
</div>
<h2 class="page-title">
{{ __('Users') }}
</h2>
</div>
<!-- Page title actions -->
<div class="col-auto ms-auto d-print-none">
<div class="btn-list">
<a href="#" class="btn btn-primary d-none d-sm-inline-block">
<svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><line x1="12" y1="5" x2="12" y2="19" /><line x1="5" y1="12" x2="19" y2="12" /></svg>
Create User
</a>
<a href="#" class="btn btn-primary d-sm-none btn-icon" aria-label="Create User">
<svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><line x1="12" y1="5" x2="12" y2="19" /><line x1="5" y1="12" x2="19" y2="12" /></svg>
</a>
</div>
</div>
</div>
</div>
</div>
<!-- Page body -->
<div class="page-body">
<div class="container-xl">
<div class="col-12">
<div class="card">
<div class="card-body border-bottom py-3">
<div class="d-flex">
<div class="text-secondary">
<button class="btn btn-info btn-icon" onclick="downloadCSV()"><svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"></path><path d="M10 15a1 1 0 0 0 1 1h2a1 1 0 0 0 1 -1v-2a1 1 0 0 0 -1 -1h-2a1 1 0 0 1 -1 -1v-2a1 1 0 0 1 1 -1h2a1 1 0 0 1 1 1"></path><path d="M17 8l2 8l2 -8"></path><path d="M7 10a2 2 0 1 0 -4 0v4a2 2 0 1 0 4 0"></path></svg></button>
<button class="btn btn-info btn-icon" onclick="downloadJSON()"><svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"></path><path d="M20 16v-8l3 8v-8"></path><path d="M15 8a2 2 0 0 1 2 2v4a2 2 0 1 1 -4 0v-4a2 2 0 0 1 2 -2z"></path><path d="M1 8h3v6.5a1.5 1.5 0 0 1 -3 0v-.5"></path><path d="M7 15a1 1 0 0 0 1 1h1a1 1 0 0 0 1 -1v-2a1 1 0 0 0 -1 -1h-1a1 1 0 0 1 -1 -1v-2a1 1 0 0 1 1 -1h1a1 1 0 0 1 1 1"></path></svg></button>
<button class="btn btn-green btn-icon" onclick="downloadXLSX()"><svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"></path><path d="M14 3v4a1 1 0 0 0 1 1h4"></path><path d="M5 12v-7a2 2 0 0 1 2 -2h7l5 5v4"></path><path d="M4 15l4 6"></path><path d="M4 21l4 -6"></path><path d="M17 20.25c0 .414 .336 .75 .75 .75h1.25a1 1 0 0 0 1 -1v-1a1 1 0 0 0 -1 -1h-1a1 1 0 0 1 -1 -1v-1a1 1 0 0 1 1 -1h1.25a.75 .75 0 0 1 .75 .75"></path><path d="M11 15v6h3"></path></svg></button>
<button class="btn btn-red btn-icon" onclick="downloadPDF()"><svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"></path><path d="M10 8v8h2a2 2 0 0 0 2 -2v-4a2 2 0 0 0 -2 -2h-2z"></path><path d="M3 12h2a2 2 0 1 0 0 -4h-2v8"></path><path d="M17 12h3"></path><path d="M21 8h-4v8"></path></svg></button>
</div>
<div class="ms-auto text-secondary">
Search:
<div class="ms-2 d-inline-block">
<input id="search-input" type="text" class="form-control" aria-label="Search users">
</div>
</div>
</div>
<div class="table-responsive mt-3">
<div id="userTable"></div>
</div>
</div>
</div>
</div>
</div>
</div>
<footer class="footer footer-transparent d-print-none">
<div class="container-xl">
<div class="col-12 col-lg-auto mt-3 mt-lg-0">
<ul class="list-inline list-inline-dots mb-0">
<li class="list-inline-item">
Copyright &copy; 2023
<a href="https://namingo.org" target="_blank" class="link-secondary">Namingo</a>.
</li>
</ul>
</div>
</div>
</div>
</footer>
</div>
{% endblock %}

View file

@ -6,7 +6,7 @@
<meta http-equiv="X-UA-Compatible" content="ie=edge"/> <meta http-equiv="X-UA-Compatible" content="ie=edge"/>
<title>{% block title %}{% endblock %} | Namingo</title> <title>{% block title %}{% endblock %} | Namingo</title>
<!-- CSS files --> <!-- CSS files -->
{% if route_is('domains') or route_is('contacts') or route_is('hosts') or route_is('epphistory') or route_is('registrars') or route_is('transactions') or route_is('overview') or route_is('reports') or route_is('transfers') %} {% if route_is('domains') or route_is('contacts') or route_is('hosts') or route_is('epphistory') or route_is('registrars') or route_is('transactions') or route_is('overview') or route_is('reports') or route_is('transfers') or route_is('users') %}
{% include 'partials/css-tables.twig' %} {% include 'partials/css-tables.twig' %}
{% else %} {% else %}
{% include 'partials/css.twig' %} {% include 'partials/css.twig' %}
@ -232,7 +232,7 @@
</a> </a>
</div> </div>
</li> </li>
<li {{ is_current_url('registrars') ? 'class="nav-item dropdown active"' : 'class="nav-item dropdown"' }}> <li {{ is_current_url('registrars') or is_current_url('users') ? 'class="nav-item dropdown active"' : 'class="nav-item dropdown"' }}>
<a class="nav-link dropdown-toggle" href="#" data-bs-toggle="dropdown" data-bs-auto-close="outside" role="button" aria-expanded="false"> <a class="nav-link dropdown-toggle" href="#" data-bs-toggle="dropdown" data-bs-auto-close="outside" role="button" aria-expanded="false">
<span class="nav-link-icon d-md-none d-lg-inline-block"><svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"></path><path d="M12 13a3 3 0 1 0 0 -6a3 3 0 0 0 0 6z"></path><path d="M12 3c7.2 0 9 1.8 9 9s-1.8 9 -9 9s-9 -1.8 -9 -9s1.8 -9 9 -9z"></path><path d="M6 20.05v-.05a4 4 0 0 1 4 -4h4a4 4 0 0 1 4 4v.05"></path></svg> <span class="nav-link-icon d-md-none d-lg-inline-block"><svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"></path><path d="M12 13a3 3 0 1 0 0 -6a3 3 0 0 0 0 6z"></path><path d="M12 3c7.2 0 9 1.8 9 9s-1.8 9 -9 9s-9 -1.8 -9 -9s1.8 -9 9 -9z"></path><path d="M6 20.05v-.05a4 4 0 0 1 4 -4h4a4 4 0 0 1 4 4v.05"></path></svg>
</span> </span>
@ -248,7 +248,7 @@
{{ __('Create Registrar') }} {{ __('Create Registrar') }}
</a> </a>
<div class="dropdown-divider"></div> <div class="dropdown-divider"></div>
<a class="dropdown-item" href="{{route('registrars')}}"> <a class="dropdown-item" href="{{route('users')}}">
{{ __('List Users') }} {{ __('List Users') }}
</a> </a>
<a class="dropdown-item" href="#"> <a class="dropdown-item" href="#">
@ -495,6 +495,8 @@
{% include 'partials/js-reports.twig' %} {% include 'partials/js-reports.twig' %}
{% elseif route_is('transfers') %} {% elseif route_is('transfers') %}
{% include 'partials/js-transfers.twig' %} {% include 'partials/js-transfers.twig' %}
{% elseif route_is('users') %}
{% include 'partials/js-users.twig' %}
{% else %} {% else %}
{% include 'partials/js.twig' %} {% include 'partials/js.twig' %}
{% endif %} {% endif %}

View file

@ -0,0 +1,107 @@
<script src="/assets/js/tabulator.min.js" defer></script>
<script src="/assets/js/tabler.min.js" defer></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.5/xlsx.full.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/3.6.0/jspdf.plugin.autotable.min.js"></script>
<script>
var table;
document.querySelector("#userTable").addEventListener('click', function(e) {
if (e.target.matches('.update-btn')) {
let id = e.target.getAttribute('data-id');
updateRecord(id);
} else if (e.target.matches('.delete-btn')) {
let id = e.target.getAttribute('data-id');
deleteRecord(id);
}
});
document.addEventListener("DOMContentLoaded", function(){
function userLinkFormatter(cell){
var value = cell.getValue();
return `<a href="/user/${value}" style="font-weight:bold;">${value}</a>`;
}
function actionsFormatter(cell, formatterParams, onRendered) {
return `
<button class="btn btn-primary btn-icon update-btn" data-id="${cell.getRow().getData().id}"><svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"></path><path d="M7 7h-1a2 2 0 0 0 -2 2v9a2 2 0 0 0 2 2h9a2 2 0 0 0 2 -2v-1"></path><path d="M20.385 6.585a2.1 2.1 0 0 0 -2.97 -2.97l-8.415 8.385v3h3l8.385 -8.415z"></path><path d="M16 5l3 3"></path></svg></button>
<button class="btn btn-danger btn-icon delete-btn" data-id="${cell.getRow().getData().id}"><svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"></path><path d="M4 7h16"></path><path d="M5 7l1 12a2 2 0 0 0 2 2h8a2 2 0 0 0 2 -2l1 -12"></path><path d="M9 7v-3a1 1 0 0 1 1 -1h4a1 1 0 0 1 1 1v3"></path><path d="M10 12l4 4m0 -4l-4 4"></path></svg></button>
`;
}
function statusFormatter(cell) {
var statusArray = cell.getValue();
if (statusArray && Array.isArray(statusArray)) {
return statusArray.map(item => item.status).join(', ');
}
return "";
}
table = new Tabulator("#userTable", {
ajaxURL:"/api/records/users", // Set the URL for your JSON data
ajaxConfig:"GET",
pagination:"local",
paginationSize:10,
ajaxResponse:function(url, params, response){
return response.records;
},
layout:"fitDataFill",
responsiveLayout: "collapse",
responsiveLayoutCollapseStartOpen:false,
resizableColumns:false,
columns:[
{formatter:"responsiveCollapse", width:30, minWidth:30, hozAlign:"center", resizable:false, headerSort:false, responsive:0},
{title:"Name", field:"username", width:200, headerSort:true, formatter: userLinkFormatter, responsive:0},
{title:"Email", field:"email", width:300, headerSort:true, responsive:2},
{title:"Roles", field:"roles_mask", width:300, headerSort:true, responsive:2},
{title: "Actions", formatter: actionsFormatter, responsive:0, headerSort: false, download:false, hozAlign: "center", cellClick:function(e, cell){ e.stopPropagation(); }},
],
placeholder:function(){
return this.getHeaderFilters().length ? "No Matching Data" : "No Data"; //set placeholder based on if there are currently any header filters
}
});
var searchInput = document.getElementById("search-input");
searchInput.addEventListener("input", function () {
var term = searchInput.value.toLowerCase();
if (term) { // Only apply filter when there's a term to search for
table.setFilter(function (data) {
return (
String(data.username).toLowerCase().includes(term) ||
String(data.email).toLowerCase().includes(term) ||
String(data.roles_mask).toString().toLowerCase().includes(term)
);
});
} else {
table.clearFilter(); // Clear the filter when the search box is emptied
}
});
});
function updateRecord(id) {
console.log("Updating record with ID: " + id);
}
function deleteRecord(id) {
console.log("Deleting record with ID: " + id);
}
function downloadCSV() {
table.download("csv", "data.csv");
}
function downloadJSON() {
table.download("json", "data.json");
}
function downloadXLSX() {
table.download("xlsx", "data.xlsx", {sheetName:"My Registrars"});
}
function downloadPDF() {
table.download("pdf", "data.pdf", {
orientation:"portrait",
title:"My Registrars",
jsPDF:{unit:"mm", format:"a4", orientation:"p"}
});
}
</script>

View file

@ -7,6 +7,7 @@ use App\Controllers\ContactsController;
use App\Controllers\HostsController; use App\Controllers\HostsController;
use App\Controllers\LogsController; use App\Controllers\LogsController;
use App\Controllers\RegistrarsController; use App\Controllers\RegistrarsController;
use App\Controllers\UsersController;
use App\Controllers\FinancialsController; use App\Controllers\FinancialsController;
use App\Controllers\ReportsController; use App\Controllers\ReportsController;
use App\Controllers\ProfileController; use App\Controllers\ProfileController;
@ -53,6 +54,9 @@ $app->group('', function ($route) {
$route->map(['GET', 'POST'], '/host/create', HostsController::class . ':create')->setName('hostcreate'); $route->map(['GET', 'POST'], '/host/create', HostsController::class . ':create')->setName('hostcreate');
$route->get('/registrars', RegistrarsController::class .':view')->setName('registrars'); $route->get('/registrars', RegistrarsController::class .':view')->setName('registrars');
$route->get('/users', UsersController::class .':view')->setName('users');
$route->get('/epphistory', LogsController::class .':view')->setName('epphistory'); $route->get('/epphistory', LogsController::class .':view')->setName('epphistory');
$route->get('/reports', ReportsController::class .':view')->setName('reports'); $route->get('/reports', ReportsController::class .':view')->setName('reports');
@ -91,9 +95,9 @@ $app->any('/api[/{params:.*}]', function (
'password' => $db['mysql']['password'], 'password' => $db['mysql']['password'],
'database' => $db['mysql']['database'], 'database' => $db['mysql']['database'],
'basePath' => '/api', 'basePath' => '/api',
'middlewares' => 'authorization,sanitation', 'middlewares' => 'authorization,sanitation,multiTenancy',
'authorization.tableHandler' => function ($operation, $tableName) { 'authorization.tableHandler' => function ($operation, $tableName) {
$restrictedTables = ['users', 'contact_authInfo', 'contact_postalInfo', 'domain_authInfo', 'secdns']; $restrictedTables = ['contact_authInfo', 'contact_postalInfo', 'domain_authInfo', 'secdns'];
return !in_array($tableName, $restrictedTables); return !in_array($tableName, $restrictedTables);
}, },
'authorization.columnHandler' => function ($operation, $tableName, $columnName) { 'authorization.columnHandler' => function ($operation, $tableName, $columnName) {
@ -102,6 +106,28 @@ $app->any('/api[/{params:.*}]', function (
'sanitation.handler' => function ($operation, $tableName, $column, $value) { 'sanitation.handler' => function ($operation, $tableName, $column, $value) {
return is_string($value) ? strip_tags($value) : $value; return is_string($value) ? strip_tags($value) : $value;
}, },
'multiTenancy.handler' => function ($operation, $tableName) {
if (isset($_SESSION['auth_roles']) && $_SESSION['auth_roles'] === 0) {
return [];
}
$userId = $_SESSION['auth_user_id'];
$columnMap = [
'contact' => 'clid',
'domain' => 'clid',
'host' => 'clid',
'poll' => 'registrar_id',
'registrar' => 'id',
'payment_history' => 'registrar_id',
'statement' => 'registrar_id',
'support_tickets' => 'user_id',
];
if (array_key_exists($tableName, $columnMap)) {
return [$columnMap[$tableName] => $userId];
}
return ['1' => '0'];
},
]); ]);
$api = new Api($config); $api = new Api($config);
$response = $api->handle($request); $response = $api->handle($request);