tramway-auth 1.1.0.3 → 2.0.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0d2473a57b2fa14a55ca4b0ebf937c407fffb0a77f36c7873ef0397986fc1d02
4
- data.tar.gz: 210d77cce88f4f85d4769856c91f1d5306bd030b165a22a23a808bb53c335ceb
3
+ metadata.gz: 296edc29aec74f9944ac494dc9fd393384f1d060bc4110aede56e023a22ec8cc
4
+ data.tar.gz: 65612c69a4d28844ef172a4c113c7d97bea422b4eceda2951876238f6c81d43b
5
5
  SHA512:
6
- metadata.gz: af9c60e64439b9f790c9c6af09416e0b7d7c626e3ffecc0c8bdf5f9d371a1e4e2da58636ee005499193e88561ff0a7fcc404819cbac9480cc2f87d0b5d977e96
7
- data.tar.gz: f4410b617a4657715bb720167a90779b9375f2d8fbbcc5b48ffaa14ea50e65c97b9ad3695acfb46d4b1a554de32a34278d5e27e026964daedaac5eedb5338ca2
6
+ metadata.gz: 3d2389875a90181944620b3188e6e2174e20933796007fc8b267b2d8ffbc7a7c11c4d7f207a973e525e470d7a58c20584602f05419c828f5c01dcc1ca5bde5dc
7
+ data.tar.gz: cfe0f77f8e1a049b1d9d1750d8d5cad9e64ba1f02dcc56ff5f56a506783bb337bce88cafa00faa7d816ff8b2ec3efd5b39baf2cfdfe4a30c85b8c695dfbb7135
data/README.md CHANGED
@@ -1,9 +1,6 @@
1
1
  # Tramway::Auth
2
2
  Short description and motivation.
3
3
 
4
- ## Usage
5
- How to use my plugin.
6
-
7
4
  ## Installation
8
5
  Add this line to your application's Gemfile:
9
6
 
@@ -21,6 +18,47 @@ Or install it yourself as:
21
18
  $ gem install tramway-auth
