forked from fa/breadcrumb-the-shire
43 lines
1.1 KiB
PHP
43 lines
1.1 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace MintyPHP\Repository;
|
||
|
|
|
||
|
|
use MintyPHP\DB;
|
||
|
|
|
||
|
|
class UserTenantRepository
|
||
|
|
{
|
||
|
|
public static function listTenantIdsByUserId(int $userId): array
|
||
|
|
{
|
||
|
|
$rows = DB::select('select tenant_id from user_tenants where user_id = ?', (string) $userId);
|
||
|
|
if (!is_array($rows)) {
|
||
|
|
return [];
|
||
|
|
}
|
||
|
|
$ids = [];
|
||
|
|
foreach ($rows as $row) {
|
||
|
|
$data = $row['user_tenants'] ?? $row;
|
||
|
|
if (is_array($data) && isset($data['tenant_id'])) {
|
||
|
|
$ids[] = (int) $data['tenant_id'];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return array_values(array_unique($ids));
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function replaceForUser(int $userId, array $tenantIds): bool
|
||
|
|
{
|
||
|
|
DB::delete('delete from user_tenants where user_id = ?', (string) $userId);
|
||
|
|
if (!$tenantIds) {
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
foreach ($tenantIds as $tenantId) {
|
||
|
|
DB::insert(
|
||
|
|
'insert into user_tenants (user_id, tenant_id, created) values (?,?,NOW())',
|
||
|
|
(string) $userId,
|
||
|
|
(string) $tenantId
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|