bravo_lock 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +3 -0
  3. data/Rakefile +39 -0
  4. data/app/assets/javascripts/bravo_lock/application.js +9 -0
  5. data/app/assets/stylesheets/bravo_lock/application.css +7 -0
  6. data/app/controllers/bravo_lock/application_controller.rb +4 -0
  7. data/app/controllers/bravo_lock/password_resets_controller.rb +11 -0
  8. data/app/controllers/bravo_lock/passwords_controller.rb +12 -0
  9. data/app/controllers/bravo_lock/registrations_controller.rb +13 -0
  10. data/app/controllers/bravo_lock/sessions_controller.rb +24 -0
  11. data/app/controllers/bravo_lock/verifications_controller.rb +17 -0
  12. data/app/helpers/bravo_lock/application_helper.rb +4 -0
  13. data/app/models/bravo_lock/password.rb +42 -0
  14. data/app/models/bravo_lock/password_reset.rb +41 -0
  15. data/app/models/bravo_lock/registration.rb +82 -0
  16. data/app/models/bravo_lock/session.rb +24 -0
  17. data/app/models/bravo_lock/verification.rb +44 -0
  18. data/app/views/bravo_lock/password_resets/new.html.erb +6 -0
  19. data/app/views/bravo_lock/passwords/edit.html.erb +9 -0
  20. data/app/views/bravo_lock/registrations/_form.html.erb +9 -0
  21. data/app/views/bravo_lock/registrations/new.html.erb +2 -0
  22. data/app/views/bravo_lock/sessions/new.html.erb +13 -0
  23. data/app/views/bravo_lock/verifications/new.html.erb +13 -0
  24. data/app/views/bravo_lock/verifications/show.html.erb +1 -0
  25. data/app/views/layouts/bravo_lock/application.html.erb +14 -0
  26. data/config/locales/bravo_lock.yml +26 -0
  27. data/config/routes.rb +13 -0
  28. data/db/migrate/20120110170542_create_bravo_lock_user.rb +24 -0
  29. data/lib/bravo_lock.rb +9 -0
  30. data/lib/bravo_lock/controller.rb +15 -0
  31. data/lib/bravo_lock/controller/helpers.rb +42 -0
  32. data/lib/bravo_lock/controller/sessions.rb +39 -0
  33. data/lib/bravo_lock/engine.rb +11 -0
  34. data/lib/bravo_lock/model.rb +14 -0
  35. data/lib/bravo_lock/model/emails.rb +30 -0
  36. data/lib/bravo_lock/model/passwords.rb +11 -0
  37. data/lib/bravo_lock/model/roles.rb +30 -0
  38. data/lib/bravo_lock/model/sessions.rb +10 -0
  39. data/lib/bravo_lock/version.rb +3 -0
  40. data/lib/tasks/bravo_lock_tasks.rake +4 -0
  41. data/test/bravo_lock_test.rb +7 -0
  42. data/test/dummy/Rakefile +7 -0
  43. data/test/dummy/app/assets/javascripts/application.js +9 -0
  44. data/test/dummy/app/assets/stylesheets/application.css +7 -0
  45. data/test/dummy/app/controllers/application_controller.rb +3 -0
  46. data/test/dummy/app/helpers/application_helper.rb +2 -0
  47. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  48. data/test/dummy/config.ru +4 -0
  49. data/test/dummy/config/application.rb +45 -0
  50. data/test/dummy/config/boot.rb +10 -0
  51. data/test/dummy/config/database.yml +25 -0
  52. data/test/dummy/config/environment.rb +5 -0
  53. data/test/dummy/config/environments/development.rb +30 -0
  54. data/test/dummy/config/environments/production.rb +60 -0
  55. data/test/dummy/config/environments/test.rb +39 -0
  56. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  57. data/test/dummy/config/initializers/inflections.rb +10 -0
  58. data/test/dummy/config/initializers/mime_types.rb +5 -0
  59. data/test/dummy/config/initializers/secret_token.rb +7 -0
  60. data/test/dummy/config/initializers/session_store.rb +8 -0
  61. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  62. data/test/dummy/config/locales/en.yml +5 -0
  63. data/test/dummy/config/routes.rb +4 -0
  64. data/test/dummy/log/development.log +0 -0
  65. data/test/dummy/public/404.html +26 -0
  66. data/test/dummy/public/422.html +26 -0
  67. data/test/dummy/public/500.html +26 -0
  68. data/test/dummy/public/favicon.ico +0 -0
  69. data/test/dummy/script/rails +6 -0
  70. data/test/integration/navigation_test.rb +10 -0
  71. data/test/test_helper.rb +10 -0
  72. metadata +219 -0
