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 +4 -4
- data/README.md +41 -3
- data/app/controllers/concerns/tramway/auth_management.rb +16 -17
- data/app/controllers/tramway/auth/application_controller.rb +7 -11
- data/app/controllers/tramway/auth/web/sessions_controller.rb +15 -12
- data/app/controllers/tramway/auth/web/sign_ups_controller.rb +33 -0
- data/app/forms/tramway/auth/session_form.rb +0 -4
- data/app/forms/tramway/auth/sign_up_form.rb +7 -0
- data/app/helpers/tramway/auth/application_helper.rb +6 -0
- data/config/routes.rb +3 -1
- data/lib/tramway/auth.rb +12 -9
- data/lib/tramway/auth/version.rb +1 -1
- metadata +4 -3
- data/app/views/tramway/auth/web/sessions/new.html.haml +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 296edc29aec74f9944ac494dc9fd393384f1d060bc4110aede56e023a22ec8cc
|
4
|
+
data.tar.gz: 65612c69a4d28844ef172a4c113c7d97bea422b4eceda2951876238f6c81d43b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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[
|
6
|
+
session[user_id_key(user.class)] = user.id
|
7
7
|
end
|
8
8
|
|
9
|
-
def sign_out
|
10
|
-
session[
|
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
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
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
|
-
|
32
|
-
|
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
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
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
|
-
|
11
|
-
|
12
|
-
|
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
|
14
|
-
if @session_form.
|
15
|
-
|
16
|
-
|
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
|
-
|
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
|
-
|
24
|
-
|
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
|
-
|
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
|
@@ -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
data/lib/tramway/auth.rb
CHANGED
@@ -5,16 +5,16 @@ require 'tramway/auth/engine'
|
|
5
5
|
module Tramway
|
6
6
|
module Auth
|
7
7
|
class << self
|
8
|
-
def
|
9
|
-
@
|
8
|
+
def authenticable_models
|
9
|
+
@authenticable_models ||= []
|
10
10
|
end
|
11
11
|
|
12
|
-
def
|
13
|
-
@
|
12
|
+
def authenticable_models=(value)
|
13
|
+
@authenticable_models ||= []
|
14
14
|
if value.is_a? Array
|
15
|
-
@
|
15
|
+
@authenticable_models += value
|
16
16
|
else
|
17
|
-
@
|
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
|
-
|
31
|
+
def root_path_for=(**options)
|
32
|
+
@root_path ||= {}
|
33
|
+
@root_path.merge! options
|
34
|
+
end
|
32
35
|
|
33
|
-
def
|
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
|
data/lib/tramway/auth/version.rb
CHANGED
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:
|
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-
|
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'
|