/** * Verifies the current user cannot delete his role. * * Request current user password before deletion of any User Roles. * * @param Request $request Current router request. * * @return void */ protected function beforeDelete(Request $request) { if (!$request->post('password') || !Crypt::hashCompare($this->user->password, $request->post('password'))) { if (!$request->is('xhr')) { Helpers\FlashMessage::set($this->labels['general']['not_authorized'], 'danger'); } $request->redirectTo('index'); } if ($this->user->role_id == $this->resource->getPrimaryKeyValue()) { if (!$request->is('xhr')) { Helpers\FlashMessage::set($this->labels['errors']['delete']['self'], 'danger'); } $request->redirectTo('index'); } parent::beforeDelete($request); }
/** * Login action. * * Updates the user login time. * * @param Request $request Current router request. * * @return void */ public function login(Request $request) { if ($request->is('post')) { if ($this->captcha) { if (!Helpers\Captcha::isValid($this->captcha)) { Helpers\FlashMessage::set($this->labels['captcha']['error'], 'danger'); return; } } $user = Models\CMSUser::find()->where('email = ?', array($request->post('email')))->first(); if ($user && Crypt::hashCompare($user->password, $request->post('password'))) { $user->save(array('login_on' => gmdate('Y-m-d H:i:s')), true); /* Regenerate Session key for prevent session id fixation. */ Core\Session()->regenerateKey(); Core\Session()->set('cms_user_info', rawurlencode(serialize($user))); Core\Session()->set('cms_user_logged', 1); Core\Session()->remove('authentication_error'); Core\Session()->remove('captcha'); /* Regenerate CSRF token for prevent token fixation. */ Core\Session()->remove('_token'); $request->regenerateToken(); if ($request->get('redirect')) { $request->redirectTo($request->get('redirect')); } else { $request->redirectTo(array('controller' => 'account')); } } else { Helpers\FlashMessage::set($this->labels['login']['error'], 'danger'); Core\Session()->set('authentication_error', true); if (Core\Config()->CAPTCHA['enabled']) { $this->loadCaptcha(Core\Config()->CAPTCHA); } } } else { if (Core\Session()->get('cms_user_logged') === 1) { $request->redirectTo(array('controller' => 'account')); } } }
/** * Encrypt fields. * * @param Base\Model $resource Currently processed resource. * * @static * @access public * * @return void */ public static function encrypt(Base\Model $resource) { foreach (self::$encryptedFields as $field => $type) { $resource->{$field} = Crypt::encrypt($resource->{$field}, Core\Config()->DB['encryption_key'], $type); } }
/** * Hashes the new password if the password is different form the stored one. * * @return void */ public function beforeSave() { if ($this->password !== $this->currentPassword) { $this->password = Crypt::hash($this->password); } }