Skip to content

Devise strategy allowing user accounts to only be valid between certain dates.

License

Notifications You must be signed in to change notification settings

jonpearse/devise_date_restrictable

Repository files navigation

Devise Date Restrictable

This is a module for Devise that allows a user account to be restricted by date range.

Installation + Setup

Add the following line to your application’s Gemfile:

gem 'devise_date_restrictable'

And then run bundle install.

Next, you will need to generate an ActiveRecord migration and run that migration.

$ rails generate devise_date_restrictable --model MODEL
rake db:migrate

Finally, add :date_restrictable to the devise line of your model, for instance:

devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :date_restrictable

Usage

This gem adds two DATE fields to your model: valid_from and valid_until. One or both may be specified in order to
restrict a user’s ability to log in either before- or after a particular date, or within a given date range.
If neither is specified, the user will be able to log in without restriction as before.

Note that dates are inclusive, that is:

- if valid_from is specified, the user will be able to log in after 00:00:00 on that date
- if valid_until is specified, the user will be able to log in until 23:59:59 on that date

Note that this gem is not timezone-aware, and therefore the server’s local time will be used for everything. I plan to modify this in a later release.

This gem also provides basic validation to ensure that if valid_from and valid_until are provided, they make sense
chronologically.

Localisation

This gem provides two localisable strings:

Authentication failure
Shown to the user if their account has been restricted by date. This follows Devise convention, will search the following I18n keys:

devise.failure.[model].account_date_restricted
devise.failure.account_date_restricted

Date validation failure
Shown if you attempt to set a user’s valid_from to be after valid_until. This follows ActiveRecord error convention, and will search the following I18n keys:

activerecord.errors.models.[model].attributes.valid_until.must_be_on_or_after
activerecord.errors.models.[model].must_be_on_or_after
activerecord.errors.messages.must_be_on_or_after
errors.attributes.valid_until.must_be_on_or_after
errors.messages.must_be_on_or_after

The human-readable name of the valid_from field is available to these translations as :field.)

Version History

1.0.0 (May 23rd, 2018)

  • [fix] added ActiveRecord::Migration version number to migration template
  • added basic date validation (it is no longer possible to punch holes in space/time with this gem, sorry)
  • added unit tests
  • general tidying for release.

0.0.2 (June 6th, 2017)

  • removing errant debug output

0.0.1 (May 28th, 2017)

  • initial release

Contributing

Go for it! I’ll happily accept any sensible pull requests :)

Obligatory sales pitch

When I’m not hacking at random gems, I’m a freelance web developer specialising in all things front-end, based in the beautiful city of Cardiff, UK.
I’m usually kept fairly busy with project work, but I’m always on the lookout for new people to do cool stuff with. Drop me a line – I’d love to hear from you!

About

Devise strategy allowing user accounts to only be valid between certain dates.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages