Yii mdmsoft 获取有权访问特定路由的用户 ID

Yii mdmsoft get user ids who have access to specific route

我正在使用 mdmsoft / yii2-admin 插件,有什么方法可以让 access/permission 的用户 ID 进入特定路由。我只需要在下拉列表中显示可以访问特定操作的用户。

在我这样做之前,我希望这个动态基于 Helper::checkRoute() 方法


$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();

第一种方法

删除->all()方法并用作子查询

$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();

第二种方法

使用 array_column

仅获取 用户 ID 的一维数组
$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();

我能够获取路由的用户 ID

//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();

感谢 Shringiraj Dewangan 在他的回答中使用数组列,这是缺失的部分