2017 © Pedro Peláez
 

yii2-extension yii2-user-module

Yii2 user module

image

yiisolutions/yii2-user-module

Yii2 user module

  • Tuesday, December 20, 2016
  • by memclutter
  • Repository
  • 1 Watchers
  • 0 Stars
  • 3 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

yii2-user-module

Latest Stable Version Total Downloads Build Status codecov License, (*1)

Yii2 user module., (*2)

Installation

Use composer, (*3)

composer require "yiisolutions/yii2-user-module: @dev"

or add to composer.json, (*4)

{
  "require": {
    "yiisolutions/yii2-user-module": "@dev"
  }
}

Configuration

For enable user module edit your configuration, (*5)

<?php

return [
    // ...
    'modules' => [
        // ...
        'user' => [
            'class' => 'yiisolutions\user\Module',
        ],
        // ...
    ],
    // ...
];

Console commands

This module provider console commands for manager users, (*6)

  • user/commands/create - create new user
  • user/commands/truncate - clear user table

Web controller actions

This module provide web controller actions:, (*7)

  • yiisolutions\user\actions\LoginAction - for user login
  • yiisolutions\user\actions\LogoutAction - for logout
  • yiisolutions\user\actions\SignUpAction - for sign up new user

For enable these actions use controller actions() method, (*8)

<?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 ...
        }
    }
}

The Versions

20/12 2016

dev-master

9999999-dev

Yii2 user module

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Memory Clutter

user yii2 module