Ejemplo n.º 1
0
 /**
  * Authenticate a user
  *
  * Authentication is handled by SoftLayer's authentication system. Passwords
  * are not stored locally.
  *
  * @param string $username
  * @param string $password
  * @throws Exception
  * @return bool
  */
 public static function authenticate($username, $password)
 {
     /*
      * Make sure the user exists locally first.
      */
     $user = Model_DbTable_User::findByUsername($username);
     if ($user == null) {
         throw new Exception('Invalid login credentials provided.');
     }
     /*
      * Attempt to authenticate to the SoftLayer API. API docs at
      * http://sldn.softlayer.com/wiki/index.php/SoftLayer_User_Customer::getPortalLoginToken
      */
     $client = SoftLayer_SoapClient::getClient('SoftLayer_User_Customer');
     try {
         $result = $client->getPortalLoginToken($username, $password);
     } catch (Exception $e) {
         throw new Exception($e->getMessage());
     }
     return true;
 }
Ejemplo n.º 2
0
 /**
  * Present a login form and handle user authentication.
  */
 public function loginAction()
 {
     /*
      * Build the login form
      */
     $form = new Zend_Form();
     $form->setMethod('post');
     $username = $form->createElement('text', 'username');
     $username->setLabel($this->view->translate->_('Username'));
     $username->setRequired(true);
     $username->addValidator('alnum');
     $password = $form->createElement('password', 'password');
     $password->setLabel($this->view->translate->_('Password'));
     $password->setRequired(true);
     $form->addElement($username);
     $form->addElement($password);
     $form->addElement('submit', 'login', array('label' => $this->view->translate->_('Login')));
     /*
      * Handle authentication
      */
     if ($this->getRequest()->isPost()) {
         $formData = $this->getRequest()->getPost();
         if ($form->isValid($formData)) {
             try {
                 Model_DbTable_User::authenticate($form->getValue('username'), $form->getValue('password'));
                 /*
                  * Set the current user session
                  */
                 $user = Model_DbTable_User::findByUsername($form->getValue('username'));
                 $currentUser = new Zend_Session_Namespace('currentUser');
                 $currentUser->id = $user->id;
                 $currentUser->username = $user->username;
                 $currentUser->apiKey = $user->apiKey;
                 $currentUser->language = $user->language;
                 $currentUser->skin = $user->skin;
                 $currentUser->isAdmin = $user->isAdmin;
                 /*
                  * Redirect back to the index page.
                  */
                 $this->_helper->_redirector->goToRouteAndExit(array('controller' => 'index', 'action' => 'index'));
             } catch (Exception $e) {
                 $this->view->errorMessage = $this->view->translate->_('Login failed.') . ' ' . $e->getMessage();
             }
         } else {
             $this->view->errorMessage = $this->view->translate->_('Login failed.') . ' ' . $this->view->translate->_('Please completely fill out the login form.');
             $form->populate($formData);
         }
     }
     $this->view->headTitle($this->view->translate->_('Login'));
     $this->view->form = $form;
 }