<commit_before><?php

/*
 * eduVPN - End-user friendly VPN.
 *
 * Copyright: 2016-2018, The Commons Conservancy eduVPN Programme
 * SPDX-License-Identifier: AGPL-3.0+
 */

namespace SURFnet\VPN\Common\Http;

use SURFnet\VPN\Common\Http\Exception\HttpException;

class AuthUtils
{
    /**
     * @return void
     */
    public static function requireUser(array $hookData, array $userList)
    {
        $userId = $hookData['auth']->id();
        if (!\in_array($userId, $userList, true)) {
            throw new HttpException(
                sprintf(
                    'user "%s" is not allowed to perform this operation',
                    $userId
                ),
                403
            );
        }
    }

    /**
     * @return void
     */
    public static function requireEntitlement(array $hookData, array $requiredEntitlementList)
    {
        $userId = $hookData['auth']->id();
        $userEntitlementList = $hookData['auth']->entitlementList();
        foreach ($userEntitlementList as $userEntitlement) {
            if (\in_array($userEntitlement, $requiredEntitlementList, true)) {
                return;
            }
        }

        throw new HttpException(
            sprintf(
                'user "%s" is missing the required entitlement',
                $userId
            ),
            403
        );
    }
}<commit_msg>introduce AuthUtils::requireAdmin that uses hookData<commit_after><?php

/*
 * eduVPN - End-user friendly VPN.
 *
 * Copyright: 2016-2018, The Commons Conservancy eduVPN Programme
 * SPDX-License-Identifier: AGPL-3.0+
 */

namespace SURFnet\VPN\Common\Http;

use SURFnet\VPN\Common\Http\Exception\HttpException;

class AuthUtils
{
    /**
     * @return void
     */
    public static function requireUser(array $hookData, array $userList)
    {
        $userId = $hookData['auth']->id();
        if (!\in_array($userId, $userList, true)) {
            throw new HttpException(
                sprintf(
                    'user "%s" is not allowed to perform this operation',
                    $userId
                ),
                403
            );
        }
    }

    /**
     * @return void
     */
    public static function requireAdmin(array $hookData)
    {
        if (false === $hookData['is_admin']) {
            throw new HttpException('user is not an administrator', 403);
        }
    }
}
{'fraction_non_alphanumeric': 0.11638655462184874, 'fraction_numerical': 0.013445378151260505, 'mean_word_length': 1.8110979929161748, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2061853', 'n_tokens_mistral': 715, 'n_tokens_neox': 677, 'n_words': 157}