Exemple #1
0
 /**
  * Updates the credentials of the currently logged in user.
  * @param array $formParams
  * @return array $response
  */
 public function update(array $formParams = array())
 {
     // get id
     $id = Daiquiri_Auth::getInstance()->getCurrentId();
     // get user
     $user = $this->getResource()->fetchRow($id);
     // get user detail keys model
     $detailKeyModel = new Auth_Model_DetailKeys();
     $detailKeys = $detailKeyModel->getResource()->fetchRows();
     // create the form object
     $form = new Auth_Form_Account(array('user' => $this->getResource()->fetchRow($id), 'detailKeys' => $detailKeys, 'changeUsername' => Daiquiri_Config::getInstance()->auth->changeUsername, 'changeEmail' => Daiquiri_Config::getInstance()->auth->changeEmail));
     // valiadate the form if POST
     if (!empty($formParams)) {
         if ($form->isValid($formParams)) {
             // get the form values
             $values = $form->getValues();
             // process the details
             $changed = false;
             $values['details'] = array();
             foreach ($detailKeys as $detailKey) {
                 if (is_array($values[$detailKey['key']])) {
                     $values['details'][$detailKey['key']] = Zend_Json::encode($values[$detailKey['key']]);
                 } else {
                     if ($values[$detailKey['key']] === null) {
                         $values['details'][$detailKey['key']] = Zend_Json::encode(array());
                     } else {
                         $values['details'][$detailKey['key']] = $values[$detailKey['key']];
                     }
                 }
                 unset($values[$detailKey['key']]);
                 if ($values['details'][$detailKey['key']] != $user['details'][$detailKey['key']]) {
                     $changed = true;
                 }
             }
             if (Daiquiri_Config::getInstance()->auth->changeUsername && $values['username'] != $user['username']) {
                 $changed = true;
             }
             if (Daiquiri_Config::getInstance()->auth->changeEmail && $values['email'] != $user['email']) {
                 $changed = true;
             }
             if ($changed) {
                 // update the user
                 $this->getResource()->updateRow($id, $values);
                 // log the event
                 Daiquiri_Log::getInstance()->notice('account updated by user');
                 // send a notification
                 if (Daiquiri_Config::getInstance()->core->notification->updateUser) {
                     $newUser = $this->getResource()->fetchRow($id);
                     $this->getModelHelper('notification')->updateUser($user, $newUser);
                 }
             }
             return array('status' => 'ok');
         } else {
             return $this->getModelHelper('CRUD')->validationErrorResponse($form);
         }
     }
     return array('form' => $form, 'status' => 'form');
 }
Exemple #2
0
 /**
  * Registers a new user.
  * @param array $formParams
  * @return array $response
  */
 public function register(array $formParams = array())
 {
     // get user detail keys model
     $detailKeyModel = new Auth_Model_DetailKeys();
     $detailKeys = $detailKeyModel->getResource()->fetchRows();
     // create the form object
     $form = new Auth_Form_Registration(array('detailKeys' => $detailKeys));
     // check if request is POST
     if (!empty($formParams)) {
         if ($form->isValid($formParams)) {
             // get the form values
             $values = $form->getValues();
             // unset some elements
             unset($values['confirm_password']);
             // process the details
             $values['details'] = array();
             foreach ($detailKeys as $detailKey) {
                 if (is_array($values[$detailKey['key']])) {
                     $values['details'][$detailKey['key']] = Zend_Json::encode($values[$detailKey['key']]);
                 } else {
                     if ($values[$detailKey['key']] === null) {
                         $values['details'][$detailKey['key']] = Zend_Json::encode(array());
                     } else {
                         $values['details'][$detailKey['key']] = $values[$detailKey['key']];
                     }
                 }
                 unset($values[$detailKey['key']]);
             }
             // produce random validation link
             $values['code'] = $this->createRandomString(32);
             // log the event
             Daiquiri_Log::getInstance()->notice("user '{$values['username']}' registered");
             // create the user and return
             $userId = $this->getResource()->registerUser($values);
             // send mail
             $link = Daiquiri_Config::getInstance()->getSiteUrl() . '/auth/registration/validate/id/' . $userId . '/code/' . $values['code'];
             $this->getModelHelper('mail')->send('auth.register', array('to' => $values['email'], 'firstname' => $values['details']['firstname'], 'lastname' => $values['details']['lastname'], 'link' => $link));
             return array('status' => 'ok');
         } else {
             return $this->getModelHelper('CRUD')->validationErrorResponse($form);
         }
     }
     return array('form' => $form, 'status' => 'form');
 }
