Two Yii2 applications Login conflict - authentication

I have two different Yii2 Basic template applications on the same hosting. When I login to the first Yii2 app and then go to login the secon Yii2 app, the first automatically logs out and vice versa. They use different cookieValidationKey in config. How to fix this.

I'm not sure if it fixes your problem but try to change configuration for identityCookie in one of the applications.
'components' => [
// ...
'user' => [
// ...
'identityCookie' => [
'name' => '_identity', // <-- change _identity to something else
'httpOnly' => true
]
]
]

Related

Laravel Multi Roles

I am just new in Laravel 6. I installed the default auth in Laravel. I want to have multiple user.
Admin - This user can monitor everything from the dashboard and other pages.
Maker - This user can create only a job.
Approver - This user can only approve the job but it can't create job.
Viewer - This user can only view all the pages of the application.
Other this I need to auto generate an OPGROUP ID for each companies. And I want to the user who is logged in only see what are the data that is save under their account.
Can you help me to achieve my goal please.
We have to follow the below steps
First, we have to create a table by using the below command in which we will store the roles
php artisan make: migration create_admins_table --create=admins
after creating the migration, we will add the columns
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken(); // I put this linke incase if it was missing
then we will run the migration
php artisan migrate
then we will run the below command
php artisan make: model Admin
do not forget to add the below code in the admin.php model if there aren't
protected $guard = 'admin';
protected $fillable = [
'name', 'email', 'password',
];
we have to set up our guard in the config/auth.php file
right after the guards, we should add the below code
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
'admin-api' => [
'driver' => 'token',
'provider' => 'admins',
],
right after the provider, we should add the below code
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
let's create controller
php artisan make: controller AdminController
let's create a view called admin and route in the web.php
Route::get()'/admin',AdminController#index)
and add the below code in the admin controller
public function __construct()
{
$this->middleware('auth:admin');
}
You can insert user roles in the table (Admin, Maker, Approver, Viewer) and direct it to views that you want

Pretty urls not redirecting in Yii2

I have an action name cart in site controller and I have used pretty URL so my URL is
WWW.test.com/cart
But I want to redirect if someone enter WWW.test.com/cart/,
then it should be redirected to WWW.test.com/cart.
I don't have cart controller.
You need to use UrlNormalizer for that:
'urlManager' => [
// ...
'normalizer' => [
'class' => yii\web\UrlNormalizer::class,
],
],
See documentation about URL normalization for more info.

How to specify new subdirectory in routes for Yii 2 module?

I'm using Yii 2 and building a RESTful API inside a Yii 2 module called apiv1.
The file config.php for the module apiv1 looks like this:
// ...
urlManager' => [
// ...
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => [
'likes',
],
],
],
];
For instance, GET /apiv1/likes works, but I'd like to set up a route to handle GET /api/v1/likes. How can this be done either individually or for the entire module as a general route from api/v1 to apiv1?
You can use the prefix attribute to customize your rest/UrlRule routes.
E.g., for your case, you should be able to do:
[
'class' => 'yii\rest\UrlRule',
'controller' => 'likes',
'prefix' => 'api/v1',
]
For more info, you can see the REST routing guide and yii-rest-rule API docs - in particular, see the $patterns and $extraPatterns properties for additional configuration options.

Routing in module doesn't work Yii 2

