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.
- checksums.yaml +4 -4
- data/MIT-LICENSE +20 -0
- data/README.md +97 -0
- data/Rakefile +23 -0
- data/app/controllers/hello/application_controller.rb +27 -0
- data/app/controllers/hello/authentication/sessions_controller.rb +81 -0
- data/app/controllers/hello/authentication/sign_in_controller.rb +39 -0
- data/app/controllers/hello/authentication/sudo_mode_controller.rb +40 -0
- data/app/controllers/hello/concerns/authentication/sign_in.rb +44 -0
- data/app/controllers/hello/concerns/management/forgot_password.rb +45 -0
- data/app/controllers/hello/concerns/management/reset_password.rb +33 -0
- data/app/controllers/hello/concerns/registration/sign_up.rb +64 -0
- data/app/controllers/hello/internationalization/locale_controller.rb +28 -0
- data/app/controllers/hello/management/accesses_controller.rb +29 -0
- data/app/controllers/hello/management/confirm_emails_controller.rb +41 -0
- data/app/controllers/hello/management/emails_controller.rb +67 -0
- data/app/controllers/hello/management/forgot_password_controller.rb +41 -0
- data/app/controllers/hello/management/passwords_controller.rb +57 -0
- data/app/controllers/hello/management/profiles_controller.rb +71 -0
- data/app/controllers/hello/management/reset_password_controller.rb +53 -0
- data/app/controllers/hello/registration/sign_up_controller.rb +63 -0
- data/app/helpers/hello/application_helper.rb +5 -0
- data/app/mailers/hello/mailer.rb +26 -0
- data/app/models/access.rb +2 -0
- data/app/models/credential.rb +2 -0
- data/app/models/email_credential.rb +2 -0
- data/app/models/password_credential.rb +2 -0
- data/app/models/user.rb +2 -0
- data/app/views/hello/authentication/_sign_in.html.erb +64 -0
- data/app/views/hello/authentication/new_session.html.erb +4 -0
- data/app/views/hello/authentication/sessions.html.erb +36 -0
- data/app/views/hello/authentication/sign_in.html.erb +3 -0
- data/app/views/hello/authentication/sudo_mode.html.erb +37 -0
- data/app/views/hello/internationalization/locales.html.erb +7 -0
- data/app/views/hello/mailer/confirm_email.html.erb +12 -0
- data/app/views/hello/mailer/forgot_password.html.erb +12 -0
- data/app/views/hello/mailer/welcome.html.erb +11 -0
- data/app/views/hello/management/accesses.html.erb +47 -0
- data/app/views/hello/management/cancel.html.erb +14 -0
- data/app/views/hello/management/email_credentials/expired_confirmation_token.html.erb +1 -0
- data/app/views/hello/management/email_credentials/index.html.erb +84 -0
- data/app/views/hello/management/password_credentials/_forgot_form.html.erb +17 -0
- data/app/views/hello/management/password_credentials/_reset_form.html.erb +39 -0
- data/app/views/hello/management/password_credentials/forgot.html.erb +10 -0
- data/app/views/hello/management/password_credentials/forgot_success.html.erb +26 -0
- data/app/views/hello/management/password_credentials/reset.html.erb +3 -0
- data/app/views/hello/management/password_credentials/show.html.erb +25 -0
- data/app/views/hello/management/user.html.erb +73 -0
- data/app/views/hello/registration/_sign_up.html.erb +86 -0
- data/app/views/hello/registration/sign_up.html.erb +3 -0
- data/app/views/hello/registration/sign_up_widget.html.erb +3 -0
- data/app/views/hello/shared/_errors.html.erb +11 -0
- data/app/views/hello/shared/_flash.html.erb +8 -0
- data/app/views/hello/shared/_nav_pills.html.erb +15 -0
- data/app/views/hello/shared/_session_expiration.html.erb +15 -0
- data/app/views/hello/shared/_settings.html.erb +41 -0
- data/config/locales/hello.en.yml +69 -0
- data/config/locales/hello.es.yml +71 -0
- data/config/locales/hello.fr.yml +71 -0
- data/config/locales/hello.pl.yml +71 -0
- data/config/locales/hello.pt-BR.yml +71 -0
- data/config/locales/hello.zh-CN.yml +71 -0
- data/config/locales/hello.zh-TW.yml +71 -0
- data/config/routes.rb +74 -0
- data/db/migrate/1_create_credentials.rb +17 -0
- data/db/migrate/2_create_accesses.rb +15 -0
- data/db/migrate/3_create_users.rb +23 -0
- data/lib/generators/hello/concerns/USAGE +8 -0
- data/lib/generators/hello/concerns/concerns_generator.rb +10 -0
- data/lib/generators/hello/from_devise/USAGE +8 -0
- data/lib/generators/hello/from_devise/from_devise_generator.rb +13 -0
- data/lib/generators/hello/from_devise/templates/from_devise.migration.rb +39 -0
- data/lib/generators/hello/install/USAGE +8 -0
- data/lib/generators/hello/install/install_generator.rb +94 -0
- data/lib/generators/hello/install/templates/application.html.erb +35 -0
- data/lib/generators/hello/install/templates/hello_helper.rb +16 -0
- data/lib/generators/hello/install/templates/initializer.rb +24 -0
- data/lib/generators/hello/install/templates/models/concerns/user/authorization.rb +21 -0
- data/lib/generators/hello/install/templates/models/user.rb +9 -0
- data/lib/generators/hello/install/templates/onboarding/index.html.erb +5 -0
- data/lib/generators/hello/install/templates/onboarding/onboarding_controller.rb +33 -0
- data/lib/generators/hello/install/templates/root/index.html.erb +7 -0
- data/lib/generators/hello/install/templates/root/root_controller.rb +6 -0
- data/lib/generators/hello/install/templates/users/controllers/users_controller.rb +71 -0
- data/lib/generators/hello/install/templates/users/views/users/index.html.erb +29 -0
- data/lib/generators/hello/install/templates/users/views/users/list.html.erb +35 -0
- data/lib/generators/hello/install/templates/users/views/users/new.html.erb +60 -0
- data/lib/generators/hello/install/templates/users/views/users/show.html.erb +74 -0
- data/lib/generators/hello/locales/USAGE +12 -0
- data/lib/generators/hello/locales/locales_generator.rb +64 -0
- data/lib/generators/hello/views/USAGE +8 -0
- data/lib/generators/hello/views/views_generator.rb +9 -0
- data/lib/hello-rails.rb +1 -0
- data/lib/hello.rb +36 -0
- data/lib/hello/CHANGES.md +26 -0
- data/lib/hello/business.rb +47 -0
- data/lib/hello/business/authentication/sign_in.rb +92 -0
- data/lib/hello/business/authentication/sign_out.rb +8 -0
- data/lib/hello/business/authentication/sudo_mode_authentication.rb +25 -0
- data/lib/hello/business/authentication/sudo_mode_expiration.rb +17 -0
- data/lib/hello/business/base.rb +33 -0
- data/lib/hello/business/internationalization/update_locale.rb +33 -0
- data/lib/hello/business/management/add_email.rb +19 -0
- data/lib/hello/business/management/cancel_account.rb +22 -0
- data/lib/hello/business/management/confirm_email.rb +36 -0
- data/lib/hello/business/management/forgot_password.rb +47 -0
- data/lib/hello/business/management/remove_email.rb +19 -0
- data/lib/hello/business/management/reset_password.rb +34 -0
- data/lib/hello/business/management/send_confirmation_email.rb +40 -0
- data/lib/hello/business/management/unlink_access.rb +8 -0
- data/lib/hello/business/management/update_profile.rb +44 -0
- data/lib/hello/business/registration/sign_up.rb +159 -0
- data/lib/hello/configuration.rb +12 -0
- data/lib/hello/encryptors.rb +6 -0
- data/lib/hello/encryptors/complex.rb +25 -0
- data/lib/hello/encryptors/simple.rb +27 -0
- data/lib/hello/engine.rb +23 -0
- data/lib/hello/errors.rb +12 -0
- data/lib/hello/locales.rb +238 -0
- data/lib/hello/middleware.rb +13 -0
- data/lib/hello/rails_active_record.rb +10 -0
- data/lib/hello/rails_active_record/access.rb +65 -0
- data/lib/hello/rails_active_record/credential.rb +51 -0
- data/lib/hello/rails_active_record/email_credential.rb +60 -0
- data/lib/hello/rails_active_record/password_credential.rb +70 -0
- data/lib/hello/rails_active_record/user.rb +99 -0
- data/lib/hello/rails_controller.rb +124 -0
- data/lib/hello/rails_controller/restrict_by_role.rb +79 -0
- data/lib/hello/rails_helper.rb +35 -0
- data/lib/hello/request_manager.rb +14 -0
- data/lib/hello/request_manager/abstract.rb +77 -0
- data/lib/hello/request_manager/factory.rb +32 -0
- data/lib/hello/request_manager/stateful.rb +53 -0
- data/lib/hello/request_manager/stateful/finder.rb +58 -0
- data/lib/hello/request_manager/stateful/session_wrapper.rb +37 -0
- data/lib/hello/request_manager/stateless.rb +35 -0
- data/lib/hello/time_zones.rb +7 -0
- data/lib/hello/utils.rb +11 -0
- data/lib/hello/utils/device_name.rb +34 -0
- data/lib/hello/version.rb +4 -0
- data/lib/tasks/hello_tasks.rake +4 -0
- data/spec/bdd/hello/authentication/authorization/authorization_role_restriction_spec.rb +291 -0
- data/spec/bdd/hello/authentication/authorization/authorization_router_constraints_spec.rb +41 -0
- data/spec/bdd/hello/authentication/authorization/authorization_sensitive_restriction_spec.rb +84 -0
- data/spec/bdd/hello/authentication/authorization/bdd.yml +1 -0
- data/spec/bdd/hello/authentication/bdd.yml +1 -0
- data/spec/bdd/hello/authentication/classic_sign_in_spec.rb +264 -0
- data/spec/bdd/hello/authentication/manage_sessions_spec.rb +292 -0
- data/spec/bdd/hello/authentication/sign_out_spec.rb +159 -0
- data/spec/bdd/hello/bdd.yml +1 -0
- data/spec/bdd/hello/internalionalization/anyone_can_change_their_locale/bdd.yml +1 -0
- data/spec/bdd/hello/internalionalization/anyone_can_change_their_locale/change_locale_on_the_locale_page_spec.rb +90 -0
- data/spec/bdd/hello/internalionalization/anyone_can_change_their_locale/change_locale_on_the_profile_page_spec.rb +64 -0
- data/spec/bdd/hello/internalionalization/anyone_can_change_their_locale/change_locale_on_the_sign_in_form_spec.rb +31 -0
- data/spec/bdd/hello/internalionalization/anyone_can_change_their_locale/change_locale_on_the_sign_up_form_spec.rb +34 -0
- data/spec/bdd/hello/internalionalization/anyone_can_change_their_timezone/bdd.yml +1 -0
- data/spec/bdd/hello/internalionalization/anyone_can_change_their_timezone/change_timezone_on_the_profile_page_spec.rb +14 -0
- data/spec/bdd/hello/internalionalization/anyone_can_change_their_timezone/change_timezone_on_the_sign_in_form_spec.rb +14 -0
- data/spec/bdd/hello/internalionalization/anyone_can_change_their_timezone/change_timezone_on_the_sign_up_form_spec.rb +14 -0
- data/spec/bdd/hello/internalionalization/bdd.yml +1 -0
- data/spec/bdd/hello/management/bdd.yml +1 -0
- data/spec/bdd/hello/management/cancel_account_spec.rb +128 -0
- data/spec/bdd/hello/management/manage_email_credentials/bdd.yml +1 -0
- data/spec/bdd/hello/management/manage_email_credentials/manage_email_credentials_emails_api_spec.rb +7 -0
- data/spec/bdd/hello/management/manage_email_credentials/manage_email_credentials_emails_page_spec.rb +252 -0
- data/spec/bdd/hello/management/manage_password_credentials/bdd.yml +1 -0
- data/spec/bdd/hello/management/manage_password_credentials/manage_password_forgot_password_spec.rb +68 -0
- data/spec/bdd/hello/management/manage_password_credentials/manage_password_page_spec.rb +60 -0
- data/spec/bdd/hello/management/manage_password_credentials/manage_password_reset_password_spec.rb +145 -0
- data/spec/bdd/hello/management/manage_profile/bdd.yml +1 -0
- data/spec/bdd/hello/management/manage_profile/manage_profile_api_spec.rb +7 -0
- data/spec/bdd/hello/management/manage_profile/manage_profile_page_spec.rb +65 -0
- data/spec/bdd/hello/management/manage_social_credentials/bdd.yml +1 -0
- data/spec/bdd/hello/management/manage_social_credentials/manage_social_credentials_pending_spec.rb +7 -0
- data/spec/bdd/hello/management/unlink_sessions_spec.rb +59 -0
- data/spec/bdd/hello/other/bdd.yml +1 -0
- data/spec/bdd/hello/other/create_user_spec.rb +70 -0
- data/spec/bdd/hello/other/impersonate_user_spec.rb +58 -0
- data/spec/bdd/hello/other/list_users_spec.rb +86 -0
- data/spec/bdd/hello/registration/bdd.yml +1 -0
- data/spec/bdd/hello/registration/classic_sign_up_spec.rb +228 -0
- data/spec/bdd/hello/registration/onboarding_process_spec.rb +101 -0
- data/spec/bdd/hello/support.rb +62 -0
- data/spec/business/hello/authentication/sign_in_spec.rb +72 -0
- data/spec/business/hello/registration/sign_up_spec.rb +258 -0
- data/spec/controllers/authentication_spec.rb +97 -0
- data/spec/controllers/localization_spec.rb +65 -0
- data/spec/controllers/request_can_carry_an_access_token_spec.rb +223 -0
- data/spec/fixtures/hello/password_mailer/confirmation +3 -0
- data/spec/fixtures/hello/password_mailer/forgot +3 -0
- data/spec/fixtures/hello/password_mailer/sign_up +3 -0
- data/spec/mailers/hello/mailer_spec.rb +53 -0
- data/spec/models/access_spec.rb +19 -0
- data/spec/models/credential_spec.rb +9 -0
- data/spec/models/email_credential_spec.rb +163 -0
- data/spec/models/hello/sign_up_model_spec.rb +64 -0
- data/spec/models/password_credential_spec.rb +75 -0
- data/spec/models/user_spec.rb +93 -0
- data/spec/others/configuration_spec.rb +25 -0
- data/spec/others/encryptors/complex_spec.rb +26 -0
- data/spec/others/encryptors/simple_spec.rb +21 -0
- data/spec/others/helper_spec.rb +10 -0
- data/spec/others/localization_consistency_spec.rb +73 -0
- data/spec/requests/forgot_password_spec.rb +36 -0
- data/spec/requests/reset_password_spec.rb +7 -0
- data/spec/requests/security/user_spec.rb +19 -0
- data/spec/routing/hello/accesses_routing_spec.rb +17 -0
- data/spec/routing/hello/emails_routing_spec.rb +37 -0
- data/spec/routing/hello/locale_routing_spec.rb +17 -0
- data/spec/routing/hello/profile_routing_spec.rb +29 -0
- data/spec/routing/hello/registration_routing_spec.rb +23 -0
- data/spec/routing/hello/sign_out_routing_spec.rb +11 -0
- data/spec/routing/hello/sudo_mode_routing_spec.rb +23 -0
- data/spec/routing/hello/users_routing_spec.rb +15 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/spec_helper/codeclimate.rb +2 -0
- data/spec/spec_helper/configure_rspec.rb +23 -0
- data/spec/spec_helper/create_database.rb +5 -0
- data/spec/spec_helper/dummy_and_test_dependencies.rb +17 -0
- data/spec/spec_helper/support.rb +1 -0
- data/spec/support/factories.rb +60 -0
- data/spec/support/feature_injection.rb +118 -0
- data/spec/support/features/feature_support_given.rb +46 -0
- data/spec/support/features/feature_support_then.rb +43 -0
- data/spec/support/helpers/aliases.rb +76 -0
- data/spec/support/helpers/configuration.rb +6 -0
- data/spec/support/helpers/current.rb +16 -0
- data/spec/support/helpers/expect.rb +61 -0
- data/spec/support/helpers/given.rb +49 -0
- data/spec/support/helpers/shortcuts.rb +24 -0
- data/spec/support/helpers/then.rb +29 -0
- data/spec/support/helpers/when.rb +108 -0
- data/spec/support/requests/request_support.rb +8 -0
- data/spec/utils/device_name_spec.rb +228 -0
- metadata +562 -11
@@ -0,0 +1,25 @@
|
|
1
|
+
module Hello
|
2
|
+
module Business
|
3
|
+
module Authentication
|
4
|
+
class SudoModeAuthentication < Base
|
5
|
+
attr_reader :access
|
6
|
+
|
7
|
+
def initialize(access)
|
8
|
+
@access = access
|
9
|
+
end
|
10
|
+
|
11
|
+
def authenticate!(password)
|
12
|
+
if access.user.password_is?(password)
|
13
|
+
access.update!(sudo_expires_at: sudo_expires_at)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def sudo_expires_at
|
20
|
+
Hello.configuration.sudo_expires_in.from_now
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Hello
|
2
|
+
module Business
|
3
|
+
class Base
|
4
|
+
include ActiveModel::Model
|
5
|
+
|
6
|
+
def errors
|
7
|
+
@errors ||= ActiveModel::Errors.new(self)
|
8
|
+
end
|
9
|
+
|
10
|
+
def error_message(extra = {})
|
11
|
+
t('error', { count: errors.count }.merge(extra))
|
12
|
+
end
|
13
|
+
|
14
|
+
def alert_message(extra = {})
|
15
|
+
t('alert', extra)
|
16
|
+
end
|
17
|
+
|
18
|
+
def success_message(extra = {})
|
19
|
+
t('success', extra)
|
20
|
+
end
|
21
|
+
|
22
|
+
def t(key, extra = {})
|
23
|
+
I18n.t("#{i18n_scope}.#{key}", extra)
|
24
|
+
end
|
25
|
+
|
26
|
+
protected
|
27
|
+
|
28
|
+
def i18n_scope
|
29
|
+
self.class.name.underscore.tr('/', '.')
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Hello
|
2
|
+
module Business
|
3
|
+
module Internationalization
|
4
|
+
class UpdateLocale < Base
|
5
|
+
def initialize(locale)
|
6
|
+
@locale = locale
|
7
|
+
end
|
8
|
+
|
9
|
+
def locale
|
10
|
+
locale_if_available || I18n.default_locale
|
11
|
+
end
|
12
|
+
|
13
|
+
def success_message(_extra = {})
|
14
|
+
super(locale_name: current_locale_name)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def locale_if_available
|
20
|
+
([@locale] & locales).first
|
21
|
+
end
|
22
|
+
|
23
|
+
def locales
|
24
|
+
Hello.configuration.locales
|
25
|
+
end
|
26
|
+
|
27
|
+
def current_locale_name
|
28
|
+
I18n.t('hello.locale_name')
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Hello
|
2
|
+
module Business
|
3
|
+
module Management
|
4
|
+
class AddEmail < Base
|
5
|
+
def initialize(email_credential)
|
6
|
+
@email_credential = email_credential
|
7
|
+
end
|
8
|
+
|
9
|
+
def success_message
|
10
|
+
super(email: @email_credential.email)
|
11
|
+
end
|
12
|
+
|
13
|
+
def error_message
|
14
|
+
@email_credential.errors.full_messages.first
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Hello
|
2
|
+
module Business
|
3
|
+
module Management
|
4
|
+
class CancelAccount < Base
|
5
|
+
|
6
|
+
def initialize(user)
|
7
|
+
@user = user
|
8
|
+
end
|
9
|
+
|
10
|
+
def cancel_account
|
11
|
+
@user.destroy!
|
12
|
+
rescue ActiveRecord::RecordNotDestroyed => invalid
|
13
|
+
false
|
14
|
+
end
|
15
|
+
|
16
|
+
def info_message
|
17
|
+
t('info')
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Hello
|
2
|
+
module Business
|
3
|
+
module Management
|
4
|
+
class ConfirmEmail < Base
|
5
|
+
attr_reader :credential
|
6
|
+
|
7
|
+
def initialize(credential)
|
8
|
+
@credential = credential
|
9
|
+
end
|
10
|
+
|
11
|
+
def validate_token(unencrypted_token)
|
12
|
+
# puts "validate_token('#{unencrypted_token}')".blue
|
13
|
+
return false unless found_credential?
|
14
|
+
return true if credential.verifying_token_is?(unencrypted_token)
|
15
|
+
@credential = nil
|
16
|
+
end
|
17
|
+
|
18
|
+
def confirm_with_token(token)
|
19
|
+
validate_token(token) && confirm_email!
|
20
|
+
end
|
21
|
+
|
22
|
+
def confirm_email!
|
23
|
+
credential.confirm_email!
|
24
|
+
end
|
25
|
+
|
26
|
+
def found_credential?
|
27
|
+
!!credential
|
28
|
+
end
|
29
|
+
|
30
|
+
def success_message
|
31
|
+
super(email: credential.email)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Hello
|
2
|
+
module Business
|
3
|
+
module Management
|
4
|
+
class ForgotPassword < Base
|
5
|
+
attr_accessor :login
|
6
|
+
attr_reader :user
|
7
|
+
|
8
|
+
def initialize(attrs = nil)
|
9
|
+
if attrs
|
10
|
+
@login = attrs[:login]
|
11
|
+
@user = find_user
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def reset
|
16
|
+
if user.present?
|
17
|
+
true
|
18
|
+
else
|
19
|
+
errors.add(:login, 'was not found')
|
20
|
+
false
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def email?
|
25
|
+
login.to_s.include? '@'
|
26
|
+
end
|
27
|
+
|
28
|
+
def success_message(_extra = {})
|
29
|
+
super(login: @login)
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
# initialize helpers
|
35
|
+
|
36
|
+
def find_user
|
37
|
+
if email?
|
38
|
+
credential = ::EmailCredential.find_by_email(login)
|
39
|
+
credential.user
|
40
|
+
else
|
41
|
+
::User.where(username: login).first
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Hello
|
2
|
+
module Business
|
3
|
+
module Management
|
4
|
+
class RemoveEmail < Base
|
5
|
+
def initialize(email_credential)
|
6
|
+
@email_credential = email_credential
|
7
|
+
end
|
8
|
+
|
9
|
+
def success_message
|
10
|
+
super(email: @email_credential.email)
|
11
|
+
end
|
12
|
+
|
13
|
+
def error_message
|
14
|
+
@email_credential.errors.full_messages.first
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Hello
|
2
|
+
module Business
|
3
|
+
module Management
|
4
|
+
class ResetPassword < Base
|
5
|
+
attr_reader :password_credential
|
6
|
+
|
7
|
+
def initialize(password_credential)
|
8
|
+
@password_credential = password_credential
|
9
|
+
end
|
10
|
+
|
11
|
+
def update_password(plain_text_password)
|
12
|
+
if @password_credential.update(password: plain_text_password)
|
13
|
+
@password_credential.reset_verifying_token!
|
14
|
+
return true
|
15
|
+
else
|
16
|
+
merge_errors_to_self
|
17
|
+
return false
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def user
|
22
|
+
password_credential.user
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def merge_errors_to_self
|
28
|
+
hash = @password_credential.errors.to_hash
|
29
|
+
hash.each { |k, v| v.each { |v1| errors.add(k, v1) } }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Hello
|
2
|
+
module Business
|
3
|
+
module Management
|
4
|
+
class SendConfirmationEmail < Base
|
5
|
+
attr_reader :controller, :email_credential
|
6
|
+
|
7
|
+
def initialize(controller, email_credential)
|
8
|
+
@controller = controller
|
9
|
+
@email_credential = email_credential
|
10
|
+
end
|
11
|
+
|
12
|
+
def deliver
|
13
|
+
token = email_credential.reset_verifying_token!
|
14
|
+
check_token!(token)
|
15
|
+
url = controller.confirm_email_url(email_credential, token)
|
16
|
+
mail = Hello::Mailer.confirm_email(email, user, url)
|
17
|
+
mail.deliver
|
18
|
+
end
|
19
|
+
|
20
|
+
def success_message(_extra = {})
|
21
|
+
super(email: email_credential.email)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def email
|
27
|
+
email_credential.email
|
28
|
+
end
|
29
|
+
|
30
|
+
def user
|
31
|
+
email_credential.user
|
32
|
+
end
|
33
|
+
|
34
|
+
def check_token!(unencrypted_token)
|
35
|
+
fail 'no match' unless email_credential.verifying_token_is?(unencrypted_token)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Hello
|
2
|
+
module Business
|
3
|
+
module Management
|
4
|
+
class UpdateProfile < Base
|
5
|
+
def initialize(user)
|
6
|
+
@user = user
|
7
|
+
self.class.send :attr_accessor, *permitted_column_names
|
8
|
+
end
|
9
|
+
|
10
|
+
def update(attrs)
|
11
|
+
# puts "update(#{attrs})".blue
|
12
|
+
clear_attrs(attrs).each do |k, v|
|
13
|
+
# puts "@user.send('#{k}=', '#{v}')".blue
|
14
|
+
@user.send("#{k}=", v)
|
15
|
+
end
|
16
|
+
@user.save
|
17
|
+
end
|
18
|
+
|
19
|
+
# def update(attrs)
|
20
|
+
# @user.update(clear_attrs(attrs))
|
21
|
+
# end
|
22
|
+
|
23
|
+
def errors
|
24
|
+
@user.errors
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def clear_attrs(attrs)
|
30
|
+
attrs.slice(*permitted_column_names)
|
31
|
+
end
|
32
|
+
|
33
|
+
def permitted_column_names
|
34
|
+
ignore_columns = %w(id created_at updated_at role)
|
35
|
+
the_columns = ::User.column_names
|
36
|
+
the_columns -= ignore_columns
|
37
|
+
the_columns.reject! { |column| column.ends_with? '_count' }
|
38
|
+
the_columns.reject! { |column| column.starts_with? 'password_' }
|
39
|
+
the_columns
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,159 @@
|
|
1
|
+
module Hello
|
2
|
+
module Business
|
3
|
+
module Registration
|
4
|
+
class SignUp < Base
|
5
|
+
attr_reader :email_credential, :password_credential, :user
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
generate_accessors
|
9
|
+
write_defaults
|
10
|
+
end
|
11
|
+
|
12
|
+
def register(attrs)
|
13
|
+
write_attrs(attrs.with_indifferent_access)
|
14
|
+
create_models
|
15
|
+
end
|
16
|
+
|
17
|
+
# errors.added? DOES NOT WORK when the validation was given a custom message :)
|
18
|
+
def email_taken?
|
19
|
+
@email_credential && @email_credential.errors.added?(:email, :taken)
|
20
|
+
end
|
21
|
+
|
22
|
+
# errors.added? DOES NOT WORK when the validation was given a custom message :)
|
23
|
+
def username_taken?
|
24
|
+
@user && user.errors.added?(:username, :taken)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
# initialize helpers
|
30
|
+
|
31
|
+
def generate_accessors
|
32
|
+
self.class.send :attr_accessor, *all_fields
|
33
|
+
end
|
34
|
+
|
35
|
+
def all_fields
|
36
|
+
user_fields + %w(email password)
|
37
|
+
end
|
38
|
+
|
39
|
+
def write_defaults
|
40
|
+
# defaults.each { |k, v| instance_variable_set(:"@#{k}", v) }
|
41
|
+
defaults.each { |k, v| send("#{k}=", v) }
|
42
|
+
end
|
43
|
+
|
44
|
+
# save helpers
|
45
|
+
|
46
|
+
def write_attrs(attrs)
|
47
|
+
# attrs.slice(*all_fields).each { |k, v| instance_variable_set(:"@#{k}", v) if v }
|
48
|
+
attrs.slice(*all_fields).each { |k, v| send("#{k}=", v) if v }
|
49
|
+
end
|
50
|
+
|
51
|
+
# NOTE:
|
52
|
+
# All validations are delegated to the models
|
53
|
+
def create_models
|
54
|
+
validate_presences
|
55
|
+
return false if errors.any?
|
56
|
+
|
57
|
+
build_and_validate_models
|
58
|
+
return false if errors.any?
|
59
|
+
|
60
|
+
save_models!
|
61
|
+
true
|
62
|
+
end
|
63
|
+
|
64
|
+
def validate_presences
|
65
|
+
validates_presence_of :email if validates_presence_of_email?
|
66
|
+
|
67
|
+
validates_presence_of :username if validates_presence_of_username?
|
68
|
+
|
69
|
+
validates_presence_of :password if validates_presence_of_password?
|
70
|
+
|
71
|
+
::User.validators.each do |validator|
|
72
|
+
next unless validator.is_a?(ActiveRecord::Validations::PresenceValidator)
|
73
|
+
next unless validator.attributes.first.to_sym != :username
|
74
|
+
options = validator.options.dup
|
75
|
+
options[:attributes] = validator.attributes
|
76
|
+
validates_with(validator.class, options)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def build_and_validate_models
|
81
|
+
# user & username
|
82
|
+
@user = ::User.new(user_attributes)
|
83
|
+
if username.blank? && !validates_presence_of_username?
|
84
|
+
user.set_generated_username
|
85
|
+
end
|
86
|
+
merge_errors_for(user)
|
87
|
+
|
88
|
+
# email_credential & email
|
89
|
+
if email.present? || validates_presence_of_email?
|
90
|
+
@email_credential = ::EmailCredential.new user: @user, email: email
|
91
|
+
merge_errors_for(email_credential)
|
92
|
+
end
|
93
|
+
|
94
|
+
# password_credential & password
|
95
|
+
@password_credential = ::PasswordCredential.new user: @user, password: password
|
96
|
+
if password.blank? && !validates_presence_of_password?
|
97
|
+
password_credential.set_generated_password
|
98
|
+
end
|
99
|
+
merge_errors_for(password_credential)
|
100
|
+
end
|
101
|
+
|
102
|
+
def merge_errors_for(model)
|
103
|
+
model.valid?
|
104
|
+
model.errors.each do |k, v|
|
105
|
+
errors.add(k, v)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def save_models!
|
110
|
+
user.save!
|
111
|
+
email_credential && email_credential.save!
|
112
|
+
password_credential.save!
|
113
|
+
end
|
114
|
+
|
115
|
+
# returns {name: "...", city: "..."}
|
116
|
+
def user_attributes
|
117
|
+
r = {}
|
118
|
+
user_fields.each { |k| r[k.to_s] = instance_variable_get(:"@#{k}") }
|
119
|
+
r['role'] = starting_role
|
120
|
+
r
|
121
|
+
end
|
122
|
+
|
123
|
+
def starting_role
|
124
|
+
'onboarding'
|
125
|
+
end
|
126
|
+
|
127
|
+
def user_fields
|
128
|
+
Hello.configuration.sign_up_fields.map(&:to_s)
|
129
|
+
end
|
130
|
+
|
131
|
+
def defaults
|
132
|
+
{
|
133
|
+
locale: I18n.locale.to_s,
|
134
|
+
time_zone: Time.zone.name
|
135
|
+
}
|
136
|
+
end
|
137
|
+
|
138
|
+
# config helpers
|
139
|
+
|
140
|
+
def validates_presence_of_email?
|
141
|
+
Hello.configuration.email_presence
|
142
|
+
end
|
143
|
+
|
144
|
+
def validates_presence_of_username?
|
145
|
+
Hello.configuration.username_presence
|
146
|
+
end
|
147
|
+
|
148
|
+
def validates_presence_of_password?
|
149
|
+
Hello.configuration.password_presence
|
150
|
+
end
|
151
|
+
|
152
|
+
# just because!
|
153
|
+
|
154
|
+
def self._reflect_on_association(*_args)
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|