init
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller\Api;
|
||||
|
||||
use Cake\Event\EventInterface;
|
||||
use Cake\Http\Exception\BadRequestException;
|
||||
use Cake\Http\Response;
|
||||
|
||||
/**
|
||||
* Users CRUD - available to admins only.
|
||||
*
|
||||
* @property \App\Model\Table\UsersTable $Users
|
||||
*/
|
||||
class UsersController extends ApiController
|
||||
{
|
||||
/**
|
||||
* @param \Cake\Event\EventInterface<\Cake\Controller\Controller> $event Event
|
||||
* @return void
|
||||
*/
|
||||
public function beforeFilter(EventInterface $event): void
|
||||
{
|
||||
parent::beforeFilter($event);
|
||||
$this->requireAdmin();
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/users
|
||||
*
|
||||
* Query: page, limit, sort, direction, q (email/name), role, active
|
||||
*
|
||||
* @return \Cake\Http\Response
|
||||
*/
|
||||
public function index(): Response
|
||||
{
|
||||
$query = $this->Users->find();
|
||||
|
||||
$q = trim((string)$this->request->getQuery('q', ''));
|
||||
if ($q !== '') {
|
||||
$like = '%' . $q . '%';
|
||||
$query->where(['OR' => [
|
||||
'Users.email LIKE' => $like,
|
||||
'Users.first_name LIKE' => $like,
|
||||
'Users.last_name LIKE' => $like,
|
||||
]]);
|
||||
}
|
||||
$role = $this->request->getQuery('role');
|
||||
if ($role) {
|
||||
$query->where(['Users.role' => $role]);
|
||||
}
|
||||
$active = $this->request->getQuery('active');
|
||||
if ($active !== null && $active !== '') {
|
||||
$query->where(['Users.active' => filter_var($active, FILTER_VALIDATE_BOOLEAN)]);
|
||||
}
|
||||
|
||||
$result = $this->paginate($query, $this->paginationSettings(
|
||||
['id', 'email', 'first_name', 'last_name', 'role', 'active', 'last_login', 'created', 'modified'],
|
||||
['Users.id' => 'asc'],
|
||||
));
|
||||
|
||||
return $this->respondPaginated($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/users/{id}
|
||||
*
|
||||
* @param string $id User id
|
||||
* @return \Cake\Http\Response
|
||||
*/
|
||||
public function view(string $id): Response
|
||||
{
|
||||
return $this->respond($this->Users->get($id));
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /api/users
|
||||
*
|
||||
* @return \Cake\Http\Response
|
||||
*/
|
||||
public function add(): Response
|
||||
{
|
||||
$this->request->allowMethod(['post']);
|
||||
$user = $this->Users->newEntity((array)$this->request->getData());
|
||||
if (!$this->Users->save($user)) {
|
||||
return $this->respondValidationError($user);
|
||||
}
|
||||
|
||||
return $this->respond($user, 201)
|
||||
->withLocation('/api/users/' . $user->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* PUT|PATCH /api/users/{id}
|
||||
*
|
||||
* @param string $id User id
|
||||
* @return \Cake\Http\Response
|
||||
*/
|
||||
public function edit(string $id): Response
|
||||
{
|
||||
$this->request->allowMethod(['put', 'patch']);
|
||||
$user = $this->Users->get($id);
|
||||
$data = (array)$this->request->getData();
|
||||
|
||||
if ($user->id === $this->currentUser()->id) {
|
||||
if (isset($data['role']) && $data['role'] !== $user->role) {
|
||||
throw new BadRequestException('You cannot change your own role.');
|
||||
}
|
||||
if (isset($data['active']) && !filter_var($data['active'], FILTER_VALIDATE_BOOLEAN)) {
|
||||
throw new BadRequestException('You cannot deactivate your own account.');
|
||||
}
|
||||
}
|
||||
|
||||
$user = $this->Users->patchEntity($user, $data);
|
||||
if (!$this->Users->save($user)) {
|
||||
return $this->respondValidationError($user);
|
||||
}
|
||||
|
||||
return $this->respond($user);
|
||||
}
|
||||
|
||||
/**
|
||||
* DELETE /api/users/{id}
|
||||
*
|
||||
* @param string $id User id
|
||||
* @return \Cake\Http\Response
|
||||
*/
|
||||
public function delete(string $id): Response
|
||||
{
|
||||
$this->request->allowMethod(['delete']);
|
||||
$user = $this->Users->get($id);
|
||||
if ($user->id === $this->currentUser()->id) {
|
||||
throw new BadRequestException('You cannot delete your own account.');
|
||||
}
|
||||
$this->Users->deleteOrFail($user);
|
||||
|
||||
return $this->respond(null, 204);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user