toretore-simple_authentication 0.1.2 → 0.1.3

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.
@@ -0,0 +1,6 @@
1
+ class LoginsController < ApplicationController
2
+
3
+ include SimpleAuthentication::ControllerMethods::Logins
4
+ include SimpleAuthentication::ControllerMethods::Logins::Behavior
5
+
6
+ end
@@ -0,0 +1,5 @@
1
+ class User < ActiveRecord::Base
2
+
3
+ include SimpleAuthentication::ModelMethods::User
4
+
5
+ end
@@ -0,0 +1,8 @@
1
+ <h2><%= t('simple_authentication.login_title') %></h2>
2
+
3
+ <ul class="authenticators">
4
+ <% for authenticator in @authenticators -%>
5
+ <li><%= link_to t(:link, :scope => [:simple_authentication, :authenticators, authenticator.identifier]),
6
+ new_login_with_authenticator_url(:authenticator => authenticator.identifier) %></li>
7
+ <% end -%>
8
+ </ul>
@@ -0,0 +1,5 @@
1
+ <p>
2
+ You're logged in as <strong><%= h(@user.display_name) %></strong>.
3
+ </p>
4
+
5
+ <%= button_to "Log out", login_url, :method => :delete %>
@@ -0,0 +1,7 @@
1
+ en:
2
+ simple_authentication:
3
+ login_required: You must be logged in to view this page
4
+ logout_required: You must be logged out to view this page
5
+ login_successful: "You're logged in"
6
+ login_failed: Login failed
7
+ login_title: Log in
@@ -0,0 +1,4 @@
1
+ ActionController::Routing::Routes.draw do |map|
2
+ map.resource :login
3
+ map.new_login_with_authenticator 'login/:authenticator', :controller => 'logins', :action => 'new'
4
+ end
@@ -0,0 +1,5 @@
1
+ Description:
2
+ Create user migration
3
+
4
+ Example:
5
+ ./script/generate simple_authentication --migration User
@@ -0,0 +1,37 @@
1
+ class SimpleAuthenticationGenerator < Rails::Generator::NamedBase
2
+ def manifest
3
+ record do |m|
4
+ m.migration_template "model:migration.rb", "db/migrate", :assigns => simple_auth_assigns,
5
+ :migration_file_name => "create_#{custom_file_name}" if options[:migration]
6
+ end
7
+ end
8
+
9
+ private
10
+
11
+ def custom_file_name
12
+ name = class_name.underscore.downcase
13
+ name = name.pluralize if ActiveRecord::Base.pluralize_table_names
14
+ name
15
+ end
16
+
17
+ def simple_auth_assigns
18
+ {
19
+ :migration_name => "Create#{custom_file_name.camelize}",
20
+ :table_name => custom_file_name,
21
+ :attributes => simple_auth_attributes
22
+ }
23
+ end
24
+
25
+ def simple_auth_attributes
26
+ returning attributes.dup do |attributes|
27
+ {"name" => "string", "email" => "string"}.each do |name, type|
28
+ attributes << Rails::Generator::GeneratedAttribute.new(name, type) unless attributes.any?{|a| a.name == name }
29
+ end
30
+ end
31
+ end
32
+
33
+ def add_options!(opt)
34
+ opt.on('--migration', 'Create migration'){|v| options[:migration] = true }
35
+ end
36
+
37
+ end
@@ -0,0 +1,6 @@
1
+ require "simple_authentication/authenticator"
2
+ require "simple_authentication/controller_methods"
3
+ require "simple_authentication/model_methods"
4
+
5
+ ActionController::Base.send(:include, SimpleAuthentication::ControllerMethods::Application)
6
+ I18n.load_path.unshift(File.join(File.dirname(__FILE__), '..', 'config', 'locales', 'simple_authentication.yml'))
@@ -0,0 +1,57 @@
1
+ module SimpleAuthentication
2
+
3
+
4
+ class Authenticator
5
+
6
+ def initialize(controller)
7
+ @controller = controller
8
+ end
9
+
10
+ def authenticate
11
+ raise NotImplementedError
12
+ end
13
+
14
+ def authentication_possible?
15
+ false
16
+ end
17
+
18
+ def authenticate_if_possible
19
+ return false unless authentication_possible?
20
+ authenticate
21
+ end
22
+
23
+ private
24
+
25
+ def controller
26
+ @controller
27
+ end
28
+
29
+ def params
30
+ controller.params
31
+ end
32
+
33
+
34
+ def self.authenticators
35
+ @authenticators ||= []
36
+ end
37
+
38
+
39
+ def self.inherited(klass)
40
+ authenticators << klass
41
+ end
42
+
43
+ public
44
+
45
+ def self.identifier
46
+ name.split('::').last.gsub(/Authenticator$/, '').underscore
47
+ end
48
+
49
+ def self.authenticator_for(identifier)
50
+ authenticators.detect{|a| a.identifier == identifier }
51
+ end
52
+
53
+
54
+ end
55
+
56
+
57
+ end
@@ -0,0 +1,157 @@
1
+ module SimpleAuthentication
2
+
3
+
4
+ module ControllerMethods
5
+
6
+
7
+ module Application
8
+
9
+
10
+ def self.included(controller)
11
+ [:logged_in?, :logged_out?, :current_user].each do |m|
12
+ controller.helper_method m
13
+ end
14
+ end
15
+
16
+
17
+ def current_user
18
+ @_current_user ||= User.find_by_id(session[:current_user_id])
19
+ end
20
+
21
+ def logged_in?
22
+ !!current_user
23
+ end
24
+
25
+ def logged_out?
26
+ !logged_in?
27
+ end
28
+
29
+
30
+ private
31
+
32
+ def current_user=(user)
33
+ @_current_user = user
34
+ session[:current_user_id] = user && user.id
35
+ end
36
+
37
+
38
+ def require_login
39
+ unless logged_in?
40
+ flash[:error] = I18n.t('simple_authentication.login_required')
41
+ redirect_to new_login_url
42
+ end
43
+ end
44
+
45
+ def require_logout
46
+ unless logged_out?
47
+ flash[:error] = I18n.t('simple_authentication.logout_required')
48
+ redirect_to login_url
49
+ end
50
+ end
51
+
52
+
53
+ end
54
+
55
+
56
+ module Logins
57
+
58
+
59
+ def show
60
+ @user = current_user
61
+ end
62
+
63
+
64
+ #If there is only one authenticator installed or if one has been specified,
65
+ #this will render its login form.
66
+ #
67
+ #If there is more than one installed and none has been specified, it will
68
+ #list all authenticators as links to their login forms.
69
+ def new
70
+ if authenticator
71
+ send(authenticator.identifier) if respond_to?(authenticator.identifier)
72
+ render :action => authenticator.identifier
73
+ else
74
+ @authenticators = SimpleAuthentication::Authenticator.authenticators
75
+ end
76
+ end
77
+
78
+
79
+ #Create a login - aka log in the user
80
+ #
81
+ #An authenticator must be specified for authentication to continue. The
82
+ #authenticator will receive the controller as its only parameter and either
83
+ #
84
+ # * respond with :ok, signalling that it takes care of everything
85
+ # * or, if the authentication was successful, return the User that
86
+ # was authenticated. current_user will then be set to this user.
87
+ # * or, if the authentication failed, return nil or false
88
+ def create
89
+ if params[:authenticator]
90
+ if authenticator && user = authenticator.new(self).authenticate
91
+ unless user == :ok#Authenticator doesn't want any help
92
+ self.current_user = user
93
+ authentication_successful
94
+ end
95
+ elsif authenticator
96
+ #First, see if the authenticator has defined a message of its own
97
+ message = I18n.t(:login_failed, :default => "##not found##",#This is hacky
98
+ :scope => [:simple_authentication, :authenticators, authenticator.identifier])
99
+ #If not, use default
100
+ message = I18n.t('simple_authentication.login_failed') if message == "##not found##"
101
+
102
+ authentication_failed message
103
+ else
104
+ authentication_failed
105
+ end
106
+ else
107
+ authentication_failed
108
+ end
109
+ end
110
+
111
+
112
+ #Destroy the login - aka "log out"
113
+ def destroy
114
+ self.current_user = nil
115
+ redirect_to new_login_url
116
+ end
117
+
118
+
119
+ private
120
+
121
+ def authenticator
122
+ @authenticator ||= SimpleAuthentication::Authenticator.authenticators.size == 1 ?
123
+ SimpleAuthentication::Authenticator.authenticators.first :
124
+ SimpleAuthentication::Authenticator.authenticator_for(params[:authenticator])
125
+ end
126
+
127
+
128
+ def authentication_successful(message = I18n.t('simple_authentication.login_successful'))
129
+ flash[:notice] = message
130
+ redirect_to login_url
131
+ end
132
+
133
+ def authentication_failed(message = I18n.t('simple_authentication.login_failed'))
134
+ flash[:error] = message
135
+ redirect_to params[:authenticator].blank? ?
136
+ new_login_url :
137
+ new_login_with_authenticator_url(:authenticator => authenticator.identifier)
138
+ end
139
+
140
+
141
+ module Behavior
142
+
143
+ def self.included(controller)
144
+ controller.before_filter :require_login, :except => [:new, :create]
145
+ controller.before_filter :require_logout, :only => [:new, :create]
146
+ end
147
+
148
+ end
149
+
150
+
151
+ end
152
+
153
+
154
+ end
155
+
156
+
157
+ end
@@ -0,0 +1,61 @@
1
+ module SimpleAuthentication
2
+ module ModelMethods
3
+ module User
4
+
5
+ def self.model_classes
6
+ @model_classes ||= []
7
+ end
8
+
9
+ #Keep a track of modules/classes that include this module
10
+ def self.included(m)
11
+ model_classes << m
12
+ m.extend ClassMethods
13
+ end
14
+
15
+ #When another module is included in this one,
16
+ #include that module in all modules/classes that include this module
17
+ #This way if another module is included in this one after this module
18
+ #has been included elsewhere, those other classes will still have access
19
+ #to the methods in the included module. Example:
20
+ #
21
+ #User.include(SimpleAuthentication::ModelMethods::User)
22
+ #SimpleAuthentication::ModelMethods::User.include(OtherModule)
23
+ #
24
+ #Normally, User wouldn't have access to OtherModule's methods,
25
+ #but we're passing them on. This way, other plugins can add
26
+ #methods to User without having to load/touch it directly.
27
+ def self.include(m)
28
+ self.model_classes.each{|c| c.send(:include, m) }
29
+ super
30
+ end
31
+
32
+ def display_name
33
+ name
34
+ end
35
+
36
+
37
+ #Class methods for User
38
+ #
39
+ #Methods included here are automatically added to User as class methods
40
+ module ClassMethods
41
+
42
+ def self.model_classes
43
+ @model_classes ||= []
44
+ end
45
+
46
+ #Keep track of model classes (in practise this is just User)
47
+ def self.extended(m)
48
+ model_classes << m
49
+ end
50
+
51
+ #When methods are included here, add them to User's eigenclass
52
+ def self.include(m)
53
+ model_classes.each{|c| c.extend m }
54
+ super
55
+ end
56
+
57
+ end
58
+
59
+ end
60
+ end
61
+ end
@@ -0,0 +1 @@
1
+ require "simple_authentication"
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :simple_authentication do
3
+ # # Task goes here
4
+ # end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: toretore-simple_authentication
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tore Darell
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-09 00:00:00 -07:00
12
+ date: 2009-03-16 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -21,8 +21,31 @@ extensions: []
21
21
 
22
22
  extra_rdoc_files: []
23
23
 
24
- files: []
25
-
24
+ files:
25
+ - lib/simple_authentication.rb
26
+ - lib/simple_authentication
27
+ - lib/simple_authentication/authenticator.rb
28
+ - lib/simple_authentication/controller_methods.rb
29
+ - lib/simple_authentication/model_methods.rb
30
+ - rails/uninstall.rb
31
+ - rails/install.rb
32
+ - rails/init.rb
33
+ - config/routes.rb
34
+ - config/locales
35
+ - config/locales/simple_authentication.yml
36
+ - app/views
37
+ - app/views/logins
38
+ - app/views/logins/new.html.erb
39
+ - app/views/logins/show.html.erb
40
+ - app/controllers
41
+ - app/controllers/logins_controller.rb
42
+ - app/models
43
+ - app/models/user.rb
44
+ - generators/simple_authentication
45
+ - generators/simple_authentication/simple_authentication_generator.rb
46
+ - generators/simple_authentication/USAGE
47
+ - generators/simple_authentication/templates
48
+ - tasks/simple_authentication_tasks.rake
26
49
  has_rdoc: false
27
50
  homepage: http://github.com/toretore/simple_authentication
28
51
  post_install_message: