Yii mdmsoft get user ids who have access to specific route

156 views Asked by At

am using mdmsoft / yii2-admin plugin is there any way to get user ids who has access/permission to specific route. I need to show only those user who can access the specific action in a dropdown.

Before I was doing this but I want this dynamic based on Helper::checkRoute() method


$usersProfiles = UserProfile::find()->all();
$authAssignmentHeadUserIds = AuthAssignment::find()
    ->orWhere(['item_name' => 'marketing-head'])
    ->orWhere(['item_name' => 'media-head'])
    ->orWhere(['item_name' => 'production-head'])
    ->select(['user_id'])
    ->all();

$userHeadProfiles = UserProfile::find()
    ->where(['in', 'user_id', $authAssignmentHeadUserIds])->all();

2

There are 2 answers

0
Sourabh Shah On BEST ANSWER

I was able to fetch user ids for routes

//Getting parent roles which had access to route
$authItemParentRoles = AuthItemChild::find()
    ->where(['child' => '/voucher/accept'])
    ->orWhere(['child' => '/voucher/*'])
    ->select(['parent'])
    ->asArray()
    ->all();

//Extracting parent role onlys from given array.
$parentRoleArray = array_column($authItemParentRoles, 'parent');

//Extracting user ids whose role is in parent role array
$usersHavingAccess = AuthAssignment::find()
    ->where(['in', 'item_name', $parentRoleArray])
    ->select(['user_id'])->all();

//Lastly fetching profile or users having access to that route.
$userHeadProfiles = UserProfile::find()
    ->where(['in', 'user_id', $usersHavingAccess])->all();

Thanks to Shringiraj Dewangan who used array column in his answer, that was the missing piece

4
Shringiraj Dewangan On

1st method

Remove ->all() method and use as sub query

$subQuery = AuthAssignment::find()
    ->orWhere(['item_name' => 'marketing-head'])
    ->orWhere(['item_name' => 'media-head'])
    ->orWhere(['item_name' => 'production-head'])
    ->select(['user_id']);
    

$userHeadProfiles = UserProfile::find()
    ->where(['in', 'user_id', $subQuery])->all();

2nd method

get only 1D array for user id for condition using array_column

$authAssignmentHeadUserIds = AuthAssignment::find()
    ->orWhere(['item_name' => 'marketing-head'])
    ->orWhere(['item_name' => 'media-head'])
    ->orWhere(['item_name' => 'production-head'])
    ->select(['user_id'])
    ->all();

$userHeadProfiles = UserProfile::find()
    ->where(['in', 'user_id', array_column($authAssignmentHeadUserIds,'user_id')])->all();