'limit' => 20,
'maxLimit' => 100,
'fields' => [
- 'name', 'mail', 'firstname', 'lastname'
+ 'id', 'name', 'mail', 'firstname', 'lastname'
],
'sortWhitelist' => [
- 'name', 'mail', 'firstname', 'lastname'
+ 'id', 'name', 'mail', 'firstname', 'lastname'
]
];
/**
* View method
*
- * @param string $name User name.
+ * @param string $id User id.
* @return \Cake\Network\Response|null
* @throws UserNotFoundException When record not found.
*/
- public function view($name) {
+ public function view($id) {
$this->request->allowMethod('get');
-
- $user = $this->Users->findByName($name)->first();
-
- if (!$user) {
- throw new UserNotFoundException(__("The user $name does not exist"));
+
+ try {
+ $user = $this->Users->get($id);
+ }
+ catch (\Exception $e) {
+ throw new UserNotFoundException(__("The user with the id $id does not exist"));
}
$this->set('user', $user);
/**
* Edit method
*
- * @param string $name User name.
+ * @param string $id User id.
* @return \Cake\Network\Response|void Redirects on successful edit, renders view otherwise.
* @throws UserNotFoundException When record not found.
*/
- public function edit($name) {
+ public function edit($id) {
$this->request->allowMethod('put');
- $user = $this->Users->findByName($name)->first();
- if (!$user) {
- throw new UserNotFoundException(__("The user $name does not exist"));
+
+ try {
+ $oldUser = $this->Users->get($id);
+ }
+ catch (\Exception $e) {
+ throw new UserNotFoundException(__("The user with the id $id does not exist"));
}
if (isset($this->request->data['name'])) {
unset($this->request->data['name']);
}
- $user = $this->Users->patchEntity($user, $this->request->data);
+ $newUser = $this->Users->patchEntity($oldUser, $this->request->data);
- if ($this->Users->save($user)) {
+ if ($this->Users->save($newUser)) {
$this->response->statusCode(200);
- $this->set('user', $user);
+ $this->set('user', $newUser);
$this->set('_serialize', 'user');
}
else {
$this->response->statusCode(400);
- throw new ValidationException($user);
+ throw new ValidationException($newUser);
}
}
/**
* Delete method
*
- * @param string $name User name.
+ * @param string $id User id.
* @return \Cake\Network\Response|null Redirects to index.
* @throws UserNotFoundException When record not found.
*/
- public function delete($name) {
+ public function delete($id) {
$this->request->allowMethod('delete');
- $user = $this->Users->findByName($name)->first();
-
- if (!$user) {
- throw new UserNotFoundException(__("The user $name does not exist"));
+
+ try {
+ $user = $this->Users->get($id);
+ }
+ catch (\Exception $e) {
+ throw new UserNotFoundException(__("The user with the id $id does not exist"));
}
$this->Users->delete($user);