waldemarnt/user-agent-bundle

bundle to handle header user-agent versions

v1.0.6 2015-10-01 23:33 UTC

This package is not auto-updated.

Last update: 2024-11-13 18:25:06 UTC


README

Suppose that we have an API or a external connection that needs validation by user-agent, by version, this bundle will fall like a glove in your pretty hands.

Usage

The list is separated by kind.

Installation

Add it as dependency in your composer.json

    "require": {
        "waldemarnt/user-agent-bundle": "2.*"
    }

Update your AppKernel.php and add this line

    new Wneto\UserAgentBundle\WnetoUserAgentBundle(),

Usage

First of all you need understand how this stuff works. For example, a browser request send headers like this:

Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.99 Safari/537.36

This example was did using POSTMAN, well, lets suppose that i need receive only requests from Mozilla that have version higher than 5.0. What i need to do that? Kepp calm, lets do it! First of all we need the configuration inside the config.yml file

# you can found a sample inside Wneto/UserAgentBundle/Resources/samples/config/user_agent.yml
wneto_user_agent:
    validation: true
    type: whitelist # blacklist or whitelist. With whitelist all request will be blocked and accept requests only from the setted patterns
    patterns:
        - { pattern: "Mozilla", version: "5.0", operator: ">" }

Lets understand, pattern is the name of the agent, the name is before the / bar in the agent, allowed is a boolean and the version will be the version that you need set the rule, the operator is used to applicate the rule for example higher than > higher of equals than >= the same in inverse < , <=

Ok now in our application we just need add a verification like this one: First inject the service @user_agent.validator.user_agent in your class.

    public function isDeviceAllowed($request){
        if($this->userAgentValidator->isEnabled()) {
            return $this->userAgentValidator->isAllowed($request->headers->get('user-agent'));
        }

        return true;
    }

Ok, the magic will happen and we will be happy forever.