~fkooman/vpn-user-portal

92644ae9d688021a52d8467b0a23f63e146b5b9f — François Kooman 3 months ago 085b772
implement /v1/users and /v1/disabled_users Admin API calls

Fixes: https://todo.sr.ht/~eduvpn/server/171
1 files changed, 42 insertions(+), 0 deletions(-)

M src/Http/AdminApiModule.php
M src/Http/AdminApiModule.php => src/Http/AdminApiModule.php +42 -0
@@ 55,6 55,16 @@ class AdminApiModule implements ServiceModuleInterface
            fn(Request $request, UserInfo $userInfo): Response => new JsonResponse(self::extractConnectedUsers($this->connectionManager->get()))
        );

        $service->get(
            '/v1/users',
            fn(Request $request, UserInfo $userInfo): Response => new JsonResponse(['users' => self::extractUsers($this->storage->userList())])
        );

        $service->get(
            '/v1/disabled_users',
            fn(Request $request, UserInfo $userInfo): Response => new JsonResponse(['disabled_users' => self::extractDisabledUsers($this->storage->userList())])
        );

        $service->post(
            '/v1/disable_user',
            function (Request $request, UserInfo $userInfo): Response {


@@ 185,4 195,36 @@ class AdminApiModule implements ServiceModuleInterface

        return $userList;
    }

    /**
     * @param array<\Vpn\Portal\Http\UserInfo> $userList
     *
     * @return array<string>
     */
    private static function extractUsers(array $userList): array
    {
        $strUserList = [];
        foreach ($userList as $userInfo) {
            $strUserList[] = $userInfo->userId();
        }

        return $strUserList;
    }

    /**
     * @param array<\Vpn\Portal\Http\UserInfo> $userList
     *
     * @return array<string>
     */
    private static function extractDisabledUsers(array $userList): array
    {
        $disabledUserList = [];
        foreach ($userList as $userInfo) {
            if ($userInfo->isDisabled()) {
                $disabledUserList[] = $userInfo->userId();
            }
        }

        return $disabledUserList;
    }
}