tramway-auth 1.2.1.1 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6093468664bc0b122b3fc7bb1cda7a7030581936ce72e3ce4994ba2fd3a9153c
4
- data.tar.gz: 340ac029ced667dcdddd10e1f201a75e05c447d462de49344dc6671c2890d06b
3
+ metadata.gz: 4adfdbe574a9d31bc8077b374b70157cfda825f0600269a4224b5ff177649f5f
4
+ data.tar.gz: 83bfe0c4bbaa456ced5403604f7d451ba1a77267300d0d7eb084c7ceadd613e2
5
5
  SHA512:
6
- metadata.gz: d3d92bd186abc10659925738a2de77e90ad698b58ce1a1a08013d6f20c324ac7ad9802a0b586b104917fd071cb2e51fcb88a512e6cb6e122232940e115ae9864
7
- data.tar.gz: e1684587ff576a464a83e79b7add3fd7ac7022f5f0dc86f0d91bf6b9c4c4cd9c3666e4381fee12330f45a368a56c438fb154118ff0860b0b0e518b13219bbbfb
6
+ metadata.gz: ac20f69be6eda14d8563147ae5642dd056c36faf70269c8cbb03c0b9433902cd75b437e6760253930925aa2662ef41b00a0fbada4b05277b060837b95192f99b
7
+ data.tar.gz: 42dcdfc8047a1ff5f13bf6985d3b101404deae427f85e5c3d7be798c3013978b0d0f9b1ce8823bb4302d56bd19f3b9c96f1c47c64e2736c8b749f47f3e36e465
@@ -3,33 +3,31 @@
3
3
  module Tramway
4
4
  module AuthManagement
5
5
  def sign_in(user)
6
- session[:user_id] = user.id
6
+ session[user_id_key(user.class)] = user.id
7
7
  end
8
8
 
9
- def sign_out
10
- session[:user_id] = nil
9
+ def sign_out(user_class = ::Tramway::User::User)
10
+ session[user_id_key(user_class.constantize)] = nil
11
11
  end
12
12
 
13
- def signed_in?
14
- current_user
13
+ def signed_in?(user_class = ::Tramway::User::User)
14
+ current_user(user_class)
15
15
  end
16
16
 
17
- def authenticate_user!
18
- redirect_to new_session_path unless signed_in?
17
+ def authenticate_user!(user_class = ::Tramway::User::User)
18
+ redirect_to new_session_path unless signed_in?(user_class)
19
19
  end
20
20
 
21
- def authenticate_admin!
22
- if signed_in?
23
- if !current_user.admin? && request.env['PATH_INFO'] != ::Tramway::Auth.root_path
24
- redirect_to ::Tramway::Auth.root_path
25
- end
26
- else
27
- redirect_to '/auth/session/new'
28
- end
21
+ def current_user(user_class = ::Tramway::User::User)
22
+ user = user_class.find_by id: session[user_id_key(user_class)]
23
+ return false unless user
24
+ "#{user_class}Decorator".constantize.decorate user
29
25
  end
30
26
 
31
- def current_user
32
- @_current_user ||= ::Tramway::User::User.find_by id: session[:user_id]
27
+ private
28
+
29
+ def user_id_key(user_class)
30
+ "#{user_class.to_s.underscore}_id".to_sym
33
31
  end
34
32
  end
35
33
  end
@@ -5,30 +5,26 @@ module Tramway::Auth
5
5
  class SessionsController < ::Tramway::Auth::Web::ApplicationController
6
6
  before_action :redirect_if_signed_in, except: :destroy
7
7
 
8
- def new
9
- @application = Tramway::Core.application&.model_class&.first || Tramway::Core.application
10
- @session_form = ::Tramway::Auth::SessionForm.new ::Tramway::User::User.new
11
- end
12
-
13
8
  def create
14
- @session_form = ::Tramway::Auth::SessionForm.new ::Tramway::User::User.active.find_or_initialize_by email: params[:user][:email]
9
+ @session_form = ::Tramway::Auth::SessionForm.new params[:model].constantize.active.find_by email: params[:user][:email]
15
10
  if @session_form.validate params[:user]
16
11
  sign_in @session_form.model
17
- redirect_to ::Tramway::Auth.root_path
12
+ redirect_to [params[:success_redirect], '?', { flash: :success_user_sign_in }.to_query].join || ::Tramway::Auth.root_path_for(@session_form.model.class)
18
13
  else
19
- render :new
14
+ redirect_to [params[:error_redirect], '?', { flash: :error_user_sign_in }.to_query].join || ::Tramway::Auth.root_path_for(@session_form.model.class)
20
15
  end
21
16
  end
22
17
 
23
18
  def destroy
24
- sign_out
25
- redirect_to ::Tramway::Auth.root_path
19
+ root_path = ::Tramway::Auth.root_path_for(current_user.class)
20
+ sign_out params[:model]
21
+ redirect_to params[:redirect] || root_path
26
22
  end
27
23
 
28
24
  private
29
25
 
30
26
  def redirect_if_signed_in
31
- redirect_to ::Tramway::Auth.root_path if signed_in? && request.env['PATH_INFO'] != ::Tramway::Auth.root_path
27
+ redirect_to ::Tramway::Auth.root_path_for(current_user.class) if params[:model].present? && signed_in?(params[:model].constantize) && request.env['PATH_INFO'] != ::Tramway::Auth.root_path_for(current_user.class)
32
28
  end
