2026-02-04 23:31:53 +01:00
|
|
|
<?php
|
|
|
|
|
|
2026-02-11 19:28:12 +01:00
|
|
|
namespace MintyPHP\Repository\Access;
|
2026-02-04 23:31:53 +01:00
|
|
|
|
|
|
|
|
use MintyPHP\DB;
|
|
|
|
|
|
2026-03-05 08:26:51 +01:00
|
|
|
class UserRoleRepository implements UserRoleRepositoryInterface
|
2026-02-04 23:31:53 +01:00
|
|
|
{
|
2026-02-23 12:58:19 +01:00
|
|
|
public function listRoleIdsByUserId(int $userId): array
|
2026-02-04 23:31:53 +01:00
|
|
|
{
|
|
|
|
|
$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));
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-23 12:58:19 +01:00
|
|
|
public function replaceForUser(int $userId, array $roleIds): bool
|
2026-02-04 23:31:53 +01:00
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
add composer-unused, comprehensive docs, and project restructure
- Add icanhazstring/composer-unused as dev dependency for dependency hygiene checks
- Add German documentation (docs/) covering architecture, conventions, workflows, and developer checklists
- Add API layer (ApiAuth, ApiBootstrap, ApiResponse), audit, scheduler, custom fields, and SSO services
- Add Microsoft OIDC SSO, API token management, and user lifecycle features
- Add swagger-ui vendor integration and OpenAPI spec
- Add production Docker setup and bin/ scripts
- Update composer dependencies, config, templates, and frontend assets throughout
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-22 15:27:35 +01:00
|
|
|
|
2026-03-04 15:56:58 +01:00
|
|
|
public function listUserIdsByRoleIds(array $roleIds): array
|
|
|
|
|
{
|
|
|
|
|
$ids = array_values(array_unique(array_map('intval', $roleIds)));
|
|
|
|
|
$ids = array_values(array_filter($ids, static fn ($id) => $id > 0));
|
|
|
|
|
if (!$ids) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$rows = DB::select(
|
|
|
|
|
'select distinct user_id from user_roles where role_id in (???)',
|
|
|
|
|
array_map('strval', $ids)
|
|
|
|
|
);
|
|
|
|
|
if (!is_array($rows)) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$userIds = [];
|
|
|
|
|
foreach ($rows as $row) {
|
|
|
|
|
$data = $row['user_roles'] ?? $row;
|
|
|
|
|
if (!is_array($data) || !isset($data['user_id'])) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$userIds[] = (int) $data['user_id'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return array_values(array_unique(array_filter($userIds, static fn ($id) => $id > 0)));
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-23 12:58:19 +01:00
|
|
|
public function countUsersByRoleIds(array $roleIds): array
|
add composer-unused, comprehensive docs, and project restructure
- Add icanhazstring/composer-unused as dev dependency for dependency hygiene checks
- Add German documentation (docs/) covering architecture, conventions, workflows, and developer checklists
- Add API layer (ApiAuth, ApiBootstrap, ApiResponse), audit, scheduler, custom fields, and SSO services
- Add Microsoft OIDC SSO, API token management, and user lifecycle features
- Add swagger-ui vendor integration and OpenAPI spec
- Add production Docker setup and bin/ scripts
- Update composer dependencies, config, templates, and frontend assets throughout
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-22 15:27:35 +01:00
|
|
|
{
|
2026-02-23 12:58:19 +01:00
|
|
|
return $this->countByRoleIds($roleIds, false);
|
add composer-unused, comprehensive docs, and project restructure
- Add icanhazstring/composer-unused as dev dependency for dependency hygiene checks
- Add German documentation (docs/) covering architecture, conventions, workflows, and developer checklists
- Add API layer (ApiAuth, ApiBootstrap, ApiResponse), audit, scheduler, custom fields, and SSO services
- Add Microsoft OIDC SSO, API token management, and user lifecycle features
- Add swagger-ui vendor integration and OpenAPI spec
- Add production Docker setup and bin/ scripts
- Update composer dependencies, config, templates, and frontend assets throughout
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-22 15:27:35 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-23 12:58:19 +01:00
|
|
|
public function countActiveUsersByRoleIds(array $roleIds): array
|
add composer-unused, comprehensive docs, and project restructure
- Add icanhazstring/composer-unused as dev dependency for dependency hygiene checks
- Add German documentation (docs/) covering architecture, conventions, workflows, and developer checklists
- Add API layer (ApiAuth, ApiBootstrap, ApiResponse), audit, scheduler, custom fields, and SSO services
- Add Microsoft OIDC SSO, API token management, and user lifecycle features
- Add swagger-ui vendor integration and OpenAPI spec
- Add production Docker setup and bin/ scripts
- Update composer dependencies, config, templates, and frontend assets throughout
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-22 15:27:35 +01:00
|
|
|
{
|
2026-02-23 12:58:19 +01:00
|
|
|
return $this->countByRoleIds($roleIds, true);
|
add composer-unused, comprehensive docs, and project restructure
- Add icanhazstring/composer-unused as dev dependency for dependency hygiene checks
- Add German documentation (docs/) covering architecture, conventions, workflows, and developer checklists
- Add API layer (ApiAuth, ApiBootstrap, ApiResponse), audit, scheduler, custom fields, and SSO services
- Add Microsoft OIDC SSO, API token management, and user lifecycle features
- Add swagger-ui vendor integration and OpenAPI spec
- Add production Docker setup and bin/ scripts
- Update composer dependencies, config, templates, and frontend assets throughout
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-22 15:27:35 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-23 12:58:19 +01:00
|
|
|
private function countByRoleIds(array $roleIds, bool $activeOnly): array
|
add composer-unused, comprehensive docs, and project restructure
- Add icanhazstring/composer-unused as dev dependency for dependency hygiene checks
- Add German documentation (docs/) covering architecture, conventions, workflows, and developer checklists
- Add API layer (ApiAuth, ApiBootstrap, ApiResponse), audit, scheduler, custom fields, and SSO services
- Add Microsoft OIDC SSO, API token management, and user lifecycle features
- Add swagger-ui vendor integration and OpenAPI spec
- Add production Docker setup and bin/ scripts
- Update composer dependencies, config, templates, and frontend assets throughout
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-22 15:27:35 +01:00
|
|
|
{
|
|
|
|
|
$roleIds = array_values(array_unique(array_map('intval', $roleIds)));
|
|
|
|
|
$roleIds = array_values(array_filter($roleIds, static fn ($id) => $id > 0));
|
|
|
|
|
if (!$roleIds) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$placeholders = implode(',', array_fill(0, count($roleIds), '?'));
|
|
|
|
|
$joinUsersSql = $activeOnly ? ' join users u on u.id = ur.user_id and u.active = 1' : '';
|
|
|
|
|
$rows = DB::select(
|
|
|
|
|
'select ur.role_id, count(distinct ur.user_id) as user_count from user_roles ur' .
|
|
|
|
|
$joinUsersSql .
|
|
|
|
|
' where ur.role_id in (' . $placeholders . ') group by ur.role_id',
|
|
|
|
|
...array_map('strval', $roleIds)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!is_array($rows)) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$result = [];
|
|
|
|
|
foreach ($rows as $row) {
|
2026-02-23 12:58:19 +01:00
|
|
|
$roleId = $this->extractIntField($row, 'role_id');
|
add composer-unused, comprehensive docs, and project restructure
- Add icanhazstring/composer-unused as dev dependency for dependency hygiene checks
- Add German documentation (docs/) covering architecture, conventions, workflows, and developer checklists
- Add API layer (ApiAuth, ApiBootstrap, ApiResponse), audit, scheduler, custom fields, and SSO services
- Add Microsoft OIDC SSO, API token management, and user lifecycle features
- Add swagger-ui vendor integration and OpenAPI spec
- Add production Docker setup and bin/ scripts
- Update composer dependencies, config, templates, and frontend assets throughout
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-22 15:27:35 +01:00
|
|
|
if ($roleId <= 0) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2026-02-23 12:58:19 +01:00
|
|
|
$result[$roleId] = $this->extractIntField($row, 'user_count');
|
add composer-unused, comprehensive docs, and project restructure
- Add icanhazstring/composer-unused as dev dependency for dependency hygiene checks
- Add German documentation (docs/) covering architecture, conventions, workflows, and developer checklists
- Add API layer (ApiAuth, ApiBootstrap, ApiResponse), audit, scheduler, custom fields, and SSO services
- Add Microsoft OIDC SSO, API token management, and user lifecycle features
- Add swagger-ui vendor integration and OpenAPI spec
- Add production Docker setup and bin/ scripts
- Update composer dependencies, config, templates, and frontend assets throughout
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-22 15:27:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-23 12:58:19 +01:00
|
|
|
private function extractIntField(array $row, string $field): int
|
add composer-unused, comprehensive docs, and project restructure
- Add icanhazstring/composer-unused as dev dependency for dependency hygiene checks
- Add German documentation (docs/) covering architecture, conventions, workflows, and developer checklists
- Add API layer (ApiAuth, ApiBootstrap, ApiResponse), audit, scheduler, custom fields, and SSO services
- Add Microsoft OIDC SSO, API token management, and user lifecycle features
- Add swagger-ui vendor integration and OpenAPI spec
- Add production Docker setup and bin/ scripts
- Update composer dependencies, config, templates, and frontend assets throughout
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-22 15:27:35 +01:00
|
|
|
{
|
|
|
|
|
foreach ($row as $value) {
|
|
|
|
|
if (!is_array($value)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (array_key_exists($field, $value)) {
|
|
|
|
|
return (int) $value[$field];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (array_key_exists($field, $row)) {
|
|
|
|
|
return (int) $row[$field];
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2026-02-04 23:31:53 +01:00
|
|
|
}
|