authentasaurus 0.8.8 → 0.8.9

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 (146) hide show
  1. data/CHANGELIST +2 -6
  2. data/TODO +0 -5
  3. data/app/controllers/areas_controller.rb +1 -1
  4. data/app/controllers/groups_controller.rb +1 -1
  5. data/app/controllers/permissions_controller.rb +1 -1
  6. data/app/controllers/recoveries_controller.rb +1 -1
  7. data/app/controllers/registrations_controller.rb +1 -1
  8. data/app/controllers/sessions_controller.rb +2 -2
  9. data/app/controllers/user_invitations_controller.rb +1 -1
  10. data/app/controllers/users_controller.rb +1 -1
  11. data/app/controllers/validations_controller.rb +1 -1
  12. data/app/models/area.rb +1 -1
  13. data/app/models/authentasaurus_emailer.rb +6 -6
  14. data/app/models/group.rb +1 -1
  15. data/app/models/permission.rb +1 -1
  16. data/app/models/recovery.rb +1 -1
  17. data/app/models/user_invitation.rb +1 -1
  18. data/app/models/validation.rb +1 -1
  19. data/app/views/authentasaurus_emailer/invitation_mail.html.erb +2 -2
  20. data/app/views/authentasaurus_emailer/recovery_mail.html.erb +2 -2
  21. data/app/views/authentasaurus_emailer/validation_mail.html.erb +2 -2
  22. data/lib/authentasaurus.rb +18 -52
  23. metadata +2 -136
  24. data/app/models/authentasaurus_session.rb +0 -3
  25. data/lib/authentasaurus/ac/acts_as_overrider.rb +0 -48
  26. data/lib/authentasaurus/ac/controllers/areas_controller.rb +0 -75
  27. data/lib/authentasaurus/ac/controllers/groups_controller.rb +0 -77
  28. data/lib/authentasaurus/ac/controllers/permissions_controller.rb +0 -76
  29. data/lib/authentasaurus/ac/controllers/recoveries_controller.rb +0 -67
  30. data/lib/authentasaurus/ac/controllers/registrations_controller.rb +0 -40
  31. data/lib/authentasaurus/ac/controllers/sessions_controller.rb +0 -57
  32. data/lib/authentasaurus/ac/controllers/user_invitations_controller.rb +0 -45
  33. data/lib/authentasaurus/ac/controllers/users_controller.rb +0 -75
  34. data/lib/authentasaurus/ac/controllers/validations_controller.rb +0 -48
  35. data/lib/authentasaurus/ac/routing.rb +0 -86
  36. data/lib/authentasaurus/ar/acts_as_authenticatable.rb +0 -107
  37. data/lib/authentasaurus/ar/acts_as_authenticatable_validatable.rb +0 -35
  38. data/lib/authentasaurus/ar/acts_as_overrider.rb +0 -49
  39. data/lib/authentasaurus/ar/authenticatable.rb +0 -64
  40. data/lib/authentasaurus/ar/migrations.rb +0 -165
  41. data/lib/authentasaurus/ar/models/recovery.rb +0 -34
  42. data/lib/authentasaurus/ar/models/session.rb +0 -78
  43. data/lib/authentasaurus/ar/models/user_invitation.rb +0 -30
  44. data/lib/authentasaurus/ar/models/validation.rb +0 -27
  45. data/lib/authentasaurus/arel/acts_as_authenticatable.rb +0 -67
  46. data/lib/authentasaurus/arel/authenticatable.rb +0 -21
  47. data/lib/authentasaurus/authorization.rb +0 -344
  48. data/lib/authentasaurus/configuration.rb +0 -30
  49. data/lib/authentasaurus/railtie.rb +0 -10
  50. data/lib/generators/authentasaurus/install/USAGE +0 -5
  51. data/lib/generators/authentasaurus/install/install_generator.rb +0 -10
  52. data/lib/generators/authentasaurus/install/templates/authentasaurus_tasks.rake +0 -48
  53. data/lib/generators/authentasaurus/install/templates/defaults.yml +0 -57
  54. data/lib/generators/authentasaurus/views/USAGE +0 -13
  55. data/lib/generators/authentasaurus/views/templates/areas/edit.html.erb +0 -7
  56. data/lib/generators/authentasaurus/views/templates/areas/index.html.erb +0 -11
  57. data/lib/generators/authentasaurus/views/templates/areas/new.html.erb +0 -7
  58. data/lib/generators/authentasaurus/views/templates/areas/show.html.erb +0 -7
  59. data/lib/generators/authentasaurus/views/templates/authentasaurus_emailer/invitation_mail.html.erb +0 -4
  60. data/lib/generators/authentasaurus/views/templates/authentasaurus_emailer/recovery_mail.html.erb +0 -7
  61. data/lib/generators/authentasaurus/views/templates/authentasaurus_emailer/validation_mail.html.erb +0 -6
  62. data/lib/generators/authentasaurus/views/templates/groups/edit.html.erb +0 -7
  63. data/lib/generators/authentasaurus/views/templates/groups/index.html.erb +0 -11
  64. data/lib/generators/authentasaurus/views/templates/groups/new.html.erb +0 -7
  65. data/lib/generators/authentasaurus/views/templates/groups/show.html.erb +0 -7
  66. data/lib/generators/authentasaurus/views/templates/permissions/edit.html.erb +0 -19
  67. data/lib/generators/authentasaurus/views/templates/permissions/index.html.erb +0 -17
  68. data/lib/generators/authentasaurus/views/templates/permissions/new.html.erb +0 -19
  69. data/lib/generators/authentasaurus/views/templates/permissions/show.html.erb +0 -7
  70. data/lib/generators/authentasaurus/views/templates/recoveries/edit.html.erb +0 -24
  71. data/lib/generators/authentasaurus/views/templates/recoveries/new.html.erb +0 -15
  72. data/lib/generators/authentasaurus/views/templates/registrations/new.html.erb +0 -21
  73. data/lib/generators/authentasaurus/views/templates/sessions/new.html.erb +0 -41
  74. data/lib/generators/authentasaurus/views/templates/sessions/no_access.html.erb +0 -1
  75. data/lib/generators/authentasaurus/views/templates/user_invitations/index.html.erb +0 -14
  76. data/lib/generators/authentasaurus/views/templates/user_invitations/new.html.erb +0 -9
  77. data/lib/generators/authentasaurus/views/templates/users/edit.html.erb +0 -27
  78. data/lib/generators/authentasaurus/views/templates/users/index.html.erb +0 -19
  79. data/lib/generators/authentasaurus/views/templates/users/new.html.erb +0 -27
  80. data/lib/generators/authentasaurus/views/templates/users/show.html.erb +0 -11
  81. data/lib/generators/authentasaurus/views/templates/validations/resend_validation_email.html.erb +0 -6
  82. data/lib/generators/authentasaurus/views/templates/validations/validate.html.erb +0 -6
  83. data/lib/generators/authentasaurus/views/views_generator.rb +0 -76
  84. data/rdoc/apple-touch-icon.png +0 -0
  85. data/rdoc/classes/Authentasaurus.html +0 -65
  86. data/rdoc/classes/Authentasaurus/Ac.html +0 -72
  87. data/rdoc/classes/Authentasaurus/Ac/ActsAsOverrider.html +0 -104
  88. data/rdoc/classes/Authentasaurus/Ac/ActsAsOverrider/ClassMethods.html +0 -397
  89. data/rdoc/classes/Authentasaurus/Ac/Routing.html +0 -61
  90. data/rdoc/classes/Authentasaurus/Ac/Routing/InstanceMethods.html +0 -390
  91. data/rdoc/classes/Authentasaurus/Ar.html +0 -77
  92. data/rdoc/classes/Authentasaurus/Ar/ActsAsAuthenticatable.html +0 -107
  93. data/rdoc/classes/Authentasaurus/Ar/ActsAsAuthenticatable/ClassMethods.html +0 -168
  94. data/rdoc/classes/Authentasaurus/Ar/ActsAsAuthenticatable/InstanceMethods.html +0 -357
  95. data/rdoc/classes/Authentasaurus/Ar/ActsAsAuthenticatableValidatable.html +0 -109
  96. data/rdoc/classes/Authentasaurus/Ar/ActsAsAuthenticatableValidatable/ClassMethods.html +0 -113
  97. data/rdoc/classes/Authentasaurus/Ar/ActsAsAuthenticatableValidatable/InstanceMethods.html +0 -54
  98. data/rdoc/classes/Authentasaurus/Ar/ActsAsOverrider.html +0 -104
  99. data/rdoc/classes/Authentasaurus/Ar/ActsAsOverrider/ClassMethods.html +0 -320
  100. data/rdoc/classes/Authentasaurus/Ar/Authenticatable.html +0 -104
  101. data/rdoc/classes/Authentasaurus/Ar/Authenticatable/ClassMethods.html +0 -182
  102. data/rdoc/classes/Authentasaurus/Ar/Migrations.html +0 -63
  103. data/rdoc/classes/Authentasaurus/Ar/Migrations/Columns.html +0 -68
  104. data/rdoc/classes/Authentasaurus/Ar/Migrations/Columns/InstanceMethods.html +0 -106
  105. data/rdoc/classes/Authentasaurus/Ar/Migrations/Tables.html +0 -68
  106. data/rdoc/classes/Authentasaurus/Ar/Migrations/Tables/InstanceMethods.html +0 -703
  107. data/rdoc/classes/Authentasaurus/Arel.html +0 -65
  108. data/rdoc/classes/Authentasaurus/Arel/ActsAsAuthenticatable.html +0 -107
  109. data/rdoc/classes/Authentasaurus/Arel/ActsAsAuthenticatable/ClassMethods.html +0 -139
  110. data/rdoc/classes/Authentasaurus/Arel/ActsAsAuthenticatable/InstanceMethods.html +0 -113
  111. data/rdoc/classes/Authentasaurus/Arel/Authenticatable.html +0 -105
  112. data/rdoc/classes/Authentasaurus/Arel/Authenticatable/ClassMethods.html +0 -119
  113. data/rdoc/classes/Authentasaurus/Authorization.html +0 -230
  114. data/rdoc/classes/Authentasaurus/Authorization/ActionController.html +0 -63
  115. data/rdoc/classes/Authentasaurus/Authorization/ActionController/ClassMethods.html +0 -263
  116. data/rdoc/classes/Authentasaurus/Authorization/ActionController/InstanceMethods.html +0 -54
  117. data/rdoc/classes/Authentasaurus/Authorization/ActionView.html +0 -54
  118. data/rdoc/classes/Authentasaurus/Authorization/CommonInstanceMethods.html +0 -240
  119. data/rdoc/created.rid +0 -1
  120. data/rdoc/css/main.css +0 -278
  121. data/rdoc/css/panel.css +0 -383
  122. data/rdoc/css/reset.css +0 -53
  123. data/rdoc/favicon.ico +0 -0
  124. data/rdoc/files/README_rdoc.html +0 -366
  125. data/rdoc/files/TODO.html +0 -64
  126. data/rdoc/files/lib/authentasaurus/ac/acts_as_overrider_rb.html +0 -52
  127. data/rdoc/files/lib/authentasaurus/ac/routing_rb.html +0 -60
  128. data/rdoc/files/lib/authentasaurus/ar/acts_as_authenticatable_rb.html +0 -52
  129. data/rdoc/files/lib/authentasaurus/ar/acts_as_authenticatable_validatable_rb.html +0 -52
  130. data/rdoc/files/lib/authentasaurus/ar/acts_as_overrider_rb.html +0 -52
  131. data/rdoc/files/lib/authentasaurus/ar/authenticatable_rb.html +0 -52
  132. data/rdoc/files/lib/authentasaurus/ar/migrations_rb.html +0 -52
  133. data/rdoc/files/lib/authentasaurus/arel/acts_as_authenticatable_rb.html +0 -52
  134. data/rdoc/files/lib/authentasaurus/arel/authenticatable_rb.html +0 -72
  135. data/rdoc/files/lib/authentasaurus/authorization_rb.html +0 -208
  136. data/rdoc/i/arrows.png +0 -0
  137. data/rdoc/i/results_bg.png +0 -0
  138. data/rdoc/i/tree_bg.png +0 -0
  139. data/rdoc/index.html +0 -15
  140. data/rdoc/js/jquery-1.3.2.min.js +0 -19
  141. data/rdoc/js/jquery-effect.js +0 -593
  142. data/rdoc/js/main.js +0 -22
  143. data/rdoc/js/searchdoc.js +0 -628
  144. data/rdoc/panel/index.html +0 -71
  145. data/rdoc/panel/search_index.js +0 -1
  146. data/rdoc/panel/tree.js +0 -1
