GeoNetAgent, LDAPWeb, server-audit, server-connection
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

165 lines
4.6 KiB

<?php
namespace App\Services;
use App\Models\LdapGroupMeta;
use App\Models\LdapUserMeta;
class LdapMetadataService
{
public function getUserMeta(string $username): array
{
$row = LdapUserMeta::query()->where('username', $username)->first();
return [
'other_email' => $row?->other_email ?? '',
'phone' => $row?->phone ?? '',
'admin_note' => $row?->admin_note ?? '',
'avatar' => $row?->avatar ?? null,
];
}
public function saveUserMeta(string $username, array $data): void
{
LdapUserMeta::query()->updateOrCreate(
['username' => $username],
[
'other_email' => $data['other_email'] ?? null,
'phone' => $data['phone'] ?? null,
'admin_note' => $data['admin_note'] ?? null,
]
);
}
public function saveUserSelfMeta(string $username, ?string $otherEmail, ?string $phone): void
{
$existing = LdapUserMeta::query()->where('username', $username)->first();
LdapUserMeta::query()->updateOrCreate(
['username' => $username],
[
'other_email' => $otherEmail,
'phone' => $phone,
'admin_note' => $existing?->admin_note,
]
);
}
public function saveAvatar(string $username, ?string $avatarBase64): void
{
LdapUserMeta::query()->updateOrCreate(
['username' => $username],
['avatar' => $avatarBase64],
);
}
public function getGroupMeta(string $groupKey): array
{
$row = LdapGroupMeta::query()->where('group_key', $groupKey)->first();
return [
'admin_note' => $row?->admin_note ?? '',
];
}
public function saveGroupMeta(string $groupKey, string $groupName, ?string $adminNote): void
{
LdapGroupMeta::query()->updateOrCreate(
['group_key' => $groupKey],
[
'group_name' => $groupName,
'admin_note' => $adminNote,
]
);
}
/**
* @param list<array<string, mixed>> $users
* @return list<array<string, mixed>>
*/
public function enrichUsers(array $users): array
{
if ($users === []) {
return [];
}
$usernames = array_column($users, 'username');
$metaByUser = LdapUserMeta::query()
->whereIn('username', $usernames)
->get()
->keyBy('username');
foreach ($users as &$user) {
$meta = $metaByUser->get($user['username']);
$user['other_email'] = $meta?->other_email ?? '';
$user['phone'] = $meta?->phone ?? '';
$user['admin_note'] = $meta?->admin_note ?? '';
}
return $users;
}
/**
* @param list<array<string, mixed>> $groups
* @return list<array<string, mixed>>
*/
public function enrichGroups(array $groups): array
{
if ($groups === []) {
return [];
}
$keys = array_column($groups, 'route_key');
$metaByKey = LdapGroupMeta::query()
->whereIn('group_key', $keys)
->get()
->keyBy('group_key');
foreach ($groups as &$group) {
$meta = $metaByKey->get($group['route_key']);
$group['admin_note'] = $meta?->admin_note ?? '';
}
return $groups;
}
/**
* @param list<array<string, mixed>> $users
* @return list<array<string, mixed>>
*/
/**
* @return list<string>
*/
public function findUsernamesByMetaSearch(string $search): array
{
$needle = '%'.addcslashes(trim($search), '%_\\').'%';
return LdapUserMeta::query()
->where(function ($query) use ($needle) {
$query->where('other_email', 'like', $needle)
->orWhere('phone', 'like', $needle)
->orWhere('admin_note', 'like', $needle);
})
->pluck('username')
->all();
}
public function filterUsersByExtendedSearch(array $users, string $search): array
{
$needle = mb_strtolower(trim($search));
if ($needle === '') {
return $users;
}
return array_values(array_filter($users, function (array $user) use ($needle) {
foreach (['other_email', 'phone', 'admin_note'] as $field) {
if (str_contains(mb_strtolower((string) ($user[$field] ?? '')), $needle)) {
return true;
}
}
return false;
}));
}
}