Ejemplo n.º 1
0
 /**
  * Sets the status of a given user to 'disabled'.
  * @param int $userId id of the user
  * @param array $formParams
  * @return array $response
  */
 public function disable($userId, array $formParams = array())
 {
     // create the form object
     $form = new Daiquiri_Form_Confirm(array('submit' => 'Disable user'));
     // valiadate the form if POST
     if (!empty($formParams)) {
         if ($form->isValid($formParams)) {
             // get the user credentials
             $user = $this->getResource()->fetchRow($userId);
             // update the user
             if ($user['status'] === 'disabled') {
                 $form->setDescription('User status is already "disabled"');
                 return $this->getModelHelper('CRUD')->validationErrorResponse($form);
             } else {
                 if (in_array($user['role'], array('manager', 'admin'))) {
                     $form->setDescription('Admins and managers can not be disabled.');
                     return $this->getModelHelper('CRUD')->validationErrorResponse($form);
                 } else {
                     // get the new status id
                     $statusId = Daiquiri_Auth::getInstance()->getStatusId('disabled');
                     // disable user in database
                     $this->getResource()->updateRow($userId, array('status_id' => $statusId));
                     // invalidate the session of the user
                     $sessionResource = new Auth_Model_Resource_Sessions();
                     foreach ($sessionResource->fetchAuthSessionsByUserId($userId) as $session) {
                         $sessionResource->deleteRow($session);
                     }
                     // send a notification mail
                     if (Daiquiri_Config::getInstance()->auth->notification->updateUser) {
                         $user = $this->getResource()->fetchRow($userId);
                         $this->getModelHelper('mail')->send('auth.updateUser', array('to' => Daiquiri_Config::getInstance()->auth->notification->mail->toArray(), 'id' => $user['id'], 'username' => $user['username'], 'firstname' => $user['details']['firstname'], 'lastname' => $user['details']['lastname']));
                     }
                     // log the event amd return
                     Daiquiri_Log::getInstance()->notice("user '{$user['username']}' disabled");
                     return array('status' => 'ok');
                 }
             }
         } else {
             return $this->getModelHelper('CRUD')->validationErrorResponse($form);
         }
     }
     return array('form' => $form, 'status' => 'form');
 }
Ejemplo n.º 2
0
 /**
  * Deletes an existing user.
  * @param int $id id of the user
  * @param array $formParams
  * @return array $response
  */
 public function delete($id, array $formParams = array())
 {
     // create the form object
     $form = new Daiquiri_Form_Danger(array('submit' => 'Delete user'));
     // valiadate the form if POST
     if (!empty($formParams)) {
         if ($form->isValid($formParams)) {
             // get the form values
             $values = $form->getValues();
             // delete the user and redirect
             $this->getResource()->deleteRow($id);
             // invalidate the session of the user
             $resource = new Auth_Model_Resource_Sessions();
             foreach ($resource->fetchAuthSessionsByUserId($id) as $session) {
                 $resource->deleteRow($session);
             }
             // log the event and return
             Daiquiri_Log::getInstance()->notice("user deleted by admin (user_id: {$id})");
             return array('status' => 'ok');
         } else {
             return $this->getModelHelper('CRUD')->validationErrorResponse($form);
         }
     }
     return array('form' => $form, 'status' => 'form');
 }