I am new in Yii 2 and my problem is about routing inside a module.
I have a module in my app which is a profile cabinet both for users and admins. I created a CabinetController instead of DefaultController and also I created a AdminController and UserController.
What I want? I want this CabinetController received request and forward it to either AdminController or UserController after verify wether the user is admin or not.
In config file I set a default route for module as "cabinet"(as I understand this is a name for default controller). And in "rules" part of UrlManager I wrote following:
'modules' => [
'cabinet' => [
'class' => 'app\modules\cabinet\Module',
'defaultRoute' => 'cabinet'
],
'utility' => [
'class' => 'c006\utility\migration\Module',
],
],
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<_c:\w+>/' => '<_c>/',
'<_c:[\w\-]+>/<_a:\w+>' => '<_c>/<_a>',
'<_m:cabinet>/<_a:\w+>' => '<_a>',
],
],
If I go to "my-site.com/cabinet" it works fine and open "admin/index" because I made it to redirest this request to AdminController/actionIndex, but once I go to somewhere like "my-site.com/cabinet/users" it respond with 404 NotFound. I open the loger and see: exception 'yii\base\InvalidRouteException' with message 'Unable to resolve the request "cabinet/desktop"
This is my CabinetController and the way I forward requests to Admin/UserController:
public function init()
{
parent::init();
$this->user = $this->findModel();
$this->controllerToUse = $this->user->isAdmin() ? 'admin' : 'user';
}
public function actionIndex()
{
return $this->module->runAction($this->controllerToUse . '/' . $this->action->id);
}
If I change defaultAction in CabinetController it run this action normally as expected. Or if I go to "my-site.com/cabinet/admin/users" again it works good, because it found a controller in the url(as I think).
Routing can be a bit tricky in Yii2, it follows a few rules you need to understand which can be found here
But if i understand you correctly Admin/UserController is part of the Cabinet module? and you want Yii to route /cabinet/users to /cabinet/admin/users
You'll need to add some rules in your UrlManager see Rules
Example:
'rules' => [
'<module:cabinet>/<action:\w+>' => '<module>/admin/<action>',
],
Hope it helps

User management hybrid auth setup

I'm trying to setup hybrid auth on the Yii user management addon, the docs are here https://github.com/thyseus/yii-user-management/blob/master/user/docs/hybridauth.txt
according to this step
Take the modules/user/vendors/index.php, rename it to 'hybridauth.php' and place it
beside your application index.php bootstrap script. This will be your hybrid auth
entry script.
for this, there is NO index.php file in modules/user/vendors/index.php but there is one in modules/user/vendors/hybridauth/index.php which i renamed to hybridauth.php and put it in http://localhost/dev/ content of the hybridauth.php are
require_once( "protected/modules/user/vendors/hybridauth/Hybrid/Auth.php" );
require_once( "protected/modules/user/vendors/hybridauth/Hybrid/Endpoint.php" );
Hybrid_Endpoint::process();
now for this
Place the hybrid auth configuration file into your application
protected/config/hybridauth.php.
i took my modules/user/vendors/hybridauth/config.php and put it in protected/config/ and renamed config.php to hybridauth.php. the content looks like this
return
array(
"base_url" => Yii::app()->createAbsoluteUrl('/').'/hybridauth.php',
"providers" => array (
...........
"Google" => array (
"enabled" => true,
"keys" => array (
"id" => "ID",
"secret" => "SECRET",
),
),
"Facebook" => array (
"enabled" => true,
"keys" => array (
"id" => "ID",
"secret" => "SECRET",
),
"scope" => "email,user_birthday" // https://developers.facebook.com/docs/reference/login/
),
),
// if you want to enable logging, set 'debug_mode' to true then provide a writable file by the web server on "debug_file"
"debug_mode" => false,
"debug_file" => "",
);
problem is when i click say on the facebook icon on my login page, it shows my main page. (index.php)
here is the link it directs too
http://localhost/dev/index.php/hybridauth.php?hauth.start=Facebook&hauth.time=1388044835
when i remove the index.php from the url
http://localhost/dev/hybridauth.php?hauth.start=Facebook&hauth.time=1388044835
i get this error
You cannot access this page directly.
any idea what i'm doing wrong? Thanks
This sound like a routing problem in your configuration:
you might want to add a route for hybridauth.php and make sure it is only accessable by GET
for example :
'components'=>array(
......
'urlManager'=>array(
'showScriptName' => false,
'urlFormat'=>'path',
'rules'=>array(
'hybridauth'=>array('user/hybridauth/index', 'verb'=>'GET', 'urlSuffix'=>'.php'),
....
),
),
),
if anyone having the same problem, here is how i solved it:
by changing the base_url to point to the actual php file:
"base_url" => Yii::app()->getBaseUrl(true).'/hybridauth.php',
If you use YUM User Managment try to import
Yii::import('application.modules.profile.models.*');
on YumUserAuthController because profile cannot be found.