bravo_lock 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +3 -0
- data/Rakefile +39 -0
- data/app/assets/javascripts/bravo_lock/application.js +9 -0
- data/app/assets/stylesheets/bravo_lock/application.css +7 -0
- data/app/controllers/bravo_lock/application_controller.rb +4 -0
- data/app/controllers/bravo_lock/password_resets_controller.rb +11 -0
- data/app/controllers/bravo_lock/passwords_controller.rb +12 -0
- data/app/controllers/bravo_lock/registrations_controller.rb +13 -0
- data/app/controllers/bravo_lock/sessions_controller.rb +24 -0
- data/app/controllers/bravo_lock/verifications_controller.rb +17 -0
- data/app/helpers/bravo_lock/application_helper.rb +4 -0
- data/app/models/bravo_lock/password.rb +42 -0
- data/app/models/bravo_lock/password_reset.rb +41 -0
- data/app/models/bravo_lock/registration.rb +82 -0
- data/app/models/bravo_lock/session.rb +24 -0
- data/app/models/bravo_lock/verification.rb +44 -0
- data/app/views/bravo_lock/password_resets/new.html.erb +6 -0
- data/app/views/bravo_lock/passwords/edit.html.erb +9 -0
- data/app/views/bravo_lock/registrations/_form.html.erb +9 -0
- data/app/views/bravo_lock/registrations/new.html.erb +2 -0
- data/app/views/bravo_lock/sessions/new.html.erb +13 -0
- data/app/views/bravo_lock/verifications/new.html.erb +13 -0
- data/app/views/bravo_lock/verifications/show.html.erb +1 -0
- data/app/views/layouts/bravo_lock/application.html.erb +14 -0
- data/config/locales/bravo_lock.yml +26 -0
- data/config/routes.rb +13 -0
- data/db/migrate/20120110170542_create_bravo_lock_user.rb +24 -0
- data/lib/bravo_lock.rb +9 -0
- data/lib/bravo_lock/controller.rb +15 -0
- data/lib/bravo_lock/controller/helpers.rb +42 -0
- data/lib/bravo_lock/controller/sessions.rb +39 -0
- data/lib/bravo_lock/engine.rb +11 -0
- data/lib/bravo_lock/model.rb +14 -0
- data/lib/bravo_lock/model/emails.rb +30 -0
- data/lib/bravo_lock/model/passwords.rb +11 -0
- data/lib/bravo_lock/model/roles.rb +30 -0
- data/lib/bravo_lock/model/sessions.rb +10 -0
- data/lib/bravo_lock/version.rb +3 -0
- data/lib/tasks/bravo_lock_tasks.rake +4 -0
- data/test/bravo_lock_test.rb +7 -0
- data/test/dummy/Rakefile +7 -0
- data/test/dummy/app/assets/javascripts/application.js +9 -0
- data/test/dummy/app/assets/stylesheets/application.css +7 -0
- data/test/dummy/app/controllers/application_controller.rb +3 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/config/application.rb +45 -0
- data/test/dummy/config/boot.rb +10 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +30 -0
- data/test/dummy/config/environments/production.rb +60 -0
- data/test/dummy/config/environments/test.rb +39 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/inflections.rb +10 -0
- data/test/dummy/config/initializers/mime_types.rb +5 -0
- data/test/dummy/config/initializers/secret_token.rb +7 -0
- data/test/dummy/config/initializers/session_store.rb +8 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +5 -0
- data/test/dummy/config/routes.rb +4 -0
- data/test/dummy/log/development.log +0 -0
- data/test/dummy/public/404.html +26 -0
- data/test/dummy/public/422.html +26 -0
- data/test/dummy/public/500.html +26 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/script/rails +6 -0
- data/test/integration/navigation_test.rb +10 -0
- data/test/test_helper.rb +10 -0
- metadata +219 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2012 YOURNAME
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = 'BravoLock'
|
18
|
+
rdoc.options << '--line-numbers'
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
|
24
|
+
load 'rails/tasks/engine.rake'
|
25
|
+
|
26
|
+
|
27
|
+
Bundler::GemHelper.install_tasks
|
28
|
+
|
29
|
+
require 'rake/testtask'
|
30
|
+
|
31
|
+
Rake::TestTask.new(:test) do |t|
|
32
|
+
t.libs << 'lib'
|
33
|
+
t.libs << 'test'
|
34
|
+
t.pattern = 'test/**/*_test.rb'
|
35
|
+
t.verbose = false
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
task :default => :test
|
@@ -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,24 @@
|
|
1
|
+
class BravoLock::SessionsController < InheritedResources::Base
|
2
|
+
include InheritedResources::DSL
|
3
|
+
actions :new, :create
|
4
|
+
|
5
|
+
|
6
|
+
# nodoc
|
7
|
+
def create
|
8
|
+
create! do
|
9
|
+
next if resource.errors.present?
|
10
|
+
sign_in resource.user
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
|
16
|
+
# nodoc
|
17
|
+
def delete
|
18
|
+
sign_out
|
19
|
+
flash[:notice] = "You have been signed out"
|
20
|
+
redirect_to main_app.root_url
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class BravoLock::VerificationsController < InheritedResources::Base
|
2
|
+
actions :new, :create, :show
|
3
|
+
|
4
|
+
|
5
|
+
# nodoc
|
6
|
+
def create
|
7
|
+
create! :location => main_app.root_url
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
# nodoc
|
12
|
+
def show
|
13
|
+
verify_and_redirect params[:id]
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
class BravoLock::Password < BravoModel::Base
|
2
|
+
|
3
|
+
attr_accessor :user
|
4
|
+
column :password_digest, :string
|
5
|
+
|
6
|
+
validates :password, :presence => true
|
7
|
+
validates :password_confirmation, :presence => true
|
8
|
+
|
9
|
+
after_save :update_user
|
10
|
+
has_secure_password
|
11
|
+
|
12
|
+
|
13
|
+
|
14
|
+
# nodoc
|
15
|
+
def update_user
|
16
|
+
user.password_digest = password_digest
|
17
|
+
user.password_token_expires_at = nil
|
18
|
+
user.password_token = nil
|
19
|
+
user.save!
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
# nodoc
|
24
|
+
def self.find(token)
|
25
|
+
raise ActiveRecord::RecordNotFound unless user = find_user(token)
|
26
|
+
new do |obj|
|
27
|
+
obj.user = user
|
28
|
+
obj.id = token
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
# nodoc
|
34
|
+
def self.find_user(token)
|
35
|
+
return nil if token.blank?
|
36
|
+
return nil unless user = User.find_by_password_token(token)
|
37
|
+
return nil if user.password_token_expires_at.past?
|
38
|
+
user
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# this is used to request instructions for resetting the password
|
2
|
+
class BravoLock::PasswordReset < BravoModel::Base
|
3
|
+
|
4
|
+
|
5
|
+
column :email, :string
|
6
|
+
memoize :user
|
7
|
+
|
8
|
+
after_create :update_user, :send_mail
|
9
|
+
|
10
|
+
validate do
|
11
|
+
next if user.present?
|
12
|
+
errors.add :email, :invalid
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
|
19
|
+
# nodoc
|
20
|
+
def send_mail
|
21
|
+
UserMailer.password_reset_instructions(user).deliver
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
# nodoc
|
26
|
+
def _user
|
27
|
+
return nil if email.blank?
|
28
|
+
return nil unless user = User.find_by_email(email)
|
29
|
+
user
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
# nodoc
|
34
|
+
def update_user
|
35
|
+
user.reset_password_token
|
36
|
+
user.save!
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
class BravoLock::Registration < BravoModel::Base
|
2
|
+
|
3
|
+
column :email, :string
|
4
|
+
column :password_digest, :string
|
5
|
+
|
6
|
+
attr_accessor :user
|
7
|
+
|
8
|
+
validates :email, :presence => true, :method => true
|
9
|
+
validates :password, :presence => true
|
10
|
+
validates :password_confirmation, :presence => true
|
11
|
+
|
12
|
+
has_secure_password
|
13
|
+
after_create :create_user, :send_mail
|
14
|
+
|
15
|
+
|
16
|
+
# used to keep track of which attributes to copy across to the user
|
17
|
+
class_attribute :attribute_list
|
18
|
+
self.attribute_list = [:email, :password_digest]
|
19
|
+
|
20
|
+
|
21
|
+
# Use this to define an attribute that should be copied across to the user
|
22
|
+
# when it's created. Use column() for attributes that you don't want copied
|
23
|
+
def self.attribute(name, type)
|
24
|
+
column name, type
|
25
|
+
self.attribute_list += [name]
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
|
32
|
+
# nodoc
|
33
|
+
def create_user
|
34
|
+
self.user = User.create! user_attributes do |obj|
|
35
|
+
obj.reset_email_token# if attribute_list.include?(:email)
|
36
|
+
obj.reset_session_token
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
# nodoc
|
42
|
+
def send_mail
|
43
|
+
UserMailer.welcome_email(user).deliver
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
# key/value hash containing :password_digest and any other attributes
|
48
|
+
# specified with self.attribute
|
49
|
+
def user_attributes
|
50
|
+
arr = attribute_list.collect { |attr| [attr, send(attr)] }
|
51
|
+
Hash[ *arr.flatten ]
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
# -- Validations --
|
57
|
+
|
58
|
+
|
59
|
+
# nodoc
|
60
|
+
def validate_email
|
61
|
+
return unless email.present?
|
62
|
+
validate_email_format && validate_email_uniqueness
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
# nodoc
|
67
|
+
def validate_email_format
|
68
|
+
return true if email =~ /^\S+@\S+\.+\S+$/i
|
69
|
+
errors.add :email, :invalid
|
70
|
+
false
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
# nodoc
|
75
|
+
def validate_email_uniqueness
|
76
|
+
obj = User.find_by_email(email)
|
77
|
+
errors.add :email, :taken if obj
|
78
|
+
!obj
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'bcrypt'
|
2
|
+
class BravoLock::Session < BravoModel::Base
|
3
|
+
|
4
|
+
column :email, :string
|
5
|
+
column :password, :string
|
6
|
+
memoize :user
|
7
|
+
|
8
|
+
|
9
|
+
validate do
|
10
|
+
next if user
|
11
|
+
errors.add :base, :invalid
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
# nodoc
|
16
|
+
def _user
|
17
|
+
return nil if email.blank? || password.blank?
|
18
|
+
return nil unless user = User.find_by_email(email)
|
19
|
+
return nil unless BCrypt::Password.new(user.password_digest) == password
|
20
|
+
user
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
class BravoLock::Verification < BravoModel::Base
|
2
|
+
|
3
|
+
|
4
|
+
column :email, :string
|
5
|
+
after_create :send_mail
|
6
|
+
memoize :user
|
7
|
+
|
8
|
+
|
9
|
+
validate do
|
10
|
+
next if user.present?
|
11
|
+
errors.add :email, :invalid
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
# NOTE: returns a User object
|
16
|
+
def self.verify!(token)
|
17
|
+
user = User.find_by_email_token!(token)
|
18
|
+
user.email_token = nil
|
19
|
+
user.email_verified = true
|
20
|
+
user.save!
|
21
|
+
user
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
|
28
|
+
# nodoc
|
29
|
+
def _user
|
30
|
+
return nil if email.blank?
|
31
|
+
return nil unless user = User.find_by_email(email)
|
32
|
+
return nil if user.email_verified?
|
33
|
+
user
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
|
38
|
+
# nodoc
|
39
|
+
def send_mail
|
40
|
+
UserMailer.email_verification_instructions(user).deliver
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
end
|