2017 © Pedro Peláez
 

yii2-extension yii2-cms-user

Flexible user registration and authentication module for Yii2

image

infoweb-internet-solutions/yii2-cms-user

Flexible user registration and authentication module for Yii2

  • Thursday, October 20, 2016
  • by infoweb-internet-solutions
  • Repository
  • 4 Watchers
  • 3 Stars
  • 846 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 3 Forks
  • 3 Open issues
  • 7 Versions
  • 0 % Grown

The README.md

CMS user module for Yii 2

Docs

Installation

The preferred way to install this extension is through composer., (*1)

Either run, (*2)

php composer.phar require infoweb-internet-solutions/yii2-cms-user "*"

or add, (*3)

"infoweb-internet-solutions/yii2-user": "*"

to the require section of your composer.json file., (*4)

Usage

Once the extension is installed, simply modify your backend configuration as follows:, (*5)

return [
    'components' => [
        ...
        // Replace default user component:
        'user' => [
            'identityClass' => 'infoweb\user\models\User',
            'enableAutoLogin' => true,
        ],
        // Add to views
        'view' => [
            'theme' => [
                'pathMap' => [
                    ...
                    '@dektrium/user/views' => '@infoweb/user/views'
                ]
            ]
        ],
    ],
    ...
    'modules' => [
        'user' => [
            'class' => 'dektrium\user\Module',
            'enableUnconfirmedLogin' => true,
            'confirmWithin' => 21600,
            'cost' => 12,
            'admins' => ['infoweb', 'admin'],
            'modelMap' => [
                'User' => 'infoweb\user\models\User',
                'UserSearch' => 'infoweb\user\models\UserSearch',
                'Profile' => 'infoweb\user\models\Profile',
            ],
            'controllerMap' => [
                'admin' => 'infoweb\user\controllers\AdminController',
                'settings' => 'infoweb\user\controllers\SettingsController',
                'security' => 'infoweb\user\controllers\SecurityController',
            ],
            'modules' => [
                // Register the custom module as a submodule
                'infoweb-user' => [
                    'class' => 'infoweb\user\Module'
                ]
            ]
        ],
    ],
    ...
    'as access' => [
        'class' => 'infoweb\user\components\AccessControl',
        'user' => 'infoweb\user\models\WebUser',
        'allowActions' => [
            'user/recovery/*',
            'user/security/logout',
            'user/registration/*'
        ],
    ],
];

To use the module, execute yii migration, (*6)

yii migrate/up --migrationPath=@vendor/infoweb-internet-solutions/yii2-cms-user/migrations
yii migrate/up --migrationPath=@vendor/dektrium/yii2-user/migrations

Separate frontend and backend user

If you want to use separate sessions for users of the frontend and backend application, a couple of configurations have to be updated., (*7)

  1. Bootstrap the session component in backend/config/main.php, (*8)

    'bootstrap' => ['session'...],
    
  2. Set the identityCookie of the user component and update the request and session components in backend/config/main.php, (*9)

    'components' => [
       ...
       'user' => [
           ...          
           'identityCookie' => [
               'name' => '_backendIdentity',
               'path' => '/admin',
               'httpOnly' => true,
           ],
       ],
       ...
       'request' => [
            'class' => 'common\components\Request',
            'web'=> '/backend/web',
            'adminUrl' => '/admin',
            'csrfParam' => '_backendCSRF',
        ],
        'session' => [
            'name' => 'PHPSESSID',
            'cookieParams' => [
                'httpOnly' => true,
                'path' => '/admin',
            ],
        ],
        ...
    ]
    
  3. Bootstrap the session component in frontend/config/main.php, (*10)

    'bootstrap' => ['session'...],
    
  4. Update the user, request and session components in frontend/config/main.php, (*11)

    ...
    'user' => [
       'identityClass' => 'infoweb\user\models\frontend\User',
       'enableAutoLogin' => true,
       'identityCookie' => [
           'name' => '_frontendIdentity',
           'path' => '/',
           'httpOnly' => true,
       ],
    ],
    'request'=>[
       'class' => 'common\components\Request',
       'web' => '/frontend/web',
       'csrfParam' => '_frontendCSRF',
    ],
    'session' => [
       'name' => 'PHPFRONTSESSID',
       'cookieParams' => [
           'httpOnly' => true,
           'path' => '/',
       ],
    ],
    ...
    
  5. At this point you can implement the models/frontend/LoginForm.php and models/frontend/SignupForm.php models and create views and controller actions for them., (*12)

  6. Some sort of access control has to be implemented in your frontend controllers to determine which actions are allowed for a frontend user. This can be done through a rbac role of by implementing an access filter as a behavior., (*13)

public function behaviors()
{
    return [
        'access' => [
            'class' => AccessControl::className(),
            'rules' => [                    
                // Login and signup pages are accessible for guests
                [
                    'actions' => ['login','signup', 'request-password-reset'],
                    'allow' => true,
                    'roles' => ['?','@'],
                ],
                // Logout page is accessible for authenticated users
                [
                    'actions' => ['logout'],
                    'allow' => true,
                    'roles' => ['@'],
                ],
                // These actions are accessible for authenticated users
                [
                    'actions' => [...],
                    'allow' => true,
                    'roles' => ['@'],
                ],
                ...
            ],
            // If access is denied, redirect to the login page
            'denyCallback' => function ($rule, $action) {
                $this->redirect(['/'])->send();
            }
        ],
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'logout' => ['post'],
            ],
        ],
    ];
}

The Versions

20/10 2016

dev-custom/chiesi

dev-custom/chiesi

Flexible user registration and authentication module for Yii2

  Sources   Download

MIT

The Requires

 

yii2 user management yii2-user infoweb

01/07 2016

dev-master

9999999-dev

Flexible user registration and authentication module for Yii2

  Sources   Download

MIT

The Requires

 

yii2 user management yii2-user infoweb

16/07 2015

1.1

1.1.0.0

Flexible user registration and authentication module for Yii2

  Sources   Download

MIT

The Requires

 

yii2 user management yii2-user infoweb

20/03 2015

1.0.3

1.0.3.0

Flexible user registration and authentication module for Yii2

  Sources   Download

MIT

The Requires

 

yii2 user management yii2-user infoweb

26/02 2015

1.0.2

1.0.2.0

Flexible user registration and authentication module for Yii2

  Sources   Download

MIT

The Requires

 

yii2 user management yii2-user infoweb

06/01 2015

1.0.1

1.0.1.0

Flexible user registration and authentication module for Yii2

  Sources   Download

MIT

The Requires

 

yii2 user management yii2-user infoweb

05/01 2015

1.0.0

1.0.0.0

Flexible user registration and authentication module for Yii2

  Sources   Download

MIT

The Requires

 

yii2 user management yii2-user infoweb