hello-rails 0.0.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (235) hide show
  1. checksums.yaml +4 -4
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +97 -0
  4. data/Rakefile +23 -0
  5. data/app/controllers/hello/application_controller.rb +27 -0
  6. data/app/controllers/hello/authentication/sessions_controller.rb +81 -0
  7. data/app/controllers/hello/authentication/sign_in_controller.rb +39 -0
  8. data/app/controllers/hello/authentication/sudo_mode_controller.rb +40 -0
  9. data/app/controllers/hello/concerns/authentication/sign_in.rb +44 -0
  10. data/app/controllers/hello/concerns/management/forgot_password.rb +45 -0
  11. data/app/controllers/hello/concerns/management/reset_password.rb +33 -0
  12. data/app/controllers/hello/concerns/registration/sign_up.rb +64 -0
  13. data/app/controllers/hello/internationalization/locale_controller.rb +28 -0
  14. data/app/controllers/hello/management/accesses_controller.rb +29 -0
  15. data/app/controllers/hello/management/confirm_emails_controller.rb +41 -0
  16. data/app/controllers/hello/management/emails_controller.rb +67 -0
  17. data/app/controllers/hello/management/forgot_password_controller.rb +41 -0
  18. data/app/controllers/hello/management/passwords_controller.rb +57 -0
  19. data/app/controllers/hello/management/profiles_controller.rb +71 -0
  20. data/app/controllers/hello/management/reset_password_controller.rb +53 -0
  21. data/app/controllers/hello/registration/sign_up_controller.rb +63 -0
  22. data/app/helpers/hello/application_helper.rb +5 -0
  23. data/app/mailers/hello/mailer.rb +26 -0
  24. data/app/models/access.rb +2 -0
  25. data/app/models/credential.rb +2 -0
  26. data/app/models/email_credential.rb +2 -0
  27. data/app/models/password_credential.rb +2 -0
  28. data/app/models/user.rb +2 -0
  29. data/app/views/hello/authentication/_sign_in.html.erb +64 -0
  30. data/app/views/hello/authentication/new_session.html.erb +4 -0
  31. data/app/views/hello/authentication/sessions.html.erb +36 -0
  32. data/app/views/hello/authentication/sign_in.html.erb +3 -0
  33. data/app/views/hello/authentication/sudo_mode.html.erb +37 -0
  34. data/app/views/hello/internationalization/locales.html.erb +7 -0
  35. data/app/views/hello/mailer/confirm_email.html.erb +12 -0
  36. data/app/views/hello/mailer/forgot_password.html.erb +12 -0
  37. data/app/views/hello/mailer/welcome.html.erb +11 -0
  38. data/app/views/hello/management/accesses.html.erb +47 -0
  39. data/app/views/hello/management/cancel.html.erb +14 -0
  40. data/app/views/hello/management/email_credentials/expired_confirmation_token.html.erb +1 -0
  41. data/app/views/hello/management/email_credentials/index.html.erb +84 -0
  42. data/app/views/hello/management/password_credentials/_forgot_form.html.erb +17 -0
  43. data/app/views/hello/management/password_credentials/_reset_form.html.erb +39 -0
  44. data/app/views/hello/management/password_credentials/forgot.html.erb +10 -0
  45. data/app/views/hello/management/password_credentials/forgot_success.html.erb +26 -0
  46. data/app/views/hello/management/password_credentials/reset.html.erb +3 -0
  47. data/app/views/hello/management/password_credentials/show.html.erb +25 -0
  48. data/app/views/hello/management/user.html.erb +73 -0
  49. data/app/views/hello/registration/_sign_up.html.erb +86 -0
  50. data/app/views/hello/registration/sign_up.html.erb +3 -0
  51. data/app/views/hello/registration/sign_up_widget.html.erb +3 -0
  52. data/app/views/hello/shared/_errors.html.erb +11 -0
  53. data/app/views/hello/shared/_flash.html.erb +8 -0
  54. data/app/views/hello/shared/_nav_pills.html.erb +15 -0
  55. data/app/views/hello/shared/_session_expiration.html.erb +15 -0
  56. data/app/views/hello/shared/_settings.html.erb +41 -0
  57. data/config/locales/hello.en.yml +69 -0
  58. data/config/locales/hello.es.yml +71 -0
  59. data/config/locales/hello.fr.yml +71 -0
  60. data/config/locales/hello.pl.yml +71 -0
  61. data/config/locales/hello.pt-BR.yml +71 -0
  62. data/config/locales/hello.zh-CN.yml +71 -0
  63. data/config/locales/hello.zh-TW.yml +71 -0
  64. data/config/routes.rb +74 -0
  65. data/db/migrate/1_create_credentials.rb +17 -0
  66. data/db/migrate/2_create_accesses.rb +15 -0
  67. data/db/migrate/3_create_users.rb +23 -0
  68. data/lib/generators/hello/concerns/USAGE +8 -0
  69. data/lib/generators/hello/concerns/concerns_generator.rb +10 -0
  70. data/lib/generators/hello/from_devise/USAGE +8 -0
  71. data/lib/generators/hello/from_devise/from_devise_generator.rb +13 -0
  72. data/lib/generators/hello/from_devise/templates/from_devise.migration.rb +39 -0
  73. data/lib/generators/hello/install/USAGE +8 -0
  74. data/lib/generators/hello/install/install_generator.rb +94 -0
  75. data/lib/generators/hello/install/templates/application.html.erb +35 -0
  76. data/lib/generators/hello/install/templates/hello_helper.rb +16 -0
  77. data/lib/generators/hello/install/templates/initializer.rb +24 -0
  78. data/lib/generators/hello/install/templates/models/concerns/user/authorization.rb +21 -0
  79. data/lib/generators/hello/install/templates/models/user.rb +9 -0
  80. data/lib/generators/hello/install/templates/onboarding/index.html.erb +5 -0
  81. data/lib/generators/hello/install/templates/onboarding/onboarding_controller.rb +33 -0
  82. data/lib/generators/hello/install/templates/root/index.html.erb +7 -0
  83. data/lib/generators/hello/install/templates/root/root_controller.rb +6 -0
  84. data/lib/generators/hello/install/templates/users/controllers/users_controller.rb +71 -0
  85. data/lib/generators/hello/install/templates/users/views/users/index.html.erb +29 -0
  86. data/lib/generators/hello/install/templates/users/views/users/list.html.erb +35 -0
  87. data/lib/generators/hello/install/templates/users/views/users/new.html.erb +60 -0
  88. data/lib/generators/hello/install/templates/users/views/users/show.html.erb +74 -0
  89. data/lib/generators/hello/locales/USAGE +12 -0
  90. data/lib/generators/hello/locales/locales_generator.rb +64 -0
  91. data/lib/generators/hello/views/USAGE +8 -0
  92. data/lib/generators/hello/views/views_generator.rb +9 -0
  93. data/lib/hello-rails.rb +1 -0
  94. data/lib/hello.rb +36 -0
  95. data/lib/hello/CHANGES.md +26 -0
  96. data/lib/hello/business.rb +47 -0
  97. data/lib/hello/business/authentication/sign_in.rb +92 -0
  98. data/lib/hello/business/authentication/sign_out.rb +8 -0
  99. data/lib/hello/business/authentication/sudo_mode_authentication.rb +25 -0
  100. data/lib/hello/business/authentication/sudo_mode_expiration.rb +17 -0
  101. data/lib/hello/business/base.rb +33 -0
  102. data/lib/hello/business/internationalization/update_locale.rb +33 -0
  103. data/lib/hello/business/management/add_email.rb +19 -0
  104. data/lib/hello/business/management/cancel_account.rb +22 -0
  105. data/lib/hello/business/management/confirm_email.rb +36 -0
  106. data/lib/hello/business/management/forgot_password.rb +47 -0
  107. data/lib/hello/business/management/remove_email.rb +19 -0
  108. data/lib/hello/business/management/reset_password.rb +34 -0
  109. data/lib/hello/business/management/send_confirmation_email.rb +40 -0
  110. data/lib/hello/business/management/unlink_access.rb +8 -0
  111. data/lib/hello/business/management/update_profile.rb +44 -0
  112. data/lib/hello/business/registration/sign_up.rb +159 -0
  113. data/lib/hello/configuration.rb +12 -0
  114. data/lib/hello/encryptors.rb +6 -0
  115. data/lib/hello/encryptors/complex.rb +25 -0
  116. data/lib/hello/encryptors/simple.rb +27 -0
  117. data/lib/hello/engine.rb +23 -0
  118. data/lib/hello/errors.rb +12 -0
  119. data/lib/hello/locales.rb +238 -0
  120. data/lib/hello/middleware.rb +13 -0
  121. data/lib/hello/rails_active_record.rb +10 -0
  122. data/lib/hello/rails_active_record/access.rb +65 -0
  123. data/lib/hello/rails_active_record/credential.rb +51 -0
  124. data/lib/hello/rails_active_record/email_credential.rb +60 -0
  125. data/lib/hello/rails_active_record/password_credential.rb +70 -0
  126. data/lib/hello/rails_active_record/user.rb +99 -0
  127. data/lib/hello/rails_controller.rb +124 -0
  128. data/lib/hello/rails_controller/restrict_by_role.rb +79 -0
  129. data/lib/hello/rails_helper.rb +35 -0
  130. data/lib/hello/request_manager.rb +14 -0
  131. data/lib/hello/request_manager/abstract.rb +77 -0
  132. data/lib/hello/request_manager/factory.rb +32 -0
  133. data/lib/hello/request_manager/stateful.rb +53 -0
  134. data/lib/hello/request_manager/stateful/finder.rb +58 -0
  135. data/lib/hello/request_manager/stateful/session_wrapper.rb +37 -0
  136. data/lib/hello/request_manager/stateless.rb +35 -0
  137. data/lib/hello/time_zones.rb +7 -0
  138. data/lib/hello/utils.rb +11 -0
  139. data/lib/hello/utils/device_name.rb +34 -0
  140. data/lib/hello/version.rb +4 -0
  141. data/lib/tasks/hello_tasks.rake +4 -0
  142. data/spec/bdd/hello/authentication/authorization/authorization_role_restriction_spec.rb +291 -0
  143. data/spec/bdd/hello/authentication/authorization/authorization_router_constraints_spec.rb +41 -0
  144. data/spec/bdd/hello/authentication/authorization/authorization_sensitive_restriction_spec.rb +84 -0
  145. data/spec/bdd/hello/authentication/authorization/bdd.yml +1 -0
  146. data/spec/bdd/hello/authentication/bdd.yml +1 -0
  147. data/spec/bdd/hello/authentication/classic_sign_in_spec.rb +264 -0
  148. data/spec/bdd/hello/authentication/manage_sessions_spec.rb +292 -0
  149. data/spec/bdd/hello/authentication/sign_out_spec.rb +159 -0
  150. data/spec/bdd/hello/bdd.yml +1 -0
  151. data/spec/bdd/hello/internalionalization/anyone_can_change_their_locale/bdd.yml +1 -0
  152. data/spec/bdd/hello/internalionalization/anyone_can_change_their_locale/change_locale_on_the_locale_page_spec.rb +90 -0
  153. data/spec/bdd/hello/internalionalization/anyone_can_change_their_locale/change_locale_on_the_profile_page_spec.rb +64 -0
  154. data/spec/bdd/hello/internalionalization/anyone_can_change_their_locale/change_locale_on_the_sign_in_form_spec.rb +31 -0
  155. data/spec/bdd/hello/internalionalization/anyone_can_change_their_locale/change_locale_on_the_sign_up_form_spec.rb +34 -0
  156. data/spec/bdd/hello/internalionalization/anyone_can_change_their_timezone/bdd.yml +1 -0
  157. data/spec/bdd/hello/internalionalization/anyone_can_change_their_timezone/change_timezone_on_the_profile_page_spec.rb +14 -0
  158. data/spec/bdd/hello/internalionalization/anyone_can_change_their_timezone/change_timezone_on_the_sign_in_form_spec.rb +14 -0
  159. data/spec/bdd/hello/internalionalization/anyone_can_change_their_timezone/change_timezone_on_the_sign_up_form_spec.rb +14 -0
  160. data/spec/bdd/hello/internalionalization/bdd.yml +1 -0
  161. data/spec/bdd/hello/management/bdd.yml +1 -0
  162. data/spec/bdd/hello/management/cancel_account_spec.rb +128 -0
  163. data/spec/bdd/hello/management/manage_email_credentials/bdd.yml +1 -0
  164. data/spec/bdd/hello/management/manage_email_credentials/manage_email_credentials_emails_api_spec.rb +7 -0
  165. data/spec/bdd/hello/management/manage_email_credentials/manage_email_credentials_emails_page_spec.rb +252 -0
  166. data/spec/bdd/hello/management/manage_password_credentials/bdd.yml +1 -0
  167. data/spec/bdd/hello/management/manage_password_credentials/manage_password_forgot_password_spec.rb +68 -0
  168. data/spec/bdd/hello/management/manage_password_credentials/manage_password_page_spec.rb +60 -0
  169. data/spec/bdd/hello/management/manage_password_credentials/manage_password_reset_password_spec.rb +145 -0
  170. data/spec/bdd/hello/management/manage_profile/bdd.yml +1 -0
  171. data/spec/bdd/hello/management/manage_profile/manage_profile_api_spec.rb +7 -0
  172. data/spec/bdd/hello/management/manage_profile/manage_profile_page_spec.rb +65 -0
  173. data/spec/bdd/hello/management/manage_social_credentials/bdd.yml +1 -0
  174. data/spec/bdd/hello/management/manage_social_credentials/manage_social_credentials_pending_spec.rb +7 -0
  175. data/spec/bdd/hello/management/unlink_sessions_spec.rb +59 -0
  176. data/spec/bdd/hello/other/bdd.yml +1 -0
  177. data/spec/bdd/hello/other/create_user_spec.rb +70 -0
  178. data/spec/bdd/hello/other/impersonate_user_spec.rb +58 -0
  179. data/spec/bdd/hello/other/list_users_spec.rb +86 -0
  180. data/spec/bdd/hello/registration/bdd.yml +1 -0
  181. data/spec/bdd/hello/registration/classic_sign_up_spec.rb +228 -0
  182. data/spec/bdd/hello/registration/onboarding_process_spec.rb +101 -0
  183. data/spec/bdd/hello/support.rb +62 -0
  184. data/spec/business/hello/authentication/sign_in_spec.rb +72 -0
  185. data/spec/business/hello/registration/sign_up_spec.rb +258 -0
  186. data/spec/controllers/authentication_spec.rb +97 -0
  187. data/spec/controllers/localization_spec.rb +65 -0
  188. data/spec/controllers/request_can_carry_an_access_token_spec.rb +223 -0
  189. data/spec/fixtures/hello/password_mailer/confirmation +3 -0
  190. data/spec/fixtures/hello/password_mailer/forgot +3 -0
  191. data/spec/fixtures/hello/password_mailer/sign_up +3 -0
  192. data/spec/mailers/hello/mailer_spec.rb +53 -0
  193. data/spec/models/access_spec.rb +19 -0
  194. data/spec/models/credential_spec.rb +9 -0
  195. data/spec/models/email_credential_spec.rb +163 -0
  196. data/spec/models/hello/sign_up_model_spec.rb +64 -0
  197. data/spec/models/password_credential_spec.rb +75 -0
  198. data/spec/models/user_spec.rb +93 -0
  199. data/spec/others/configuration_spec.rb +25 -0
  200. data/spec/others/encryptors/complex_spec.rb +26 -0
  201. data/spec/others/encryptors/simple_spec.rb +21 -0
  202. data/spec/others/helper_spec.rb +10 -0
  203. data/spec/others/localization_consistency_spec.rb +73 -0
  204. data/spec/requests/forgot_password_spec.rb +36 -0
  205. data/spec/requests/reset_password_spec.rb +7 -0
  206. data/spec/requests/security/user_spec.rb +19 -0
  207. data/spec/routing/hello/accesses_routing_spec.rb +17 -0
  208. data/spec/routing/hello/emails_routing_spec.rb +37 -0
  209. data/spec/routing/hello/locale_routing_spec.rb +17 -0
  210. data/spec/routing/hello/profile_routing_spec.rb +29 -0
  211. data/spec/routing/hello/registration_routing_spec.rb +23 -0
  212. data/spec/routing/hello/sign_out_routing_spec.rb +11 -0
  213. data/spec/routing/hello/sudo_mode_routing_spec.rb +23 -0
  214. data/spec/routing/hello/users_routing_spec.rb +15 -0
  215. data/spec/spec_helper.rb +9 -0
  216. data/spec/spec_helper/codeclimate.rb +2 -0
  217. data/spec/spec_helper/configure_rspec.rb +23 -0
  218. data/spec/spec_helper/create_database.rb +5 -0
  219. data/spec/spec_helper/dummy_and_test_dependencies.rb +17 -0
  220. data/spec/spec_helper/support.rb +1 -0
  221. data/spec/support/factories.rb +60 -0
  222. data/spec/support/feature_injection.rb +118 -0
  223. data/spec/support/features/feature_support_given.rb +46 -0
  224. data/spec/support/features/feature_support_then.rb +43 -0
  225. data/spec/support/helpers/aliases.rb +76 -0
  226. data/spec/support/helpers/configuration.rb +6 -0
  227. data/spec/support/helpers/current.rb +16 -0
  228. data/spec/support/helpers/expect.rb +61 -0
  229. data/spec/support/helpers/given.rb +49 -0
  230. data/spec/support/helpers/shortcuts.rb +24 -0
  231. data/spec/support/helpers/then.rb +29 -0
  232. data/spec/support/helpers/when.rb +108 -0
  233. data/spec/support/requests/request_support.rb +8 -0
  234. data/spec/utils/device_name_spec.rb +228 -0
  235. metadata +562 -11
