Yii2 as beforeRequest authentication rule in config not working for pages - authentication

In a Yii2 advance application, I am trying to allow users to the login page and about page only.
So i put in the /common/config/web.php configuration file the following rule
'as beforeRequest' => [ //if guest user access site so, redirect to login page.
'class' => 'yii\filters\AccessControl',
'rules' => [
[
'actions' => ['login', 'error', 'request-password-reset', 'about'],
'allow' => true,
],
[
'allow' => true,
'roles' => ['#'],
],
],
],
The login, error and request-password-reset it is working fine but not the about page.
I also tried '/page/about' and 'page/about' but i had no luck.
Any ideas how to fix or troubleshoot this.
Thanks

It should be 'view' instead of 'about'
I am sorry I found the answer the rule it is fine but i am using a page component and the PageController it is getting the view from the admin (database) in the following controller
class PageController extends Controller{
public function actionView($slug)
{
$model = Page::find()->where(['slug'=>$slug, 'status'=>Page::STATUS_PUBLISHED])->one();
if (!$model) {
throw new NotFoundHttpException(Yii::t('frontend', 'Page not found'));
}
$viewFile = $model->view ?: 'view';
return $this->render($viewFile, ['model'=>$model]);
}}
So in the rule I need to use 'view' for all the views rendered by the PageController

try this code
'as beforeRequest' => [ //if guest user access site so, redirect to login page.
'class' => 'yii\filters\AccessControl',
'rules' => [
[
'actions' => ['login', 'error', 'request-password-reset', 'about'],
'roles' => ['#']
'allow' => true,
],
[
'allow' => true,
'roles' => ['?'],
],
],
],
i hope it will work for you

Related

Get controller and action of URL

I'm extending the UrlRuleInterface class for custom URL rules
I'm trying to redirect the url to add a suffix / if the url doesn't have it.
public function parseRequest($manager, $request)
{
$matches = explode('/', $request->getPathInfo());
//if last array is empty, unset it!
if (empty(end($matches))) {
unset($matches[count($matches) - 1]);
} else {
//add code to redirect to controller and action here
}
How do i get the controller and action of the URL so i can redirect my custom URLs to add the /
The forcing suffix works fine on other URLS, but not my custom URLS.
in my main.php i have this
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => true,
'suffix' => '/',
'normalizer' => [
'class' => 'yii\web\UrlNormalizer',
],
'rules' => require ('urls.php'),
],

cakePHP3 Form Authentication fails on identify() and manual DefaultPasswordHasher check()

I copied code from examples to create a basic login screen based on the table individuals with email and password. My AppController has this:
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => ['username' => 'email', 'password' => 'password'],
'userModel' => 'Individuals']
],
'loginAction' => [
'controller' => 'Individuals',
'action' => 'login'
],
'loginRedirect' => [
'controller' => 'Associations',
'action' => 'login'
],
'logoutRedirect' => [
'controller' => 'Association',
'action' => 'login',
'home'
]
]);
Password resets are done via a token emailed to the user. The controller saves the unencrypted value and /src/Model/Entity/Individual.php has _setPassword that ensures the database has an encrypted value. Every save for the same password is different but that, I gather, is normal.
protected function _setPassword($password) {
if (strlen($password) > 0) {
return (new DefaultPasswordHasher)->hash($password);
}
}
My login function started with the standard stuff, but it always returns false
$user = $this->Auth->identify();
My login function now has this debug code that always gets a "no match"
debug($this->request->data);
$email = $this->request->data['email'];
$pwd = $this->request->data['password'];
$user = $this->Individuals->find()
->select(['Individuals.id', 'Individuals.email', 'Individuals.password'])
->where(['Individuals.email' => $email])
->first();
if ($user) {
if ((new DefaultPasswordHasher)->check($pwd, $user->password)) {
debug('match');
}
else{
debug('no match');
}
if ($user->password == (new DefaultPasswordHasher)->hash($pwd)) {
debug('match2');
}
else {
debug('no match2');
}
}
There's a lot more code in and around that and I'm pretty confident I've got it right. Let me know if you need more. I'm keen to crack this.
thanks in advance.

Standalone rbac setup for module yii2

