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
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('easy_authentication', '0.1.0') do |e|
6
+ e.description = "Easy Role Authentication for Ruby on Rails 2.2 (i18n)"
7
+ e.url = "http://github.com/innetra/easy_role_authentication"
8
+ e.author = "Ivan Torres"
9
+ e.email = "mexpolk@gmail.com"
10
+ e.ignore_pattern = ["tmp/*", "script/*"]
11
+ e.development_dependencies = []
12
+ end
13
+
14
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].each { |f| load f }
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{easy_authentication}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Ivan Torres"]
9
+ s.date = %q{2009-01-17}
10
+ s.description = %q{Easy Role Authentication for Ruby on Rails 2.2 (i18n)}
11
+ s.email = %q{mexpolk@gmail.com}
12
+ s.extra_rdoc_files = ["tasks/sysadmin.rake", "tasks/rights.rake", "lib/user_methods.rb", "lib/password_authentication.rb", "lib/helper_methods.rb", "lib/controller_methods.rb", "lib/cookie_authentication.rb"]
13
+ s.files = ["easy_authentication.gemspec", "Manifest", "tasks/sysadmin.rake", "tasks/rights.rake", "generators/easy_authentication/templates/stylesheets/users.css", "generators/easy_authentication/templates/stylesheets/login.css", "generators/easy_authentication/templates/stylesheets/roles.css", "generators/easy_authentication/templates/stylesheets/default.css", "generators/easy_authentication/templates/helpers/shadowbox_helper.rb", "generators/easy_authentication/templates/helpers/form_helper.rb", "generators/easy_authentication/templates/migrations/easy_authentication.rb", "generators/easy_authentication/templates/views/user_roles/edit.html.erb", "generators/easy_authentication/templates/views/user_password/edit.html.erb", "generators/easy_authentication/templates/views/user_password/reset_password.html.erb", "generators/easy_authentication/templates/views/user_password/forgot_password.html.erb", "generators/easy_authentication/templates/views/sessions/new.html.erb", "generators/easy_authentication/templates/views/users/index.html.erb", "generators/easy_authentication/templates/views/users/show.html.erb", "generators/easy_authentication/templates/views/users/_user.html.erb", "generators/easy_authentication/templates/views/users/edit.html.erb", "generators/easy_authentication/templates/views/users/new.html.erb", "generators/easy_authentication/templates/views/users/_form.html.erb", "generators/easy_authentication/templates/views/roles/index.html.erb", "generators/easy_authentication/templates/views/roles/show.html.erb", "generators/easy_authentication/templates/views/roles/edit.html.erb", "generators/easy_authentication/templates/views/roles/new.html.erb", "generators/easy_authentication/templates/views/roles/_form.html.erb", "generators/easy_authentication/templates/site_keys.rb", "generators/easy_authentication/templates/models/user_mailer.rb", "generators/easy_authentication/templates/models/right.rb", "generators/easy_authentication/templates/models/user.rb", "generators/easy_authentication/templates/models/role.rb", "generators/easy_authentication/templates/controllers/user_roles_controller.rb", "generators/easy_authentication/templates/controllers/sessions_controller.rb", "generators/easy_authentication/templates/controllers/roles_controller.rb", "generators/easy_authentication/templates/controllers/user_password_controller.rb", "generators/easy_authentication/templates/controllers/users_controller.rb", "generators/easy_authentication/templates/locales/en.easy_authentication.yml", "generators/easy_authentication/templates/locales/es-MX.easy_authentication.yml", "generators/easy_authentication/templates/layouts/easy_authentication_login.erb", "generators/easy_authentication/templates/layouts/easy_authentication.erb", "generators/easy_authentication/easy_authentication_generator.rb", "test/test_helper.rb", "test/easy_authentication_test.rb", "Rakefile", "init.rb", "lib/user_methods.rb", "lib/password_authentication.rb", "lib/helper_methods.rb", "lib/controller_methods.rb", "lib/cookie_authentication.rb"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{http://github.com/innetra/easy_role_authentication}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Easy_authentication"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{easy_authentication}
19
+ s.rubygems_version = %q{1.3.1}
20
+ s.summary = %q{Easy Role Authentication for Ruby on Rails 2.2 (i18n)}
21
+ s.test_files = ["test/test_helper.rb", "test/easy_authentication_test.rb"]
22
+
23
+ if s.respond_to? :specification_version then
24
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
25
+ s.specification_version = 2
26
+
27
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
28
+ else
29
+ end
30
+ else
31
+ end
32
+ end
@@ -0,0 +1,163 @@
1
+ require "digest/sha1"
2
+ class EasyAuthenticationGenerator < Rails::Generator::Base
3
+
4
+ default_options :skip_layout => false, :skip_migrations => false,
5
+ :skip_routes => false
6
+
7
+ def manifest
8
+ record do |m|
9
+
10
+ # Controllers
11
+ controllers.each do |controller_name|
12
+ m.template "controllers/#{controller_name}_controller.rb",
13
+ File.join("app/controllers", "#{controller_name}_controller.rb")
14
+ m.directory(File.join("app/views", controller_name))
15
+ end
16
+
17
+ # Helpers
18
+ helpers.each do |helper_name|
19
+ m.template "helpers/#{helper_name}_helper.rb",
20
+ File.join("app/helpers", "#{helper_name}_helper.rb")
21
+ end
22
+
23
+ # Views
24
+ views.each do |view_name|
25
+ m.template "views/#{view_name}.html.erb",
26
+ File.join("app/views", "#{view_name}.html.erb")
27
+ end
28
+
29
+ # EasyAuthentication Layouts
30
+ unless options[:skip_layouts]
31
+ # Only if layout creation is needed.
32
+ m.template "layouts/easy_authentication.erb",
33
+ File.join("app/views/layouts", "easy_authentication.erb")
34
+ end
35
+ m.template "layouts/easy_authentication_login.erb",
36
+ File.join("app/views/layouts", "easy_authentication_login.erb")
37
+
38
+ # Stylesheets
39
+ unless options[:skip_css]
40
+ m.directory("public/stylesheets/easy_authentication")
41
+ stylesheets.each do |stylesheet_name|
42
+ m.template "stylesheets/#{stylesheet_name}.css",
43
+ File.join("public/stylesheets/easy_authentication", "#{stylesheet_name}.css")
44
+ end
45
+ end
46
+
47
+ # Models
48
+ models.each do |model_name|
49
+ m.template "models/#{model_name}.rb",
50
+ File.join("app/models", "#{model_name}.rb")
51
+ end
52
+
53
+ # Site Keys
54
+ unless defined? AUTH_SITE_KEY
55
+ m.template "site_keys.rb", "config/initializers/site_keys.rb",
56
+ :assigns => {
57
+ :auth_site_key => make_token,
58
+ :auth_digest_stretches => 10
59
+ }
60
+ end
61
+
62
+ # Locales
63
+ m.template "locales/en.easy_authentication.yml",
64
+ "config/locales/en.easy_authentication.yml"
65
+ m.template "locales/es-MX.easy_authentication.yml",
66
+ "config/locales/es-MX.easy_authentication.yml"
67
+
68
+ # Necessary Routes
69
+ unless options[:skip_routes]
70
+ generate_routes
71
+ end
72
+
73
+ # Migrations
74
+ unless options[:skip_migrations]
75
+ m.migration_template "migrations/easy_authentication.rb", "db/migrate",
76
+ :migration_file_name => "create_easy_authentication"
77
+ end
78
+
79
+ end
80
+ end
81
+
82
+ protected
83
+
84
+ def secure_digest(*args)
85
+ Digest::SHA1.hexdigest(args.flatten.join("--"))
86
+ end
87
+
88
+ def make_token
89
+ secure_digest(Time.now, (1..10).map{ rand.to_s })
90
+ end
91
+
92
+ def controllers
93
+ %w[ roles sessions user_roles users user_password ]
94
+ end
95
+
96
+ def helpers
97
+ %w[ form shadowbox ]
98
+ end
99
+
100
+ def views
101
+ %w[ roles/edit roles/_form roles/index roles/new roles/show sessions/new
102
+ user_roles/edit users/edit users/index users/new users/show users/_user
103
+ users/_form user_password/edit user_password/forgot_password
104
+ user_password/reset_password ]
105
+ end
106
+
107
+ def stylesheets
108
+ %w[ default login roles users ]
109
+ end
110
+
111
+ def models
112
+ %w[ right role user ]
113
+ end
114
+
115
+ def banner
116
+ "Usage: #{$0} easy_authentication"
117
+ end
118
+
119
+ def add_options!(opt)
120
+ opt.separator ""
121
+ opt.separator "Options:"
122
+ opt.on("--skip-layout",
123
+ "Don't generate the authentication layout for views (I'll user my own)") { |v| options[:skip_layout] = v }
124
+ opt.on("--skip-migrations",
125
+ "Don't generate migrations") { |v| options[:skip_migrations] = v }
126
+ opt.on("--skip-routes",
127
+ "Don't map resources in routes file") { |v| options[:skip_routes] = v }
128
+ end
129
+
130
+ def gsub_file(relative_destination, regexp, *args, &block)
131
+ path = destination_path(relative_destination)
132
+ content = File.read(path).gsub(regexp, *args, &block)
133
+ File.open(path, 'wb') { |file| file.write(content) }
134
+ end
135
+
136
+ def generate_routes
137
+ sentinel = 'ActionController::Routing::Routes.draw do |map|'
138
+
139
+ # Do not change indentation in this method!!!
140
+ gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
141
+ %{#{match}\n
142
+ # Easy Authentication
143
+ map.login "/login", :controller => "sessions", :action => "new"
144
+ map.logout "/logout", :controller => "sessions", :action => "destroy"
145
+ map.forgot_password "/forgot_password", :controller => "user_password",
146
+ :action => "forgot_password"
147
+ map.reset_password "/reset_password/:login/:token",
148
+ :controller => "user_password", :action => "reset_password", :method => "get"
149
+ map.reset_password "/reset_password",
150
+ :controller => "user_password", :action => "update_password", :method => "post"
151
+ map.change_password "/change_password", :controller => "user_password",
152
+ :action => "edit"
153
+
154
+ map.resources :roles
155
+ map.resources :sessions, :only => [:create]
156
+ map.resources :users
157
+ map.resources :user_roles, :only => [:edit, :update]
158
+ map.resources :user_password, :only => [:edit, :update]
159
+ # Easy Authentication
160
+ }
161
+ end
162
+ end
163
+ end
@@ -0,0 +1,79 @@
1
+ class RolesController < ApplicationController
2
+
3
+ <% unless options[:skip_layout] -%>
4
+ layout "easy_authentication"
5
+ <% end -%>
6
+
7
+ before_filter :fetch_right_groups, :only => [:new, :edit]
8
+
9
+ def index
10
+ @roles = Role.all
11
+
12
+ respond_to do |format|
13
+ format.html # index.html.erb
14
+ format.xml { render :xml => @roles }
15
+ end
16
+ end
17
+
18
+ def new
19
+ @role = Role.new
20
+
21
+ respond_to do |format|
22
+ format.html # new.html.erb
23
+ format.xml { render :xml => @role }
24
+ end
25
+ end
26
+
27
+ def create
28
+ params[:role][:right_ids] ||= []
29
+ @role = Role.new(params[:role])
30
+
31
+ respond_to do |format|
32
+ if @role.save
33
+ flash[:notice] = t("roles.flash.create")
34
+ format.html { redirect_to(@role) }
35
+ format.xml { render :xml => @role, :status => :created, :location => @role }
36
+ else
37
+ format.html { render :action => "new" }
38
+ format.xml { render :xml => @role.errors, :status => :unprocessable_entity }
39
+ end
40
+ end
41
+ end
42
+
43
+ def show
44
+ @role = Role.find_by_id(params[:id])
45
+ @right_groups = @role.rights.all(:order => "controller_name, action_name").group_by { |p| p.controller_name }
46
+
47
+ respond_to do |format|
48
+ format.html # show.html.erb
49
+ format.xml { render :xml => @role }
50
+ end
51
+ end
52
+
53
+ def edit
54
+ @role = Role.find_by_id(params[:id])
55
+ end
56
+
57
+ def update
58
+ @role = Role.find_by_id(params[:id])
59
+
60
+ respond_to do |format|
61
+ if @role.update_attributes(params[:role])
62
+ flash[:notice] = t("roles.flash.update")
63
+ format.html { redirect_to(@role) }
64
+ format.xml { head :ok }
65
+ else
66
+ @right_groups = Right.all(:order => "controller_name, action_name").group_by { |p| p.controller_name }
67
+ format.html { render :action => "edit" }
68
+ format.xml { render :xml => @role.errors, :status => :unprocessable_entity }
69
+ end
70
+ end
71
+ end
72
+
73
+ protected
74
+
75
+ def fetch_right_groups
76
+ @right_groups = Right.all(:order => "controller_name, action_name").group_by { |p| p.controller_name }
77
+ end
78
+
79
+ end
@@ -0,0 +1,44 @@
1
+ class SessionsController < ApplicationController
2
+
3
+ layout "easy_authentication_login"
4
+
5
+ skip_before_filter :login_required, :only => [:new, :create]
6
+
7
+ def new
8
+ end
9
+
10
+ def create
11
+ logout_keeping_session!
12
+ user = User.authenticate(params[:login], params[:password])
13
+ if user
14
+ # Protects against session fixation attacks, causes request forgery
15
+ # protection if user resubmits an earlier form using back
16
+ # button. Uncomment if you understand the tradeoffs.
17
+ # reset_session
18
+ self.current_user = user
19
+ new_cookie_flag = (params[:remember_me] == "1")
20
+ handle_remember_cookie! new_cookie_flag
21
+ redirect_back_or_default('/')
22
+ flash[:notice] = t("sessions.flash.login", :full_name => user.full_name)
23
+ else
24
+ note_failed_signin
25
+ @login = params[:login]
26
+ @remember_me = params[:remember_me]
27
+ render :action => 'new'
28
+ end
29
+ end
30
+
31
+ def destroy
32
+ logout_killing_session!
33
+ flash[:notice] = t("sessions.flash.logout")
34
+ redirect_back_or_default('/')
35
+ end
36
+
37
+ protected
38
+ # Track failed login attempts
39
+ def note_failed_signin
40
+ flash[:error] = t("sessions.flash.error")
41
+ logger.warn "Failed login for '#{params[:login]}' from #{request.remote_ip} at #{Time.now.utc}"
42
+ end
43
+
44
+ end
@@ -0,0 +1,82 @@
1
+ class UserPasswordController < ApplicationController
2
+
3
+ <% unless options[:skip_layout] -%>
4
+ layout "easy_authentication", :only => [ :edit, :update ]
5
+ <% end -%>
6
+
7
+ skip_before_filter :login_required, :except => [ :edit, :update ]
8
+
9
+ def edit
10
+ @user = User.find_by_login(params[:id])
11
+ end
12
+
13
+ def update
14
+ @user = User.find_by_login(params[:id])
15
+
16
+ respond_to do |format|
17
+ if @user.authenticated?(params[:user][:current_password])
18
+ if @user.update_attributes(params[:user])
19
+ flash[:notice] = t("user_password.edit.flash.update")
20
+ format.html { redirect_to(home_url) }
21
+ format.xml { head :ok }
22
+ else
23
+ format.html { render :action => "edit" }
24
+ format.xml { render :xml => @user.errors,
25
+ :status => :unprocessable_entity }
26
+ end
27
+ else
28
+ flash[:error] = t("user_password.edit.flash.update_error")
29
+ format.html { render :action => "edit" }
30
+ format.xml { render :xml => @user.errors,
31
+ :status => :unprocessable_entity }
32
+ end
33
+ end
34
+ end
35
+
36
+ def forgot_password
37
+ respond_to do |format|
38
+ format.html { render :layout => "easy_authentication_login" }
39
+ end
40
+ end
41
+
42
+ def send_password_token
43
+
44
+ if User.reset_password(params[:login])
45
+ flash[:notice] = t("user_password.send_password_token.flash.sent")
46
+ else
47
+ flash[:error] = t("user_password.send_password_token.flash.error")
48
+ end
49
+
50
+ respond_to do |format|
51
+ format.html { redirect_to(login_url) }
52
+ end
53
+
54
+ end
55
+
56
+ def reset_password
57
+ respond_to do |format|
58
+ if @user = User.find_by_login_and_password_reset_token(params[:login], params[:token])
59
+ format.html { render :layout => "easy_authentication_login" }
60
+ else
61
+ flash[:error] = t("user_password.reset_password.flash.invalid_token")
62
+ format.html { redirect_to(login_url) }
63
+ end
64
+ end
65
+ end
66
+
67
+ def update_password
68
+ @user = User.find_by_login_and_password_reset_token(params[:user][:login], params[:user][:password_reset_token])
69
+ params[:user][:password_reset_token] = nil
70
+
71
+ respond_to do |format|
72
+ if @user.update_attributes(params[:user])
73
+ format.html { redirect_to(login_url) }
74
+ else
75
+ flash[:error] = t("user_password.reset_password.flash.invalid_token")
76
+ logger.info @user.errors.inspect
77
+ format.html { render :action => "reset_password" }
78
+ end
79
+ end
80
+ end
81
+
82
+ end
@@ -0,0 +1,34 @@
1
+ class UserRolesController < ApplicationController
2
+
3
+ <% unless options[:skip_layout] -%>
4
+ layout "easy_authentication"
5
+ <% end -%>
6
+
7
+ before_filter :load_roles
8
+
9
+ def edit
10
+ @user = User.find_by_login(params[:id])
11
+ end
12
+
13
+ def update
14
+ @user = User.find_by_login(params[:id])
15
+
16
+ respond_to do |format|
17
+ if @user.update_attributes(params[:user])
18
+ flash[:notice] = t("user_roles.flash.update")
19
+ format.html { redirect_to(user_url(@user)) }
20
+ format.xml { head :ok }
21
+ else
22
+ format.html { render :action => "edit" }
23
+ format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
24
+ end
25
+ end
26
+ end
27
+
28
+ protected
29
+
30
+ def load_roles
31
+ @roles = Role.all
32
+ end
33
+
34
+ end