Git Product home page Git Product logo

useragentbundle's Introduction

User Agent Version Bundle

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.

useragentbundle's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.