Get permissions associated with a user

Each permissions is an associative array with keys: action, objectType,
objectId.  Wildcards are respectively '*', '*' and 0.

@param string $userId Name of role

@return array
424365
public static function getUserACL($userId)
    {
        global $PPHP;
        $db = $PPHP['db'];

        $flat = $db->selectArray(
            "
            SELECT action, objectType, objectId
            FROM acl_users
            WHERE userId=?
            ",
            array($userId)
        );
        return is_array($flat) ? $flat : array();
    }
395869
public function listAction($userId)
    {
        $userManager = $this->get('phlexible_user.user_manager');
        $systemUid = $userManager->getSystemUserId();

        $skipUserIds = [$userId];
        if (!in_array($systemUid, $skipUserIds)) {
            $skipUserIds[] = $systemUid;
        }

        $users = [];
        foreach ($userManager->findAll() as $user) {
            if (in_array($user->getId(), $skipUserIds)) {
                continue;
            }

            $users[$user->getDisplayName()] = [
                'uid' => $user->getId(),
                'name' => $user->getDisplayName(),
            ];
        }

        ksort($users);
        $users = array_values($users);

        return new JsonResponse($users);
    }
875088
public static function get_contexts_for_userid(int $userid) : contextlist {
        global $DB;

        $contextlist = new contextlist();

        // The role_capabilities table contains user data.
        $contexts = [
            CONTEXT_SYSTEM,
            CONTEXT_USER,
            CONTEXT_COURSECAT,
            CONTEXT_COURSE,
            CONTEXT_MODULE,
            CONTEXT_BLOCK
        ];
        list($insql, $inparams) = $DB->get_in_or_equal($contexts, SQL_PARAMS_NAMED);
        $sql = "SELECT ctx.id
                  FROM {context} ctx
                  JOIN {role_capabilities} rc
                    ON rc.contextid = ctx.id
                   AND ((ctx.contextlevel {$insql} AND rc.modifierid = :modifierid)
                    OR (ctx.contextlevel = :contextlevel AND ctx.instanceid = :userid))";
        $params = [
            'modifierid' => $userid,
            'contextlevel' => CONTEXT_USER,
            'userid' => $userid
         ];
        $params += $inparams;

        $contextlist->add_from_sql($sql, $params);

        // The role_assignments table contains user data.
        $contexts = [
            CONTEXT_SYSTEM,
            CONTEXT_USER,
            CONTEXT_COURSECAT,
            CONTEXT_COURSE,
            CONTEXT_MODULE,
            CONTEXT_BLOCK
        ];
        list($insql, $inparams) = $DB->get_in_or_equal($contexts, SQL_PARAMS_NAMED);
        $params = [
            'userid' => $userid,
            'modifierid' => $userid
         ];
        $params += $inparams;
        $sql = "SELECT ctx.id
                  FROM {role_assignments} ra
                  JOIN {context} ctx
                    ON ctx.id = ra.contextid
                   AND ctx.contextlevel {$insql}
                 WHERE (ra.userid = :userid
                    OR ra.modifierid = :modifierid)
                   AND ra.component != 'tool_cohortroles'";
        $contextlist->add_from_sql($sql, $params);

        return $contextlist;
    }
390539
public function activeForUser($userId)
    {
        return $this->forUser($userId)->reject(function ($client) {
            return $client->revoked;
        })->values();
    }
308852
def get_user_permissions(uid, **kwargs):
    
    try:
        _get_user(uid)

        user_perms = db.DBSession.query(Perm).filter(Perm.id==RolePerm.perm_id,
                                                  RolePerm.role_id==Role.id,
                                                  Role.id==RoleUser.role_id,
                                                  RoleUser.user_id==uid).all()
        return user_perms
    except:
        raise HydraError("Permissions not found for user (user_id={})".format(uid))
