0

我正在使用 mdmsoft / yii2-admin 插件是否有任何方法可以获取有权访问/许可特定路由的用户 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();

4

2 回答 2

1

第一种方法

删除->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();
于 2020-12-14T10:48:36.880 回答
0

我能够获取路线的用户 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,这是缺失的部分

于 2020-12-14T11:11:24.217 回答