How to use authentication and authorization in Yii framework?

WBOY
Release: 2023-06-03 19:42:01
Original
966 people have browsed it

In web applications, authentication and authorization are very important functions. They ensure that only authorized users can access protected resources. The Yii framework provides some built-in authentication and authorization functions to facilitate developers to implement these functions.

Basic Concepts of Authentication and Authorization

In Web applications, authentication is the process of determining who a user is. Typically, users provide a username and password, and after authentication is completed in the background, they are granted access to the application's protected resources. The authorization process is to determine which resources this user can access. Typically, applications assign users to different roles, each with access to different resources.

Authentication and authorization in the Yii framework

The Yii framework provides some built-in authentication and authorization functions that can easily implement these functions. The authentication process is implemented through the identity authenticator (identity class). By default, the Yii framework provides a cookie-based authenticator, which creates a persistent cookie when a user logs in, stores the user's authentication information in it, and uses the cookie to authenticate the user in subsequent requests. identity.

The authorization process is implemented through the access control filter. The Yii framework provides a built-in access controller that can easily define which users can access which resources. Access controllers can be customized through a callback function to better meet the needs of the application.

Using the Authenticator of the Yii Framework

To use the Authenticator of the Yii Framework, you need to create an authenticator class. The authenticator class must inherit from the yiiwebIdentityInterface interface and implement the methods of this interface. Among them, the most important methods are findIdentity() and getId().

The findIdentity() method is used to find the user in the authenticator. This method allows you to verify the username and password provided by the user and returns an authentication information object. The getId() method is used to return the user ID in the authentication information object.

The following is a sample authenticator class:

namespace appmodels;

use Yii;
use yiidbActiveRecord;
use yiiwebIdentityInterface;

class User extends ActiveRecord implements IdentityInterface
{
    // other code

    public static function findIdentity($id)
    {
        return static::findOne(['id' => $id]);
    }

    public function getId()
    {
        return $this->id;
    }

    // other code
}
Copy after login

Once you have finished creating the authenticator class, you can configure it as an application component:

'components' => [
    // ...
    'user' => [
        'identityClass' => 'appmodelsUser',
        // other configuration
    ],
    // ...
],
Copy after login

Now you can use the Yii::$app->user component to authenticate the user:

// 获取当前已登录的用户对象
$user = Yii::$app->user->identity;

// 登录用户
if ($user->validatePassword($password)) {
    Yii::$app->user->login($user);
}

// 登出用户
Yii::$app->user->logout();
Copy after login

Using the Yii Framework’s Access Controller

To use the Yii Framework’s Access Controller, You need to add a behavior in your controller. The behavior is an instance of the Yii iltersAccessControl class and can be configured in the controller's behaviors() method.

The access controller will determine whether the user has permission to access the current request through a callback function. The callback function should return a Boolean value indicating whether access to the request is allowed.

The following is a sample controller:

namespace appcontrollers;

use Yii;
use yiiwebController;
use yiiiltersAccessControl;

class SiteController extends Controller
{
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
        ];
    }

    // other code
}
Copy after login

In the above example, we define an access rule, indicating that only logged in users can access the controller. You can define multiple rules to better define which users have access to which resources.

Summary

Authentication and authorization functions are very important functions in web applications. The Yii framework provides some built-in authentication and authorization functions to facilitate developers to implement these functions. Using the Yii framework's authenticators and access controllers, you can easily authenticate users and control their access to protected resources.

The above is the detailed content of How to use authentication and authorization in Yii framework?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!