Example #1
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);
     }
 }
Example #2
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);
             }
         }
     }
 }