Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to overwrite limit specific group or domain or client? #126

Open
kgpanteon opened this issue Apr 26, 2022 · 1 comment
Open

How to overwrite limit specific group or domain or client? #126

kgpanteon opened this issue Apr 26, 2022 · 1 comment

Comments

@kgpanteon
Copy link

The global path limit applying limit to all users. Is there anyway to overwrite limit for specific users?

@kgpanteon
Copy link
Author

@jaytaph Able to achieve it

services.yml

apibundle.listener.rate_limit_checked.set:
        class: ApiBundle\EventListener\ClientRateLimitListener  
        arguments: [ '@request_stack' ]
        tags:
            - { name: kernel.event_listener, event: 'ratelimit.checked.ratelimit', method: 'setRateLimit' }

ClientRateLimitListener.php

<?php

namespace ApiBundle\EventListener;

use Noxlogic\RateLimitBundle\Events\CheckedRateLimitEvent;
use Symfony\Component\HttpFoundation\RequestStack;
use Noxlogic\RateLimitBundle\Annotation\RateLimit;


class ClientRateLimitListener  {

    /**
     * @var RequestStack
     */
    private $requestStack;
    private $container = null;

    /**
     * @param RequestStack $requestStack
     */
    public function __construct(RequestStack $requestStack) {
        $this->requestStack = $requestStack;
    }

    public function setRateLimit(CheckedRateLimitEvent $checkedRateLimitEvent) {

        $request = $this->requestStack->getCurrentRequest();
        $customHeader = $request->headers->get('x-custom-header');

        if (!empty($customHeader ) && !is_null($customHeader )) {
            $checkedRateLimitEvent->setRateLimit(new RateLimit(array(
                'limit' => 1000,
                'period' => 60,
                'methods' => array(0 => '*')
            )));
        }
    }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant