dashboard for org
This commit is contained in:
@@ -4,10 +4,11 @@ namespace App\Modules\Clients\Controllers;
|
||||
|
||||
use App\Controllers\BaseController;
|
||||
use App\Modules\Clients\Models\ClientModel;
|
||||
use App\Services\AccessService;
|
||||
|
||||
class Clients extends BaseController
|
||||
{
|
||||
protected $clientModel;
|
||||
protected ClientModel $clientModel;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
@@ -16,11 +17,19 @@ class Clients extends BaseController
|
||||
|
||||
public function index()
|
||||
{
|
||||
// Проверка права на просмотр
|
||||
if (!$this->access->canView('clients')) {
|
||||
return $this->forbiddenResponse('У вас нет прав для просмотра клиентов');
|
||||
}
|
||||
|
||||
$config = $this->getTableConfig();
|
||||
|
||||
return $this->renderTwig('@Clients/index', [
|
||||
'title' => 'Клиенты',
|
||||
'tableHtml' => $this->renderTable($config),
|
||||
'can_create' => $this->access->canCreate('clients'),
|
||||
'can_edit' => $this->access->canEdit('clients'),
|
||||
'can_delete' => $this->access->canDelete('clients'),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -29,8 +38,6 @@ class Clients extends BaseController
|
||||
*/
|
||||
protected function getTableConfig(): array
|
||||
{
|
||||
$organizationId = session()->get('active_org_id');
|
||||
|
||||
return [
|
||||
'id' => 'clients-table',
|
||||
'url' => '/clients/table',
|
||||
@@ -51,14 +58,16 @@ class Clients extends BaseController
|
||||
'url' => '/clients/edit/{id}',
|
||||
'icon' => 'fa-solid fa-pen',
|
||||
'class' => 'btn-outline-primary',
|
||||
'title' => 'Редактировать'
|
||||
'title' => 'Редактировать',
|
||||
'type' => 'edit',
|
||||
],
|
||||
[
|
||||
'label' => '',
|
||||
'url' => '/clients/delete/{id}',
|
||||
'icon' => 'fa-solid fa-trash',
|
||||
'class' => 'btn-outline-danger',
|
||||
'title' => 'Удалить'
|
||||
'title' => 'Удалить',
|
||||
'type' => 'delete',
|
||||
]
|
||||
],
|
||||
'emptyMessage' => 'Клиентов пока нет',
|
||||
@@ -66,33 +75,28 @@ class Clients extends BaseController
|
||||
'emptyActionUrl' => base_url('/clients/new'),
|
||||
'emptyActionLabel'=> 'Добавить клиента',
|
||||
'emptyActionIcon' => 'fa-solid fa-plus',
|
||||
'scope' => function ($builder) use ($organizationId) {
|
||||
$builder->where('organization_id', $organizationId);
|
||||
},
|
||||
'can_edit' => $this->access->canEdit('clients'),
|
||||
'can_delete' => $this->access->canDelete('clients'),
|
||||
];
|
||||
}
|
||||
|
||||
public function table()
|
||||
public function table(?array $config = null, ?string $pageUrl = null)
|
||||
{
|
||||
$isPartial = $this->request->getGet('format') === 'partial' || $this->isAjax();
|
||||
|
||||
if ($isPartial) {
|
||||
// AJAX — только tbody + tfoot
|
||||
return $this->renderTable(null, true);
|
||||
// Проверка права на просмотр
|
||||
if (!$this->access->canView('clients')) {
|
||||
return $this->forbiddenResponse('У вас нет прав для просмотра клиентов');
|
||||
}
|
||||
|
||||
// Прямой запрос — полная страница
|
||||
$config = $this->getTableConfig();
|
||||
$tableHtml = $this->renderTable($config, false);
|
||||
|
||||
return $this->renderTwig('@Clients/index', [
|
||||
'title' => $config['pageTitle'] ?? 'Клиенты',
|
||||
'tableHtml' => $tableHtml,
|
||||
]);
|
||||
|
||||
return parent::table($config, '/clients');
|
||||
}
|
||||
|
||||
public function new()
|
||||
{
|
||||
// Проверка права на создание
|
||||
if (!$this->access->canCreate('clients')) {
|
||||
return $this->forbiddenResponse('У вас нет прав для создания клиентов');
|
||||
}
|
||||
|
||||
$data = [
|
||||
'title' => 'Добавить клиента',
|
||||
'client' => null,
|
||||
@@ -103,6 +107,11 @@ class Clients extends BaseController
|
||||
|
||||
public function create()
|
||||
{
|
||||
// Проверка права на создание
|
||||
if (!$this->access->canCreate('clients')) {
|
||||
return $this->forbiddenResponse('У вас нет прав для создания клиентов');
|
||||
}
|
||||
|
||||
$organizationId = session()->get('active_org_id');
|
||||
|
||||
$rules = [
|
||||
@@ -133,12 +142,12 @@ class Clients extends BaseController
|
||||
|
||||
public function edit($id)
|
||||
{
|
||||
$organizationId = session()->get('active_org_id');
|
||||
// Проверка права на редактирование
|
||||
if (!$this->access->canEdit('clients')) {
|
||||
return $this->forbiddenResponse('У вас нет прав для редактирования клиентов');
|
||||
}
|
||||
|
||||
$client = $this->clientModel
|
||||
->where('id', $id)
|
||||
->where('organization_id', $organizationId)
|
||||
->first();
|
||||
$client = $this->clientModel->forCurrentOrg()->find($id);
|
||||
|
||||
if (!$client) {
|
||||
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound('Клиент не найден');
|
||||
@@ -154,13 +163,13 @@ class Clients extends BaseController
|
||||
|
||||
public function update($id)
|
||||
{
|
||||
$organizationId = session()->get('active_org_id');
|
||||
// Проверка права на редактирование
|
||||
if (!$this->access->canEdit('clients')) {
|
||||
return $this->forbiddenResponse('У вас нет прав для редактирования клиентов');
|
||||
}
|
||||
|
||||
// Проверяем что клиент принадлежит организации
|
||||
$client = $this->clientModel
|
||||
->where('id', $id)
|
||||
->where('organization_id', $organizationId)
|
||||
->first();
|
||||
// Проверяем что клиент принадлежит организации через forCurrentOrg()
|
||||
$client = $this->clientModel->forCurrentOrg()->find($id);
|
||||
|
||||
if (!$client) {
|
||||
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound('Клиент не найден');
|
||||
@@ -193,13 +202,13 @@ class Clients extends BaseController
|
||||
|
||||
public function delete($id)
|
||||
{
|
||||
$organizationId = session()->get('active_org_id');
|
||||
// Проверка права на удаление
|
||||
if (!$this->access->canDelete('clients')) {
|
||||
return $this->forbiddenResponse('У вас нет прав для удаления клиентов');
|
||||
}
|
||||
|
||||
// Проверяем что клиент принадлежит организации
|
||||
$client = $this->clientModel
|
||||
->where('id', $id)
|
||||
->where('organization_id', $organizationId)
|
||||
->first();
|
||||
// Проверяем что клиент принадлежит организации через forCurrentOrg()
|
||||
$client = $this->clientModel->forCurrentOrg()->find($id);
|
||||
|
||||
if (!$client) {
|
||||
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound('Клиент не найден');
|
||||
@@ -210,4 +219,22 @@ class Clients extends BaseController
|
||||
session()->setFlashdata('success', 'Клиент удалён');
|
||||
return redirect()->to('/clients');
|
||||
}
|
||||
|
||||
/**
|
||||
* Возврат ответа "Доступ запрещён"
|
||||
*
|
||||
* @param string $message
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
protected function forbiddenResponse(string $message = 'Доступ запрещён')
|
||||
{
|
||||
if ($this->request->isAJAX()) {
|
||||
return service('response')
|
||||
->setStatusCode(403)
|
||||
->setJSON(['error' => $message]);
|
||||
}
|
||||
|
||||
session()->setFlashdata('error', $message);
|
||||
return redirect()->to('/');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user