yiisolutions / yii2-user-module
Yii2 user module
Installs: 9
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Type:yii2-extension
Requires
- yiisoft/yii2: ~2.0.0
Requires (Dev)
- phpunit/phpunit: 5.5.*
This package is not auto-updated.
Last update: 2024-11-09 20:45:21 UTC
README
Yii2 user module.
Installation
Use composer
composer require "yiisolutions/yii2-user-module: @dev"
or add to composer.json
{ "require": { "yiisolutions/yii2-user-module": "@dev" } }
Configuration
For enable user module edit your configuration
<?php return [ // ... 'modules' => [ // ... 'user' => [ 'class' => 'yiisolutions\user\Module', ], // ... ], // ... ];
Console commands
This module provider console commands for manager users
user/commands/create
- create new useruser/commands/truncate
- clear user table
Web controller actions
This module provide web controller actions:
yiisolutions\user\actions\LoginAction
- for user loginyiisolutions\user\actions\LogoutAction
- for logoutyiisolutions\user\actions\SignUpAction
- for sign up new user
For enable these actions use controller actions()
method
<?php namespace app\controllers; use Yii; use yii\web\Controller; use yii\web\IdentityInterface; use yiisolutions\user\actions\LoginAction; use yiisolutions\user\actions\LogoutAction; use yiisolutions\user\actions\SignUpAction; use yiisolutions\user\events\LoginEvent; use yiisolutions\user\models\LoginFormInterface; class AccountController extends Controller { public function actions() { return [ 'login' => [ 'class' => LoginAction::className(), 'view' => 'login', // use @app/views/account/login.php view file 'on loginSuccess' => [$this, 'onLoginSuccess'], // alternative success callback (default redirect to back) 'on loginFailed' => [$this, 'onLoginFailed'], // do something when login failed (for example, logging) ], 'logout' => [ 'class' => LogoutAction::className(), ], 'sign-up' => [ 'class' => SignUpAction::className(), ], ]; } /** * Run when login success. */ public function onLoginSuccess(LoginEvent $event, LoginFormInterface $model) { // do something ... $username = $model->getUserIdentity()->username; Yii::info("User '{$username}' logged in"); // override default action return value $event->return = $this->redirect('/profile'); } /** * Run when login error. */ public function onLoginFailed(LoginEvent $event, LoginFormInterface $model) { // do something ... $user = $model->getUserIdentity(); if ($user instanceof IdentityInterface) { // do something, when exists user fail login ... } else { // do something, else ... } $user = $model->getUserIdentity(); if ($user) { // send email notification, increment attempt counter etc ... } } }