I`ve got two modules in my yii2-basic application. For common users I have "user" table and for superadmin users I have the second "superadmin" table. There are two users with different sessions in my app - users and superadmins. I need to use different, standalone rbac for superadmin users table. Is it possible to setup two different rbacs in two modules? Now it is setup in web.php file globally. Maybe it is possible somehow to setup authManager in module separately, not in global web.php file?
Thanks in advance!
I figured it out! In your module.php file specify authManager with different table names, you are supposed to create them before. You also can specify rbac submodule of mdm\admin if you want to use it as well. Specify there another usertable - SuperAdmin in the code below.
public function init()
{
parent::init();
$this->modules = [
'rbac' => [
'class' => 'mdm\admin\Module',
'controllerMap' => [
'assignment' => [
'class' => 'mdm\admin\controllers\AssignmentController',
'userClassName' => 'app\models\SuperAdmin',
'idField' => 'id',
'usernameField' => 'username',
],
],
'layout' => 'left-menu',
'mainLayout' => '#app/views/layouts/rbac.php',
],
];
$config = [
'components' => [
'authManager' => [
'class' => 'yii\rbac\DbManager',
'itemTable' => 'superadmin_auth_item',
'assignmentTable' => 'superadmin_auth_assignment',
'itemChildTable' => 'superadmin_auth_item_child',
'ruleTable' => 'superadmin_auth_rule',
],
],
'as access' => [
'class' => 'app\components\SuperAdminAccessControl',//your overridden AccessControl class
'allowActions' => [
'admin/rbac/*',
'admin/default/login',
'admin/default/logout',
]
],
];
\Yii::$app->authManager->itemTable = 'superadmin_auth_item';
\Yii::$app->authManager->assignmentTable = 'superadmin_auth_assignment';
\Yii::$app->authManager->itemChildTable = 'superadmin_auth_item_child';
\Yii::$app->authManager->ruleTable = 'superadmin_auth_rule';
\Yii::configure(\Yii::$app, $config);
}
Overridden AccessControl class:
namespace app\components;
class SuperAdminAccessControl extends \mdm\admin\components\AccessControl
{
function __construct() {
$this->setUser('superadmin');//table name
}
}

yii2 custom REST api issue

I have tried all the way to make these run but no use.There is lots of problem and confusion i m going through.
I have make api which return all the countries which is working fine.Now need to write api function to list all the states of perticular country.
api : http://phpserver:8090/ssn-project/newzit/api/web/state/customstate?country_id=102
StateController.php
class StateController extends ActiveController{
public $modelClass = 'api\modules\state\models\State';
public function actionCustomState($country_id)
{
$model = new $this->modelClass;
$result = $model::find()
->where(['country_id' => $country_id])
->all();
return $result;
}
}
main.php
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => ['country/country','state/state','category/category','seller/seller'],
'extraPatterns' => [
'GET CustomState' => 'CustomState',
],
]
],
]
Am I doing anything wrong.Please help
What do you mean by 'controller' => ['country/country','state/state','category/category','seller/seller'] ? This will be treated as module/controller. You have placed all controllers inside different modules? With this logic, your api url will be
http://phpserver:8090/ssn-project/newzit/api/web/state/state/customstate?country_id=102
instead of
http://phpserver:8090/ssn-project/newzit/api/web/state/customstate?country_id=102
Found the solution.
made 'pluralize'=>false and used custom-state in url
My main.php
'rules' => [
[
'pluralize'=>false,
'class' => 'yii\rest\UrlRule',
'controller' => ['country/country','state/state','category/category','seller/seller','contactus/contactus'],
'extraPatterns' => [
'GET custom-state' => 'custom-state',
],
]
],
Thank you.

Module route with rewrite redirect to product on Prestahop

I created a custom route for an artist page:
public function hookModuleRoutes($params)
{
return [
'module-artists-artist' => [
'controller' => 'artist',
'rule' => 'artists/{id_artist}',
'keywords' => [
'id_artist' => ['regexp' => '[0-9]+', 'param' => 'id_artist'],
],
'params' => [
'fc' => 'module',
'module' => 'artists',
'controller' => 'artist'
],
],
];
}
If I test with /artists/1, this works. But I want to add the link_rewrite property. So I modified the configuration like this:
public function hookModuleRoutes($params)
{
return [
'module-artists-artist' => [
'controller' => 'artist',
'rule' => 'artists/{id_artist}-{rewrite}',
'keywords' => [
'id_artist' => ['regexp' => '[0-9]+', 'param' => 'id_artist'],
'rewrite' => ['regexp' => '[_a-zA-Z0-9\pL\pS-]*'],
],
'params' => [
'fc' => 'module',
'module' => 'artists',
'controller' => 'artist'
],
],
];
}
But when I try /artists/1-baxter, I'm redirected to the product page of product with ID 1. My artist controller is never called.
[Debug] This page has moved
Please use the following URL instead: http://localhost:8000/fr/estampes/1-est-ce-que-etre
How can I solve it?
This is because URLs generated by your pattern also match the product URL pattern, which has higherprecedence. PrestaShop doesn't check whether product exists, it just redirects directly to ProductController. Page patterns in PrestaShop differ from each other so that URLs can be quickly recognized to be associated with X controller. You can confirm this by checking the default patterns.
You can check the product URL pattern in back-office: SEO & URLs or in DispatcherCore class. Anyway, if you want an easy fix I'd suggest making this pattern:
artists/{id_artist}/{rewrite}