1230399
public static function get_contexts_for_userid(int $userid) : contextlist {
        $params = ['modulename' => 'assign',
                   'contextlevel' => CONTEXT_MODULE,
                   'userid' => $userid,
                   'graderid' => $userid,
                   'aouserid' => $userid,
                   'asnuserid' => $userid,
                   'aufuserid' => $userid,
                   'aumuserid' => $userid];

        $sql = "SELECT ctx.id
                  FROM {course_modules} cm
                  JOIN {modules} m ON cm.module = m.id AND m.name = :modulename
                  JOIN {assign} a ON cm.instance = a.id
                  JOIN {context} ctx ON cm.id = ctx.instanceid AND ctx.contextlevel = :contextlevel
                  JOIN {assign_grades} ag ON a.id = ag.assignment AND (ag.userid = :userid OR ag.grader = :graderid)";

        $contextlist = new contextlist();
        $contextlist->add_from_sql($sql, $params);

        $sql = "SELECT ctx.id
                  FROM {course_modules} cm
                  JOIN {modules} m ON cm.module = m.id AND m.name = :modulename
                  JOIN {assign} a ON cm.instance = a.id
                  JOIN {context} ctx ON cm.id = ctx.instanceid AND ctx.contextlevel = :contextlevel
                  JOIN {assign_overrides} ao ON a.id = ao.assignid
                 WHERE ao.userid = :aouserid";

        $contextlist->add_from_sql($sql, $params);

        $sql = "SELECT ctx.id
                  FROM {course_modules} cm
                  JOIN {modules} m ON cm.module = m.id AND m.name = :modulename
                  JOIN {assign} a ON cm.instance = a.id
                  JOIN {context} ctx ON cm.id = ctx.instanceid AND ctx.contextlevel = :contextlevel
                  JOIN {assign_submission} asn ON a.id = asn.assignment
                 WHERE asn.userid = :asnuserid";

        $contextlist->add_from_sql($sql, $params);

        $sql = "SELECT ctx.id
                  FROM {course_modules} cm
                  JOIN {modules} m ON cm.module = m.id AND m.name = :modulename
                  JOIN {assign} a ON cm.instance = a.id
                  JOIN {context} ctx ON cm.id = ctx.instanceid AND ctx.contextlevel = :contextlevel
                  JOIN {assign_user_flags} auf ON a.id = auf.assignment
                 WHERE auf.userid = :aufuserid";

        $contextlist->add_from_sql($sql, $params);

        $sql = "SELECT ctx.id
                  FROM {course_modules} cm
                  JOIN {modules} m ON cm.module = m.id AND m.name = :modulename
                  JOIN {assign} a ON cm.instance = a.id
                  JOIN {context} ctx ON cm.id = ctx.instanceid AND ctx.contextlevel = :contextlevel
                  JOIN {assign_user_mapping} aum ON a.id = aum.assignment
                 WHERE aum.userid = :aumuserid";

        $contextlist->add_from_sql($sql, $params);

        manager::plugintype_class_callback('assignfeedback', self::ASSIGNFEEDBACK_INTERFACE,
                'get_context_for_userid_within_feedback', [$userid, $contextlist]);
        manager::plugintype_class_callback('assignsubmission', self::ASSIGNSUBMISSION_INTERFACE,
                'get_context_for_userid_within_submission', [$userid, $contextlist]);

        return $contextlist;
    }
1262146
public function findAllByUser( $userId )
    {
        $select = $this->select()
                       ->join( 'user_right_x_user',
                               new Sql\Expression(
                                    '?.? = ?.? AND ?.? = ?',
                                    array(
                                        self::$tableName,
                                        'id',
                                        'user_right_x_user',
                                        'rightId',
                                        'user_right_x_user',
                                        'userId',
                                        $userId,
                                    ),
                                    array(
                                        Sql\Expression::TYPE_IDENTIFIER,
                                        Sql\Expression::TYPE_IDENTIFIER,
                                        Sql\Expression::TYPE_IDENTIFIER,
                                        Sql\Expression::TYPE_IDENTIFIER,
                                        Sql\Expression::TYPE_IDENTIFIER,
                                        Sql\Expression::TYPE_IDENTIFIER,
                                        Sql\Expression::TYPE_VALUE,
                                    )
                               ),
                               array(
                                   'granted' => 'userId'
                               ),
                               Sql\Select::JOIN_LEFT )
                        ->order( array(
                            'group'     => 'ASC',
                            'resource'  => 'ASC',
                            'privilege' => 'ASC',
                        ) );

        /* @var $result \Zend\Db\Adapter\Driver\ResultInterface */

        $return = array();
        $result = $this->sql()
                       ->prepareStatementForSqlObject( $select )
                       ->execute();

        foreach ( $result as $row )
        {
            $return[] = $this->selected( $row );
        }

        return $return;
    }
