Installing Yii Users and Rights to Newly Created Yii app

You are viewing revision #10 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.

next (#17) »

  1. Download
  2. unzip
  3. Create tables for user module
  4. configure
  5. install Rights module
  6. change the main config file
  7. change main layout

After creating an yii app . we need to implement permission, Authentication and Authorization .so the best way is to use the Yii users and Rights modules.

its very simple to install these modules

Download ¶

Download the yii-user user and rights rights extensions and unzip them.

unzip ¶

unzip the modules to

/your-app/protected/modules/user

/your-app/protected/modules/rights

Create tables for user module ¶

then goto /protected/modules/user/data/schema.mysql.sql ** Need to update schema.mysql.sql to create field lastvist_at instead of lastvist

open the sql file ,and create the tables manually(that i prefer) , remove the 'tbl_' prefix from the tables(i prefer)

configure ¶

then configure the config/main

under import

'import'=>array(
        ...
        'application.modules.user.models.*',
        'application.modules.user.components.*',
        'application.modules.rights.*',
        'application.modules.rights.components.*',
        ...
),

under modules

'modules'=>array(
        
        'user'=>array(
                'tableUsers' => 'users',
                'tableProfiles' => 'profiles',
                'tableProfileFields' => 'profiles_fields',
        ),
        'rights'=>array(
                'install'=>true,
        ),
       
),

under components

'components'=>array(
       
        'user'=>array(
                'class'=>'RWebUser',
                // enable cookie-based authentication
                'allowAutoLogin'=>true,
                'loginUrl'=>array('/user/login'),
        ),
        'authManager'=>array(
                'class'=>'RDbAuthManager',
                'connectionID'=>'db',
                'defaultRoles'=>array('Authenticated', 'Guest'),
        ),
        
),

after saving the main config file

install Rights module ¶

install the rights

so that you have to login to user/login

(according to your url style)

your-app/index.php/rights/install

or

your-app/index.php?r=rights/install

after successful installation

change the main config file ¶

change the main config file to

under modules

'user'=>array(
                'tableUsers' => 'users',
                'tableProfiles' => 'profiles',
                'tableProfileFields' => 'profiles_fields',
					 # 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'),
        ),
		
		//Modules Rights
   'rights'=>array(
			
				'superuserName'=>'Admin', // Name of the role with super user privileges. 
			   'authenticatedName'=>'Authenticated',  // Name of the authenticated user role. 
			   'userIdColumn'=>'id', // Name of the user id column in the database. 
			   'userNameColumn'=>'username',  // Name of the user name column in the database. 
			   'enableBizRule'=>true,  // Whether to enable authorization item business rules. 
			   'enableBizRuleData'=>true,   // Whether to enable data for business rules. 
			   'displayDescription'=>true,  // Whether to use item description instead of name. 
			   'flashSuccessKey'=>'RightsSuccess', // Key to use for setting success flash messages. 
			   'flashErrorKey'=>'RightsError', // Key to use for setting error flash messages. 
			  
			   'baseUrl'=>'/rights', // Base URL for Rights. Change if module is nested. 
			   'layout'=>'rights.views.layouts.main',  // Layout to use for displaying Rights. 
			   'appLayout'=>'application.views.layouts.main', // Application layout. 
			   'cssFile'=>'rights.css', // Style sheet file to use for Rights. 
			   'install'=>false,  // Whether to enable installer. 
			   'debug'=>false, 
		),

Your application is now ready .

change main layout ¶

change the main layout.

$this->widget('zii.widgets.CMenu',array(
			'items'=>array(
				array('label'=>'Home', 'url'=>array('/site/index')),
				array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')),
				array('label'=>'Contact', 'url'=>array('/site/contact')),
				array('label'=>'Login', 'url'=>array('/user/login'),
                                array('label'=>'Rights', 'url'=>array('/rights'), 
 'visible'=>Yii::app()->user->isGuest),
				array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/user/logout'), 'visible'=>!Yii::app()->user->isGuest)
			),
		));