public function actionLogout(User $user, CSRF $CSRF)
 {
     $valid = $CSRF->check(Request::get($CSRF->csrfParam));
     if ($valid) {
         $user->logout(true);
     }
     $this->redirect(Url::set()->removeAllArgs()->getAbsolute())->send(true);
 }
 public function actionIndex(User $user, Session $session)
 {
     $placeholders = [];
     if ($session->hasFlash($this->keySessionFlash)) {
         $placeholders['content'] = i18n::t('successActivate');
         return $this->render('success', $placeholders);
     } elseif ($user->isGuest() && ($users = Users::activate(Request::get('token')))) {
         // auto-login
         $user->addMulti($users->toArray(['id', 'username', 'url']));
         $user->login();
         $session->setFlash($this->keySessionFlash);
         $this->response->redirect(Url::set()->removeAllArgs()->getAbsoluteUrl(true))->send(true);
         return null;
     }
     return $this->notPage('@frontend.views/layouts/notPage');
 }
 public function actionSignup(User $user, CSRF $CSRF, Session $session, Mail $mail)
 {
     if ($user->isLogged()) {
         $placeholders['content'] = $this->getMessageLogout($CSRF, 'signupLogout');
         return $this->render('success', $placeholders);
     }
     if (($message = $session->getFlash($this->keySessionFlash)) && isset($message['email'])) {
         $placeholders['content'] = $this->template->getChunk('@common.views/elements/alert-success', ['output' => i18n::t($this->keySessionFlash, ['email' => $message['email']])]);
         return $this->render('success', $placeholders);
     }
     $model = new SignupForm();
     // redirect
     Event::on($model, SignupForm::EVENT_AFTER_SIGNUP, function (ModelEvent $event) use($session, $mail, $model) {
         $this->sendMail($mail, $event->result, $model);
         $session->setFlash('successSignup', ['email' => $event->result->email]);
         $this->response->refresh()->send(true);
     });
     $model->load($_POST);
     $placeholders['model'] = $model;
     return $this->render('index.php', $placeholders);
 }
Exemple #4
0
 /**
  * Checks a role (RBAC).
  * @param array $roles
  * @return bool
  * @throws FilterException
  */
 protected function matchRole(array $roles)
 {
     if (!$this->user instanceof \rock\user\User) {
         throw new FilterException(FilterException::UNKNOWN_CLASS, ['class' => '\\rock\\user\\User']);
     }
     // all roles
     if (in_array('*', $roles)) {
         return true;
     } elseif (in_array('?', $roles) && $this->user->isGuest()) {
         return true;
         // Authenticated
     } elseif (in_array('@', $roles) && !$this->user->isGuest()) {
         return true;
     }
     foreach ($roles as $role) {
         if (!$this->user->check($role)) {
             $this->sendHeaders();
             return false;
         }
     }
     return true;
 }
Exemple #5
0
        return !Rock::$app->user->isGuest();
    }
    return \rock\helpers\ArrayHelper::getValue(Rock::$app->user->getAll(), $keys);
}, 'call' => function (array $call, array $params = [], Template $template) {
    if (!isset($call[1])) {
        $call[1] = null;
    }
    list($class, $method) = $call;
    if ($class === 'context') {
        $object = $template->context;
        $function = [$object, $method];
    } elseif (function_exists($class) && !$class instanceof \Closure) {
        return call_user_func_array($class, $params);
    } else {
        $object = \rock\di\Container::load($class);
        if (!method_exists($object, $method)) {
            throw new \rock\base\BaseException(\rock\base\BaseException::UNKNOWN_METHOD, ['method' => "{$class}::{$method}"]);
        }
        $function = [$object, $method];
    }
    return call_user_func_array($function, $params);
}], 'title' => 'Demo', 'metaTags' => ['charset' => '<meta charset="' . Rock::$app->charset . '" />'], 'linkTags' => ['favicon' => '<link rel="Shortcut Icon" type="image/x-icon" href="/favicon.ico?10">'], 'snippets' => ['request.get' => ['class' => \rock\snippets\request\Get::className()], 'request.post' => ['class' => \rock\snippets\request\Post::className()], 'csrf' => ['class' => \rock\snippets\CSRF::className()], 'captchaView' => ['class' => \rock\snippets\CaptchaView::className()], 'activeForm' => ['class' => \rock\snippets\ActiveForm::className()]]], 'execute' => ['class' => \rock\execute\CacheExecute::className()], 'i18n' => ['class' => \rock\i18n\i18n::className(), 'pathsDicts' => ['ru' => ['@rock/messages/ru/lang.php', '@rock/messages/ru/validate.php'], 'en' => ['@rock/messages/en/lang.php', '@rock/messages/en/validate.php']], 'locale' => [\rock\LocaleProperties::className(), 'locale']], 'date' => ['class' => \rock\date\DateTime::className(), 'locale' => [\rock\LocaleProperties::className(), 'locale'], 'formats' => ['dmy' => function (\rock\date\DateTime $dateTime) {
    $nowYear = date('Y');
    $lastYear = $dateTime->format('Y');
    return $nowYear > $lastYear ? $dateTime->format('j F Y') : $dateTime->format('d F');
}, 'dmyhm' => function (\rock\date\DateTime $dateTime) {
    $nowYear = date('Y');
    $lastYear = $dateTime->format('Y');
    return $nowYear > $lastYear ? $dateTime->format('j F Y H:i') : $dateTime->format('j F H:i');
}]], 'mail' => ['class' => \rock\mail\Mail::className(), 'From' => 'support@' . (new \rock\request\Request())->getHost(), 'FromName' => 'Rock Framework'], 'url' => ['class' => \rock\url\Url::className()], 'request' => ['class' => \rock\request\Request::className(), 'locale' => [\rock\LocaleProperties::className(), 'locale']], 'response' => ['class' => \rock\response\Response::className(), 'locale' => [\rock\LocaleProperties::className(), 'locale']], 'htmlResponseFormatter' => ['class' => \rock\response\HtmlResponseFormatter::className()], 'jsonResponseFormatter' => ['class' => \rock\response\JsonResponseFormatter::className()], 'xmlResponseFormatter' => ['class' => \rock\response\XmlResponseFormatter::className()], 'rssResponseFormatter' => ['class' => \rock\response\RssResponseFormatter::className()], 'session' => ['class' => \rock\session\Session::className(), 'cookieParams' => ['httponly' => true, 'lifetime' => 60 * 60 * 24 * 60, 'setUseCookies' => \rock\session\Session::USE_ONLY_COOKIES]], 'cookie' => ['class' => \rock\cookie\Cookie::className()], 'security' => ['class' => Security::className()], 'sanitize' => ['class' => \rock\sanitize\Sanitize::className()], 'validate' => ['class' => \rock\validate\Validate::className(), 'locale' => [\rock\LocaleProperties::className(), 'locale']], 'csrf' => ['class' => \rock\csrf\CSRF::className()], 'captcha' => ['class' => \rock\captcha\Captcha::className(), 'length' => 0, 'whiteNoiseDensity' => 1 / 6, 'blackNoiseDensity' => 1 / 30], 'user' => ['class' => \rock\user\User::className(), 'container' => 'user'], 'rbac' => ['class' => \rock\rbac\DBManager::className()], 'log' => ['class' => \rock\log\Log::className()], Role::className() => ['class' => Role::className()], Permission::className() => ['class' => Permission::className()]], require __DIR__ . '/widgets.php');
 protected function login(User $user, Users $users)
 {
     $data = $users->toArray();
     $user->addMulti(ArrayHelper::intersectByKeys($data, ['id', 'username', 'url']));
     $user->login();
 }