forked from fa/breadcrumb-the-shire
117 lines
3.3 KiB
PHP
117 lines
3.3 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace MintyPHP\Service;
|
||
|
|
|
||
|
|
use MintyPHP\Repository\RoleRepository;
|
||
|
|
use MintyPHP\Service\SettingService;
|
||
|
|
|
||
|
|
class RoleService
|
||
|
|
{
|
||
|
|
public static function list(): array
|
||
|
|
{
|
||
|
|
return RoleRepository::list();
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function listPaged(array $options): array
|
||
|
|
{
|
||
|
|
return RoleRepository::listPaged($options);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function findByUuid(string $uuid): ?array
|
||
|
|
{
|
||
|
|
return RoleRepository::findByUuid($uuid);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function findById(int $id): ?array
|
||
|
|
{
|
||
|
|
return RoleRepository::find($id);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function createFromAdmin(array $input, int $currentUserId = 0): array
|
||
|
|
{
|
||
|
|
$form = self::sanitizeBase($input);
|
||
|
|
$errors = self::validateBase($form);
|
||
|
|
|
||
|
|
if ($errors) {
|
||
|
|
return ['ok' => false, 'errors' => $errors, 'form' => $form];
|
||
|
|
}
|
||
|
|
|
||
|
|
$createdId = RoleRepository::create([
|
||
|
|
'description' => $form['description'],
|
||
|
|
'created_by' => $currentUserId > 0 ? $currentUserId : null,
|
||
|
|
]);
|
||
|
|
|
||
|
|
if (!$createdId) {
|
||
|
|
return ['ok' => false, 'errors' => [t('Role can not be created')], 'form' => $form];
|
||
|
|
}
|
||
|
|
|
||
|
|
$createdRole = RoleRepository::find((int) $createdId);
|
||
|
|
$uuid = $createdRole['uuid'] ?? null;
|
||
|
|
if (!empty($input['is_default']) && $createdId) {
|
||
|
|
SettingService::setDefaultRoleId((int) $createdId);
|
||
|
|
}
|
||
|
|
return ['ok' => true, 'form' => $form, 'uuid' => $uuid, 'id' => (int) $createdId];
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function updateFromAdmin(int $roleId, array $input, int $currentUserId = 0): array
|
||
|
|
{
|
||
|
|
$form = self::sanitizeBase($input);
|
||
|
|
$errors = self::validateBase($form);
|
||
|
|
|
||
|
|
if ($errors) {
|
||
|
|
return ['ok' => false, 'errors' => $errors, 'form' => $form];
|
||
|
|
}
|
||
|
|
|
||
|
|
$updated = RoleRepository::update($roleId, [
|
||
|
|
'description' => $form['description'],
|
||
|
|
'modified_by' => $currentUserId > 0 ? $currentUserId : null,
|
||
|
|
]);
|
||
|
|
|
||
|
|
if (!$updated) {
|
||
|
|
return ['ok' => false, 'errors' => [t('Role can not be updated')], 'form' => $form];
|
||
|
|
}
|
||
|
|
|
||
|
|
return ['ok' => true, 'form' => $form];
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function deleteByUuid(string $uuid): array
|
||
|
|
{
|
||
|
|
$uuid = trim($uuid);
|
||
|
|
if ($uuid === '') {
|
||
|
|
return ['ok' => false, 'status' => 404, 'error' => 'not_found'];
|
||
|
|
}
|
||
|
|
|
||
|
|
$role = RoleRepository::findByUuid($uuid);
|
||
|
|
if (!$role || !isset($role['id'])) {
|
||
|
|
return ['ok' => false, 'status' => 404, 'error' => 'not_found'];
|
||
|
|
}
|
||
|
|
|
||
|
|
if (($role['description'] ?? '') === 'Admin') {
|
||
|
|
return ['ok' => false, 'status' => 403, 'error' => 'admin_role_protected'];
|
||
|
|
}
|
||
|
|
|
||
|
|
$deleted = RoleRepository::delete((int) $role['id']);
|
||
|
|
if (!$deleted) {
|
||
|
|
return ['ok' => false, 'status' => 500, 'error' => 'delete_failed'];
|
||
|
|
}
|
||
|
|
|
||
|
|
return ['ok' => true, 'role' => $role];
|
||
|
|
}
|
||
|
|
|
||
|
|
private static function sanitizeBase(array $input): array
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
'description' => trim((string) ($input['description'] ?? '')),
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
private static function validateBase(array $form): array
|
||
|
|
{
|
||
|
|
$errors = [];
|
||
|
|
if ($form['description'] === '') {
|
||
|
|
$errors[] = t('Description cannot be empty');
|
||
|
|
}
|
||
|
|
return $errors;
|
||
|
|
}
|
||
|
|
}
|