Giter Club home page Giter Club logo

yii2-user's Introduction

Yii 2 User

Yii 2 User - User authentication module

Notes

2014/6/14 - Yii 2 has recently implemented a breaking change which may affect the logout functionality. If you are getting a "Method Not Allowed - #405", you will need to add <?= Html::csrfMetaTags() ?> to the <head> section in your main layout file. See here and here.

2014/4/28 - I have just released 2.0.0-alpha. This release is basically a code overhaul and does not contain any functionality changes.

IF THIS BROKE YOUR APP, the quick fix is to limit your version via composer: "amnah/yii2-user": "1.0.0@beta". Otherwise, please see the Upgrade Notes to sync your app with this latest version.

Demo

Features

  • Quick setup - works out of the box so you can see what it does
  • Easily extendable
  • Registration using email and/or username
  • Login using email and/or username
  • Email confirmation (+ resend functionality)
  • Social authentication (facebook, twitter, google, linkedin, reddit)
  • Account page
    • Updates email, username, and password
    • Requires current password
  • Profile page
    • Lists custom fields for users, e.g., full_name
  • Password recovery
  • Admin crud via GridView

Installation

  • Install Yii 2 using your preferred method
  • Install package via composer "amnah/yii2-user": "~2.0"
  • Update config file config/web.php and config/db.php
// app/config/web.php
return [
    'components' => [
        // NOTE: in the yii2-advanced-app, the user component should be updated in
        // 'frontend/config/main.php' and/or 'backend/config/main.php' (OR you can add it
        // to 'common/config' if you remove it from frontend/backend)
        'user' => [
            'class' => 'amnah\yii2\user\components\User',
        ],
        'mail' => [
            'class' => 'yii\swiftmailer\Mailer',
            'useFileTransport' => true,
            'messageConfig' => [
                'from' => ['[email protected]' => 'Admin'], // this is needed for sending emails
                'charset' => 'UTF-8',
            ]
        ]
    ],
    'modules' => [
        'user' => [
            'class' => 'amnah\yii2\user\Module',
            // set custom module properties here ...
        ],
    ],
];
// app/config/db.php
return [
    'class' => 'yii\db\Connection',
    // set up db info
];
  • Run migration file
    • php yii migrate --migrationPath=@vendor/amnah/yii2-user/migrations
  • Go to your application in your browser
    • http://localhost/pathtoapp/web/user
  • Log in as admin using neo/neo (change it!)
  • Set up module properties as desired
  • Optional - Update the nav links in your main layout app/views/layouts/main.php
// app/views/layouts/main.php
<?php
'items' => [
    ['label' => 'Home', 'url' => ['/site/index']],
    ['label' => 'About', 'url' => ['/site/about']],
    ['label' => 'Contact', 'url' => ['/site/contact']],
    ['label' => 'User', 'url' => ['/user']],
    Yii::$app->user->isGuest ?
        ['label' => 'Login', 'url' => ['/user/login']] :
        ['label' => 'Logout (' . Yii::$app->user->displayName . ')',
            'url' => ['/user/logout'],
            'linkOptions' => ['data-method' => 'post']],
],

Release Notes (Upgrade Notes)

  • 2014/6/12 - Release 2.1.0-alpha2
  • 2014/5/19 - Release 2.1.0-alpha
  • 2014/4/28 - Release 2.0.0-alpha
  • 2014/4/17 - Release 1.0.0-beta

Development Notes

How do I check user permissions?

This package contains a custom permissions system. Every user has a role, and that role has permissions in the form of database columns. It should follow the format: can_{permission name}.

For example, the role table has a column named can_admin by default. To check if the user can perform admin actions:

if (!Yii::$app->user->can("admin")) {
    throw new HttpException(403, 'You are not allowed to perform this action.');
}
// --- or ----
$user = User::findOne(1);
if ($user->can("admin")) {
    // do something
};

Add more database columns for permissions as needed. If you need something more powerful, look into setting up [RBAC] (https://github.com/yiisoft/yii2/blob/master/docs/guide/authorization.md).

Note: If you set up an authManager component for RBAC, then Yii::$app->user->can() will use that instead of this module's custom role table.

How do I add captcha to the forms?

Check out this great 3-step guide by dektrium. (Please note that the scenarios for the validation rules will depend on your project requirements.)

How do I extend this package?

You can extend the classes directly. Depending on which ones you need, set the proper config property:

// app/config/web.php
'components' => [
    'user' => [
        'class' => 'app\components\MyUser',
    ],
],
'modules' => [
    'user' => [
        'class' => 'app\modules\MyModule',
        'controllerMap' => [
            'default' => 'app\controllers\MyDefaultController',
        ],
        'modelClasses'  => [
            'Profile' => 'app\models\MyProfile',
        ],
        'emailViewPath' => '@app/mail/user', // example: @app/mail/user/confirmEmail.php
    ],
],

For view files, you can use the theme component.

// app/config/web.php
'components' => [
    'view' => [
        'theme' => [
            'pathMap' => [
                '@vendor/amnah/yii2-user/views' => '@app/views/user', // example: @app/views/user/default/login.php
            ],
        ],
    ],
],

I need more control. Can I just extend the whole thing?

You can always fork the package and modify it as needed.

Or, if you want, you can integrate the package directly into your app by copying the files. This would make it more difficult to get updates, but it also guarantees that your app won't break after running composer update.

To do so, you can use the helper command CopyController.

  • Add the module to your config/console.php to gain access to the command (Note: this is CONSOLE config)
// app/config/console.php
'modules' => [
    'user' => [
        'class' => 'amnah\yii2\user\Module',
    ],
],
php yii user/copy --from=@vendor/amnah/yii2-user --to=@app/modules/user --namespace=app\\modules\\user
  • Update config to point to your new package
// app/config/web.php + app/config/console.php
'modules' => [
    'user' => [
        'class' => 'app\modules\user\Module',
    ],
],

Alternatively, you can do this manually. Just copy/paste the files wherever you'd like and change the namespaces in the files. Replace amnah\yii2\user with app\modules\user.

Todo

yii2-user's People

Contributors

amnah avatar jilizart avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.