1
0
Files
breadcrumb-the-shire/core/Repository/Access/PermissionRepository.php

128 lines
4.6 KiB
PHP
Raw Normal View History

2026-02-11 19:28:12 +01:00
<?php
namespace MintyPHP\Repository\Access;
use MintyPHP\DB;
use MintyPHP\Repository\Support\RepoQuery;
use MintyPHP\Repository\Support\RepositoryArrayHelper;
2026-02-11 19:28:12 +01:00
/** Reads and writes permission records; supports active/system flag filtering. */
2026-03-05 08:26:51 +01:00
class PermissionRepository implements PermissionRepositoryInterface
2026-02-11 19:28:12 +01:00
{
2026-02-23 12:58:19 +01:00
public function list(): array
2026-02-11 19:28:12 +01:00
{
$rows = DB::select('select id, `key`, description, active, is_system, created from permissions order by `key` asc');
return RepositoryArrayHelper::unwrapList($rows, 'permissions');
2026-02-11 19:28:12 +01:00
}
2026-02-23 12:58:19 +01:00
public function listActive(): array
2026-02-11 19:28:12 +01:00
{
$rows = DB::select(
'select id, `key`, description, active, is_system, created from permissions where active = 1 order by `key` asc'
);
return RepositoryArrayHelper::unwrapList($rows, 'permissions');
2026-02-11 19:28:12 +01:00
}
2026-02-23 12:58:19 +01:00
public function listPaged(array $options): array
2026-02-11 19:28:12 +01:00
{
$search = trim((string) ($options['search'] ?? ''));
$allowedOrder = [
'key' => '`key`',
'description' => 'description',
'active' => 'active',
'created' => 'created',
];
[$limit, $offset] = RepoQuery::sanitizeLimitOffset($options);
[$order, $dir] = RepoQuery::sanitizeOrder($options, array_keys($allowedOrder), 'key', 'asc');
$orderBy = $allowedOrder[$order] ?? $allowedOrder['key'];
$where = [];
$params = [];
RepoQuery::addLikeFilter($where, $params, ['permissions.`key`', 'permissions.description'], $search);
$activeValue = array_key_exists('active', $options) ? $options['active'] : null;
RepoQuery::addEnumFilter($where, $params, $activeValue, [
['aliases' => ['1', 'true', 'active'], 'sql' => 'permissions.active = 1'],
['aliases' => ['0', 'false', 'inactive'], 'sql' => 'permissions.active = 0'],
]);
$whereSql = $where ? ' where ' . implode(' and ', $where) : '';
$total = DB::selectValue('select count(*) from permissions' . $whereSql, ...$params);
$query = 'select id, `key`, description, active, is_system, created from permissions' . $whereSql .
' order by ' . $orderBy . ' ' . $dir . ' limit ? offset ?';
$queryParams = array_merge($params, [(string) $limit, (string) $offset]);
$rows = DB::select($query, ...$queryParams);
return ['rows' => RepositoryArrayHelper::unwrapList($rows, 'permissions'), 'total' => (int) $total];
2026-02-11 19:28:12 +01:00
}
2026-02-23 12:58:19 +01:00
public function find(int $id): ?array
2026-02-11 19:28:12 +01:00
{
$row = DB::selectOne(
'select id, `key`, description, active, is_system, created from permissions where id = ? limit 1',
(string) $id
);
return RepositoryArrayHelper::unwrap($row, 'permissions');
2026-02-11 19:28:12 +01:00
}
2026-02-23 12:58:19 +01:00
public function findByKey(string $key): ?array
2026-02-11 19:28:12 +01:00
{
$row = DB::selectOne(
'select id, `key`, description, active, is_system, created from permissions where `key` = ? limit 1',
$key
);
return RepositoryArrayHelper::unwrap($row, 'permissions');
2026-02-11 19:28:12 +01:00
}
2026-02-23 12:58:19 +01:00
public function create(array $data): ?int
2026-02-11 19:28:12 +01:00
{
$key = trim((string) ($data['key'] ?? ''));
$desc = trim((string) ($data['description'] ?? ''));
$active = (int) ($data['active'] ?? 1);
$isSystem = (int) ($data['is_system'] ?? 0);
$result = DB::insert(
'insert into permissions (`key`, description, active, is_system, created) values (?,?,?,?,NOW())',
$key,
$desc !== '' ? $desc : null,
(string) $active,
(string) $isSystem
);
return $result ? (int) $result : null;
}
2026-02-23 12:58:19 +01:00
public function update(int $id, array $data): bool
2026-02-11 19:28:12 +01:00
{
$key = trim((string) ($data['key'] ?? ''));
$desc = trim((string) ($data['description'] ?? ''));
$active = (int) ($data['active'] ?? 1);
$isSystem = (int) ($data['is_system'] ?? 0);
$result = DB::update(
'update permissions set `key` = ?, description = ?, active = ?, is_system = ? where id = ?',
$key,
$desc !== '' ? $desc : null,
(string) $active,
(string) $isSystem,
(string) $id
);
return $result !== false;
}
2026-02-23 12:58:19 +01:00
public function delete(int $id): bool
2026-02-11 19:28:12 +01:00
{
$result = DB::delete('delete from permissions where id = ?', (string) $id);
return (bool) $result;
}
2026-03-19 18:41:08 +01:00
public function listSystem(): array
{
$rows = DB::select(
'select id, `key`, description, active, is_system, created from permissions where is_system = 1 order by `key` asc'
);
return RepositoryArrayHelper::unwrapList($rows, 'permissions');
}
2026-02-11 19:28:12 +01:00
}