97 lines
3.4 KiB
PHP
97 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Models\AlertHistoryModel;
|
|
use App\Models\WorkspaceMemberModel;
|
|
use App\Models\WorkspaceModel;
|
|
use CodeIgniter\Controller;
|
|
use CodeIgniter\HTTP\RequestInterface;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
/**
|
|
* BaseController provides a convenient place for loading components
|
|
* and performing functions that are needed by all your controllers.
|
|
*
|
|
* Extend this class in any new controllers:
|
|
* ```
|
|
* class Home extends BaseController
|
|
* ```
|
|
*
|
|
* For security, be sure to declare any new methods as protected or private.
|
|
*/
|
|
abstract class BaseController extends Controller
|
|
{
|
|
/**
|
|
* Be sure to declare properties for any property fetch you initialized.
|
|
* The creation of dynamic property is deprecated in PHP 8.2.
|
|
*/
|
|
|
|
protected $session;
|
|
protected array $sharedData = [];
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
|
|
{
|
|
// Load here all helpers you want to be available in your controllers that extend BaseController.
|
|
// Caution: Do not put the this below the parent::initController() call below.
|
|
$this->helpers = ['form', 'url'];
|
|
|
|
// Caution: Do not edit this line.
|
|
parent::initController($request, $response, $logger);
|
|
|
|
// Preload any models, libraries, etc, here.
|
|
$this->session = service('session');
|
|
$workspaceList = [];
|
|
$activeWorkspaceId = (int) $this->session->get('active_workspace_id');
|
|
$userId = (int) $this->session->get('user_id');
|
|
if ($userId > 0) {
|
|
$workspaceList = (new WorkspaceModel())->forUser($userId);
|
|
if ($activeWorkspaceId <= 0 && $workspaceList !== []) {
|
|
$activeWorkspaceId = (int) $workspaceList[0]['id'];
|
|
$this->session->set('active_workspace_id', $activeWorkspaceId);
|
|
}
|
|
}
|
|
|
|
$alertBadgeCount = 0;
|
|
if ($userId > 0 && $activeWorkspaceId > 0) {
|
|
$alertBadgeCount = (new AlertHistoryModel())->countRecentTriggeredAlerts($activeWorkspaceId, 3600);
|
|
}
|
|
|
|
$themePref = (string) ($this->session->get('theme_preference') ?? 'light');
|
|
if (! in_array($themePref, ['light', 'dark', 'system'], true)) {
|
|
$themePref = 'light';
|
|
}
|
|
|
|
$canWorkspaceAdmin = false;
|
|
if ((string) $this->session->get('role') === 'superadmin') {
|
|
$canWorkspaceAdmin = true;
|
|
} elseif ($userId > 0 && $activeWorkspaceId > 0) {
|
|
$mem = (new WorkspaceMemberModel())
|
|
->where('workspace_id', $activeWorkspaceId)
|
|
->where('user_id', $userId)
|
|
->first();
|
|
$canWorkspaceAdmin = $mem !== null && (string) ($mem['role'] ?? '') === 'admin';
|
|
}
|
|
|
|
$this->sharedData = [
|
|
'currentUser' => [
|
|
'id' => $this->session->get('user_id'),
|
|
'name' => $this->session->get('name'),
|
|
'email' => $this->session->get('email'),
|
|
'role' => $this->session->get('role'),
|
|
],
|
|
'activeWorkspaceId' => $activeWorkspaceId,
|
|
'workspaceList' => $workspaceList,
|
|
'alertBadgeCount' => $alertBadgeCount,
|
|
'themePreference' => $themePref,
|
|
'canWorkspaceAdmin' => $canWorkspaceAdmin,
|
|
];
|
|
|
|
service('renderer')->setData($this->sharedData);
|
|
}
|
|
}
|