953173
public function get($userId)
    {
        $endpoint = '/admin/users/'.$userId.'.json';
        $response = $this->request($endpoint);
        return $this->createObject(User::class, $response['user']);
    }
543981
public function getItemsByUser($userId)
    {
        if (empty($userId)) {
            return [];
        }

        $query = (new Query)->select('b.*')
            ->from(['a' => $this->assignmentTable, 'b' => $this->itemTable])
            ->where('{{a}}.[[item_name]]={{b}}.[[name]]')
            ->andWhere(['a.user_id' => (string) $userId]);

        $roles = [];
        foreach ($query->all($this->db) as $row) {
            $roles[$row['name']] = $this->populateItem($row);
        }
        return $roles;
    }
384784
public function participantsUserIds($userId = null)
    {
        $users = $this->participants()->lists('user_id');

        if ($userId) {
            $users[] = $userId;
        }

        return $users;
    }
779072
public static function getAllMenuIdArrByUserId($userId)
    {
        $data = static::find()
            ->findByUserId($userId)
            ->asArray()
            ->all();

        $menuIdArr = [];
        foreach ($data as $item) {
            $menu = DpAdminMenu::find()
                ->findByMenuId($item['menu_id'])
                ->asArray()
                ->one();
            if ($menu) {
                $menuIdArr[] = $item['menu_id'];
            }
        }

        return $menuIdArr;
    }
188418
private function getGroupPermissionHandles($id)
    {
        $permissions = Craft::$app->userPermissions->getPermissionsByGroupId($id);
        $permissions = MigrationManagerHelper::getPermissionHandles($permissions);
        return $permissions;
    }
15389
public function getPermissions($user_id = null)
    {
        static $permissions = array();

        if (!isset($user_id)) {
            $user_id = $this->uid;
        }

        if (isset($permissions[$user_id])) {
            return $permissions[$user_id];
        }

        $user = $this->get($user_id);

        if (empty($user['role_id'])) {
            return $permissions[$user_id] = array();
        }

        $role = array();
        if (isset($user['role_permissions'])) {
            $role['permissions'] = $user['role_permissions'];
        } else {
            $role = $this->role->get($user['role_id']);
        }

        if (empty($role['permissions'])) {
            return $permissions[$user_id] = array();
        }

        return $permissions[$user_id] = (array) $role['permissions'];
    }
910475
public static function getReportToUsers(string $userId): array
    {
        $table = TableRegistry::get(Configure::read('Users.table'));
        $users = $table->find()
            ->where([
                'reports_to' => $userId
            ])
            ->all()
            ->toArray();

        return $users;
    }
981395
private function getUserObjectPermissionByArray($permissions, UserInterface $user)
    {
        if (empty($permissions)) {
            return [];
        }

        $userPermission = [];
        $roles = $user->getRoleObjects();
        foreach ($roles as $role) {
            $roleId = $role->getId();
            if (!isset($permissions[$roleId])) {
                continue;
            }

            $userPermission = $this->cumulatePermissions($userPermission, $permissions[$roleId]);
        }

        return $userPermission;
    }
443180
public function get_all_groups($userid) {
        if (isset($this->usergroups[$userid])) {
            return $this->usergroups[$userid];
        }

        $grouping = $this->get_instance()->teamsubmissiongroupingid;
        $return = groups_get_all_groups($this->get_course()->id, $userid, $grouping);

        $this->usergroups[$userid] = $return;

        return $return;
    }
768803
protected static function checkRole($userId)
    {
        $appId = static::get('app.id', null);

        if (!empty($appId) && !empty($userId)) {
            $roleId = UserAppRole::getRoleIdByAppIdAndUserId($appId, $userId);
            $roleInfo = ($roleId) ? Role::getCachedInfo($roleId) : null;
            if (!empty($roleInfo) && !array_get($roleInfo, 'is_active', false)) {
                throw new ForbiddenException('Role is not active.');
            }
        }
    }