33
29
  end
34
30
  end
@@ -4,19 +4,20 @@ class Tramway::Auth::Web::SignUpsController < Tramway::Auth::Web::ApplicationCon
4
4
  before_action :check_authenticable_models
5
5
 
6
6
  def create
7
- @form = "#{model_class}SignUpForm".constantize.new model_class.new
8
- if @form.submit params[:record]
9
- additional_params = { flash: :success }
10
- url = if params[:redirect].present?
11
- [params[:redirect], '?', additional_params.to_query].join
7
+ @form = "Public::#{model_class}SignUpForm".constantize.new model_class.new
8
+ if @form.submit params[:user]
9
+ additional_params = { flash: :success_user_sign_up }
10
+ url = if params[:success_redirect].present?
11
+ [params[:success_redirect], '?', additional_params.to_query].join
12
12
  else
13
13
  Rails.application.routes.url_helpers.root_path(flash: :success)
14
14
  end
15
+ sign_in @form.model if @form.class.sign_in_after
15
16
  redirect_to url
16
17
  else
17
- additional_params = { flash: :error, errors: @form.errors.messages, record: @form.attributes }
18
- url = if params[:redirect].present?
19
- [params[:redirect], '?', additional_params.to_query].join
18
+ additional_params = { flash: :error_user_sign_up, errors: @form.errors.messages, record: @form.attributes }
19
+ url = if params[:error_redirect].present?
20
+ [params[:error_redirect], '?', additional_params.to_query].join
20
21
  else
21
22
  Rails.application.routes.url_helpers.root_path(**additional_params)
22
23
  end
@@ -5,10 +5,6 @@ module Tramway::Auth
5
5
  properties :email
6
6
  attr_accessor :password
7
7
 
8
- def model_name
9
- User
10
- end
11
-
12
8
  def validate(params)
13
9
  (!model.new_record? && model.authenticate(params[:password])).tap do |result|
14
10
  add_wrong_email_or_password_error unless result
@@ -0,0 +1,5 @@
1
+ class Tramway::Auth::SignUpForm < Tramway::Core::ApplicationForm
2
+ class << self
3
+ attr_accessor :sign_in_after
4
+ end
5
+ end
@@ -7,6 +7,12 @@ module Tramway
7
7
  include ::FontAwesome5::Rails::IconHelper
8
8
  include Tramway::Core::TitleHelper
9
9
  include Tramway::Admin::NavbarHelper
10
+ include Tramway::Admin::CasesHelper
11
+ include Tramway::Admin::RussianCasesHelper
12
+
13
+ def decorator_class(model_name = nil)
14
+ "#{model_name || model_class}Decorator".constantize
15
+ end
10
16
  end
11
17
  end
12
18
  end
data/config/routes.rb CHANGED
@@ -2,7 +2,8 @@
2
2
 
3
3
  Tramway::Auth::Engine.routes.draw do
4
4
  scope module: :web do
5
- resource :session, only: %i[new create destroy]
5
+ resource :session, only: %i[new create]
6
+ get 'sign_out', to: 'sessions#destroy'
6
7
  resource :sign_up, only: :create
7
8
  end
8
9
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Tramway
4
4
  module Auth
5
- VERSION = '1.2.1.1'
5
+ VERSION = '2.0.1'
6
6
  end
7
7
  end
data/lib/tramway/auth.rb CHANGED
@@ -28,10 +28,13 @@ module Tramway
28
28
  @layout_path ||= 'tramway/user/application'
29
29
  end
30
30
 
31
- attr_writer :root_path
31
+ def root_path_for=(**options)
32
+ @root_path ||= {}
33
+ @root_path.merge! options
34
+ end
32
35
 
33
- def root_path
34
- @root_path || '/'
36
+ def root_path_for(user_class)
37
+ @root_path&.dig(user_class) || '/'
35
38
  end
36
39
  end
37
40
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tramway-auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1.1
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Kalashnikov
@@ -29,11 +29,11 @@ files:
29
29
  - app/controllers/tramway/auth/web/sessions_controller.rb
30
30
  - app/controllers/tramway/auth/web/sign_ups_controller.rb
31
31
  - app/forms/tramway/auth/session_form.rb
32
+ - app/forms/tramway/auth/sign_up_form.rb
32
33
  - app/helpers/tramway/auth/application_helper.rb
33
34
  - app/jobs/tramway/auth/application_job.rb
34
35
  - app/mailers/tramway/auth/application_mailer.rb
35
36
  - app/models/tramway/auth/application_record.rb
36
- - app/views/tramway/auth/web/sessions/new.html.haml
37
37
  - config/locales/en/helpers.yml
38
38
  - config/locales/en/locale.yml
39
39
  - config/locales/ru/helpers.yml
@@ -1,7 +0,0 @@
1
- - title
2
- .row
3
- .col-md-6
4
- = simple_form_for @session_form.model, url: session_path, method: :post, html: { class: 'form-horizontal' } do |f|
5
- = f.input :email, as: :string
6
- = f.input :password
7
- = f.button :submit, t('helpers.links.enter'), class: 'btn btn-success'