innetra-easy_authentication 0.1.0

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 (51) hide show
  1. data/Rakefile +14 -0
  2. data/easy_authentication.gemspec +32 -0
  3. data/generators/easy_authentication/easy_authentication_generator.rb +163 -0
  4. data/generators/easy_authentication/templates/controllers/roles_controller.rb +79 -0
  5. data/generators/easy_authentication/templates/controllers/sessions_controller.rb +44 -0
  6. data/generators/easy_authentication/templates/controllers/user_password_controller.rb +82 -0
  7. data/generators/easy_authentication/templates/controllers/user_roles_controller.rb +34 -0
  8. data/generators/easy_authentication/templates/controllers/users_controller.rb +72 -0
  9. data/generators/easy_authentication/templates/helpers/form_helper.rb +5 -0
  10. data/generators/easy_authentication/templates/helpers/shadowbox_helper.rb +23 -0
  11. data/generators/easy_authentication/templates/layouts/easy_authentication.erb +40 -0
  12. data/generators/easy_authentication/templates/layouts/easy_authentication_login.erb +22 -0
  13. data/generators/easy_authentication/templates/locales/en.easy_authentication.yml +84 -0
  14. data/generators/easy_authentication/templates/locales/es-MX.easy_authentication.yml +100 -0
  15. data/generators/easy_authentication/templates/migrations/easy_authentication.rb +54 -0
  16. data/generators/easy_authentication/templates/models/right.rb +2 -0
  17. data/generators/easy_authentication/templates/models/role.rb +12 -0
  18. data/generators/easy_authentication/templates/models/user.rb +3 -0
  19. data/generators/easy_authentication/templates/models/user_mailer.rb +0 -0
  20. data/generators/easy_authentication/templates/site_keys.rb +2 -0
  21. data/generators/easy_authentication/templates/stylesheets/default.css +249 -0
  22. data/generators/easy_authentication/templates/stylesheets/login.css +111 -0
  23. data/generators/easy_authentication/templates/stylesheets/roles.css +26 -0
  24. data/generators/easy_authentication/templates/stylesheets/users.css +21 -0
  25. data/generators/easy_authentication/templates/views/roles/_form.html.erb +37 -0
  26. data/generators/easy_authentication/templates/views/roles/edit.html.erb +19 -0
  27. data/generators/easy_authentication/templates/views/roles/index.html.erb +21 -0
  28. data/generators/easy_authentication/templates/views/roles/new.html.erb +19 -0
  29. data/generators/easy_authentication/templates/views/roles/show.html.erb +30 -0
  30. data/generators/easy_authentication/templates/views/sessions/new.html.erb +25 -0
  31. data/generators/easy_authentication/templates/views/user_password/edit.html.erb +35 -0
  32. data/generators/easy_authentication/templates/views/user_password/forgot_password.html.erb +16 -0
  33. data/generators/easy_authentication/templates/views/user_password/reset_password.html.erb +22 -0
  34. data/generators/easy_authentication/templates/views/user_roles/edit.html.erb +27 -0
  35. data/generators/easy_authentication/templates/views/users/_form.html.erb +47 -0
  36. data/generators/easy_authentication/templates/views/users/_user.html.erb +4 -0
  37. data/generators/easy_authentication/templates/views/users/edit.html.erb +14 -0
  38. data/generators/easy_authentication/templates/views/users/index.html.erb +21 -0
  39. data/generators/easy_authentication/templates/views/users/new.html.erb +14 -0
  40. data/generators/easy_authentication/templates/views/users/show.html.erb +53 -0
  41. data/init.rb +5 -0
  42. data/lib/controller_methods.rb +14 -0
  43. data/lib/cookie_authentication.rb +63 -0
  44. data/lib/helper_methods.rb +198 -0
  45. data/lib/password_authentication.rb +64 -0
  46. data/lib/user_methods.rb +109 -0
  47. data/tasks/rights.rake +35 -0
  48. data/tasks/sysadmin.rake +27 -0
  49. data/test/easy_authentication_test.rb +8 -0
  50. data/test/test_helper.rb +3 -0
  51. metadata +113 -0
