Thursday, 21 May 2015

Yii User Management module: get users based on roles.

In Yii, we can have User Management Module.

And

Rights Module

From this module, if we want users based on user Roles, we can get users like following:

public function getUsersBaseOnRole($role) {
$users = array();
$result = Yii::app()->db->createCommand()
->select('userid, P.first_name')
->from('AuthAssignment A')
->join('tbl_profiles P', 'A.userid = P.user_id')
->where('itemname=:role', array(
':role' => $role,))
->queryAll() ;
if (! empty($result)) {
foreach ($result as $row) {
$users[$row['userid']] = $row['first_name'];
}
}
return $users;
}

Usage:

echo "Second Admins: <br/>";
$secondAdmins = GalleryContent::model()->getUsersBaseOnRole('Second Admin');
echo '<pre>';print_r($secondAdmins);echo '</pre>';

No comments:

Post a Comment