22
19
  ```
23
20
 
21
+ ## Usage
22
+
23
+ ### Set authentication
24
+
25
+ *config/initializers/tramway/auth.rb*
26
+ ```ruby
27
+ Tramway::Auth.root_path_for YourModel => "/your_path", AnotherYourModel => "/another_path"
28
+ ```
29
+
30
+ ### Add sign up
31
+
32
+ #### 1. Create model sign up form
33
+
34
+ We have `User` model. System will create instances of this model on Sign Up. Then we should create `UserSignUpForm`.
35
+
36
+ ```ruby
37
+ class UserSignUpForm < Tramway::Core::ApplicationForm
38
+ properties :email, :password # you may add all you needed properties here
39
+
40
+ def initiailize(object)
41
+ super(object).tap do
42
+ self.submit_message = 'Sign Up'
43
+ form_properties email: :string,
44
+ first_name: :string,
45
+ last_name: :string,
46
+ password: :default
47
+ end
48
+ end
49
+ end
50
+ ```
51
+
52
+ #### 2. Add password encryption into the User model
53
+
54
+ **Don't forget that User model must inherited of Tramway::Core::ApplicationRecord**
55
+
56
+ ```ruby
57
+ class User < ApplicationRecord
58
+ has_secure_password
59
+ end
60
+ ```
61
+
24
62
  ## Contributing
25
63
  Contribution directions go here.
26
64
 
@@ -3,33 +3,32 @@
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
+
25
+ "#{user_class}Decorator".constantize.decorate user
29
26
  end
30
27
 
31
- def current_user
32
- @_current_user ||= ::Tramway::User::User.find_by id: session[:user_id]
28
+ private
29
+
30
+ def user_id_key(user_class)
31
+ "#{user_class.to_s.underscore}_id".to_sym
33
32
  end
34
33
  end
35
34
  end
@@ -1,17 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Tramway
4
- module Auth
5
- class ApplicationController < ActionController::Base
6
- layout ::Tramway::Auth.layout_path
7
- protect_from_forgery with: :exception
8
- before_action :application
3
+ class Tramway::Auth::ApplicationController < Tramway::Core::ApplicationController
4
+ layout ::Tramway::Auth.layout_path
5
+ protect_from_forgery with: :exception
6
+ before_action :application
9
7
 
10
- def application
11
- if ::Tramway::Core.application
12
- @application = Tramway::Core.application&.model_class&.first || Tramway::Core.application
13
- end
14
- end
8
+ def application
9
+ if ::Tramway::Core.application
10
+ @application = Tramway::Core.application&.model_class&.first || Tramway::Core.application
15
11
  end
16
12
  end
17
13
  end
@@ -5,29 +5,32 @@ 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
- @session_form = ::Tramway::Auth::SessionForm.new ::Tramway::User::User.new
10
- end
11
-
12
8
  def create
13
- @session_form = ::Tramway::Auth::SessionForm.new ::Tramway::User::User.active.find_or_initialize_by email: params[:user][:email]
14
- if @session_form.validate params[:user]
15
- sign_in @session_form.model
16
- redirect_to ::Tramway::Auth.root_path
9
+ @session_form = ::Tramway::Auth::SessionForm.new params[:model].constantize.active.find_by email: params[:user][:email]
10
+ if @session_form.model.present?
11
+ if @session_form.validate params[:user]
12
+ sign_in @session_form.model
13
+ redirect_to [params[:success_redirect], '?', { flash: :success_user_sign_in }.to_query].join || ::Tramway::Auth.root_path_for(@session_form.model.class)
14
+ else
15
+ redirect_to [params[:error_redirect], '?', { flash: :error_user_sign_in }.to_query].join || ::Tramway::Auth.root_path_for(@session_form.model.class)
16
+ end
17
17
  else
18
- render :new
18
+ redirect_to [params[:error_redirect], '?', { flash: :error_user_sign_in }.to_query].join || ::Tramway::Auth.root_path_for(params[:model].constantize)
19
19
  end
20
20
  end
21
21
 
22
22
  def destroy
23
- sign_out
24
- redirect_to ::Tramway::Auth.root_path
23
+ root_path = ::Tramway::Auth.root_path_for(current_user.class)
24
+ sign_out params[:model]
25
+ redirect_to params[:redirect] || root_path
25
26
  end
26
27
 
27
28
  private
28
29
 
29
30
  def redirect_if_signed_in
30
- redirect_to ::Tramway::Auth.root_path if signed_in? && request.env['PATH_INFO'] != ::Tramway::Auth.root_path
31
+ if params[:model].present? && signed_in?(params[:model].constantize) && request.env['PATH_INFO'] != ::Tramway::Auth.root_path_for(current_user.class)
32
+ redirect_to ::Tramway::Auth.root_path_for(current_user.class)
33
+ end
31
34
  end
32
35
  end
33
36
  end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Tramway::Auth::Web::SignUpsController < Tramway::Auth::Web::ApplicationController
4
+ before_action :check_authenticable_models
5
+
6
+ def create
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
+ else
13
+ Rails.application.routes.url_helpers.root_path(flash: :success)
14
+ end
15
+ sign_in @form.model if @form.class.sign_in_after
16
+ redirect_to url
17
+ else
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
21
+ else
22
+ Rails.application.routes.url_helpers.root_path(**additional_params)
23
+ end
24
+ redirect_to url
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def check_authenticable_models
31
+ return unless params[:model].in? Tramway::Auth.authenticable_models
32
+ end
33
+ 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,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Tramway::Auth::SignUpForm < Tramway::Core::ApplicationForm
4
+ class << self
5
+ attr_accessor :sign_in_after
6
+ end
7
+ 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
@@ -2,6 +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'
7
+ resource :sign_up, only: :create
6
8
  end
7
9
  end
@@ -5,16 +5,16 @@ require 'tramway/auth/engine'
5
5
  module Tramway
6
6
  module Auth
7
7
  class << self
8
- def authenticable_classes
9
- @authenticable_classes ||= []
8
+ def authenticable_models
9
+ @authenticable_models ||= []
10
10
  end
11
11
 
12
- def authenticable_classes=(value)
13
- @authenticable_classes ||= []
12
+ def authenticable_models=(value)
13
+ @authenticable_models ||= []
14
14
  if value.is_a? Array
15
- @authenticable_classes += value
15
+ @authenticable_models += value
16
16
  else
17
- @authenticable_classes << value
17
+ @authenticable_models << value
18
18
  end
19
19
  end
20
20
 
@@ -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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Tramway
4
4
  module Auth
5
- VERSION = '1.1.0.3'
5
+ VERSION = '2.0.2'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tramway-auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0.3
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Kalashnikov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-05 00:00:00.000000000 Z
11
+ date: 2020-09-15 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Rails engine for auth
14
14
  email:
@@ -27,12 +27,13 @@ files:
27
27
  - app/controllers/tramway/auth/application_controller.rb
28
28
  - app/controllers/tramway/auth/web/application_controller.rb
29
29
  - app/controllers/tramway/auth/web/sessions_controller.rb
30
+ - app/controllers/tramway/auth/web/sign_ups_controller.rb
30
31
  - app/forms/tramway/auth/session_form.rb
32
+ - app/forms/tramway/auth/sign_up_form.rb
31
33
  - app/helpers/tramway/auth/application_helper.rb
32
34
  - app/jobs/tramway/auth/application_job.rb
33
35
  - app/mailers/tramway/auth/application_mailer.rb
34
36
  - app/models/tramway/auth/application_record.rb
35
- - app/views/tramway/auth/web/sessions/new.html.haml
36
37
  - config/locales/en/helpers.yml
37
38
  - config/locales/en/locale.yml
38
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'