43 lines
1.1 KiB
PHP
43 lines
1.1 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace MintyPHP\Repository;
|
||
|
|
|
||
|
|
use MintyPHP\DB;
|
||
|
|
|
||
|
|
class UserRoleRepository
|
||
|
|
{
|
||
|
|
public static function listRoleIdsByUserId(int $userId): array
|
||
|
|
{
|
||
|
|
$rows = DB::select('select role_id from user_roles where user_id = ?', (string) $userId);
|
||
|
|
if (!is_array($rows)) {
|
||
|
|
return [];
|
||
|
|
}
|
||
|
|
$ids = [];
|
||
|
|
foreach ($rows as $row) {
|
||
|
|
$data = $row['user_roles'] ?? $row;
|
||
|
|
if (is_array($data) && isset($data['role_id'])) {
|
||
|
|
$ids[] = (int) $data['role_id'];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return array_values(array_unique($ids));
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function replaceForUser(int $userId, array $roleIds): bool
|
||
|
|
{
|
||
|
|
DB::delete('delete from user_roles where user_id = ?', (string) $userId);
|
||
|
|
if (!$roleIds) {
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
foreach ($roleIds as $roleId) {
|
||
|
|
DB::insert(
|
||
|
|
'insert into user_roles (user_id, role_id, created) values (?,?,NOW())',
|
||
|
|
(string) $userId,
|
||
|
|
(string) $roleId
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|