Exemple #3
0
 /**
  * @brief   constructor - initialises password cryptography and all required database tables
  * 
  * Sets up everything needed for the Zend Authentication mechanism and hooks up the
  * desired password crypto method with the password check. 
  */
 protected function __construct()
 {
     // get the acl class, this could be more general
     $this->_acl = new Daiquiri_Acl();
     // store roles in auth object
     $roleModel = new Auth_Model_Roles();
     $this->_roles = $roleModel->getResource()->fetchValues('role');
     // store status in auth object
     $statusModel = new Auth_Model_Status();
     $this->_status = $statusModel->getResource()->fetchValues('status');
     // get user detail keys
     $detailKeysModel = new Auth_Model_DetailKeys();
     $this->_detailKeys = $detailKeysModel->getResource()->fetchValues('key');
     // get treatment from default crypt object
     try {
         $crypt = Daiquiri_Crypt_Abstract::factory();
     } catch (Exception $e) {
         $crypt = null;
     }
     if ($crypt !== null) {
         $treatment = $crypt->getTreatment();
         // get treatment for users
         $userTreatment = $treatment;
         $activeId = $this->getStatusId('active');
         if (is_numeric($activeId)) {
             $userTreatment .= 'AND status_id=' . $activeId;
         }
         // get treatement for apps
         $appTreatment = $treatment . ' AND active=1';
         // set properties of the user adapter
         $this->_userAdapter = new Zend_Auth_Adapter_DbTable();
         $this->_userAdapter->setTableName('Auth_User');
         $this->_userAdapter->setIdentityColumn('username');
         $this->_userAdapter->setCredentialColumn('password');
         $this->_userAdapter->setCredentialTreatment($userTreatment);
         // set properties of the app adapter
         $this->_appAdapter = new Zend_Auth_Adapter_DbTable();
         $this->_appAdapter->setTableName('Auth_Apps');
         $this->_appAdapter->setIdentityColumn('appname');
         $this->_appAdapter->setCredentialColumn('password');
         $this->_appAdapter->setCredentialTreatment($appTreatment);
     }
 }
Exemple #4
0
 /**
  * Initializes the database with the init data for the meetings module.
  */
 public function init()
 {
     // create status entries
     $authStatusModel = new Auth_Model_Status();
     if ($authStatusModel->getResource()->countRows() === 0) {
         foreach ($this->_init->options['init']['auth']['status'] as $status) {
             $a = array('status' => $status);
             $r = $authStatusModel->create($a);
             $this->_check($r, $a);
         }
     }
     // create roles entries
     $authRoleModel = new Auth_Model_Roles();
     if ($authRoleModel->getResource()->countRows() === 0) {
         foreach ($this->_init->options['init']['auth']['roles'] as $role) {
             $a = array('role' => $role);
             $r = $authRoleModel->create($a);
             $this->_check($r, $a);
         }
     }
     // create detail keys entries
     $authDetailKeysModel = new Auth_Model_DetailKeys();
     if ($authDetailKeysModel->getResource()->countRows() === 0) {
         foreach ($this->_init->options['init']['auth']['detailKeys'] as &$a) {
             if (!isset($a['type'])) {
                 $a['type_id'] = 0;
             } else {
                 $a['type_id'] = array_search($a['type'], Auth_Model_DetailKeys::$types);
                 unset($a['type']);
             }
             $r = $authDetailKeysModel->create($a);
             $this->_check($r, $a);
         }
     }
     // create users
     $authUserModel = new Auth_Model_User();
     if ($authUserModel->getResource()->countRows() === 0) {
         foreach ($this->_init->options['init']['auth']['user'] as $credentials) {
             // get the corresponding role_id and status_id
             $credentials['role_id'] = Daiquiri_Auth::getInstance()->getRoleId($credentials['role']);
             unset($credentials['role']);
             $credentials['status_id'] = Daiquiri_Auth::getInstance()->getStatusId($credentials['status']);
             unset($credentials['status']);
             // pre-process password first
             $credentials['new_password'] = $credentials['password'];
             $credentials['confirm_password'] = $credentials['password'];
             unset($credentials['password']);
             // process detail keys
             foreach ($this->_init->options['init']['auth']['detailKeys'] as $detailKey) {
                 if (in_array(Auth_Model_DetailKeys::$types[$detailKey['type_id']], array('radio', 'select'))) {
                     $options = Zend_Json::decode($detailKey['options']);
                     $option_id = array_search($credentials[$detailKey['key']], $options);
                     $credentials[$detailKey['key']] = $option_id;
                 } else {
                     if (in_array(Auth_Model_DetailKeys::$types[$detailKey['type_id']], array('checkbox', 'multiselect'))) {
                         $options = Zend_Json::decode($detailKey['options']);
                         $values = array();
                         foreach ($credentials[$detailKey['key']] as $value) {
                             $values[] = array_search($value, $options);
                         }
                         $credentials[$detailKey['key']] = $values;
                     }
                 }
             }
             // fake request parametes to make
             Zend_Controller_Front::getInstance()->getRequest()->setParams($credentials);
             // create user
             $r = $authUserModel->create($credentials);
             // clean up request
             Zend_Controller_Front::getInstance()->getRequest()->setParams(array());
             $this->_check($r, $credentials);
         }
     }
     // create apps
     $authAppsModel = new Auth_Model_Apps();
     if ($authAppsModel->getResource()->countRows() === 0) {
         foreach ($this->_init->options['init']['auth']['apps'] as $credentials) {
             // pre-process password first
             $credentials['new_password'] = $credentials['password'];
             $credentials['confirm_password'] = $credentials['password'];
             unset($credentials['password']);
             // fake request parametes to make
             Zend_Controller_Front::getInstance()->getRequest()->setParams($credentials);
             // create user
             $r = $authAppsModel->create($credentials);
             // clean up request
             Zend_Controller_Front::getInstance()->getRequest()->setParams(array());
             $this->_check($r, $credentials);
         }
     }
     // create acl ressources
     $authResourcesModel = new Auth_Model_Resources();
     if ($authResourcesModel->getResource()->countRows() === 0) {
         foreach ($this->_init->options['init']['auth']['resources'] as $resource) {
             $a = array('resource' => $resource);
             $r = $authResourcesModel->create($a);
             $this->_check($r, $a);
         }
     }
     // create acl rules, needs to be after create apps
     $authRulesModel = new Auth_Model_Rules();
     if ($authRulesModel->getResource()->countRows() === 0) {
         foreach ($this->_init->options['init']['auth']['rules'] as $role => $rule) {
             foreach ($rule as $resource => $permissions) {
                 $a = array('role' => $role, 'resource' => $resource, 'permissions' => implode(',', $permissions));
                 $r = $authRulesModel->create($a);
                 $this->_check($r, $a);
             }
         }
     }
 }
