In Yii2, access control is commonly implemented using Access Control Filters (ACFs) or the Role-Based Access Control (RBAC) system. These mechanisms define rules to restrict access to certain actions or controllers unless the user is authenticated.
Here’s how Yii2 typically directs a user to the login page if authentication is required:
Using Access Control Filter (ACF)
-
Define Access Rules:
You define access rules within the behaviors method of a controller. The AccessControl component is utilized for this.
-
Behavior Method Implementation:
In the controller, you specify the AccessControl filter and define rules that restrict certain actions to authenticated users.
Here is an example implementation in a controller:
namespace app\controllers;
use yii\web\Controller;
use yii\filters\AccessControl;
class SiteController extends Controller
{
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::class,
'rules' => [
[
'allow' => true,
'actions' => ['login', 'error'],
'roles' => ['?'], // ? means guest users
],
[
'allow' => true,
'actions' => ['logout', 'index'],
'roles' => ['@'], // @ means authenticated users
],
],
],
];
}
public function actionLogin()
{
// Login logic here
}
public function actionLogout()
{
// Logout logic here
}
public function actionIndex()
{
// Restricted content for authenticated users
}
}
Flow of Execution:
-
Incoming Request:
The user requests a page or an action.
-
Access Control Check:
Yii2 evaluates the access rules defined in the behaviors method of the requested controller.
-
Rule Matching:
- If the user’s role matches the rule (e.g., they are authenticated and trying to access
index), the controller action is executed.
- If the user’s role does not match (e.g., they are not authenticated and trying to access
index), Yii2 will redirect the user to the login page.
-
Redirection to Login Page:
When a guest user attempts to access a restricted action, Yii2 redirects them to the login page based on the LoginUrl configuration value. By default, this is set in the application’s configuration file (config/web.php):
'components' => [
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
'loginUrl' => ['site/login'], // default loginUrl configuration
],
// other components
],
When the user attempts to access a restricted page:
- Yii2 checks the access rules.
- If access is denied, Yii2 will redirect the user to the route specified in
loginUrl.
Customization:
You can customize the login URL by modifying the loginUrl property in your application’s configuration:
'components' => [
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
'loginUrl' => ['custom/login'], // custom loginUrl
],
],
Using Role-Based Access Control (RBAC)
If you prefer using RBAC, you can set it up and define permissions and roles in more detail. RBAC allows more fine-grained control compared to the simple ACF approach. However, the process of redirecting unauthenticated users to the login page follows a similar principle.
Summary
Yii2 utilizes the AccessControl filter to check if a user trying to access a restricted action is authenticated. If not, it redirects them to the login page based on the loginUrl configuration. This behavior is configured and customized within the application configuration and controller’s behavior method.