Yii Framework Forum: How To Create Access Role In Yii ? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How To Create Access Role In Yii ?

#1 User is offline   hari maliya 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 31-May 12

Posted 07 January 2013 - 08:35 AM

Hi guys

How to create access role in yii and where in yii application part ?
I want to create access role in yii application but i have a problem and dont know about where  to assign role in yii like 
i have three department role
1.admin -: admin have a all access role in our application 
2.staff -: staff same of page and access role like to edit or update 
3.user  -: user have a all access page only viewing in our application

These type of role can set in controller but i can justify where to write all access in yii and how to set access role ,

thank 
hari maliya




0

#2 User is offline   luc 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 211
  • Joined: 22-June 10
  • Location:france

Posted 07 January 2013 - 06:52 PM

Hi,
Yii offers you a plenty of nice tools: see filters, auth .... See also auth category of extensions.

Cheers.
Hey Ho !
Let's go !
0

#3 User is offline   arun_cumer 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 09-January 13

Posted 10 January 2013 - 01:10 AM

hi this is Arun from India . i created new blog after studying yiiblog i am getting
error while creating a new "Post"


CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`blog`.`tbl_post`, CONSTRAINT `FK_post_author` FOREIGN KEY (`author_id`) REFERENCES `tbl_user` (`id`) ON DELETE CASCADE). The SQL statement executed was: INSERT INTO `tbl_post` (`title`, `content`, `tags`, `status`, `create_time`, `update_time`, `author_id`) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4, :yp5, :yp6)
0

#4 User is offline   parkerfrank 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 02-October 12

Posted 14 May 2013 - 12:18 AM

here any example to create Access Rules ...

echo Yii::app()->user->getState('roles');
public function accessRules()
{
return array(
array('allow', //allow admin user to perform 'index'&'view' actions
'actions'=>array('index','view'),
'users'=>array('admin'),
'roles'=>array('admin'),
),
);
}
0

#5 User is offline   hari maliya 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 31-May 12

Posted 27 June 2013 - 03:39 AM

Hi guys

I sold there question answer but i use another type method like this:

Step 1: Write a same code in UserIdentity.php file
    public function authenticate()
    {
	   $user = User::model()->findByAttributes(array('email'=>$this->username));

		if ($user===null) { // No user found!
			$this->errorCode=self::ERROR_USERNAME_INVALID;
		} else if ($user->password !== ($this->password) ) { // Invalid password!
			$this->errorCode=self::ERROR_PASSWORD_INVALID;
		} else { // Okay!
		    $this->errorCode=self::ERROR_NONE;
		    // Store the role in a session and assign action for role 
		    $this->setState('role', $user->role);
		   $this->_id = $user->id;
		}
		return !$this->errorCode;
	}


setp 2: Then After write same code in YourController.php, you to access and assign role in your staff, admin, user.
 public function accessRules()
	{
	
	if( Yii::app()->user->getState('role') =="admin")
	{
             $arr =array('index','calendar','contact','staff','service');   // give all access to admin
        }else if( Yii::app()->user->getState('role') =="staff")
		{
			$arr =array('index','staff','staffcalendar','update');   // give all access to staff
		}
		else
		{
          $arr = array('');          //  no access to other user
        }
		
        return array(			
		array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>$arr,
				'users'=>array('@'),
			),
						
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}


Step 3: Now you can access role based user and perform action.

Thanks
hari maliya
1

#6 User is offline   Prashant Thakkar 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 29
  • Joined: 04-May 13
  • Location:India

Posted 06 July 2013 - 12:45 AM

View Posthari maliya, on 07 January 2013 - 08:35 AM, said:

Hi guys

How to create access role in yii and where in yii application part ?
I want to create access role in yii application but i have a problem and dont know about where  to assign role in yii like 
i have three department role
1.admin -: admin have a all access role in our application 
2.staff -: staff same of page and access role like to edit or update 
3.user  -: user have a all access page only viewing in our application

These type of role can set in controller but i can justify where to write all access in yii and how to set access role ,

thank 
hari maliya






Hello,
I am facing the same problem... did you found the way to execute the script that adds to auth tables.
0

#7 User is offline   Prashant Thakkar 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 29
  • Joined: 04-May 13
  • Location:India

Posted 06 July 2013 - 04:41 AM

View PostPrashant Thakkar, on 06 July 2013 - 12:45 AM, said:

Hello,
I am facing the same problem... did you found the way to execute the script that adds to auth tables.


Solved : http://www.yiiframew...rization-roles/
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users