v0.9.4
This commit is contained in:
commit
217b733bf2
286 files changed
+19180
No files matched your search
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Modules\Users;
|
||||
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\Rule;
|
||||
use IriksIT\Manager\Models\Group;
|
||||
use IriksIT\Manager\Support\AuditTrail;
|
||||
use IriksIT\Manager\Support\ModuleRegistry;
|
||||
use IriksIT\Manager\Support\Names;
|
||||
|
||||
/** The groups and what each allows. Only for a super admin (the `groups.manage` gate). */
|
||||
class GroupController
|
||||
{
|
||||
public function __construct(private readonly ModuleRegistry $modules) {}
|
||||
|
||||
public function index(): JsonResponse
|
||||
{
|
||||
return response()->json([
|
||||
'groups' => Group::query()->withCount('users')->orderBy('name')->get()
|
||||
->map(fn (Group $group) => [
|
||||
'id' => $group->id,
|
||||
'name' => $group->name,
|
||||
'description' => $group->description,
|
||||
'start' => $group->start,
|
||||
'require_two_factor' => (bool) $group->require_two_factor,
|
||||
'permissions' => $group->allowed(),
|
||||
'accounts' => $group->users_count,
|
||||
])->values(),
|
||||
'catalogue' => $this->modules->catalogue(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
$group = Group::create($this->validated($request));
|
||||
|
||||
return response()->json(['id' => $group->id], 201);
|
||||
}
|
||||
|
||||
public function update(Request $request, string $group): JsonResponse
|
||||
{
|
||||
$group = Group::query()->findOrFail($group);
|
||||
app(AuditTrail::class)->about($group);
|
||||
$group->update($this->validated($request, $group));
|
||||
|
||||
return response()->json(['saved' => true]);
|
||||
}
|
||||
|
||||
/** The accounts in it stay; they lose what only this group allowed. */
|
||||
public function destroy(string $group): JsonResponse
|
||||
{
|
||||
$group = Group::query()->findOrFail($group);
|
||||
app(AuditTrail::class)->about($group);
|
||||
$group->users()->detach();
|
||||
$group->delete();
|
||||
|
||||
return response()->json(['deleted' => true]);
|
||||
}
|
||||
|
||||
/** @return array{name: string, description: string|null, start: string|null, permissions: list<string>, require_two_factor: bool} */
|
||||
private function validated(Request $request, ?Group $group = null): array
|
||||
{
|
||||
$table = Names::table('groups');
|
||||
|
||||
$request->validate([
|
||||
'name' => ['required', 'string', 'max:60', Rule::unique($table, 'name')->ignore($group?->id)],
|
||||
'description' => ['nullable', 'string', 'max:500'],
|
||||
'start' => ['nullable', 'string', 'max:200', 'regex:#^/[A-Za-z0-9/_.-]*$#'],
|
||||
'require_two_factor' => ['nullable', 'boolean'],
|
||||
'permissions' => ['array'],
|
||||
'permissions.*' => ['string', Rule::in($this->modules->permissionValues())],
|
||||
]);
|
||||
|
||||
$description = $request->string('description')->trim()->toString();
|
||||
|
||||
return [
|
||||
'name' => $request->string('name')->trim()->toString(),
|
||||
'description' => $description !== '' ? $description : null,
|
||||
'start' => $request->filled('start') ? $request->string('start')->toString() : null,
|
||||
'permissions' => $this->modules->complete($request->array('permissions')),
|
||||
// Left out of the request, it stays as it was: an older screen must not switch it off by not knowing it.
|
||||
'require_two_factor' => $request->has('require_two_factor') ? $request->boolean('require_two_factor') : (bool) $group?->require_two_factor,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user