access controlle based on user' group

I have two tables

tbl_user

user_id

fname

group_id

tbl_group

group_id

group_name

group_id | group_name


1 | group_1

2 | group_2

3 | group_3

user_id | fname | group_id


1 | user1 | 2

2 | user2 | 1

3 | user3 | 2

How can i allow user to access controller to logged in users who has specific group

example: users who have group1 can only access reportController

OR

users who have group1,group2 can only access reportController ,user have group_3 can not access

Dear Friend,

I hope this helps.

Create a static method in User.php.




public static function fetchUsers($groupId)

    {

         $arr=array();

         $users=User::model()->findAllByAttributes(array('group_id'=>$groupId));

         foreach($users as $user)

           { 

               $arr[]=$user->fname;

           }

         return $arr;

    }



Modify access rules in ReportController.php




public function accessRules()

	{

		return array(

			array('allow',

				'actions'=>array('index','view','create','update','admin','delete'),

                                     //include also all the actions...

				'users'=>array_merge(User::fetchUsers(1),User::fetchUsers(2))

			),

			array('deny',  // deny all users

				'users'=>array('*'),

			),

		);

	}