/**
  * Removes the given user.
  *
  * @param int $id User's ID
  * @return void Redirects to previous page
  */
 public function delete($id)
 {
     $this->loadModel('User.Users');
     $user = $this->Users->get($id, ['contain' => ['Roles']]);
     if (in_array(ROLE_ID_ADMINISTRATOR, $user->role_ids) && $this->Users->countAdministrators() === 1) {
         $this->Flash->danger(__d('user', 'You cannot remove this user as it is the last administrator available.'));
     } else {
         if ($this->Users->delete($user)) {
             NotificationManager::canceled($user)->send();
             $this->Flash->success(__d('user', 'User successfully removed!'));
             $this->redirect($this->referer());
         } else {
             $this->Flash->danger(__d('user', 'User could not be removed.'));
         }
     }
     $this->title(__d('user', 'Remove User Account'));
     $this->redirect($this->referer());
 }
 /**
  * test __callStatic() for canceled().
  *
  * @return void
  */
 public function testCallStaticCanceled()
 {
     $user = new User();
     $canceledMessage = NotificationManager::canceled($user);
     $this->assertInstanceOf('User\\Notification\\Message\\CanceledMessage', $canceledMessage);
 }
 /**
  * Here is where user's account is actually removed.
  *
  * @param int $userId The ID of the user whose account is being canceled
  * @param string $code Cancellation code, code is a MD5 hash of user's encrypted
  *  password + site's salt
  * @return void Redirects to previous page
  */
 public function cancel($userId, $code)
 {
     $this->loadModel('User.Users');
     $user = $this->Users->find()->where(['id' => $userId])->contain(['Roles'])->limit(1)->first();
     if (in_array(ROLE_ID_ADMINISTRATOR, $user->role_ids) && $this->Users->countAdministrators() === 1) {
         $this->Flash->warning(__d('user', 'You are the last administrator in the system, your account cannot be canceled.'));
         $this->redirect($this->referer());
     }
     if ($user && $code == $user->cancel_code) {
         if ($this->Users->delete($user)) {
             NotificationManager::canceled($user)->send();
             $this->Flash->success(__d('user', 'Account successfully canceled'));
         } else {
             $this->Flash->danger(__d('user', 'Account could not be canceled due to an internal error, please try again later.'));
         }
     } else {
         $this->Flash->warning(__d('user', 'Not user was found, invalid cancellation URL.'));
     }
     $this->title(__d('user', 'Account Cancellation'));
     $this->redirect($this->referer());
 }