/** * Action when going to the main login page * */ public function indexAction() { $req = new Zend_Session_Namespace(Zend_Registry::get('siteUrl') . '_request'); if (Zend_Auth::getInstance()->hasIdentity()) { if (isset($req->uri) && $req->uri != '') { $uri = $req->uri; $req->unsetAll(); $this->_helper->redirector->gotoUrl($uri); } else { $this->_helper->redirector->gotoRoute(array(), 'default', true); } } $loginOptions = Zend_Registry::get('applicationLoginOptions'); $authAdapter = new Ot_Model_DbTable_AuthAdapter(); $adapters = $authAdapter->getEnabledAdapters(); if (!$adapters || $adapters->count() == 0) { throw new Ot_Exception_Data('ot-login-index:noAdaptersEnabled'); } $loginForms = array(); $realm = 'local'; //set a default value for $realm, since it's required foreach ($adapters as $adapter) { if (!$adapter->adapterKey) { throw new Ot_Exception_Data('ot-login-index:adapterMissingKey'); } $a = new $adapter->class(); $form = new Ot_Form_LoginRealm($adapter->adapterKey, $a->autoLogin(), $a->allowUserSignUp()); $form->setAction($this->view->url(array(), 'login', true)); $loginForms[$adapter->adapterKey] = array('form' => $form, 'realm' => $adapter->adapterKey, 'name' => $adapter->name, 'description' => $adapter->description, 'autoLogin' => $a->autoLogin()); } $formUserId = null; $formPassword = null; $validForm = false; $realm = $this->_getParam('realm', $realm); if ($this->_request->isPost()) { $form = $loginForms[$realm]['form']; if (!$form->isValid($_POST)) { $realm = $form->getValue('realm'); if (isset($loginForms[$realm]) && $loginForms[$realm]['autoLogin']) { $formUserId = ''; $formPassword = ''; $validForm = true; } $this->_helper->messenger->addError('msg-error-invalidFormInfo'); } else { $validForm = true; } } $authRealm = new Zend_Session_Namespace('authRealm'); $authRealm->setExpirationHops(1); if (isset($authRealm->realm) && $authRealm->autoLogin || $this->_request->isPost() && $validForm) { if (isset($authRealm->realm) && !$this->_request->isPost()) { $realm = $authRealm->realm; } else { if ($form->getValue('realm')) { $realm = $form->getValue('realm'); } } $username = $formUserId ? $formUserId : $form->getValue('username'); $password = $formPassword ? $formPassword : $form->getValue('password'); $redirectUri = $form->getValue('redirectUri'); $authAdapter = new Ot_Model_DbTable_AuthAdapter(); $adapter = $authAdapter->find($realm); $className = (string) $adapter->class; // Set up the authentication adapter $authAdapter = new $className($username, $password, $redirectUri); $auth = Zend_Auth::getInstance(); $authRealm->realm = $realm; $authRealm->autoLogin = $authAdapter->autoLogin(); // Attempt authentication, saving the result $result = $auth->authenticate($authAdapter); $authRealm->unsetAll(); if ($result->isValid()) { $username = $auth->getIdentity()->username; $realm = $auth->getIdentity()->realm; $account = new Ot_Model_DbTable_Account(); $thisAccount = $account->getByUsername($username, $realm); if (is_null($thisAccount)) { $password = $account->generatePassword(); $acctData = array('username' => $username, 'password' => md5($password), 'realm' => $realm, 'role' => $this->_helper->configVar('newAccountRole'), 'lastLogin' => time()); $identity = $auth->getIdentity(); if (isset($identity->firstName)) { $acctData['firstName'] = $identity->firstName; } if (isset($identity->lastName)) { $acctData['lastName'] = $identity->lastName; } if (isset($identity->emailAddress)) { $acctData['emailAddress'] = $identity->emailAddress; } if ($loginOptions['generateAccountOnLogin'] != 1) { $auth->clearIdentity(); $authAdapter->autoLogout(); throw new Ot_Exception_Access('msg-error-createAccountNotAllowed'); } $accountId = $account->insert($acctData); $thisAccount = $account->getByAccountId($accountId); } else { // update last login time $data = array('accountId' => $thisAccount->accountId, 'lastLogin' => time()); $account->update($data, null); } $auth->getStorage()->write($thisAccount); $loggerOptions = array('accountId' => $thisAccount->accountId, 'role' => is_array($thisAccount->role) ? implode(',', $thisAccount->role) : $thisAccount->role, 'attributeName' => 'accountId', 'attributeId' => $thisAccount->accountId); $this->_helper->log(Zend_Log::INFO, 'User ' . $username . ' logged in.', $loggerOptions); if (isset($req->uri) && $req->uri != '') { $uri = $req->uri; $req->unsetAll(); return $this->_helper->redirector->gotoUrl($uri); } else { return $this->_helper->redirector->gotoRoute(array(), 'default', true); } } else { if (count($result->getMessages()) == 0) { $this->_helper->messenger->addError('msg-error-invalidUsername'); } else { foreach ($result->getMessages() as $m) { $this->_helper->messenger->addInfo($m); } } } } // If we have a single adapter that auto logs in, we forward on. if (count($loginForms) == 1) { $method = reset($loginForms); if ($method['autoLogin']) { $authRealm->realm = $method['realm']; $authRealm->autoLogin = true; return $this->_helper->redirector->gotoRoute(array('realm' => $authRealm->realm), 'login', true); } } if (isset($req->uri) && $req->uri != '') { $this->_helper->messenger->addInfo('msg-info-loginBeforeContinuing'); } $this->view->assign(array('loginForms' => $loginForms, 'realm' => $realm)); }
/** * 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))); }