@@ -0,0 +1,109 @@
1
+ module EasyAuthentication
2
+ module UserMethods
3
+
4
+ def self.included(recipient)
5
+ recipient.extend(ClassMethods)
6
+ recipient.class_eval do
7
+
8
+ include InstanceMethods
9
+ include EasyAuthentication::PasswordAuthentication
10
+ include EasyAuthentication::CookieAuthentication
11
+
12
+ has_and_belongs_to_many :roles
13
+
14
+ validates_presence_of :first_name
15
+ validates_presence_of :last_name
16
+ validates_presence_of :email
17
+ validates_presence_of :login, :on => :create
18
+
19
+ validates_format_of :login, :with => /^[a-z_.]+$/
20
+ validates_format_of :email,
21
+ :with => /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/
22
+
23
+ validates_length_of :login, :minimum => 4
24
+
25
+ validates_uniqueness_of :email, :case_sensitive => false
26
+ validates_uniqueness_of :login, :case_sensitive => false
27
+
28
+ # Virtual attribute for the unencrypted password and current_password
29
+ attr_accessor :password
30
+ attr_accessor :current_password
31
+
32
+ validates_presence_of :password, :if => :password_required?
33
+ validates_presence_of :password_confirmation, :if => :password_required?
34
+ validates_confirmation_of :password, :if => :password_required?
35
+ validates_length_of :password, :minimum => 6, :if => :password_required?
36
+
37
+ before_save :encrypt_password
38
+
39
+ # prevents a user from submitting a crafted form that bypasses activation
40
+ # anything else you want your user to change should be added here.
41
+ attr_accessible :first_name, :last_name, :full_name, :email, :login,
42
+ :password, :password_confirmation, :current_password, :role_ids,
43
+ :password_reset_token
44
+
45
+ end
46
+ end
47
+
48
+ module ClassMethods
49
+
50
+ # Generate reset password token (it does not reset password!)
51
+ def reset_password(login)
52
+ return if login.blank?
53
+ return unless u = User.first(:conditions => "login = '#{login}' OR email = '#{login}'")
54
+ u.password_reset_token = make_token
55
+ u.save
56
+ end
57
+
58
+ def authenticate(login, password)
59
+ return if login.blank? || password.blank?
60
+ u = User.first(:conditions => "login = '#{login}' OR email = '#{login}'")
61
+ u = u.authenticated?(password) ? u : nil
62
+ end
63
+
64
+ # Encrypts some data with the salt
65
+ def encrypt(password, salt)
66
+ Digest::SHA1.hexdigest("--#{salt}--#{password}--")
67
+ end
68
+
69
+ end # EasyAuthentication::User::ClassMethods
70
+
71
+ module InstanceMethods
72
+
73
+ def to_param
74
+ self.login
75
+ end
76
+
77
+ def full_name
78
+ "#{self.first_name} #{self.last_name}"
79
+ end
80
+
81
+ # Encrypts the password with the user salt
82
+ def encrypt(password)
83
+ self.class.encrypt(password, password_salt)
84
+ end
85
+
86
+ def authenticated?(password)
87
+ password_hash == encrypt(password)
88
+ end
89
+
90
+ def authorized?(controller_name, action_name)
91
+ self.roles.each do |role|
92
+ return true if role.rights.find_by_controller_name_and_action_name(controller_name, action_name)
93
+ end
94
+ return false
95
+ end
96
+
97
+ protected
98
+
99
+ # before filter
100
+ def encrypt_password
101
+ return if password.blank?
102
+ self.password_salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
103
+ self.password_hash = encrypt(password)
104
+ end
105
+
106
+ end # EasyAuthentication::User::InstanceMethods
107
+
108
+ end # EasyAuthentication::UserMethods
109
+ end # EasyAuthentication
data/tasks/rights.rake ADDED
@@ -0,0 +1,35 @@
1
+ namespace :easy_authentication do
2
+ desc "Read routes to create rights"
3
+ task :rights => :environment do
4
+
5
+ controllers = {}
6
+
7
+ # Inserts unregistered actions
8
+ ActionController::Routing::Routes.routes.each do |route|
9
+ if route.parameter_shell.has_key?(:controller)
10
+ unless controllers.has_key? route.parameter_shell[:controller]
11
+ controllers[route.parameter_shell[:controller]] = []
12
+ end
13
+ unless controllers[route.parameter_shell[:controller]].include? route.parameter_shell[:action]
14
+ controllers[route.parameter_shell[:controller]] << route.parameter_shell[:action]
15
+ end
16
+ unless Right.find_by_controller_name_and_action_name(
17
+ route.parameter_shell[:controller], route.parameter_shell[:action])
18
+ Right.create!(
19
+ :controller_name => route.parameter_shell[:controller],
20
+ :action_name => route.parameter_shell[:action])
21
+ end
22
+ end
23
+ end
24
+
25
+ # De-Register invalid actions
26
+ Right.all.each do |right|
27
+ if (controllers.has_key? right.controller_name)
28
+ right.delete unless controllers[right.controller_name].include? right.action_name
29
+ else
30
+ right.delete
31
+ end
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,27 @@
1
+ namespace :easy_authentication do
2
+ desc "Create sysadmin user"
3
+ task :sysadmin => :environment do
4
+
5
+ unless User.find_by_login("sysadmin")
6
+
7
+ right_ids = []
8
+
9
+ for right in Right.all do
10
+ right_ids << right.id
11
+ end
12
+
13
+ Role.create!( :name => "sysadmin",
14
+ :right_ids => right_ids )
15
+
16
+
17
+ User.create!( :first_name => "System",
18
+ :last_name => "Administrator",
19
+ :login => "sysadmin",
20
+ :password => "monkey",
21
+ :password_confirmation => "monkey",
22
+ :email => "sysadmin@innetra.com",
23
+ :role_ids => [1] )
24
+
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class EasyAuthenticationTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: innetra-easy_authentication
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ivan Torres
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-17 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Easy Role Authentication for Ruby on Rails 2.2 (i18n)
17
+ email: mexpolk@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - tasks/sysadmin.rake
24
+ - tasks/rights.rake
25
+ - lib/user_methods.rb
26
+ - lib/password_authentication.rb
27
+ - lib/helper_methods.rb
28
+ - lib/controller_methods.rb
29
+ - lib/cookie_authentication.rb
30
+ files:
31
+ - easy_authentication.gemspec
32
+ - Manifest
33
+ - tasks/sysadmin.rake
34
+ - tasks/rights.rake
35
+ - generators/easy_authentication/templates/stylesheets/users.css
36
+ - generators/easy_authentication/templates/stylesheets/login.css
37
+ - generators/easy_authentication/templates/stylesheets/roles.css
38
+ - generators/easy_authentication/templates/stylesheets/default.css
39
+ - generators/easy_authentication/templates/helpers/shadowbox_helper.rb
40
+ - generators/easy_authentication/templates/helpers/form_helper.rb
41
+ - generators/easy_authentication/templates/migrations/easy_authentication.rb
42
+ - generators/easy_authentication/templates/views/user_roles/edit.html.erb
43
+ - generators/easy_authentication/templates/views/user_password/edit.html.erb
44
+ - generators/easy_authentication/templates/views/user_password/reset_password.html.erb
45
+ - generators/easy_authentication/templates/views/user_password/forgot_password.html.erb
46
+ - generators/easy_authentication/templates/views/sessions/new.html.erb
47
+ - generators/easy_authentication/templates/views/users/index.html.erb
48
+ - generators/easy_authentication/templates/views/users/show.html.erb
49
+ - generators/easy_authentication/templates/views/users/_user.html.erb
50
+ - generators/easy_authentication/templates/views/users/edit.html.erb
51
+ - generators/easy_authentication/templates/views/users/new.html.erb
52
+ - generators/easy_authentication/templates/views/users/_form.html.erb
53
+ - generators/easy_authentication/templates/views/roles/index.html.erb
54
+ - generators/easy_authentication/templates/views/roles/show.html.erb
55
+ - generators/easy_authentication/templates/views/roles/edit.html.erb
56
+ - generators/easy_authentication/templates/views/roles/new.html.erb
57
+ - generators/easy_authentication/templates/views/roles/_form.html.erb
58
+ - generators/easy_authentication/templates/site_keys.rb
59
+ - generators/easy_authentication/templates/models/user_mailer.rb
60
+ - generators/easy_authentication/templates/models/right.rb
61
+ - generators/easy_authentication/templates/models/user.rb
62
+ - generators/easy_authentication/templates/models/role.rb
63
+ - generators/easy_authentication/templates/controllers/user_roles_controller.rb
64
+ - generators/easy_authentication/templates/controllers/sessions_controller.rb
65
+ - generators/easy_authentication/templates/controllers/roles_controller.rb
66
+ - generators/easy_authentication/templates/controllers/user_password_controller.rb
67
+ - generators/easy_authentication/templates/controllers/users_controller.rb
68
+ - generators/easy_authentication/templates/locales/en.easy_authentication.yml
69
+ - generators/easy_authentication/templates/locales/es-MX.easy_authentication.yml
70
+ - generators/easy_authentication/templates/layouts/easy_authentication_login.erb
71
+ - generators/easy_authentication/templates/layouts/easy_authentication.erb
72
+ - generators/easy_authentication/easy_authentication_generator.rb
73
+ - test/test_helper.rb
74
+ - test/easy_authentication_test.rb
75
+ - Rakefile
76
+ - init.rb
77
+ - lib/user_methods.rb
78
+ - lib/password_authentication.rb
79
+ - lib/helper_methods.rb
80
+ - lib/controller_methods.rb
81
+ - lib/cookie_authentication.rb
82
+ has_rdoc: true
83
+ homepage: http://github.com/innetra/easy_role_authentication
84
+ post_install_message:
85
+ rdoc_options:
86
+ - --line-numbers
87
+ - --inline-source
88
+ - --title
89
+ - Easy_authentication
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: "0"
97
+ version:
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: "1.2"
103
+ version:
104
+ requirements: []
105
+
106
+ rubyforge_project: easy_authentication
107
+ rubygems_version: 1.2.0
108
+ signing_key:
109
+ specification_version: 2
110
+ summary: Easy Role Authentication for Ruby on Rails 2.2 (i18n)
111
+ test_files:
112
+ - test/test_helper.rb
113
+ - test/easy_authentication_test.rb