203 lines
7.4 KiB
PHP
203 lines
7.4 KiB
PHP
<?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\Rule;
|
|
use Illuminate\Validation\ValidationException;
|
|
use IriksIT\Manager\Models\Group;
|
|
use IriksIT\Manager\Models\UserSetting;
|
|
use IriksIT\Manager\Support\AuditTrail;
|
|
use IriksIT\Manager\Support\Menu;
|
|
|
|
/**
|
|
* 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),
|
|
'view' => UserSetting::value($user, 'view'),
|
|
'tutorial' => UserSetting::value($user, 'tutorial'),
|
|
'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);
|
|
$this->settings($request, $user);
|
|
|
|
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);
|
|
app(AuditTrail::class)->about($user);
|
|
|
|
$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);
|
|
}
|
|
|
|
$this->settings($request, $user);
|
|
|
|
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);
|
|
app(AuditTrail::class)->about($user);
|
|
|
|
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]);
|
|
}
|
|
|
|
/**
|
|
* Which face of the manager the account looks at (`full`, `simple`, or empty for what its rights give) and whether
|
|
* it gets the tour at its next load. Only what the form sent is changed.
|
|
*/
|
|
private function settings(Request $request, $user): void
|
|
{
|
|
$values = [];
|
|
|
|
if ($request->exists('view')) {
|
|
$values['view'] = $request->input('view') ?: null;
|
|
}
|
|
|
|
if ($request->exists('tutorial')) {
|
|
$values['tutorial'] = $request->boolean('tutorial') ? 'pending' : (UserSetting::value($user, 'tutorial') === 'pending' ? 'skipped' : UserSetting::value($user, 'tutorial'));
|
|
}
|
|
|
|
if ($values !== []) {
|
|
UserSetting::put($user, $values);
|
|
}
|
|
}
|
|
|
|
/** @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'],
|
|
'view' => ['nullable', Rule::in([Menu::FULL, Menu::SIMPLE])],
|
|
'tutorial' => ['nullable', 'boolean'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
}
|