Exemple #5
0
 /**
  * Updates an existing user.
  * @param int $id id of the user
  * @param array $formParams
  * @return array $response
  */
 public function update($id, array $formParams = array())
 {
     // get the status model, the roles model and the roles
     $status = Daiquiri_Auth::getInstance()->getStatus();
     $roles = Daiquiri_Auth::getInstance()->getRoles();
     unset($roles[1]);
     // unset the guest user
     // get user
     $user = $this->getResource()->fetchRow($id);
     if (empty($user)) {
         throw new Daiquiri_Exception_NotFound();
     }
     // get user detail keys model
     $detailKeyModel = new Auth_Model_DetailKeys();
     $detailKeys = $detailKeyModel->getResource()->fetchRows();
     // create the form object
     $form = new Auth_Form_UpdateUser(array('detailKeys' => $detailKeys, 'status' => $status, 'roles' => $roles, 'changeUsername' => Daiquiri_Config::getInstance()->auth->changeUsername, 'changeEmail' => Daiquiri_Config::getInstance()->auth->changeEmail, 'user' => $user));
     // check if request is POST
     if (!empty($formParams)) {
         if ($form->isValid($formParams)) {
             // get the form values
             $values = $form->getValues();
             // process the details
             $changed = false;
             $values['details'] = array();
             foreach ($detailKeys as $detailKey) {
                 if (is_array($values[$detailKey['key']])) {
                     $values['details'][$detailKey['key']] = Zend_Json::encode($values[$detailKey['key']]);
                 } else {
                     if ($values[$detailKey['key']] === null) {
                         $values['details'][$detailKey['key']] = Zend_Json::encode(array());
                     } else {
                         $values['details'][$detailKey['key']] = $values[$detailKey['key']];
                     }
                 }
                 unset($values[$detailKey['key']]);
                 if ($values['details'][$detailKey['key']] != $user['details'][$detailKey['key']]) {
                     $changed = true;
                 }
             }
             if (Daiquiri_Config::getInstance()->auth->changeUsername && $values['username'] !== $user['username']) {
                 $changed = true;
             }
             if (Daiquiri_Config::getInstance()->auth->changeEmail && $values['email'] !== $user['email']) {
                 $changed = true;
             }
             if ($values['role_id'] !== $user['role_id']) {
                 $changed = true;
             }
             if ($values['status_id'] !== $user['status_id']) {
                 $changed = true;
             }
             if ($changed) {
                 // update the user
                 $this->getResource()->updateRow($id, $values);
                 // log the event and return
                 Daiquiri_Log::getInstance()->notice("user '{$user['username']}' updated");
                 // send a notification
                 if (Daiquiri_Config::getInstance()->core->notification->updateUser) {
                     $newUser = $this->getResource()->fetchRow($id);
                     $this->getModelHelper('notification')->updateUser($user, $newUser);
                 }
             }
             return array('status' => 'ok');
         } else {
             return $this->getModelHelper('CRUD')->validationErrorResponse($form);
         }
     }
     return array('form' => $form, 'status' => 'form');
 }