@@ -1,34 +0,0 @@
1
- module Authentasaurus::Ar::Models
2
- module Recovery
3
- extend ActiveSupport::Concern
4
-
5
- included do
6
- require "digest/sha1"
7
-
8
- unloadable
9
-
10
- belongs_to :user
11
-
12
- before_validation :make_token!, :on => :create
13
- before_save :send_recovery
14
-
15
- scope :valid, lambda { { :conditions => ["updated_at <= ?", Authentasaurus::Configuration.instance.configuration[:modules][:recoverable][:token_expires_after].days.from_now] } }
16
-
17
- validates_uniqueness_of :user_id
18
- validates_presence_of :email
19
- validates_presence_of :user_id, :message => :"recovery.user_id.blank"
20
- validates_format_of :email, :with => %r{[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}}
21
- end
22
-
23
- module ClassMethods
24
- end
25
-
26
- def make_token!
27
- self.token = Digest::SHA1.hexdigest "#{Time.now.to_i} #{rand} #{self.email}"
28
- end
29
-
30
- def send_recovery
31
- AuthentasaurusEmailer.deliver_recovery_mail(self.user, self.token) if Authentasaurus::Configuration.instance.configuration[:modules][:recoverable][:send_email]
32
- end
33
- end
34
- end
@@ -1,78 +0,0 @@
1
- # This class represents a session model, a session authenticates a username and a password.
2
- #
3
- # A session behaves just like an ActiveRecord model
4
- module Authentasaurus::Ar::Models
5
- module Session
6
- extend ActiveSupport::Concern
7
-
8
- included do # :nodoc:
9
- include ActiveModel::Validations
10
- include ActiveModel::Conversion
11
-
12
- attr_accessor :username, :password, :remember
13
- validates_presence_of :username, :password
14
-
15
- attr_reader :user
16
- end
17
-
18
- module ClassMethods
19
- # Takes a hash of attributes keys and values just like new and authenticates the information.
20
- # Returns true or false
21
- def create(*attrs)
22
- attributes = attrs.extract_options!
23
- attrs = attrs.flatten
24
- self_obj = self.new attributes
25
- self_obj.save(attrs)
26
- return self_obj
27
- end
28
-
29
- # Takes an id (usually from an ActiveController session) and returns a User object
30
- def current_user(id, session_type = Authentasaurus::Configuration.instance.user_model.to_sym)
31
- session_type.to_s.camelize.constantize.find id
32
- end
33
- end
34
-
35
- # Takes a hash of attributes keys and values just like ActiveRecord models
36
- def initialize(attributes = nil)
37
- if attributes
38
- attributes.each do |key,value|
39
- send(key.to_s + '=', value)
40
- end
41
- else
42
- self.remember = false
43
- end
44
- end
45
-
46
- # Authenticates the information saved in the attributes
47
- # Returns true or false
48
- def save(*session_types)
49
- session_types = session_types.flatten
50
-
51
- if session_types.empty?
52
- user_model = Authentasaurus::Configuration.instance.user_model.to_sym
53
- session_types = [user_model]
54
- end
55
-
56
- ret = true
57
- session_types.each do |type|
58
- @user = type.to_s.camelize.constantize.authenticate(self.username.downcase, self.password, self.remember == "1")
59
- if @user.nil?
60
- self.errors.add_to_base I18n.t(:invalid_login, :scope => [:authentasaurus, :messages, :sessions])
61
- ret &= false
62
- else
63
- ret = true
64
- break
65
- end
66
- end
67
- ret
68
- end
69
-
70
- def new_record? #:nodoc:
71
- true
72
- end
73
-
74
- def persisted? #:nodoc:
75
- false
76
- end
77
- end
78
- end
@@ -1,30 +0,0 @@
1
- module Authentasaurus::Ar::Models
2
- module UserInvitation
3
- extend ActiveSupport::Concern
4
-
5
- included do
6
- require 'digest/sha2'
7
- validates_presence_of :email
8
- validates_uniqueness_of :email, :scope => :token
9
- validates_format_of :email, :with => %r{[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}}
10
-
11
- before_validation :create_token
12
- #send email
13
- after_create :send_invitation
14
- end
15
-
16
- module ClassMethods
17
- end
18
-
19
- def send_invitation
20
- AuthentasaurusEmailer.deliver_invitation_mail(self.email, self.token) if Authentasaurus::Configuration.instance.configuration[:modules][:invitable][:send_email]
21
- end
22
-
23
- private
24
- def create_token
25
- return if self.email.nil? || self.email.blank?
26
- string_to_hash=self.email + "invitable.olation" + self.email.hash.to_s
27
- self.token = Digest::SHA2.hexdigest(string_to_hash)
28
- end
29
- end
30
- end
@@ -1,27 +0,0 @@
1
- module Authentasaurus::Ar::Models
2
- module Validation
3
- extend ActiveSupport::Concern
4
-
5
- included do
6
- belongs_to :user, :polymorphic => true
7
-
8
- # Check that everything is there
9
- validates_presence_of :user_id, :validation_code, :user_type, :email
10
- # Check foreign keys
11
- validates_associated :user
12
- # Check unique user
13
- validates_uniqueness_of :user_id, :scope => [:user_type, :email]
14
- validates_uniqueness_of :validation_code
15
-
16
- #send email
17
- after_create :send_validation
18
- end
19
-
20
- module ClassMethods
21
- end
22
-
23
- def send_validation
24
- AuthentasaurusEmailer.deliver_validation_mail(self.user.name, self.email, self.validation_code) if Authentasaurus::Configuration.instance.configuration[:modules][:validatable][:send_email]
25
- end
26
- end
27
- end
@@ -1,67 +0,0 @@
1
- module Authentasaurus::Arel
2
- module ActsAsAuthenticatable
3
- extend ActiveSupport::Concern
4
-
5
- module ClassMethods
6
- ## Authenticates the username and password
7
- def authenticate(username, password, remember = false)
8
- case(self.format)
9
- when ActiveResource::Formats::XmlFormat
10
- user = self.new Hash.from_xml(self.post(:signin,:username => username, :password => password, :remember => remember).body).values.first
11
- when ActiveResource::Formats::JsonFormat
12
- user = self.new ActiveSupport::JSON.decode(self.post(:signin,:username => username, :password => password, :remember => remember).body)
13
- else
14
- user = self.new Hash.from_xml(self.post(:signin,:username => username, :password => password, :remember => remember).body).values.first
15
- end
16
-
17
- unless user.nil?
18
- if self.sync && !self.sync_to.nil?
19
- last_update = user.attributes.delete "updated_at"
20
- local_user = self.sync_to.find_or_initialize_by_username user.username, user.attributes
21
-
22
- unless local_user.new_record?
23
- last_update_datetime = (last_update.kind_of?(String)) ? (DateTime.parse(last_update)) : (last_update)
24
-
25
- if local_user.updated_at < last_update_datetime
26
- local_user.update_attributes user.attributes
27
- end
28
- else
29
- local_user.password = password
30
- local_user.password_confirmation = password
31
-
32
- local_user.hashed_password = user.hashed_password
33
- local_user.password_seed = user.password_seed
34
-
35
- self.sync_to.default_data.each do |key,value|
36
- local_user.send(key.to_s + '=', value)
37
- end
38
-
39
- local_user.save
40
- end
41
- end
42
- end
43
- return user
44
- end
45
- end
46
-
47
- def sync
48
- if self.class.sync && !self.class.sync_to.nil?
49
- user = self.dup
50
- last_update = user.attributes.delete "updated_at"
51
- local_user = self.class.sync_to.find_or_initialize_by_username user.username, user.attributes
52
-
53
- unless local_user.new_record?
54
- local_user.update_attributes user.attributes
55
- else
56
- self.sync_to.default_data.each do |key,value|
57
- local_user.send(key.to_s + '=', value)
58
- end
59
-
60
- local_user.save
61
- end
62
- else
63
- false
64
- end
65
- end
66
- end
67
- end
@@ -1,21 +0,0 @@
1
- module Authentasaurus::Arel
2
- module Authenticatable
3
- extend ActiveSupport::Concern
4
-
5
- module ClassMethods
6
- def authenticatable(*args)
7
- self.unloadable
8
- options = args.extract_options!
9
-
10
- self.site = options[:site] || Authentasaurus::Configuration.instance.configuration[:modules][:remote][self.name.underscore.gsub(/_sync/, "").to_sym][:site]
11
- self.element_name = options[:session_element].try(:to_s) || Authentasaurus::Configuration.instance.configuration[:modules][:remote][self.name.underscore.gsub(/_sync/, "").to_sym][:session_element]
12
- self.sync = options[:sync] || Authentasaurus::Configuration.instance.configuration[:modules][:remote][self.name.underscore.gsub(/_sync/, "").to_sym][:sync]
13
- self.sync_to = options[:sync_to].try(:to_s).try(:camelize).try(:constantize) || Authentasaurus::Configuration.instance.configuration[:modules][:remote][self.name.underscore.gsub(/_sync/, "").to_sym][:sync_to].camelize.constantize
14
-
15
-
16
- # include authentication methods
17
- include Authentasaurus::Arel::ActsAsAuthenticatable
18
- end
19
- end
20
- end
21
- end
@@ -1,344 +0,0 @@
1
- # =Authorization Helpers
2
- # The authorization module provides helpers for restricting access to your controllers.
3
- #
4
- # Each controller is considered an area in Authentasaurus, for example UsersController stands for
5
- # "users" area. Each area must be defined in the Areas table so Authentasaurus can control access
6
- # to it.
7
- #
8
- # Authentasaurus provides a simple rake task to define areas automatically:
9
- #
10
- # rake authentasaurus:create_areas
11
- #
12
- # == Restricting Access
13
- # You can restrict access to any controller using one of the following class methods on your
14
- # controller.
15
- #
16
- # At login, Authentasaurus will load the permissions of the group the user belongs to in the session
17
- # and will use them to authorize access to the area.
18
- #
19
- # There are three levels of restriction in Authentasaurus, login, read and write; with the exception
20
- # of login, read and write don't have any logic behind them; they are defined using the Permission and
21
- # are only symbolically named i.e.: you can use read instead of write and vice versa it only depends on
22
- # how you use them.
23
- #
24
- # === Restricting access to logged in users
25
- # You can restrict access to an area to logged in users only using the ActionController::ClassMethods#require_login
26
- # class method.
27
- #
28
- # Consider the following example restricting access to the pages controller to only logged in users:
29
- #
30
- # class PagesController < ActionController::Base
31
- # require_login
32
- # ...
33
- # end
34
- #
35
- # You can also specify which actions to restrict:
36
- #
37
- # require_login :new, :create, :index
38
- #
39
- # Authentasaurus will automatically redirect users to the sign-in page if they try accessing the area
40
- # while they are not logged in. Once the user logs in he/she is redirected back to his/her original destination
41
- # unless you explicitly skip that behaviour:
42
- #
43
- # require_login :skip_request => true
44
- #
45
- # === Restricting access according to permissions
46
- # Unlike the login restriction, permissions restrictions checks if the user is logged in and has
47
- # the permission to access the area.
48
- #
49
- # Users get permissions from their parent group and permissions are dynamically set in the database.
50
- #
51
- # Authentasaurus currently supports only two permissions, read and write, both permissions are
52
- # symbolically named, they have no meaning.
53
- #
54
- # ==== Restricting access to users with read permission
55
- #
56
- # class PagesController < ActionController::Base
57
- # require_read
58
- # ...
59
- # end
60
- #
61
- # ActionController::ClassMethods#require_read takes the same options as ActionController::ClassMethods#require_login
62
- # and ActionController::ClassMethods#require_write
63
- #
64
- # ==== Restricting access to users with write permission
65
- #
66
- # class PagesController < ActionController::Base
67
- # require_write
68
- # ...
69
- # end
70
- #
71
- # ActionController::ClassMethods#require_write takes the same options as ActionController::ClassMethods#require_login
72
- # and ActionController::ClassMethods#require_read
73
- #
74
- # == Checking if the user is logged in in actions or views
75
- # Along with the class helpers, Authentasaurus includes a helper to check if the user is logged in
76
- # inside any of your actions:
77
- #
78
- # is_logged_in?
79
- #
80
- # Check ActionController::CommonInstanceMethods#is_logged_in? for more information.
81
- #
82
- # == Checking permissions in actions or views
83
- # You can also check if the logged in user has a certain permission.
84
- #
85
- # Consider the following example to check if the logged in user has read permission on the current
86
- # area
87
- #
88
- # has?(:read)
89
- #
90
- # You can also check permissions on an area while in another, for example to check if the current
91
- # user has write permission on the users area:
92
- #
93
- # has?(:write,:users)
94
- #
95
- # Check ActionController::CommonInstanceMethods#has? for more information.
96
- #
97
- # == Retrieving the current user in actions or views
98
- # To get the logged in user you can use the following helper:
99
- #
100
- # current_user
101
- #
102
- # Check ActionController::CommonInstanceMethods#current_user for more information.
103
- module Authentasaurus::Authorization
104
- module CommonInstanceMethods
105
- # Returns an object of the current user
106
- #
107
- # <b>Parameters:</b>
108
- #
109
- # user_model - The model class representing a user (User by default)
110
- def current_user(user_model = nil)#:doc:
111
- user_model = Authentasaurus::Configuration.instance.user_model.camelize.constantize if user_model.nil?
112
- return user_model.find session[:user_id] if session[:user_id]
113
- end
114
-
115
- # Checks if the current user has the appropriate permission
116
- #
117
- # <b>ex</b>:
118
- # has?(:write) or has?(:read, :users)
119
- #
120
- # <b>Parameters:</b>
121
- #
122
- # permission - The permission to check, either :read or :write
123
- # area - The area to check the permission on, by default checks the current area.
124
- def has?(permission, area = nil) #:doc:
125
- return false unless is_logged_in?
126
- check = false
127
- case permission
128
- when :write
129
- unless area
130
- user_permissions = session[:user_permissions]
131
- check = user_permissions[:write].find { |perm| perm==self.controller_name || perm=="all" }
132
- else
133
- user_permissions = session[:user_permissions]
134
- check = user_permissions[:write].find { |perm| perm==area.to_s || perm=="all" }
135
- end
136
- when :read
137
- unless area
138
- user_permissions = session[:user_permissions]
139
- check = user_permissions[:read].find { |perm| perm==self.controller_name || perm=="all" }
140
- else
141
- user_permissions = session[:user_permissions]
142
- check = user_permissions[:read].find { |perm| perm==area.to_s || perm=="all" }
143
- end
144
- end
145
- return check
146
- end
147
-
148
- # Checks if the current user is logged in but takes no further action
149
- #
150
- # <b>Parameters:</b>
151
- #
152
- # user_model - The model class representing a user (User by default)
153
- def is_logged_in?(user_model = nil) #:doc:
154
- user_model = Authentasaurus::Configuration.instance.user_model.camelize.constantize if user_model.nil?
155
- unless user_model.find_by_id(session[:user_id]) || !defined?(cookie_login?)
156
- return cookie_login?(user_model)
157
- end
158
- return true
159
- end
160
- end
161
-
162
- module ActionController
163
- def self.included(base) # :nodoc:
164
- base.send :extend, ClassMethods
165
- base.send :include, InstanceMethods
166
- end
167
-
168
- module ClassMethods
169
-
170
- # Requires the user to login before accessing the actions specified
171
- #
172
- # <b>ex:</b> Tells Authentasaurus that the action destroy requires login and that Authentasaurus
173
- # shouldn't store the request in the session (typically for logout actions).
174
- #
175
- # require_login :destroy, :skip_request => true
176
- #
177
- # <b>Options</b>
178
- #
179
- # :skip_request - skips saving the original request (to redirect to after login)
180
- # :user_model - if defined, authentasaurus will use that model instead of the default "User"
181
- # :if - specifies a method, proc or string to call to determine if the authorization should occur
182
- # :unless - specifies a method, proc or string to call to determine if the authorization should not occur
183
- def require_login (*attrs)
184
- options = attrs.extract_options!.symbolize_keys
185
- attrs = attrs.flatten
186
-
187
- unless attrs.empty?
188
- before_filter :only => attrs, :if => options[:if], :unless => options[:unless] do |controller|
189
- controller.instance_eval {check_logged_in !options[:skip_request].nil?, options[:user_model]}
190
- end
191
- else
192
- before_filter :if => options[:if], :unless => options[:unless] do |c|
193
- c.instance_eval {check_logged_in !options[:skip_request].nil?, options[:user_model]}
194
- end
195
- end
196
- end
197
-
198
- # Requires the user to have a write permission to that area to access the actions specified
199
- #
200
- # <b>ex:</b> Tells Authentasaurus that the actions create_user and delete_user requires login and write
201
- # permission.
202
- #
203
- # require_write :create_user, :delete_user
204
- #
205
- # <b>Options</b>
206
- #
207
- # :skip_request - skips saving the original request (to redirect to after login)
208
- # :user_model - if defined, authentasaurus will use that model instead of the default "User"
209
- # :if - specifies a method, proc or string to call to determine if the authorization should occur
210
- # :unless - specifies a method, proc or string to call to determine if the authorization should not occur
211
- def require_write(*attrs)
212
- options = attrs.extract_options!.symbolize_keys
213
- attrs = attrs.flatten
214
-
215
-
216
- unless attrs.empty?
217
- before_filter :only => attrs, :if => options[:if], :unless => options[:unless] do |controller|
218
- controller.instance_eval { check_write_permissions !options[:skip_request].nil?, options[:user_model] }
219
- end
220
- else
221
- before_filter :if => options[:if], :unless => options[:unless] do |c|
222
- c.instance_eval {check_write_permissions !options[:skip_request].nil?, options[:user_model]}
223
- end
224
- end
225
- end
226
-
227
- # Requires the user to have a read permission to that area to access the actions specified
228
- #
229
- # <b>ex:</b> Tells Authentasaurus that the action show_user requires login and read permission.
230
- #
231
- # require_read :show_user
232
- #
233
- # <b>Options</b>
234
- #
235
- # :skip_request - skips saving the original request (to redirect to after login)
236
- # :user_model - if defined, authentasaurus will use that model instead of the default "User"
237
- # :if - specifies a method, proc or string to call to determine if the authorization should occur
238
- # :unless - specifies a method, proc or string to call to determine if the authorization should not occur
239
- def require_read(*attrs)
240
- options = attrs.extract_options!.symbolize_keys
241
- attrs = attrs.flatten
242
-
243
- unless attrs.empty?
244
- before_filter :only => attrs, :if => options[:if], :unless => options[:unless] do |controller|
245
- controller.instance_eval { check_read_permissions !options[:skip_request].nil?, options[:user_model] }
246
- end
247
- else
248
- before_filter :if => options[:if], :unless => options[:unless] do |c|
249
- c.instance_eval { check_read_permissions !options[:skip_request].nil?, options[:user_model] }
250
- end
251
- end
252
- end
253
- end
254
-
255
- module InstanceMethods #:nodoc:
256
- private
257
- include CommonInstanceMethods
258
-
259
- # Checks if the current user is logged in and redirects to the login path if the user is not logged in.
260
- #
261
- # If skip_request is set to true, the user won't be redirected to the original url after he/she logs in.
262
- def check_logged_in(skip_request = false, user_model = nil) #:nodoc:
263
- user_model = Authentasaurus::Configuration.instance.user_model.camelize.constantize if user_model.nil?
264
- unless is_logged_in?(user_model)
265
- login_required skip_request
266
- end
267
- end
268
-
269
- # Checks if the current user is logged in and has write permission over the current controller, redirects to no access
270
- # page if the user hasn't the permissions and redirects to the login path if the user is not logged in
271
- #
272
- # If skip_request is set to true, the user won't be redirected to the original url after he/she logs in.
273
- def check_write_permissions(skip_request = false, user_model = nil) #:nodoc:
274
- user_model = Authentasaurus::Configuration.instance.user_model.camelize.constantize if user_model.nil?
275
- if is_logged_in?(user_model) && user_model.new.respond_to?(:permissions)
276
- user_permissions = session[:user_permissions]
277
- check = user_permissions[:write].find { |perm| perm==self.controller_name || perm=="all" }
278
- unless check
279
- redirect_to no_access_authentasaurus_sessions_path
280
- end
281
- else
282
- login_required skip_request
283
- end
284
- end
285
-
286
- # Checks if the current user is logged in and has read permission over the current controller, redirects to no access
287
- # page if the user hasn't the permissions and redirects to the login path if the user is not logged in
288
- #
289
- # If skip_request is set to true, the user won't be redirected to the original url after he/she logs in.
290
- def check_read_permissions(skip_request = false, user_model = nil) #:nodoc:
291
- user_model = Authentasaurus::Configuration.instance.user_model.camelize.constantize if user_model.nil?
292
- if is_logged_in?(user_model) && user_model.new.respond_to?(:permissions)
293
- user_permissions = session[:user_permissions]
294
- check = user_permissions[:read].find { |perm| perm==self.controller_name || perm=="all" }
295
- unless check
296
- redirect_to no_access_authentasaurus_sessions_path
297
- end
298
- else
299
- login_required skip_request
300
- end
301
- end
302
-
303
- # Logs in the user through a remember me cookie
304
- def cookie_login?(user_model = nil) #:nodoc:
305
- user_model = Authentasaurus::Configuration.instance.user_model.camelize.constantize if user_model.nil?
306
-
307
- if cookies[:remember_me_token]
308
- user = user_model.find_by_remember_me_token cookies[:remember_me_token]
309
- if user.nil?
310
- cookies.delete :remember_me_token
311
- return false
312
- else
313
- session[:user_id] = user.id
314
- session[:user_permissions] = {:read => user.permissions.collect{|per| per.area.name if per.read}, :write => user.permissions.collect{|per| per.area.name if per.write}}
315
- return true
316
- end
317
- else
318
- return false
319
- end
320
- end
321
-
322
- # Redirects the user to the login page
323
- #
324
- # If skip_request is set to true, the user won't be redirected to the original url after he/she logs in.
325
- def login_required(skip_request) #:nodoc:
326
- unless(skip_request)
327
- session[:original_url]=request.url
328
- end
329
- flash.now[:alert] = t(:login_required, :scope => [:authentasaurus, :action_controller, :errors, :messages])
330
- redirect_to new_authentasaurus_session_path
331
- end
332
-
333
- def controller_instance #:nodoc:
334
- self
335
- end
336
- end
337
- end
338
-
339
- module ActionView # :nodoc:
340
- def self.included(base)
341
- base.send :include, CommonInstanceMethods
342
- end
343
- end
344
- end