yiisoft/user

dev-master / 3.0.x-dev 2021-09-07 20:55 UTC

This package is auto-updated.

Last update: 2021-09-07 21:01:15 UTC


README

68747470733a2f2f796969736f66742e6769746875622e696f2f646f63732f696d616765732f7969695f6c6f676f2e737667

Yii User


Latest Stable Version Total Downloads Build status Scrutinizer Code Quality Code Coverage Mutation testing badge static analysis

The package handles user-related functionality:

  • Login and logout.
  • Getting currently logged in identity.
  • Changing current identity.
  • Access checking for current user.
  • Auto login or "remember me" based on request cookie.

Requirements

  • PHP 7.4 or higher.

Installation

The package could be installed with composer:

composer require yiisoft/user --prefer-dist

General usage

Working with identity

...

Auto login through cookie

In order to log user in automatically based on request cookie presence, use CookieLoginMiddleware.

By default, you should set cookie for auto login manually in your application after logging user in:

public function login(
    \Psr\Http\Message\ServerRequestInterface $request,
    \Psr\Http\Message\ResponseFactoryInterface $responseFactory,
    \Yiisoft\User\Login\Cookie\CookieLogin $cookieLogin
): \Psr\Http\Message\ResponseInterface {
    $body = $request->getParsedBody();
    
    // Get user identity in based on body contents,
    // log user in.
    
    $response = $responseFactory->createResponse();
    if ($body['rememberMe'] ?? false) {
        $response = $cookieLogin->addCookie($identity, $response);
    }
    return $response;
}

In the above rememberMe in the body may come from a "remember me" checkbox in the form. End user decides if he wants to be logged in automatically. If you do not need the user to be able to choose and want to always use "remember me", you can enable it via params.php:

return [
    'yiisoft/user' => [
        'cookieLogin' => [
            'addCookie' => true,
        ],
    ],
];

Testing

Unit testing

The package is tested with PHPUnit. To run tests:

./vendor/bin/phpunit

Mutation testing

The package tests are checked with Infection mutation framework. To run it:

./vendor/bin/infection

Static analysis

The code is statically analyzed with Psalm. To run static analysis:

./vendor/bin/psalm

License

The Yii User is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.

Maintained by Yii Software.