I am trying to setup Yii2 advanced like a traditional user/admin system. Frontend would be /user and backend would be /admin, and would use their respective table in the database (user and admin). I have not renamed frontend and backend to user and admin yet..
Using migrate generated the 'user' table, with all it's fields. I registered to create a new user, all that works perfect. I then copied the 'user' table and named it 'admin', and changed the username to admin. I can change the password, or truncate it, register new admin user, then remove the registration from the backend later. The admin table in the db itself isn't the issue as I am not getting that far when I reach the error..
I have setup and used Yii2 advanced just fine on the frontend (user) side of it. Of course, you have Yii::$app->user and it works just fine on the frontend. I can login, it uses the 'users' table. Frontend works great...
Now on the backend (admin) I need it to use the 'admin' table. I know you specify the table to use in the model. I copied /common/models/User.php and have /common/models/Admin.php and updated the function to use the 'admin' table instead.
I also copied /vendor/yiisoft/yii2/web/User.php and put it in /common/models/web/Admin.php (and renamed the name of the class from User to Admin)
Then I edited the /backend/config/main.php to reflect the changes for Admin (class and identityClass).
/backend/config/main.php
'components' => [
'admin' => [
'identityClass' => 'common\models\Admin',
'class' => 'common\models\web\Admin',
'enableAutoLogin' => true,
],
],
/common/models/web/Admin.php
class Admin extends Component { ... }
/common/models/Admin.php
class Admin extends ActiveRecord implements IdentityInterface {
public static function tableName()
{
return '{{%admin}}';
}
}
Error: User::identityClass must be set. <-- As you can see, it's still references the User model some how...
Also, when I get this setup, would I use Yii::$app->admin instead of Yii::$app->user ? Like for checking if they are logged in using isGuest.
I want to be sure that a user can't login to frontend, then manually go to backend and be logged in!
I have solved this :)
You have to edit the main config of each (frontend and backend) and specify the 'identityClass' for the user component, and add 'session' and 'request' to the list.
Example of frontend config:
'components' => [
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'identityCookie' => [
'name' => '_frontendUser', // unique for frontend
]
],
'session' => [
'name' => 'PHPFRONTSESSID',
'savePath' => sys_get_temp_dir(),
],
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => '[RANDOM KEY HERE]',
'csrfParam' => '_frontendCSRF',
],
],
Example of backend config:
'components' => [
'user' => [
'identityClass' => 'common\models\Admin',
'enableAutoLogin' => true,
'identityCookie' => [
'name' => '_backendUser', // unique for backend
]
],
'session' => [
'name' => 'PHPBACKSESSID',
'savePath' => sys_get_temp_dir(),
],
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => '[DIFFERENT UNIQUE KEY]',
'csrfParam' => '_backendCSRF',
],
],
For a more detailed guide, you can read the wiki I created.
Wiki: [Guide] How to actually separate Frontend and Backend on Yii2 Advanced
Related
excuse me I want to ask, about how to make permission rules from my user table in yii2 framework,
which I have a table named user.in it contains a column of positions with the contents "user" and "admin" I want to set that when logging in only my admin can upload files.
I want to know how to solve it??
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::class,
'rulesConfig' => ['class'=> AccessRule::className()
],
'only' => ['upload'],
'rules' => [
[
'allow' => true,
// 'actions' => ["upload"],
'roles' => ['#'],
],
],
],
];
}
There is the possibility to use the PHPManager, which saves the roles within files.
Or the DBManager, which brings its own database tables where it stores the things.
There is a wonderful article by Yii that might help you, it explains everything in detail.
Link to Guide
i am new in yii2 and using advance template .
i am using yii2/admin for roles , permission. but i can't get the menu manage using yii2/admin
the image for the menu manage
like this
how to get this interface for manage menu
i read this
when i run
(index.php?r=admin/menu)
i got an error
Invalid Configuration – yii\base\InvalidConfigException
The table does not exist: {{%menu}}
how to create menu table.
i got a migration file from
vendor/mdmsoft/yii2-admin/migrations/m140602_111327_create_menu_table.php
how to run this migration
please go through with this link MDM Yii2-admin Basic Configuration
add in backend/congif/main.php
'modules' => [
'admin' => [
'class' => 'mdm\admin\Module',
'layout' => 'left-menu', // it can be '#path/to/your/layout'.
'controllerMap' => [
'assignment' => [
'class' => 'mdm\admin\controllers\AssignmentController',
'userClassName' => 'common\models\User',
'idField' => 'id'
],
],
'menus' => [
'assignment' => [
'label' => 'Grand Access' // change label
],
'route' => null, // disable menu route
]
],
],
for run migration use this, through cmd/terminal
yii migrate --migrationPath=#mdm/admin/migrations
I created a module in yii2 under api/modules/v1 folder, the problem is when I request:
Yii::$app->user->login($user, 3600);
The identity got saved temporary until I refresh the page or I request another url, When I reviewed the log file, I found this :
User '1' logged in from ::1. Session not enabled.
Also I tried to add :
'enableAutoLogin' => true,
'enableSession' => true,
user component configuration :
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'enableSession' => true,
'identityCookie' => [
'name' => '_APIUser', // unique for backend
'path' => '/api/web/v1' // correct path for the backend app.
]
],
'session' => [
'name' => '_apiSessionId', // unique for frontend
'savePath' => __DIR__ . '/../runtime', // a temporary folder on frontend
],
But the same problem occurred.
Have you model common\models\User implements "login" method? If so, login with this code
Yii::$app->user->identity->login($user, 3600);
it seems like you are configuring 'common\config\main' as you have indicated frontend and backend session details in the same config.
Leave the common\config\main blank and you have to configure your backend\config\main and frontend\config\main separately. Your login code should work.
Module section config
'user' => [
'class' => 'dektrium\user\Module',
'modelMap' => [
'User' => 'app\models\DL\User',
'registrationForm' => 'app\models\DL\registrationForm',
],
'controllerMap' => [
/*'registration' => 'app\controllers\user\RegistrationController',
'admin' => 'app\controllers\user\AdminController'*/
],
'layout' => '#app/views/layouts/container',
'defaultRoute' => 'profile',
'admins' => ['admin'],
'enableFlashMessages' => false,
'params' => [
'menuItems' => [
'label' => 'Users',
'url' => ['/user/admin']
]
]
],
Yii console application (./yii) showing me error
'Calling unknown method:
app\controllers\user\AdminController::getHelpSummary()'
If I uncomment the controllerMap section, I can't understand why it autoloads in console app if my AdminController extends web controller not console.
This is commands from user module.
Do you really need the user module in console?
Yii2 console and web applications have separated configuration files by default. If you changed this default and use the same config for both of them, you must take care about consistency.
You can check the list of loaded configs in ./yii.
You need to specify a valid defaultRoute for the console application.
With 'defaultRoute' => 'profile', ./yiimay try to load a Controller which requires the user module.
Try adding it in the console configuration.
In my Yii2 application I'm trying to force all users to be authenticated. If they're not already authenticated they should be redirected to the login page.
In Yii1 I did this by creating a class that would check if a user was logged in and attaching that class to the onBeginRequest behavior in my main config file.
// Yii 1
'behaviors' => array(
'onBeginRequest' => array(
'class' => 'application.components.RequireLogin',
)
),
How can I get the same behavior in Yii2? I know I can use behavior to do this, but I wan't to add this behavior to my main config file so all requests are first checked for authentication.
The working behaviors method looks like this:
// Yii2
public function behaviors() {
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['login', 'error'],
'allow' => true,
],
[
'allow' => true,
'roles' => ['#'],
],
],
],
];
}
Ok, so I had to add the following code below 'components' => [...]
'as beforeRequest' => [
'class' => 'yii\filters\AccessControl',
'rules' => [
[
'actions' => ['login', 'error'],
'allow' => true,
],
[
'allow' => true,
'roles' => ['#'],
],
],
],
Read more about the format: http://www.yiiframework.com/doc-2.0/guide-concept-configurations.html#configuration-format
I'm actually not versed into Yii2 (but very much so into Yii1).
One solution that can be employed in Yii1 and I guess also in Yii2 is having a filter method in a master Controller class. Typically a single controller serves as a master controller. If you don't have one, create it and everyone should extend it. You can implement this probably not as a filter but in other methods of this 'master controller' (init() ?)
If all activity is going through controller class then you're set.