@@ -0,0 +1,9 @@
1
+ <%= simple_form_for resource, :url => registrations_url do |f| %>
2
+
3
+ <%= f.input :email, :as => :string %>
4
+ <%= f.input :password, :as => :password %>
5
+ <%= f.input :password_confirmation, :as => :password %>
6
+
7
+ <%= f.submit %>
8
+
9
+ <% end %>
@@ -0,0 +1,2 @@
1
+ <h1>Sign up</h1>
2
+ <%= render :partial => 'form' %>
@@ -0,0 +1,13 @@
1
+ <h1>Sign in</h1>
2
+ <%= simple_form_for resource do |f| %>
3
+
4
+ <% if resource.errors.present? %>
5
+ <p>Invalid email or password</p>
6
+ <% end %>
7
+
8
+ <%= f.input :email, :as => :string %>
9
+ <%= f.input :password, :as => :password %>
10
+ <p><%= f.submit "Sign in" %></p>
11
+ <p><%= link_to 'Forgotten your password?', new_password_reset_path %></p>
12
+
13
+ <% end %>
@@ -0,0 +1,13 @@
1
+ <h1>Email not verified</h1>
2
+
3
+ <p>
4
+ We could not sign you in because your email address hasn't been verified. Please follow the instructions
5
+ in our welcome email to verify your adress. Use the form below to have the email resent.
6
+ </p>
7
+
8
+ <%= simple_form_for resource do |f| %>
9
+
10
+ <%= f.input :email, :as => :string %>
11
+ <%= f.submit "Resend" %>
12
+
13
+ <% end %>
@@ -0,0 +1 @@
1
+ <h1>Email verified</h1>
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>BravoLock</title>
5
+ <%= stylesheet_link_tag "bravo_lock/application" %>
6
+ <%= javascript_include_tag "bravo_lock/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,26 @@
1
+ en:
2
+ flash:
3
+ bravo_lock:
4
+
5
+ passwords:
6
+ update:
7
+ notice: "Your password has been updated."
8
+
9
+ password_resets:
10
+ create:
11
+ notice: "Instructions have been sent to your email address."
12
+
13
+ registrations:
14
+ create:
15
+ notice: "Sign up successful. Confirmation instructions have been sent to your email address."
16
+
17
+ sessions:
18
+ create:
19
+ notice: "You have been signed in."
20
+
21
+ verifications:
22
+ create:
23
+ notice: "Instructions have been sent to your email address."
24
+
25
+ verified:
26
+ notice: "Your email address has been verified"
data/config/routes.rb ADDED
@@ -0,0 +1,13 @@
1
+ BravoLock::Engine.routes.draw do
2
+
3
+ resources :passwords
4
+ resources :password_resets
5
+ resources :registrations
6
+ resources :sessions
7
+ resources :verifications
8
+
9
+ match '/sign_in', :to => 'sessions#new', :as => :sign_in
10
+ match '/sign_out', :to => 'sessions#delete', :as => :sign_out
11
+ match '/sign_up', :to => 'registrations#new', :as => :sign_up
12
+
13
+ end
@@ -0,0 +1,24 @@
1
+ class CreateBravoLockUser < ActiveRecord::Migration
2
+ def change
3
+
4
+ create_table :users do |t|
5
+ t.string :email
6
+ t.string :email_token
7
+ t.boolean :email_verified, :default => false
8
+
9
+ t.string :password_digest
10
+ t.string :password_token
11
+ t.datetime :password_token_expires_at
12
+
13
+ t.string :session_token
14
+ t.string :roles, :default => '--- []'
15
+ t.datetime :created_at
16
+ end
17
+
18
+ add_index :users, :email
19
+ add_index :users, :email_token
20
+ add_index :users, :password_token
21
+ add_index :users, :session_token
22
+
23
+ end
24
+ end
data/lib/bravo_lock.rb ADDED
@@ -0,0 +1,9 @@
1
+ module BravoLock
2
+ end
3
+
4
+ require 'bravo_model'
5
+ require 'inherited_resources'
6
+ require 'simple_form'
7
+
8
+ require "bravo_lock/model"
9
+ require "bravo_lock/engine"
@@ -0,0 +1,15 @@
1
+ require 'bravo_lock/controller/helpers'
2
+ require 'bravo_lock/controller/sessions'
3
+
4
+ module BravoLock
5
+ module Controller
6
+
7
+
8
+ def self.included(base)
9
+ base.send :include, Helpers
10
+ base.send :include, Sessions
11
+ end
12
+
13
+
14
+ end
15
+ end
@@ -0,0 +1,42 @@
1
+ module BravoLock
2
+ module Controller
3
+ module Helpers
4
+
5
+
6
+ def self.included(base)
7
+ base.send :helper_method, :current_user
8
+ end
9
+
10
+
11
+ # used as a before_filter
12
+ #
13
+ # NOTE: sign_in_path is not recognised from here because this module is included
14
+ # in the main app itself. bravo_lock.sign_in_path would work but that assumes the
15
+ # engine is mounted as :bravo_lock. Another option to is to add a 'sign_in' route
16
+ # to the main app pointing to 'bravo_lock/sessions#new' but that might break if
17
+ # the controller or action changes. This is a workaround for now
18
+ def authenticate!
19
+ return true if current_user
20
+ session[:redirect] = request.fullpath
21
+ redirect_to :controller => 'bravo_lock/sessions', :action => 'new' # *note
22
+ false
23
+ end
24
+
25
+
26
+ # nodoc
27
+ def current_user
28
+ @current_user ||= User.find_by_session_token(session[:user])
29
+ end
30
+
31
+
32
+ # nodoc
33
+ def verify_and_redirect(token)
34
+ user = BravoLock::Verification.verify! token
35
+ flash[:notice] = I18n.t('flash.bravo_lock.verifications.verified.notice', :default => "Email verified")
36
+ redirect_to sign_in(user)
37
+ end
38
+
39
+
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,39 @@
1
+ module BravoLock
2
+ module Controller
3
+ module Sessions
4
+
5
+
6
+ # nodoc
7
+ def after_sign_in_path
8
+ return params[:redirect] unless params[:redirect].blank?
9
+ session.delete(:redirect) || main_app.home_path
10
+ end
11
+
12
+
13
+ # sign in the user, no verification checks of any kind
14
+ def sign_in!(user)
15
+ session[:user] = user.session_token
16
+ end
17
+
18
+
19
+ # signs in the user if verified, returns the path they should be redirected to
20
+ def sign_in(user)
21
+ if user.email_verified?
22
+ sign_in! user
23
+ return after_sign_in_path
24
+ else
25
+ attrs = {:user_verification => {:email => user.email}}
26
+ return new_verification_path(attrs)
27
+ end
28
+ end
29
+
30
+
31
+ # nodoc
32
+ def sign_out
33
+ session[:user] = nil
34
+ end
35
+
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,11 @@
1
+ require 'bravo_lock/controller'
2
+ module BravoLock
3
+ class Engine < Rails::Engine
4
+ isolate_namespace BravoLock
5
+
6
+ initializer "bravo_lock.action_controller" do
7
+ ActionController::Base.send :include, BravoLock::Controller
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ module BravoLock::Model
2
+
3
+ def self.included(base)
4
+ base.send :include, Emails
5
+ base.send :include, Passwords
6
+ base.send :include, Roles
7
+ base.send :include, Sessions
8
+ end
9
+
10
+ end
11
+ require 'bravo_lock/model/emails'
12
+ require 'bravo_lock/model/passwords'
13
+ require 'bravo_lock/model/roles'
14
+ require 'bravo_lock/model/sessions'
@@ -0,0 +1,30 @@
1
+ module BravoLock::Model::Emails
2
+ extend ActiveSupport::Concern
3
+
4
+
5
+ # nodoc
6
+ def email=(value)
7
+ value = value.downcase if value.is_a?(String)
8
+ super(value)
9
+ end
10
+
11
+
12
+ # nodoc
13
+ def reset_email_token
14
+ self.email_token = SecureRandom.hex(16)
15
+ end
16
+
17
+
18
+ end
19
+
20
+
21
+ module BravoLock::Model::Emails::ClassMethods
22
+
23
+
24
+ # nodoc
25
+ def self.find_by_email(email)
26
+ return nil if email.blank?
27
+ where(:email => email.downcase).first
28
+ end
29
+
30
+ end
@@ -0,0 +1,11 @@
1
+ module BravoLock::Model::Passwords
2
+
3
+
4
+ # nodoc
5
+ def reset_password_token
6
+ self.password_token = SecureRandom.hex(16)
7
+ self.password_token_expires_at = 2.days.from_now
8
+ end
9
+
10
+
11
+ end
@@ -0,0 +1,30 @@
1
+ module BravoLock::Model::Roles
2
+ extend ActiveSupport::Concern
3
+
4
+
5
+ included do
6
+ serialize :roles
7
+ end
8
+
9
+
10
+ # nodoc
11
+ def add_role(role)
12
+ roles << role.to_s
13
+ end
14
+
15
+
16
+ # nodoc
17
+ def add_role!(role)
18
+ add_role(role)
19
+ save!
20
+ end
21
+
22
+
23
+ # nodoc
24
+ def is?(*arr)
25
+ arr.each { |r| return true if roles.include?(r.to_s) }
26
+ false
27
+ end
28
+
29
+
30
+ end
@@ -0,0 +1,10 @@
1
+ module BravoLock::Model::Sessions
2
+
3
+
4
+ # nodoc
5
+ def reset_session_token
6
+ self.session_token = SecureRandom.hex(16)
7
+ end
8
+
9
+
10
+ end
@@ -0,0 +1,3 @@
1
+ module BravoLock
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :bravo_lock do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class BravoLockTest < ActiveSupport::TestCase
4
+ test "truth" do
5
+ assert_kind_of Module, BravoLock
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
3
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
4
+
5
+ require File.expand_path('../config/application', __FILE__)
6
+
7
+ Dummy::Application.load_tasks
@@ -0,0 +1,9 @@
1
+ // This is a manifest file that'll be compiled into including all the files listed below.
2
+ // Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
3
+ // be included in the compiled file accessible from http://example.com/assets/application.js
4
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
5
+ // the compiled file.
6
+ //
7
+ //= require jquery
8
+ //= require jquery_ujs
9
+ //= require_tree .
@@ -0,0 +1,7 @@
1
+ /*
2
+ * This is a manifest file that'll automatically include all the stylesheets available in this directory
3
+ * and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
4
+ * the top of the compiled file, but it's generally better to create a new file per style scope.
5
+ *= require_self
6
+ *= require_tree .
7
+ */
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery
3
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag "application" %>
6
+ <%= javascript_include_tag "application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Dummy::Application