Ejemplo n.º 1
0
 /**
  * allows a user to signup for an account
  *
  */
 public function signupAction()
 {
     $realm = $this->_getParam('realm', null);
     if (is_null($realm)) {
         throw new Ot_Exception_Input('msg-error-realmNotFound');
     }
     // Set up the auth adapter
     $authAdapter = new Ot_Model_DbTable_AuthAdapter();
     $adapter = $authAdapter->find($realm);
     if (is_null($adapter)) {
         throw new Ot_Exception_Data($this->view->translate('ot-login-signup:realmNotFound', array('<b>' . $realm . '</b>')));
     }
     if ($adapter->enabled == 0) {
         throw new Ot_Exception_Access('msg-error-authNotSupported');
     }
     $className = (string) $adapter->class;
     $auth = new $className();
     if (!$auth->manageLocally()) {
         throw new Ot_Exception_Access('msg-error-authNotSupported');
     }
     if (!$auth->allowUserSignUp()) {
         throw new Ot_Exception_Access('msg-error-authNotAllowed');
     }
     $form = new Ot_Form_Signup();
     $form->removeElement('realm');
     if ($this->_request->isPost()) {
         if ($form->isValid($_POST)) {
             if ($form->getValue('password') == $form->getValue('passwordConf')) {
                 $accountData = array('username' => $form->getValue('username'), 'password' => md5($form->getValue('password')), 'realm' => $realm, 'role' => $this->_helper->configVar('newAccountRole'), 'emailAddress' => $form->getValue('emailAddress'), 'firstName' => $form->getValue('firstName'), 'lastName' => $form->getValue('lastName'), 'timezone' => $form->getValue('timezone'));
                 $account = new Ot_Model_DbTable_Account();
                 if ($account->accountExists($accountData['username'], $accountData['realm'])) {
                     $this->_helper->messenger->addError('msg-error-usernameTaken');
                 } else {
                     $dba = Zend_Db_Table::getDefaultAdapter();
                     $dba->beginTransaction();
                     try {
                         $accountData['accountId'] = $account->insert($accountData);
                         $aar = new Ot_Account_Attribute_Register();
                         $vars = $aar->getVars($accountData['accountId']);
                         $values = $form->getValues();
                         foreach ($vars as $varName => $var) {
                             if (isset($values['accountAttributes'][$varName])) {
                                 $var->setValue($values['accountAttributes'][$varName]);
                                 $aar->save($var, $accountData['accountId']);
                             }
                         }
                         $cahr = new Ot_CustomAttribute_HostRegister();
                         $thisHost = $cahr->getHost('Ot_Profile');
                         if (is_null($thisHost)) {
                             throw new Ot_Exception_Data('msg-error-objectNotSetup');
                         }
                         $customAttributes = $thisHost->getAttributes($accountData['accountId']);
                         foreach ($customAttributes as $attributeName => $a) {
                             if (array_key_exists($attributeName, $values['customAttributes'])) {
                                 $a['var']->setValue($values['customAttributes'][$attributeName]);
                                 $thisHost->saveAttribute($a['var'], $accountData['accountId'], $a['attributeId']);
                             }
                         }
                     } catch (Exception $e) {
                         $dba->rollback();
                         throw $e;
                     }
                     $dba->commit();
                     $loggerOptions = array('attributeName' => 'accountId', 'attributeId' => $accountData['accountId']);
                     $this->_helper->log(Zend_Log::INFO, 'User ' . $accountData['username'] . ' created an account.', $loggerOptions);
                     $dt = new Ot_Trigger_Dispatcher();
                     $dt->setVariables($accountData);
                     $dt->password = $form->getValue('password');
                     $dt->loginMethod = $realm;
                     $dt->dispatch('Login_Index_Signup');
                     $authAdapterModel = new Ot_Model_DbTable_AuthAdapter();
                     $adapter = $authAdapterModel->find($realm);
                     $className = (string) $adapter->class;
                     // Set up the authentication adapter
                     $authAdapter = new $className($accountData['username'], $form->getValue('password'));
                     $auth = Zend_Auth::getInstance();
                     $authRealm = new Zend_Session_Namespace('authRealm');
                     $authRealm->setExpirationHops(1);
                     $authRealm->realm = $realm;
                     $authRealm->autoLogin = $authAdapter->autoLogin();
                     // Attempt authentication, saving the result
                     $result = $auth->authenticate($authAdapter);
                     $authRealm->unsetAll();
                     $req = new Zend_Session_Namespace(Zend_Registry::get('siteUrl') . '_request');
                     $this->_helper->messenger->addSuccess('msg-info-accountCreated');
                     if ($result->isValid()) {
                         $account = new Ot_Model_DbTable_Account();
                         $thisAccount = $account->getByUsername($accountData['username'], $realm);
                         $auth->getStorage()->write($thisAccount);
                         if (isset($req->uri) && $req->uri != '') {
                             $uri = $req->uri;
                             $req->unsetAll();
                             $this->_helper->redirector->gotoUrl($uri);
                         } else {
                             $this->_helper->redirector->gotoRoute(array(), 'default', true);
                         }
                     } else {
                         $this->_helper->redirector->gotoRoute(array('realm' => $realm), 'login', true);
                     }
                 }
             } else {
                 $this->_helper->messenger->addError('msg-error-passwordsNotMatch');
             }
         } else {
             $this->_helper->messenger->addError('msg-error-invalidFormInfo');
         }
     }
     $this->_helper->pageTitle('ot-login-signup:title');
     $this->view->headScript()->appendFile($this->view->baseUrl() . '/scripts/ot/jquery.plugin.passStrength.js');
     $this->view->assign(array('realm' => $realm, 'form' => $form));
 }
