Yii 1.1: auth

A modern and responsive user interface for Yii's authorization manager
96 followers

Auth is a new module from the creator of the popular Rights module for managing user permissions in Yii applications. It's a completely new, modern and responsive user interface for Yii's authorization manager (CAuthManager) built using the also popular Bootstrap extension.

Auth is NOT a new version of Rights, even though those familiar with Rights might feel at home using it. It was developed from scratch following Yii's conventions and it's both easier to use and extend.

Links

Requirements

Usage

You can read about how to setup and use this module from its README on GitHub.

Changes

v1.6.0 (Jan 23, 2013)

  • Moved admins to AuthBehavior
  • Change AuthFilter to require login
  • Translation fixes

v1.5.0 (Jan 17, 2013)

  • Change module to only allow admin users
  • Rename translations (e.g. en_us to en)
  • Translation fixes

v1.4.1 (Jan 12, 2013)

  • Fix caching of access checks
  • Add Russian translation (thanks Ragazzo)

v1.4.0 (Jan 12, 2013)

  • Add support for caching access checks
  • Add initial support for CPhpAuthManager
  • Fix capitalization for cyrillic languages
  • Ukranian translation (thanks MadAnd)

v1.3.0 (Jan 4, 2013)

  • Added support for bypassing access checks for certain users
  • Fixed controller ids when translations are enabled

v1.2.1 (Jan 1, 2013)

  • Refactored grid column classes
  • Fixed a minor bug in listing assignments

v1.2.0 (Dec 31, 2012)

  • Added CachedDbAuthManager
  • Added authorization item subcontrollers

v1.1.0 (Dec 30, 2012)

  • PHP 5.3.0 requirement dropped
  • Refactored code

v1.0.0 (Dec 29, 2012)

  • Initial release

Total 20 comments

#16265 report it
leo4all at 2014/02/04 01:40pm
Old Version 0.9

In the old version of this extension there's was some helper functions or some functionality that I am looking for. does anyone know how can accomplish the same functionality as before:

1 -

Yii::app()->getModule('authorization')->auth->assignRole($_POST['roleName'], $model->id);

2-

<div class="row">
         <?php echo CHtml::label('Role', 'roleName'); ?>
         <?php echo CHtml::dropDownList('roleName', 
                 Yii::app()->getModule('authorization')->auth->getAssignedRole($model->id), 
                 Yii::app()->getModule('authorization')->auth->getRoleSelectOptions());
         ?>
 </div>
#15463 report it
kavitama at 2013/11/12 08:55pm
yiistrap support

HI,

Is it already compatible with or is it planned to be compatible with?

Regards Kavi

#15283 report it
toph at 2013/10/23 09:05am
Authentication with nested modules

For nested modules you have to change the code in auth/filters/AuthFilter.php from