@@ -0,0 +1,60 @@
1
+ module Hello
2
+ module RailsActiveRecord
3
+ class EmailCredential < ::Credential
4
+
5
+ # VALIDATIONS
6
+ before_destroy :cannot_destroy_last_email_credential
7
+
8
+ validate :hello_validations
9
+ validates_uniqueness_of :email
10
+
11
+ # SETTERS
12
+ def email=(v)
13
+ super(v.to_s.downcase.delete(' '))
14
+ end
15
+
16
+ # CUSTOM METHODS
17
+
18
+ def email_confirmed?
19
+ !!confirmed_at
20
+ end
21
+
22
+ def email_delivered?
23
+ !!email_delivered_at
24
+ end
25
+
26
+ def email_delivered_at
27
+ verifying_token_digested_at
28
+ end
29
+
30
+ def confirm_email!
31
+ update! verifying_token_digest: nil, verifying_token_digested_at: nil, confirmed_at: 1.second.ago
32
+ end
33
+
34
+ private
35
+
36
+ def hello_validations
37
+ validates_presence_of :email
38
+ return false if errors[:email].any?
39
+
40
+ c = Hello.configuration
41
+ validates_length_of :email, in: c.email_length
42
+ return false if errors[:email].any?
43
+
44
+ validates_format_of :email, with: c.email_regex
45
+ return false if errors[:email].any?
46
+ end
47
+
48
+ def cannot_destroy_last_email_credential
49
+ return if hello_is_user_being_destroyed?
50
+ return unless is_last_email_credential?
51
+ errors[:base] << 'must have at least one credential'
52
+ false
53
+ end
54
+
55
+ def is_last_email_credential?
56
+ user.email_credentials.count == 1
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,70 @@
1
+ module Hello
2
+ module RailsActiveRecord
3
+ class PasswordCredential < ::Credential
4
+
5
+ # VALIDATIONS
6
+ validates_presence_of :password, on: :create
7
+ # validates_presence_of :digest
8
+
9
+ # before_destroy :cannot_destroy_last_password_credential
10
+ validate :hello_validations
11
+
12
+ # SETTERS
13
+
14
+ attr_reader :password
15
+
16
+ def password=(value)
17
+ # puts "password=('#{value}')".blue
18
+ self.digest = @password = nil if value.blank?
19
+ @password = value
20
+
21
+ self.digest = complex_encryptor.encrypt(value)
22
+ end
23
+
24
+ # CUSTOM METHODS
25
+
26
+ def password_is?(plain_text_password)
27
+ complex_encryptor.match(plain_text_password, digest)
28
+ end
29
+
30
+ def set_generated_password
31
+ self.password = simple_encryptor.single(4) # 8 chars
32
+ end
33
+
34
+ private
35
+
36
+ def hello_validations
37
+ return true unless digest_changed?
38
+
39
+ return false if errors[:password].any?
40
+ c = Hello.configuration
41
+
42
+ validates_length_of :password, in: c.password_length
43
+ return false if errors[:password].any?
44
+
45
+ validates_format_of :password, with: c.password_regex
46
+ return false if errors[:password].any?
47
+ end
48
+
49
+ def complex_encryptor
50
+ Hello::Encryptors::Complex.instance
51
+ end
52
+
53
+ def simple_encryptor
54
+ Hello::Encryptors::Simple.instance
55
+ end
56
+
57
+ # # TODO: code for multiple passwords
58
+ # def cannot_destroy_last_password_credential
59
+ # return if hello_is_user_being_destroyed?
60
+ # return if not is_last_password_credential?
61
+ # errors[:base] << "must have at least one credential"
62
+ # false
63
+ # end
64
+
65
+ # def is_last_password_credential?
66
+ # user.password_credentials.count == 1
67
+ # end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,99 @@
1
+ module Hello
2
+ module RailsActiveRecord
3
+ class User < ::ActiveRecord::Base
4
+ self.table_name = 'users'
5
+
6
+ # ASSOCIATIONS
7
+
8
+ has_many :credentials, dependent: :destroy, class_name: '::Credential'
9
+ has_many :email_credentials, dependent: :destroy, class_name: '::EmailCredential'
10
+ has_one :password_credential, dependent: :destroy, class_name: '::PasswordCredential'
11
+ has_many :password_credentials, dependent: :destroy, class_name: '::PasswordCredential'
12
+ has_many :accesses, dependent: :destroy, class_name: '::Access'
13
+
14
+ alias :main_password_credential :password_credential
15
+
16
+ # VALIDATIONS
17
+
18
+ validates_presence_of :username, :locale, :time_zone
19
+ validates_uniqueness_of :username
20
+ validate :hello_validations
21
+
22
+ # SETTERS
23
+
24
+ def username=(v)
25
+ super(v.to_s.downcase.remove(' '))
26
+ end
27
+
28
+ # OVERRIDES
29
+
30
+ def destroy
31
+ # In Rails 4.0
32
+ # 'this instance' and the 'user in the credential instance'
33
+ # are 2 separate instances, making it impossible for them to share state
34
+ # therefore, an instance variable used as a flag will not work for Rails 4.0
35
+ # It will however, work for Rails 4.1 and 4.2
36
+ # @hello_is_this_being_destroyed = true
37
+ Thread.current['Hello.destroying_user'] = true
38
+ super
39
+ end
40
+
41
+ # def hello_is_this_being_destroyed?
42
+ # !!@hello_is_this_being_destroyed
43
+ # end
44
+
45
+
46
+ # CUSTOM METHODS
47
+
48
+ def as_json_web_api
49
+ as_json
50
+ end
51
+
52
+ def password_is?(plain_text_password)
53
+ password_credential.password_is?(plain_text_password)
54
+ end
55
+
56
+ def role_is?(role)
57
+ send("#{role}?")
58
+ end
59
+
60
+ def in_any_role?(roles)
61
+ roles.each { |r| role_is?(r) and return true }
62
+ false
63
+ end
64
+
65
+ def set_generated_username
66
+ loop do
67
+ self.username = _make_up_new_username
68
+ break if _username_unique?
69
+ end
70
+ end
71
+
72
+ private
73
+
74
+ def hello_validations
75
+ c = Hello.configuration
76
+
77
+ validates_inclusion_of :locale, in: c.locales
78
+ validates_inclusion_of :time_zone, in: c.time_zones
79
+ validates_format_of :username, with: c.username_regex
80
+ validates_length_of :username, in: c.username_length
81
+ end
82
+
83
+ def _make_up_new_username
84
+ Hello::Encryptors::Simple.instance.single(16) # 16 chars
85
+ end
86
+
87
+ def _username_unique?
88
+ !self.class.unscoped.where(username: username).where.not(id: id).exists?
89
+ end
90
+
91
+ # def username_suggestions
92
+ # email1 = email.to_s.split('@').first
93
+ # name1 = name.to_s.split(' ')
94
+ # ideas = [name1, email1].flatten
95
+ # [ideas.sample, rand(999)].join.parameterize
96
+ # end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,124 @@
1
+ module Hello
2
+ module RailsController
3
+ extend ActiveSupport::Concern
4
+
5
+ autoload :RestrictByRole, 'hello/rails_controller/restrict_by_role'
6
+
7
+ module ClassMethods
8
+ def sign_out!(options = {})
9
+ before_action(options) { sign_out! }
10
+ end
11
+
12
+ def kick(*args)
13
+ options, roles = Hello::Utils.trailing_options(args)
14
+ before_action(options) { kick(*roles) }
15
+ end
16
+
17
+ def dont_kick(*args)
18
+ options, roles = Hello::Utils.trailing_options(args)
19
+ before_action(options) { dont_kick(*roles) }
20
+ end
21
+
22
+ def dont_kick_people
23
+ # :)
24
+ end
25
+
26
+ def sudo_mode(options = {})
27
+ before_action(options) { sudo_mode }
28
+ end
29
+ end
30
+
31
+ included do
32
+ around_action :hello_around_action
33
+
34
+ helper_method :sign_up_disabled,
35
+ :current_user, :current_accesses, :current_access,
36
+ :signed_in?, :is_current_access?, :sudo_mode?
37
+
38
+ delegate :sign_in!, :sign_out!, :signed_in?,
39
+ :current_user, :is_current_user?,
40
+ :current_access, :current_accesses, :is_current_access?,
41
+ :session_token=, :session_tokens,
42
+ to: :hello_manager
43
+
44
+ delegate :kick, :dont_kick,
45
+ to: :restrict_by_role
46
+ end
47
+
48
+ def hello_manager
49
+ env['hello'] ||= Hello::RequestManager.create(request)
50
+ end
51
+
52
+
53
+
54
+ def sign_up_disabled
55
+ Hello.configuration.sign_up_disabled || action_name=='disabled'
56
+ end
57
+
58
+ def hello_store_url_on_session!
59
+ if hello_manager.stateful?
60
+ session[:url] = url_for(params.permit!.merge(only_path: true))
61
+ end
62
+ end
63
+
64
+
65
+
66
+ def sudo_mode?
67
+ current_access && current_access.sudo_expires_at.future?
68
+ end
69
+
70
+ def sudo_mode
71
+ unless sudo_mode?
72
+ hello_store_url_on_session!
73
+ render_sudo_mode_form
74
+ end
75
+ end
76
+
77
+ def render_sudo_mode_form
78
+ render 'hello/authentication/sudo_mode'
79
+ end
80
+
81
+
82
+
83
+ private
84
+
85
+ def restrict_by_role
86
+ @_hello_rbr ||= RestrictByRole.new(self)
87
+ end
88
+
89
+
90
+ def use_locale(locale)
91
+ locale ||= current_user && current_user.locale
92
+ locale ||= session['locale']
93
+ locale ||= recommended_locale.to_s
94
+
95
+ I18n.locale = session['locale'] = locale
96
+ end
97
+
98
+ def hello_around_action(&block)
99
+ use_locale(nil)
100
+
101
+ if current_user
102
+ # begin keep-alive
103
+ Access.cached_destroy_all_expired
104
+ current_access.keep_alive!
105
+ expires_in = view_context.time_ago_in_words(current_access.expires_at)
106
+ logger.info " #{'Hello Session'.bold.light_blue} expires in #{expires_in}"
107
+ # end keep-alive
108
+
109
+ Thread.current['Hello.destroying_user'] = nil
110
+ Time.use_zone(current_user.time_zone, &block)
111
+ Thread.current['Hello.destroying_user'] = nil
112
+ else
113
+ yield
114
+ end
115
+ end
116
+
117
+ def recommended_locale
118
+ y = Hello.configuration.locales
119
+ x = http_accept_language.compatible_language_from(y)
120
+ x || I18n.default_locale
121
+ end
122
+
123
+ end
124
+ end
@@ -0,0 +1,79 @@
1
+ module Hello
2
+ module RailsController
3
+ class RestrictByRole
4
+
5
+ def initialize(controller)
6
+ @controller = controller
7
+ end
8
+
9
+ def kick(*roles)
10
+ to_home_page if current_user.in_any_role?(roles)
11
+ end
12
+
13
+ def dont_kick(*roles)
14
+ to_home_page if not current_user.in_any_role?(roles)
15
+ end
16
+
17
+ private
18
+
19
+ def current_user
20
+ @controller.current_user || ::User.new(role: 'guest')
21
+ end
22
+
23
+ def to_home_page
24
+ if current_user.guest?
25
+ to_sign_in
26
+ elsif current_user.onboarding?
27
+ to_onboarding
28
+ else
29
+ to_root
30
+ end
31
+ end
32
+
33
+ def to_root
34
+ respond_to do |format|
35
+ format.html { redirect_to '/' }
36
+ format.json do
37
+ data = { 'message' => 'Access Denied.' }
38
+ status = :forbidden # 403
39
+ render json: data, status: status
40
+ end
41
+ end
42
+ end
43
+
44
+ def to_sign_in
45
+ respond_to do |format|
46
+ format.html do
47
+ hello_store_url_on_session!
48
+ redirect_to hello.sign_in_path
49
+ end
50
+ format.json do
51
+ data = { 'message' => 'An active access token must be used to query information about the current user.' }
52
+ status = :unauthorized # 401
53
+ render json: data, status: status
54
+ end
55
+ end
56
+ end
57
+
58
+ def to_onboarding
59
+ respond_to do |format|
60
+ format.html { redirect_to '/onboarding' }
61
+ format.json do
62
+ data = { 'message' => 'Access Denied, visit /onboarding and complete your registration.' }
63
+ status = :forbidden # 403
64
+ render json: data, status: status
65
+ end
66
+ end
67
+ end
68
+
69
+ def method_missing(method, *args, &block)
70
+ if @controller.respond_to?(method)
71
+ @controller.send(method, *args, &block)
72
+ else
73
+ super
74
+ end
75
+ end
76
+
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,35 @@
1
+ module Hello
2
+ module RailsHelper
3
+ def method_missing(method, *args, &block)
4
+ # # http://candland.net/2012/04/17/rails-routes-used-in-an-isolated-engine/
5
+ # puts "LOOKING FOR ROUTES #{method}"
6
+ return super unless method.to_s.end_with?('_path', '_url')
7
+ return super unless main_app.respond_to?(method)
8
+ main_app.send(method, *args)
9
+ end
10
+
11
+ def respond_to?(method)
12
+ return super unless method.to_s.end_with?('_path', '_url')
13
+ return super unless main_app.respond_to?(method)
14
+ true
15
+ end
16
+
17
+ # [['English', 'en']]
18
+ def hello_locale_select_options
19
+ available_locales_with_names.map { |k, v| [v, k] }
20
+ end
21
+
22
+ def human_current_locale
23
+ t('hello.locale_name')
24
+ end
25
+
26
+ def current_locale
27
+ session['locale']
28
+ end
29
+
30
+ def available_locales_with_names
31
+ Hello::Locales.all.select { |k, _v| Hello.configuration.locales.include? k }
32
+ end
33
+
34
+ end
35
+ end