Git Product home page Git Product logo

simple_spam_prevent's Introduction

SimpleSpamPrevent

SimpleSpamPrevent is a gem to prevent repetetive requests from logged in users.

Their are many gems available to show captcha but they dont help us when a user is logged in. Using this GEM, You can configure each rails action to a limit ‘x’ in ‘y’ seconds. So if a user is logged in and he hits the same page more than ‘x’ number of times in the last ‘y’ seconds, then he will be redirected to a page where he needs to enter captcha.

Requirements

rails >= 3.0

Setup

Add to your Gemfile:

gem 'simple_spam_prevent'

Add the following line to config/routes.rb:

mount SimpleSpamPrevent::Engine => "/simple_spam_prevent", :as => 'simple_spam_prevent'

Run:

rake db:migrate

Usage

Let say you want to block repetetive requests if greater than 10 on index action of ‘HomeController’ in 2 sec.

Let say the model of logged_in entity in your app is User. Define a method in application controller current_user which gives you current logged in users id.

Then inside Home controller add

before_filter(only: [:action]) { prevent_spam_from(klass, max_times, interval) }

In our case:

before_filter(only: [:index]) { prevent_spam_from(User, 10, 2.second) }

Note: This assumes that you have a function “current_#{klass}” defined. (In this case current_user) which returns the ActiveRecord object of the logged in user.

If you have some other method defined instead of “current_#{klass}” which gives us the activerecord object, then use

before_filter(only: [:action]) { prevent_spam(:klass, #id_of_user, max_times, interval) }

If function “foo” gives us the active record obj for logged in user, then we can write,

before_filter(only: [:index]) { prevent_spam(:user, foo.id, 10, 2.second) }

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.