if (($module = $controller->getModule()) !== null){
$itemName .= $module->getId() . '.';

to

if (($module = $controller->getModule()) !== null){
    $itemName .= str_replace('/','.',$module->getId()) . '.';

in order to replace the / with . in the module's id.

#15114 report it
bambinou at 2013/10/08 01:02pm
Thank you

Hi Whatif,

I tried your code and downloaded the 1.6.0 version. To be honest, I really appreciate your help but I think I will give up with Yii. The way it is built is just not right, every single extensions are a nightmare to get working. It is always a bit of a guess work and this is annoying me now. I have been for 4 nights on this problem and keep getting errors after errors...The goal of a good framework is to gain time but I am losing so much time that this framework is actually going against me. I tried the forum for help, live chat....there is not enough people around knowledgeable enough or the ones who know don't want to give you the answers in the live chat...

Why Yii does not come with a pre-added extension user and rights with it's own GUI? This is seriously the most basic thing to have when you build an app. They should add on and give us a quick help on our to remove it rather than trying to install modules with bad docs all over the place..........I give up and thanks again for your help.

Regards,

Ben

Ben

#15102 report it
whatif at 2013/10/07 08:33pm
About the error: "WebUser.admins" is read only.

Hi @bambinou

If user has 'admins' property like below, CWebUser may treat this as a writable property in fact it is not writable, I think. This may be a bug in yii-auth(1.7.0). Besides this, the Auth menu does not show up in yii-auth(1.7.0). So I recommend you use yii-auth(1.6.0) instead of using yii-auth(1.7.0) until this bug is resolved by the author.

[php]   (config/main.php)

//yii-user module
        'user' => array(
            // enable cookie-based authentication
            'class' => 'WebUser',
            'allowAutoLogin' => true,
            'loginUrl' => array('/user/login'),
            'admins' => array('admin, demo'), // users with full access
        ),
#15101 report it
bambinou at 2013/10/07 06:23pm
Thanks for your help whatif

Hi Whatif,

I have copied your main.php file but now I am getting this:

CException

Property "WebUser.admins" is read only. (C:\xampp\htdocs\project1\framework\base\CComponent.php:170)

Any idea why please?

Thank you,

Ben

#15090 report it
whatif at 2013/10/06 08:21pm
Example configuration of main.php using yii-bootstrap, yii-user and yii-auth(1.6.0) modules

Hi everyone

Here I attach my working config of main.php using yii-bootstrap, yii-user and yii-auth( 1.6.0 ) modules as a reference. I hope it may be useful for setting these modules.

But if you use yii-auth(1.7.0), the amins should be relocated like the last part of this comment.

I think yii-auth(1.7.0) has bug. It does not show any Auth menu. I recommend you use Auth(1.6.0) till the author resolve this bug.

<?php
 
// Define a path alias for the Bootstrap extension as it's used internally.
// In this example we assume that you unzipped the extension under protected/extensions.
Yii::setPathOfAlias('bootstrap', dirname(__FILE__) . '/../extensions/bootstrap');
 
// uncomment the following to define a path alias
// Yii::setPathOfAlias('local','path/to/local-folder');
// This is the main Web application configuration. Any writable
// CWebApplication properties can be configured here.
return array(
    'basePath' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..',
    'name' => 'yoursite',
    'theme' => 'bootstrap', // requires you to copy the theme under your themes directory
    // preloading 'log' component
    'preload' => array('log'),
    // autoloading model and component classes
    'import' => array(
        'application.models.*',
        'application.components.*',
        'application.modules.user.models.*',
        'application.modules.user.components.*',
        'application.modules.auth.*',
        'application.modules.auth.components.*',
    ),
    'defaultController' => 'post',
    'modules' => array(
        //phs//yii-user module
        'user' => array(
            # encrypting method (php hash function)
            'hash' => 'md5',
            # send activation email
            'sendActivationMail' => true,
            # allow access for non-activated users
            'loginNotActiv' => false,
            # activate user on registration (only sendActivationMail = false)
            'activeAfterRegister' => false,
            # automatically login from registration
            'autoLogin' => true,
            # registration path
            'registrationUrl' => array('/user/registration'),
            # recovery password path
            'recoveryUrl' => array('/user/recovery'),
            # login form path
            'loginUrl' => array('/user/login'),
            # page after login
            'returnUrl' => array('/user/profile'),
            # page after logout
            'returnLogoutUrl' => array('/user/login'),
        ),
        'auth' => array(
            'strictMode' => true, // when enabled authorization items cannot be assigned children of the same type.
            'userClass' => 'User', // the name of the user model class.
            'userIdColumn' => 'id', // the name of the user id column.
            'userNameColumn' => 'username', // the name of the user name column.
//            if you use yii-auth(1.6.0), use one of the below appLayout.
//            'appLayout' => 'application.views.layouts.main', // the layout used by the module.
            'appLayout' => 'webroot.themes.bootstrap.views.layouts.main', // the layout used by bootstrap theme.
//            if you use yii-auth(1.7.0), use one of the below defaultLayout.
//            'defaultLayout' => 'application.views.layouts.main', // the layout used by the module.
//            'defaultLayout' => 'webroot.themes.bootstrap.views.layouts.main', // the layout used by bootstrap theme.
            'viewDir' => null, // the path to view files to use with this module.
        ),
        // uncomment the following to use Gii
        'gii' => array(
            'class' => 'system.gii.GiiModule',
            'password' => 'YourPassword',
            // If removed, Gii defaults to localhost only. Edit carefully to taste.
            'ipFilters' => array('127.0.0.1', '::1'),
            'generatorPaths' => array(
                'bootstrap.gii',
            ),
        ),
    ),
    // application components
    'components' => array(
        'bootstrap' => array(
            'class' => 'bootstrap.components.Bootstrap',
        ),
        'authManager' => array(
//            'class' => 'auth.components.CDbAuthManager',
            'class' => 'auth.components.CachedDbAuthManager',
            'cachingDuration' => 3600,
            'behaviors' => array(
                'auth' => array(
                    'class' => 'auth.components.AuthBehavior',
                    /**
                     * The 'admin', 'demo' are the usernames of superuser.
                     * These usernames must be the same as your login username.
                     * For example, if your login username does not need email, then these username does not need email. 
                     */
                    'admins' => array('admin', 'demo'), // users with full access
                ),
            ),
        ),
        //yii-user module
        'user' => array(
            /** I made WebUser extends AuthWebUser  instead of WebUser extending CWebUser 
             * so that Auth can be applied to WebUser class like this example: class WebUser extends AuthWebUser.
             * ( WebUser is the class at protected/modules/user/WebUser.php )
             * ( AuthWebUser is the class at protected/modules/auth/AuthWebUser.php )
             * So I dont need to set 'class' => 'auth.components.AuthWebUser'
             * But if you dont use yii-user module then you have to set 'class' => 'auth.components.AuthWebUser',
             */
            'class' => 'WebUser',
//            'class' => 'auth.components.AuthWebUser',
            'allowAutoLogin' => true,
            'loginUrl' => array('/user/login'),
        ),
        // uncomment the following to use a MySQL database
//        'db' => array(
//            'connectionString' => 'sqlite:protected/data/blog.db',
//            'tablePrefix' => 'tbl_',
//        ),
        'db' => array(
            'connectionString' => 'mysql:host=localhost;dbname=yourdb',
            'emulatePrepare' => true,
            'username' => 'yourname',
            'password' => 'yourpassword',
            'charset' => 'utf8',
            'tablePrefix' => 'tbl_',
        ),
        'errorHandler' => array(
            // use 'site/error' action to display errors
            'errorAction' => 'site/error',
        ),
        'urlManager' => array(
            'urlFormat' => 'path',
//            'showScriptName' => true,
            'showScriptName' => false,
            'rules' => array(
                //For blog
                'post/<id:\d+>' => 'post/view',
                'post/<id:\d+>/<title:.*?>' => 'post/view',
                'posts/<tag:.*?>' => 'post/index',
                //For basic controllers
                '<controller:\w+>/<id:\d+>' => '<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
                //For module controllers
                '<module:\w+>/<controller:\w+>/<action:\w+>/<id:\d+>' => '<module>/<controller>/<action>',
                '<module:\w+>/<controller:\w+>/<action:\w+>' => '<module>/<controller>/<action>',
            ),
        ),
        'log' => array(
            'class' => 'CLogRouter',
            'routes' => array(
                array(
                    'class' => 'CFileLogRoute',
                    'levels' => 'error, warning',
                ),
            // uncomment the following to show log messages on web pages
            /*
              array(
              'class'=>'CWebLogRoute',
              ),
             */
            ),
        ),
//        'log' => array(
//            'class' => 'CLogRouter',
//            'routes' => array(
//                array(
//                    'class' => 'ext.yii-debug-toolbar.YiiDebugToolbarRoute',
//                    'ipFilters' => array('127.0.0.1', '192.168.1.215'),
//                ),
//            ),
//        ),
    ),
    // application-level parameters that can be accessed
    // using Yii::app()->params['paramName']
    'params' => require(dirname(__FILE__) . '/params.php'),
);

But if you use yii-auth(1.7.0), the amins should be relocated like below.

'authManager' => array(
//            'class' => 'CDbAuthManager',
            'class' => 'auth.components.CachedDbAuthManager',
            'cachingDuration' => 3600,
            'behaviors' => array(
                'auth' => array(
                    'class' => 'auth.components.AuthBehavior',
//                    'admins' => array('admin', 'demo'), // users with full access
                ),
            ),
        ),
        //yii-user module
        'user' => array(
            // enable cookie-based authentication
            'class' => 'WebUser',
            'allowAutoLogin' => true,
            'loginUrl' => array('/user/login'),
            'admins' => array('admin, demo'), // users with full access
        ),

Cheers

#15089 report it
bambinou at 2013/10/06 04:25pm
Can't get it to work

Hi,

I am really struggling with this extension. I have by main.php blank, just added boostrap and some of the other code as epxlained here but something is missing somewhere.

Could you please be more precise for Yii beginners because there is soemthing not right in the doc. thank you.

<?php
<?php
 
// uncomment the following to define a path alias
// Yii::setPathOfAlias('local','path/to/local-folder');
// This is the main Web application configuration. Any writable
// CWebApplication properties can be configured here.
Yii::setPathOfAlias('bootstrap', dirname(__FILE__).'/../extensions/bootstrap'); 
return array(
    'theme' => 'bootstrap',
    'basePath' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..',
    'name' => 'My Web Application',
    // preloading 'log' component
    'preload' => array('log'),
    // autoloading model and component classes
    'import' => array(
        'application.models.*',
        'application.components.*',
 
    ),
    'modules' => array(
        'auth',
        'gii' => array(
            'generatorPaths' => array(
                'bootstrap.gii',
            ),
        ),
        // uncomment the following to enable the Gii tool
        'gii' => array(
            'class' => 'system.gii.GiiModule',
            'password' => '12345',
            // If removed, Gii defaults to localhost only. Edit carefully to taste.
            'ipFilters' => array('127.0.0.1', '::1'),
        ),
    ),
    // application components
    'components' => array(
     'authManager'=>array(
                  'class'=>'CDbAuthManager',
                  'connectionID'=>'db',
                    'behaviors' => array(
                        'auth' => array(
                            'class' => 'auth.components.AuthBehavior',
                            'admins' => array('admin@yoursite.com'),
                        ),
                    ),
                ),
        'bootstrap' => array(
            'class' => 'bootstrap.components.Bootstrap',
        ),
 
 'user' => array(
'allowAutoLogin'=>true,
                        'class'=>'auth.components.AuthWebUser',
        ),
 
 
        // uncomment the following to enable URLs in path-format
 
          'urlManager'=>array(
          'urlFormat'=>'path',
          'rules'=>array(
          '<controller:\w+>/<id:\d+>'=>'<controller>/view',
          '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
          '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
          ),
          ),
 
        /*
        'db' => array(
            'connectionString' => 'sqlite:' . dirname(__FILE__) . '/../data/testdrive.db',
        ),
        */
        // uncomment the following to use a MySQL database
 
          'db'=>array(
          'connectionString' => 'mysql:host=localhost;dbname=porkxx001',
          'emulatePrepare' => true,
          'username' => 'root',
          'password' => '',
          'charset' => 'utf8',
          ),
 
        'errorHandler' => array(
            // use 'site/error' action to display errors
            'errorAction' => 'site/error',
        ),
        'log' => array(
            'class' => 'CLogRouter',
            'routes' => array(
                array(
                    'class' => 'CFileLogRoute',
                    'levels' => 'error, warning',
                ),
            // uncomment the following to show log messages on web pages
            /*
              array(
              'class'=>'CWebLogRoute',
              ),
             */
            ),
        ),
    ),
    // application-level parameters that can be accessed
    // using Yii::app()->params['paramName']
    'params' => array(
        // this is used in contact page
        'adminEmail' => 'webmaster@example.com',
    ),
    'auth' => array(
  'strictMode' => true, // when enabled authorization items cannot be assigned children of the same type.
  'userClass' => 'User', // the name of the user model class.
  'userIdColumn' => 'id', // the name of the user id column.
  'userNameColumn' => 'name', // the name of the user name column.
  'defaultLayout' => 'application.views.layouts.main', // the layout used by the module.
  'viewDir' => null, // the path to view files to use with this module.
),
 
    'authManager'=>array(
  'class'=>'auth.components.CachedDbAuthManager',
  'cachingDuration'=>3600,
),
 
);
#15002 report it
Sharon Lavie at 2013/09/27 01:38pm
some corrections to the README file

first of all, great extension, thanks alot.

if you work with CDbAuthManager meaning you work with db authorization management, the config/main.php needs the following:

before the array add -

Yii::setPathOfAlias('bootstrap', dirname(__FILE__).'/../extensions/bootstrap');

inside the array -

'components'=>array(
     'authManager'=>array(
                  'class'=>'CDbAuthManager',
                  'connectionID'=>'db',
                    'behaviors' => array(
                        'auth' => array(
                            'class' => 'auth.components.AuthBehavior',
                            'admins' => array('admin@yoursite.com'),
                        ),
                    ),
                ),
 
      'user'=>array(
            // enable cookie-based authentication
            'allowAutoLogin'=>true,
                        'class'=>'auth.components.AuthWebUser',
        ),
)
 
'modules'=>array(
             'auth'
),

to components/Controller you need to add the following code: (notice that the README file has auth.components.AuthFilter, which is incorrect for version 1.6.0)

public function filters() {
     return array( 
        //it's important to add site/error, so an unpermitted user will get the error.
        array('auth.filters.AuthFilter - site/index site/login site/logout site/error'),
            );
        }

And it's very important to remove the filters function from each controller.

#14947 report it
johnsnails at 2013/09/23 08:05pm
User with YiiBooster

Hey,

Just wondering does using Yii Booster fulfill the requirement of: Bootstrap extension 1.0.0 or above

I am going to try get it to work (hopefully I can use this extension with things like inline editing offered in YiiBooster) and will come back with my results.

Thanks!

#14815 report it
Ednei at 2013/09/11 03:50pm
Yiistrap compatibility?

Is there any version of Auth ready to use with Yiistrap?

#14691 report it
engvard at 2013/09/03 04:29pm
Will the dependency on deprecated extension be removed/replaced?

As the description says, auth uses this extension: Bootstrap, which seems to be deprecated according to comments.

Will this dependency be removed or replaced, for instance, with new bootstrap extension (yiistrap)?

#14480 report it
leo4all at 2013/08/15 05:35pm
version

So we had two version of this Extension, Both had the same version 1.0.6

Plus the one from here had the class CachedAccessRecord

which one had been improve??

And how module should be???

(moduleId.)controllerId.actionId

(Forum.)post.index?

#14355 report it
whatif at 2013/08/05 11:10pm
How to fix ' "AuthModule.defaultLayout" is not defined.'

Hi

If you use yii-auth-1.6.0.zip downloaded from this extension site, you should add defaultLayout property to class AuthModule ( auth/AuthModule.php ) in line 40 like below:

public $appLayout = 'application.views.layouts.main';
public $defaultLayout = 'application.views.layouts.main';

If you use yii-auth-master downloaded from github, you should add appLayout property to class AuthModule ( auth/AuthModule.php ) in line 40 like below:

public $appLayout = 'application.views.layouts.main';
public $defaultLayout = 'application.views.layouts.main';

This is because these versions use different property name. So add both properties to the class AuthModule to avoid error safely.

This is quite confusing for Yii users.

I hope the auth author may resolve this inconsistency soon.

#14354 report it
whatif at 2013/08/05 10:54pm
How to fix ' "AuthWebUser.admins" is not defined.'

Hi

If you use yii-auth-1.6.0.zip downloaded from this extension site, you should config the config/main.php like below:

components = array(
     'authManager' => array(
        'behaviors' => array(
            'auth' => array(
                'class' => 'auth.components.AuthBehavior',
                'admins' => array('admin', 'demo'),
            ),
        ),
    ),
    'user' => array(
        // enable cookie-based authentication
        'allowAutoLogin' => true,
        'class' => 'auth.components.AuthWebUser',
    ),
  )

If you use yii-auth-master downloaded from github, you should config the config/main.php like below:

components = array(
     'authManager' => array(

    ),
    'user' => array(
        // enable cookie-based authentication
        'allowAutoLogin' => true,
        'class' => 'auth.components.AuthWebUser',
        'behaviors' => array(
            'auth' => array(
                'class' => 'auth.components.AuthBehavior',
                'admins' => array('admin', 'demo'),
            ),
        ),
    ),
  )

This is because in line 23 of class AuthWebUser (auth/components/AuthWebUser.php), they use different admins.

yii-auth-1.6.0 version: $this->setIsAdmin(in_array($this->name, Yii::app()->authManager->admins));

yii-auth-master version: $this->setIsAdmin(in_array($this->name, $this->admins));

This is quite confusing for Yii users. I hope the auth author may resolve this inconsistency soon.

#14352 report it
Ednei at 2013/08/05 09:34pm
Issue

Property "AuthWebUser.admins" is not defined. Someone fixed this issue?

#14151 report it
fahadakram at 2013/07/23 03:06am
how to solve this error

Property "AuthWebUser.admins" is not defined.

#13850 report it
Néstor Acevedo at 2013/07/01 09:04pm
only RbacSuperAdmin works

hi.

only superadmin is working in all the site. for another things, what i allow not only sa, else admins (RbacAdmin) is displaying a denied access.

how to set correctly permissions in the controller for not only sa?

#13593 report it
Cstdenis at 2013/06/09 07:40pm
Bootstrap CSS

It's not loading the CSS for bootstrap (menu/pagination show up as bullets, etc), what am I doing wrong?

#13519 report it
Néstor Acevedo at 2013/06/03 01:40pm
Property "AuthModule.defaultLayout" is not defined.

hi. i get the following error:

Property "AuthModule.defaultLayout" is not defined.

how to solve it?

thnx

Leave a comment

Please to leave your comment.

Create extension