Ejemplo n.º 2
0
 /**
  * Adds a user to the system
  *
  */
 public function addAction()
 {
     $account = new Ot_Model_DbTable_Account();
     $defaultRole = $this->_helper->configVar('defaultRole');
     $form = new Ot_Form_Account(true);
     $form->populate(array('roleSelect' => array($defaultRole)));
     $acl = Zend_Registry::get('acl');
     $permissions = $acl->getResources($defaultRole);
     if ($this->_request->isPost()) {
         if ($form->isValid($_POST)) {
             $password = $account->generatePassword();
             $accountData = array('username' => $form->getValue('username'), 'password' => md5($password), 'realm' => $form->getValue('realm'), 'firstName' => $form->getValue('firstName'), 'lastName' => $form->getValue('lastName'), 'emailAddress' => $form->getValue('emailAddress'), 'timezone' => $form->getValue('timezone'), 'role' => (array) $form->getValue('role'));
             if (!isset($accountData['role']) || count($accountData['role']) < 1) {
                 $accountData['role'] = $this->_helper->configVar('defaultRole');
             }
             $dba = Zend_Db_Table::getDefaultAdapter();
             $dba->beginTransaction();
             if ($account->accountExists($accountData['username'], $accountData['realm'])) {
                 $this->_helper->messenger->addError('msg-error-accountTaken');
             } else {
                 try {
                     $accountData['accountId'] = $account->insert($accountData);
                     $aar = new Ot_Account_Attribute_Register();
                     $vars = $aar->getVars($accountData['accountId']);
                     $values = $form->getValues();
                     foreach ($vars as $varName => $var) {
                         if (isset($values['accountAttributes'][$varName])) {
                             $var->setValue($values['accountAttributes'][$varName]);
                             $aar->save($var, $this->_userData['accountId']);
                         }
                     }
                     $cahr = new Ot_CustomAttribute_HostRegister();
                     $thisHost = $cahr->getHost('Ot_Profile');
                     if (is_null($thisHost)) {
                         throw new Ot_Exception_Data('msg-error-objectNotSetup');
                     }
                     $customAttributes = $thisHost->getAttributes($accountData['accountId']);
                     foreach ($customAttributes as $attributeName => $a) {
                         if (array_key_exists($attributeName, $values['customAttributes'])) {
                             $a['var']->setValue($values['customAttributes'][$attributeName]);
                             $thisHost->saveAttribute($a['var'], $this->_userData['accountId'], $a['attributeId']);
                         }
                     }
                 } catch (Exception $e) {
                     $dba->rollback();
                     throw $e;
                 }
                 $accountData['password'] = $password;
                 $this->_helper->messenger->addSuccess('msg-info-accountCreated');
                 $td = new Ot_Trigger_Dispatcher();
                 $td->setVariables($accountData);
                 $role = new Ot_Model_DbTable_Role();
                 $roles = array();
                 foreach ($accountData['role'] as $r) {
                     $roles[] = $role->find($r)->name;
                 }
                 $otAuthAdapter = new Ot_Model_DbTable_AuthAdapter();
                 $thisAdapter = $otAuthAdapter->find($accountData['realm']);
                 $td->role = implode(',', $roles);
                 $td->loginMethod = $thisAdapter->name;
                 $authAdapter = new $thisAdapter->class();
                 if ($authAdapter->manageLocally()) {
                     $this->_helper->messenger->addSuccess('msg-info-accountPasswordCreated');
                     $td->dispatch('Admin_Account_Create_Password');
                 } else {
                     $td->dispatch('Admin_Account_Create_NoPassword');
                 }
                 $dba->commit();
                 $logOptions = array('attributeName' => 'accountId', 'attributeId' => $accountData['accountId']);
                 $this->_helper->log(Zend_Log::INFO, 'Account was added', $logOptions);
                 $this->_helper->redirector->gotoRoute(array('action' => 'all'), 'account', true);
             }
         } else {
             $this->_helper->messenger->addError('msg-error-invalidForm');
         }
     }
     $this->view->headScript()->appendFile($this->view->baseUrl() . '/scripts/ot/jquery.tooltip.min.js');
     $this->view->headScript()->appendFile($this->view->baseUrl() . '/scripts/ot/account/permissionsTable.js');
     $this->_helper->pageTitle('ot-account-add:title');
     $this->view->assign(array('form' => $form, 'permissions' => $permissions, 'permissionList' => Zend_Json::encode($permissions)));
 }