Add multiple tables for login in web.config - mysql

I developing a site with two different registrations, and I have 2 different table, One is user and 2nd is admin.In the web.config how to I can set both tables in the components section. For this I am tried this my code is that :-
'user' => [
'identityClass' => 'app\models\Users',
'enableAutoLogin' => true,
],
'admin' => [
//'class' => 'yii\web\User'
'identityClass' => 'app\models\adminUser',
'enableAutoLogin' => true,
],
But when run run my application its showing error message erro message is that :-
Invalid Configuration – yii\base\InvalidConfigException
I also tried this multiple user identity in config Yii2 when I add class its showing blank page.
Thank you

Related

yii2 - Keep user logged in when website is idle

The user appears to be automatically logged out if the website has been idle for about an hour. Is it possible that they are not logged out for at least a month.
I've tried changing the parameters.
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => false,
'authTimeout' => 3600*24*30,
],
'session'=>[
'class' => 'yii\web\Session',
'timeout' => 3600*24*30,
],
I've tried session php.ini parameters:
And I've tried setting the login parameters:
Yii::$app->user->login($this->getUserById($userId), 3600*24*30);

Yii2 mail catcher [duplicate]

This question already has an answer here:
Override Yii2 Swiftmailer Recipient
(1 answer)
Closed 5 years ago.
During the development and testing, I want the Yii2 mailer (swiftmailer) not to send emails to actual addresses, but replace it with developers` emails. Is there any setting in config to do it?
Just set the useFileTransport to true in component configuration for the development environment and all emails will not be sent but will be saved as files so you can easily test everything.
You should set configs for prod and dev environment.
Path for dev config (if you have advanced application template): yourProject/environments/dev/common/config/main-local.php and for prod: yourProject/environments/prod/common/config/main-local.php.
You may use EmailTarget class for logs.
Example of config for dev environment to reach the clue:
return [
'bootstrap' => ['log'],
'components' => [
...
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
'useFileTransport' => true,//set this property to false to send mails to real email addresses
//comment the following array to send mail using php's mail function
],
'log' => [
'targets' =>
[
[
'class' => 'yii\log\EmailTarget',
'levels' => ['error'],
'except' => ['yii\web\HttpException:404'],
'message' => [
'to' => ['example#mail.ru'],
'from' => ['yourproject#mail.ru'],
'subject' => ' Errors ',
]
],
],
],
],
];
And similarly for prod environment but with different emails.
If you don't want to use EmailTarget class you may just set your emails for dev in params config here: yourProject/environments/dev/common/config/params-local.php.
And path for prod: yourProject/environments/prod/common/config/params-local.php
Params config example:
return [
'sendToEmails' => ['email1#mail.ru', 'email2#mail.ru']
];
And then you may use the variable in your code this way: Yii::$app->params['sendToEmails'] to get the array of emails to send messages to.
Don't forget to do php init command in your project after compliting your configs.
You may see the detailed docs about environments here.

http 403 exception not displaying properly

I have implemented a set of rules but when a user attempts to access a restricted area, I am getting a text output instead of a nice boostrap alert. How can I get this working again?
Rule:
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['index', 'view'],
'rules' => [
[
'roles' => ['#'],
'actions' => ['index', 'view'],
'allow' => true,
],
],
],
Exception message:
An Error occurred while handling another error:
exception 'yii\web\ForbiddenHttpException' with message 'You are not allowed to perform this action!.' in ###\vendor\yiisoft\yii2\filters\AccessControl.php:151
Stack trace:...
Thanks Bizley for your help. I followed your instructions and discovered that I rather stupidly copied my rules to my site/error page meaning that I accidentally blocked the ability to see the error page!!! I removed the offending rule and all is working.

Yii 2 - Exception error with message 'Login Required'

An Error occurred while handling another error:
exception 'yii\web\ForbiddenHttpException' with message 'Login
Required' in
C:\wamp\www\k\kometonline\vendor\yiisoft\yii2\web\User.php:431
Am getting this error after installing RBAC in backend admin login page (site.com/backend/web/site/login). Whats the main cause of this problem. I don't know what code to post. Please comment below If you need any code. Thanks in advance.
I encountered the same error while installing RBAC in backend admin login page while following this tutorial:
RBAC Super Simple with Admin and User
You may try doing the changes you made at the frontend login SiteController and see if works. The difference between these two SiteControllers is that the frontend already uses access rules in its behavior method.
From there you can compare the SiteControllers at backend and frontend and see what makes it work. In my case I simply added one line
'only' => ['logout'],
just below
'class' => AccessControl::className(),
and it worked!
While I know the solution worked for the OP and is a few years old, I wanted to post how I solved this for myself, in the hope of offering an alternate solution.
In my case, I specified the following actions
'rules' => [
[
'actions' => ['logout', 'index',],
'allow' => true,
'roles' => ['#'],
],
This specified that the index and logout was protected by a password, The
*"'roles' => ['#']"*
say that only authenticated users can invoke these actions.
Therefore, when my application restarted, it tried to direct to the login action and error presented. I solved this by specifying a rule for non logged in users (a.k.a guests) by specifying the role
*"'roles' => ['?']"*
My behavior method therefore changed to
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['logout', 'index',],
'allow' => true,
'roles' => ['#'],
],
[
'actions' => ['login'],
'allow' => true,
'roles' => ['?'],
],
],
],
];
}

Yii2 advanced accessing frontend from backend using alias

I trying to access frontend/web from backend menu using alias
yii:yii2 advanced
webserver : XAMPP
IDE:net beans
codes I modified:
C:\xampp\htdocs\advanced\common\config\aliases.php
Yii::setAlias('fronthome', dirname(dirname(__DIR__)) . '/frontend/web/');
C:\xampp\htdocs\advanced\backend\views\layouts\main.php
if (Yii::$app->user->isGuest) {
$menuItems[] = ['label' => 'Login', 'url' => ['/site/login']];
$menuItems[] = ['label' => 'fronthome', 'url' => Yii::getAlias('#fronthome')];
but when accessing "fronthome" menu via backend menu; browser was returning the url:
http://localhost/advanced/backend/web/C:/xampp/htdocs/advanced/frontend/web
what i wanted browser to give:
http://localhost/advanced/frontend/web/
can some one please put some light... I searched but there was no elegant solution which I could find via alias.
Thanks
I got it working by making the below change
C:\xampp\htdocs\advanced\common\config\aliases.php
Yii::setAlias('fronthome', dirname(dirname(__DIR__)) . '/frontend/web/');
to
Yii::setAlias('fronthome', '../../frontend/web/');
As the links in the comments are outdated here is a snippet from the Yii2 docs:
Creating links from backend to frontend
Often it's required to create links from the backend application to the frontend application. Since the frontend application may contain its own URL manager rules you need to duplicate that for the backend application by naming it differently:
return [
'components' => [
'urlManager' => [
// here is your normal backend url manager config
],
'urlManagerFrontend' => [
// here is your frontend URL manager config
],
],
];
//After it is done, you can get an URL pointing to frontend like the following:
echo Yii::$app->urlManagerFrontend->createUrl(...);
current link to copy of relevant doc: https://yii2-framework.readthedocs.io/en/stable/guide/tutorial-advanced-app/
The way I have implemented this is to add a switch to my backend/config/bootstrap.php
switch (YII_ENV) {
case 'dev':
$frontend = 'https://my-domain.lan/';
break;
case 'test':
$frontend = 'https://my-domain.test/';
break;
case 'prod':
$frontend = 'https://my-domain.com/';
break;
}
and then in my backend/config/main.php I added the following
'urlManagerFrontend' => [
'class' => UrlManager::className(),
'enablePrettyUrl' => true,
'showScriptName' => false,
'baseUrl' => $frontend,
'rules' => [
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
]
],
and then where I want to use the frontend link:
echo Html::a('link',Yii::$app->urlManagerFrontend->createUrl('controller/view'))
Try using params.php
Add a key on params.php on your common/config folder
'frontendUrl' => 'http://frontendUrl/',
Then you could access it anywhere on your view by:
<?= Yii::$app->params['frontendUrl'] ?>
Hope this helps :)