/**
  * Logs admin into the system
  * @param $login
  * @param $password
  * @return \SkullyAdmin\Models\Admin|null
  */
 public function login($login, $password)
 {
     /** @var \RedBean_SimpleModel $adminBean */
     $adminBean = R::findOne('admin', "status = ? and email = ?", array(Admin::STATUS_ACTIVE, $login));
     if (!empty($adminBean)) {
         /** @var \SkullyAdmin\Models\Admin $admin */
         $admin = $adminBean->box();
         if ($admin->get('password_hash') == UtilitiesHelper::toHash($password, $admin->get('salt'), $this->app->config('globalSalt'))) {
             $adminSessions = R::find('adminsession', "admin_id = ?", array($admin->getID()));
             if (!empty($adminSessions)) {
                 R::trashAll($adminSessions);
             }
             // when everything ok, regenerate session
             session_regenerate_id(true);
             // change session ID for the current session and invalidate old session ID
             $adminId = $admin->getID();
             $sessionId = session_id();
             $adminsession = $this->app->createModel('adminsession', array("admin_id" => $adminId, "session_id" => $sessionId));
             $this->app->getSession()->set('adminId', $admin->getID());
             R::store($adminsession);
             return $admin;
         }
     }
     return null;
 }
Example #2
0
 public function beforeSave()
 {
     if (!empty($this->password)) {
         $this->set('salt', time());
         $this->set('password_hash', UtilitiesHelper::toHash($this->password, $this->get('salt'), $this->app->config('globalSalt')));
     }
     $this->removeProperty('password');
     $this->removeProperty('password_confirmation');
 }