Users module: accounts, groups, permissions with needs, manager:create-user

This commit is contained in:
Matthijs Iriks committed 2026-12-04 01:25:29 +01:00
1 parent 08289db744
commit f86f5aea28
25 files changed
+744 -58

No files matched your search

+168
View File
@@ -0,0 +1,168 @@
<?php
namespace IriksIT\Manager\Modules\Users;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\ValidationException;
use IriksIT\Manager\Models\Group;
/**
* The accounts of the manager and the groups each is in.
*
* Managing accounts must not be a way to more rights than one has. So, for anyone who is no
* super admin: an account is only changed or deleted when it may do nothing more than oneself
* (`managerOutranks`), and it is only put in a group that gives nothing more than one has
* oneself (`managerMayGive`). Nobody becomes a super admin here: that is `manager:create-user`.
*
* The answers have the shape the screens of the sites used as props.
*/
class AccountController
{
public function index(Request $request): JsonResponse
{
$me = $request->user();
$model = $this->model();
return response()->json([
'accounts' => $model::query()->with('managerGroups')->orderBy('name')->orderBy('id')->get()
->map(fn ($user) => [
'id' => $user->getKey(),
'name' => $user->name,
'email' => $user->email,
'is_super_admin' => $user->isManagerSuperAdmin(),
'is_you' => $user->is($me),
'groups' => $user->managerGroups->sortBy('name')->map(fn (Group $group) => ['id' => $group->id, 'name' => $group->name])->values(),
'can_change' => $me->managerOutranks($user),
'can_delete' => $me->managerOutranks($user) && ! $user->is($me),
])->values(),
'groups' => Group::query()->orderBy('name')->get()
->map(fn (Group $group) => [
'id' => $group->id,
'name' => $group->name,
'description' => $group->description,
'can_give' => $me->managerMayGive($group),
])->values(),
]);
}
public function store(Request $request): JsonResponse
{
$request->validate([...$this->rules(), 'password' => ['required', 'string', 'min:12', 'max:200']]);
// Everything is checked before anything is stored: a refused group leaves no account behind.
$details = $this->details($request, null);
$groups = $this->groups($request);
$model = $this->model();
$user = new $model;
$user->forceFill([...$details, 'password' => Hash::make($request->string('password')->toString())])->save();
$user->managerGroups()->sync($groups);
return response()->json(['id' => $user->getKey()], 201);
}
public function update(Request $request, string $account): JsonResponse
{
$me = $request->user();
$user = $this->find($account);
abort_unless($me->managerOutranks($user), 403);
$request->validate([...$this->rules(), 'password' => ['nullable', 'string', 'min:12', 'max:200']]);
$details = $this->details($request, $user);
// Groups mean nothing for a super admin, and nobody changes their own.
$groups = $user->isManagerSuperAdmin() || $user->is($me) ? null : $this->groups($request);
$user->forceFill($details);
// Empty: the password stays as it is.
if ($request->filled('password')) {
$user->forceFill(['password' => Hash::make($request->string('password')->toString())]);
}
$user->save();
if ($groups !== null) {
$user->managerGroups()->sync($groups);
}
return response()->json(['saved' => true]);
}
public function destroy(Request $request, string $account): JsonResponse
{
$me = $request->user();
$user = $this->find($account);
abort_unless($me->managerOutranks($user), 403);
if ($user->is($me)) {
return response()->json(['message' => 'You cannot delete your own account.'], 422);
}
$user->managerGroups()->detach();
$user->delete();
return response()->json(['deleted' => true]);
}
/** @return class-string<Authenticatable&Model> */
private function model(): string
{
return config('manager.user_model');
}
private function find(string $id)
{
return $this->model()::query()->findOrFail($id);
}
/** @return array<string, list<mixed>> */
private function rules(): array
{
return [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255'],
'groups' => ['array'],
'groups.*' => ['integer'],
];
}
/**
* Name and email address, the address in lower case and not in use by another account.
*
* @return array{name: string, email: string}
*/
private function details(Request $request, $user): array
{
$email = $request->string('email')->trim()->lower()->toString();
if ($this->model()::query()->where('email', $email)->when($user !== null, fn ($query) => $query->whereKeyNot($user->getKey()))->exists()) {
throw ValidationException::withMessages(['email' => 'Another account already uses this email address.']);
}
return ['name' => $request->string('name')->trim()->toString(), 'email' => $email];
}
/**
* The ids of the groups that were ticked. One that does not exist, or that gives more than
* the account that is logged in has, is refused.
*
* @return list<int>
*/
private function groups(Request $request): array
{
$me = $request->user();
$ids = array_values(array_unique(array_map(fn (mixed $id) => is_numeric($id) ? (int) $id : 0, $request->array('groups'))));
$groups = Group::query()->whereKey($ids)->get();
if ($groups->count() !== count($ids) || $groups->contains(fn (Group $group) => ! $me->managerMayGive($group))) {
throw ValidationException::withMessages(['groups' => 'You can only choose groups that allow nothing more than your own account.']);
}
return $ids;
}
}