devise_token_auth 1.0.0 → 1.1.0
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.
Potentially problematic release.
This version of devise_token_auth might be problematic. Click here for more details.
- checksums.yaml +5 -5
- data/README.md +2 -2
- data/app/controllers/devise_token_auth/application_controller.rb +0 -1
- data/app/controllers/devise_token_auth/concerns/resource_finder.rb +11 -12
- data/app/controllers/devise_token_auth/concerns/set_user_by_token.rb +15 -28
- data/app/controllers/devise_token_auth/confirmations_controller.rb +14 -19
- data/app/controllers/devise_token_auth/omniauth_callbacks_controller.rb +46 -21
- data/app/controllers/devise_token_auth/passwords_controller.rb +15 -19
- data/app/controllers/devise_token_auth/registrations_controller.rb +31 -39
- data/app/controllers/devise_token_auth/unlocks_controller.rb +1 -1
- data/app/models/devise_token_auth/concerns/active_record_support.rb +34 -0
- data/app/models/devise_token_auth/concerns/mongoid_support.rb +19 -0
- data/app/models/devise_token_auth/concerns/user.rb +9 -23
- data/app/models/devise_token_auth/concerns/user_omniauth_callbacks.rb +2 -2
- data/app/validators/{email_validator.rb → devise_token_auth/email_validator.rb} +1 -1
- data/config/locales/he.yml +50 -0
- data/config/locales/ja.yml +1 -1
- data/lib/devise_token_auth.rb +5 -3
- data/lib/devise_token_auth/blacklist.rb +2 -0
- data/lib/devise_token_auth/version.rb +1 -1
- data/lib/generators/devise_token_auth/install_generator.rb +3 -87
- data/lib/generators/devise_token_auth/install_generator_helpers.rb +98 -0
- data/lib/generators/devise_token_auth/install_mongoid_generator.rb +46 -0
- data/lib/generators/devise_token_auth/templates/devise_token_auth_create_users.rb.erb +0 -7
- data/lib/generators/devise_token_auth/templates/user_mongoid.rb.erb +56 -0
- data/test/controllers/custom/custom_confirmations_controller_test.rb +1 -1
- data/test/controllers/devise_token_auth/confirmations_controller_test.rb +41 -20
- data/test/controllers/devise_token_auth/omniauth_callbacks_controller_test.rb +2 -0
- data/test/controllers/devise_token_auth/passwords_controller_test.rb +115 -94
- data/test/controllers/devise_token_auth/registrations_controller_test.rb +31 -4
- data/test/controllers/devise_token_auth/sessions_controller_test.rb +0 -38
- data/test/controllers/devise_token_auth/token_validations_controller_test.rb +2 -1
- data/test/dummy/app/{models → active_record}/lockable_user.rb +0 -0
- data/test/dummy/app/{models → active_record}/mang.rb +0 -0
- data/test/dummy/app/{models → active_record}/only_email_user.rb +0 -0
- data/test/dummy/app/{models → active_record}/scoped_user.rb +2 -2
- data/test/dummy/app/{models → active_record}/unconfirmable_user.rb +1 -2
- data/test/dummy/app/{models → active_record}/unregisterable_user.rb +3 -3
- data/test/dummy/app/active_record/user.rb +6 -0
- data/test/dummy/app/controllers/overrides/sessions_controller.rb +1 -1
- data/test/dummy/app/models/{user.rb → concerns/favorite_color.rb} +7 -8
- data/test/dummy/app/mongoid/lockable_user.rb +38 -0
- data/test/dummy/app/mongoid/mang.rb +46 -0
- data/test/dummy/app/mongoid/only_email_user.rb +33 -0
- data/test/dummy/app/mongoid/scoped_user.rb +50 -0
- data/test/dummy/app/mongoid/unconfirmable_user.rb +44 -0
- data/test/dummy/app/mongoid/unregisterable_user.rb +47 -0
- data/test/dummy/app/mongoid/user.rb +49 -0
- data/test/dummy/config/application.rb +23 -1
- data/test/dummy/config/boot.rb +4 -0
- data/test/dummy/config/initializers/devise.rb +12 -0
- data/test/dummy/db/migrate/20140715061447_devise_token_auth_create_users.rb +0 -7
- data/test/dummy/db/migrate/20140715061805_devise_token_auth_create_mangs.rb +0 -7
- data/test/dummy/db/migrate/20141222035835_devise_token_auth_create_only_email_users.rb +0 -7
- data/test/dummy/db/migrate/20141222053502_devise_token_auth_create_unregisterable_users.rb +0 -7
- data/test/dummy/db/migrate/20150708104536_devise_token_auth_create_unconfirmable_users.rb +0 -7
- data/test/dummy/db/migrate/20160103235141_devise_token_auth_create_scoped_users.rb +0 -7
- data/test/dummy/db/migrate/20160629184441_devise_token_auth_create_lockable_users.rb +0 -7
- data/test/dummy/db/schema.rb +1 -28
- data/test/dummy/tmp/generators/app/models/azpire/v1/human_resource/user.rb +9 -0
- data/test/dummy/tmp/generators/config/initializers/devise_token_auth.rb +50 -0
- data/test/dummy/tmp/generators/config/routes.rb +4 -0
- data/test/dummy/tmp/generators/db/migrate/20190112150327_devise_token_auth_create_azpire_v1_human_resource_users.rb +56 -0
- data/test/lib/devise_token_auth/blacklist_test.rb +11 -0
- data/test/lib/generators/devise_token_auth/install_generator_test.rb +51 -31
- data/test/lib/generators/devise_token_auth/install_generator_with_namespace_test.rb +51 -31
- data/test/models/concerns/mongoid_support_test.rb +31 -0
- data/test/models/only_email_user_test.rb +0 -8
- data/test/models/user_test.rb +1 -1
- data/test/test_helper.rb +12 -2
- metadata +91 -27
- data/config/initializers/devise.rb +0 -198
- data/test/dummy/tmp/generators/app/views/devise/mailer/confirmation_instructions.html.erb +0 -5
- data/test/dummy/tmp/generators/app/views/devise/mailer/reset_password_instructions.html.erb +0 -8
@@ -34,7 +34,7 @@ module DeviseTokenAuth
|
|
34
34
|
def show
|
35
35
|
@resource = resource_class.unlock_access_by_token(params[:unlock_token])
|
36
36
|
|
37
|
-
if @resource
|
37
|
+
if @resource.persisted?
|
38
38
|
client_id, token = @resource.create_token
|
39
39
|
@resource.save!
|
40
40
|
yield @resource if block_given?
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module DeviseTokenAuth::Concerns::ActiveRecordSupport
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
serialize :tokens, JSON unless tokens_has_json_column_type?
|
6
|
+
|
7
|
+
# can't set default on text fields in mysql, simulate here instead.
|
8
|
+
after_save :set_empty_token_hash
|
9
|
+
after_initialize :set_empty_token_hash
|
10
|
+
end
|
11
|
+
|
12
|
+
class_methods do
|
13
|
+
# It's abstract replacement .find_by
|
14
|
+
def dta_find_by(attrs = {})
|
15
|
+
find_by(attrs)
|
16
|
+
end
|
17
|
+
|
18
|
+
protected
|
19
|
+
|
20
|
+
def tokens_has_json_column_type?
|
21
|
+
database_exists? && table_exists? && columns_hash['tokens'] && columns_hash['tokens'].type.in?([:json, :jsonb])
|
22
|
+
end
|
23
|
+
|
24
|
+
def database_exists?
|
25
|
+
ActiveRecord::Base.connection_pool.with_connection { |con| con.active? } rescue false
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
protected
|
30
|
+
|
31
|
+
def set_empty_token_hash
|
32
|
+
self.tokens ||= {} if has_attribute?(:tokens)
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module DeviseTokenAuth::Concerns::MongoidSupport
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
def as_json(options = {})
|
5
|
+
options[:except] = (options[:except] || []) + [:_id]
|
6
|
+
hash = super(options)
|
7
|
+
hash['id'] = to_param
|
8
|
+
hash
|
9
|
+
end
|
10
|
+
|
11
|
+
class_methods do
|
12
|
+
# It's abstract replacement .find_by
|
13
|
+
def dta_find_by(attrs = {})
|
14
|
+
find_by(attrs)
|
15
|
+
rescue Mongoid::Errors::DocumentNotFound
|
16
|
+
nil
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -20,19 +20,21 @@ module DeviseTokenAuth::Concerns::User
|
|
20
20
|
devise_modules.delete(:omniauthable)
|
21
21
|
else
|
22
22
|
devise :database_authenticatable, :registerable,
|
23
|
-
:recoverable, :
|
23
|
+
:recoverable, :validatable, :confirmable
|
24
24
|
end
|
25
25
|
|
26
|
-
|
26
|
+
if const_defined?('ActiveRecord') && ancestors.include?(ActiveRecord::Base)
|
27
|
+
include DeviseTokenAuth::Concerns::ActiveRecordSupport
|
28
|
+
end
|
29
|
+
|
30
|
+
if const_defined?('Mongoid') && ancestors.include?(Mongoid::Document)
|
31
|
+
include DeviseTokenAuth::Concerns::MongoidSupport
|
32
|
+
end
|
27
33
|
|
28
34
|
if DeviseTokenAuth.default_callbacks
|
29
35
|
include DeviseTokenAuth::Concerns::UserOmniauthCallbacks
|
30
36
|
end
|
31
37
|
|
32
|
-
# can't set default on text fields in mysql, simulate here instead.
|
33
|
-
after_save :set_empty_token_hash
|
34
|
-
after_initialize :set_empty_token_hash
|
35
|
-
|
36
38
|
# get rid of dead tokens
|
37
39
|
before_save :destroy_expired_tokens
|
38
40
|
|
@@ -101,18 +103,6 @@ module DeviseTokenAuth::Concerns::User
|
|
101
103
|
[client_id, token, expiry]
|
102
104
|
end
|
103
105
|
|
104
|
-
module ClassMethods
|
105
|
-
protected
|
106
|
-
|
107
|
-
def tokens_has_json_column_type?
|
108
|
-
database_exists? && table_exists? && columns_hash['tokens'] && columns_hash['tokens'].type.in?([:json, :jsonb])
|
109
|
-
end
|
110
|
-
|
111
|
-
def database_exists?
|
112
|
-
ActiveRecord::Base.connection_pool.with_connection { |con| con.active? } rescue false
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
106
|
def valid_token?(token, client_id = 'default')
|
117
107
|
return false unless tokens[client_id]
|
118
108
|
return true if token_is_current?(token, client_id)
|
@@ -154,7 +144,7 @@ module DeviseTokenAuth::Concerns::User
|
|
154
144
|
updated_at && last_token &&
|
155
145
|
|
156
146
|
# ensure that previous token falls within the batch buffer throttle time of the last request
|
157
|
-
|
147
|
+
updated_at.to_time > Time.zone.now - DeviseTokenAuth.batch_request_buffer_throttle &&
|
158
148
|
|
159
149
|
# ensure that the token is valid
|
160
150
|
::BCrypt::Password.new(last_token) == token
|
@@ -223,10 +213,6 @@ module DeviseTokenAuth::Concerns::User
|
|
223
213
|
|
224
214
|
protected
|
225
215
|
|
226
|
-
def set_empty_token_hash
|
227
|
-
self.tokens ||= {} if has_attribute?(:tokens)
|
228
|
-
end
|
229
|
-
|
230
216
|
def destroy_expired_tokens
|
231
217
|
if tokens
|
232
218
|
tokens.delete_if do |cid, v|
|
@@ -5,7 +5,7 @@ module DeviseTokenAuth::Concerns::UserOmniauthCallbacks
|
|
5
5
|
|
6
6
|
included do
|
7
7
|
validates :email, presence: true,if: :email_provider?
|
8
|
-
validates :email, email
|
8
|
+
validates :email, 'devise_token_auth/email' => true, allow_nil: true, allow_blank: true, if: :email_provider?
|
9
9
|
validates_presence_of :uid, unless: :email_provider?
|
10
10
|
|
11
11
|
# only validate unique emails among email registration users
|
@@ -23,6 +23,6 @@ module DeviseTokenAuth::Concerns::UserOmniauthCallbacks
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def sync_uid
|
26
|
-
self.uid = email if
|
26
|
+
self.uid = email if email_provider?
|
27
27
|
end
|
28
28
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
class EmailValidator < ActiveModel::EachValidator
|
3
|
+
class DeviseTokenAuth::EmailValidator < ActiveModel::EachValidator
|
4
4
|
def validate_each(record, attribute, value)
|
5
5
|
unless value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
|
6
6
|
record.errors[attribute] << email_invalid_message
|
@@ -0,0 +1,50 @@
|
|
1
|
+
he:
|
2
|
+
devise_token_auth:
|
3
|
+
sessions:
|
4
|
+
not_confirmed: "הודעת אישור נשלחה לחשבון שלך בכתובת '%{email}'. עליך לפעול לפי ההנחיות שבדוא\"ל לפני הפעלת החשבון שלך"
|
5
|
+
bad_credentials: "נתוני כניסה שגויים. בבקשה נסה שוב."
|
6
|
+
not_supported: "השתמש ב- POST / sign_in כדי להיכנס. GET אינו נתמך."
|
7
|
+
user_not_found: "המשתמש לא נמצא או לא היה מחובר."
|
8
|
+
token_validations:
|
9
|
+
invalid: "נתוני כניסה שגויים"
|
10
|
+
registrations:
|
11
|
+
missing_confirm_success_url: "חסר פרמטר 'confirm_success_url'."
|
12
|
+
redirect_url_not_allowed: "הפניה אל '%{redirect_url}' אינה מותרת."
|
13
|
+
email_already_exists: "כבר קיים חשבון עבור '%{email}'"
|
14
|
+
account_with_uid_destroyed: "חשבון עם UID '%{uid}' הושמד."
|
15
|
+
account_to_destroy_not_found: "לא ניתן לאתר חשבון להשמדה."
|
16
|
+
user_not_found: "המשתמש לא נמצא."
|
17
|
+
passwords:
|
18
|
+
missing_email: "עליך לספק כתובת דוא\"ל."
|
19
|
+
missing_redirect_url: "כתובת אתר להפניה מחדש חסרה."
|
20
|
+
not_allowed_redirect_url: "הפניה אל '%{redirect_url}' אינה מותרת."
|
21
|
+
sended: "אימייל נשלח ל '%{email}' המכיל הוראות לאיפוס הסיסמה שלך."
|
22
|
+
user_not_found: "לא ניתן למצוא משתמש עם הדוא\"ל '%{email}'."
|
23
|
+
password_not_required: "חשבון זה אינו דורש סיסמה. במקום זאת, השתמש בחשבון '%{provider}' שלך."
|
24
|
+
missing_passwords: "עליך למלא את השדות 'סיסמה' ו'אישור סיסמה'."
|
25
|
+
successfully_updated: "הסיסמה שלך עודכנה בהצלחה."
|
26
|
+
unlocks:
|
27
|
+
missing_email: "עליך לספק כתובת דוא\"ל."
|
28
|
+
sended: "הודעת אימייל נשלחה אל '%{email}' המכילה הוראות לביטול הנעילה של חשבונך."
|
29
|
+
user_not_found: "ניתן למצוא את המשתמש עם הדוא\"ל '%{email}'"
|
30
|
+
errors:
|
31
|
+
messages:
|
32
|
+
validate_sign_up_params: "שלח נתוני רישום תקינים בגוף הבקשה."
|
33
|
+
validate_account_update_params: "שלחו בבקשה נתוני עדכון חשבון תקינים בגוף הבקשה."
|
34
|
+
not_email: "אינו דוא\"ל"
|
35
|
+
devise:
|
36
|
+
mailer:
|
37
|
+
confirmation_instructions:
|
38
|
+
confirm_link_msg: "תוכל לאשר את כתובת הדוא\"ל של החשבון שלך באמצעות הקישור הבא:"
|
39
|
+
confirm_account_link: "אשר את החשבון שלי"
|
40
|
+
reset_password_instructions:
|
41
|
+
request_reset_link_msg: "מישהו ביקש קישור לשינוי הסיסמה שלך. תוכל לעשות זאת באמצעות הקישור הבא."
|
42
|
+
password_change_link: "שנה את הסיסמה שלי"
|
43
|
+
ignore_mail_msg: "אם לא ביקשת זאת, התעלם מדוא\"ל זה."
|
44
|
+
no_changes_msg: "הסיסמה שלך לא תשתנה עד שתגיע לקישור שלמעלה ותיצור סיסמה חדשה."
|
45
|
+
unlock_instructions:
|
46
|
+
account_lock_msg: "החשבון שלך ננעל עקב מספר מופרז של ניסיונות כניסה לא מוצלחים."
|
47
|
+
unlock_link_msg: "לחץ על הקישור למטה כדי לבטל את נעילת החשבון שלך:"
|
48
|
+
unlock_link: "בטל את הנעילה של החשבון שלי"
|
49
|
+
hello: "שלום"
|
50
|
+
welcome: "ברוך הבא"
|
data/config/locales/ja.yml
CHANGED
@@ -34,7 +34,7 @@ ja:
|
|
34
34
|
confirm_link_msg: "下記のリンクからアカウントを有効化できます:"
|
35
35
|
confirm_account_link: "アカウントを有効化する"
|
36
36
|
reset_password_instructions:
|
37
|
-
request_reset_link_msg: "
|
37
|
+
request_reset_link_msg: "パスワード変更のリクエストが送信されました。下記のリンクからパスワードの変更ができます。"
|
38
38
|
password_change_link: "パスワードを変更する"
|
39
39
|
ignore_mail_msg: "もしこの内容に覚えがない場合は、このメールを無視してください。"
|
40
40
|
no_changes_msg: "上記のリンクにアクセスして新しいパスワードを作成するまで、現在のパスワードは変更されません。"
|
data/lib/devise_token_auth.rb
CHANGED
@@ -1,11 +1,13 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'devise'
|
4
|
+
|
5
|
+
module DeviseTokenAuth
|
6
|
+
end
|
7
|
+
|
4
8
|
require 'devise_token_auth/engine'
|
5
9
|
require 'devise_token_auth/controllers/helpers'
|
6
10
|
require 'devise_token_auth/controllers/url_helpers'
|
7
11
|
require 'devise_token_auth/url'
|
8
12
|
require 'devise_token_auth/errors'
|
9
|
-
|
10
|
-
module DeviseTokenAuth
|
11
|
-
end
|
13
|
+
require 'devise_token_auth/blacklist'
|
@@ -1,20 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative 'install_generator_helpers'
|
4
|
+
|
3
5
|
module DeviseTokenAuth
|
4
6
|
class InstallGenerator < Rails::Generators::Base
|
5
7
|
include Rails::Generators::Migration
|
8
|
+
include DeviseTokenAuth::InstallGeneratorHelpers
|
6
9
|
|
7
10
|
class_option :primary_key_type, type: :string, desc: 'The type for primary key'
|
8
11
|
|
9
|
-
source_root File.expand_path('templates', __dir__)
|
10
|
-
|
11
|
-
argument :user_class, type: :string, default: 'User'
|
12
|
-
argument :mount_path, type: :string, default: 'auth'
|
13
|
-
|
14
|
-
def create_initializer_file
|
15
|
-
copy_file('devise_token_auth.rb', 'config/initializers/devise_token_auth.rb')
|
16
|
-
end
|
17
|
-
|
18
12
|
def copy_migrations
|
19
13
|
if self.class.migration_exists?('db/migrate', "devise_token_auth_create_#{user_class.pluralize.gsub('::','').underscore}")
|
20
14
|
say_status('skipped', "Migration 'devise_token_auth_create_#{user_class.pluralize.gsub('::','').underscore}' already exists")
|
@@ -47,90 +41,12 @@ module DeviseTokenAuth
|
|
47
41
|
end
|
48
42
|
end
|
49
43
|
|
50
|
-
def include_controller_concerns
|
51
|
-
fname = 'app/controllers/application_controller.rb'
|
52
|
-
line = 'include DeviseTokenAuth::Concerns::SetUserByToken'
|
53
|
-
|
54
|
-
if File.exist?(File.join(destination_root, fname))
|
55
|
-
if parse_file_for_line(fname, line)
|
56
|
-
say_status('skipped', 'Concern is already included in the application controller.')
|
57
|
-
elsif is_rails_api?
|
58
|
-
inject_into_file fname, after: "class ApplicationController < ActionController::API\n" do <<-'RUBY'
|
59
|
-
include DeviseTokenAuth::Concerns::SetUserByToken
|
60
|
-
RUBY
|
61
|
-
end
|
62
|
-
else
|
63
|
-
inject_into_file fname, after: "class ApplicationController < ActionController::Base\n" do <<-'RUBY'
|
64
|
-
include DeviseTokenAuth::Concerns::SetUserByToken
|
65
|
-
RUBY
|
66
|
-
end
|
67
|
-
end
|
68
|
-
else
|
69
|
-
say_status('skipped', "app/controllers/application_controller.rb not found. Add 'include DeviseTokenAuth::Concerns::SetUserByToken' to any controllers that require authentication.")
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
def add_route_mount
|
74
|
-
f = 'config/routes.rb'
|
75
|
-
str = "mount_devise_token_auth_for '#{user_class}', at: '#{mount_path}'"
|
76
|
-
|
77
|
-
if File.exist?(File.join(destination_root, f))
|
78
|
-
line = parse_file_for_line(f, 'mount_devise_token_auth_for')
|
79
|
-
|
80
|
-
if line
|
81
|
-
existing_user_class = true
|
82
|
-
else
|
83
|
-
line = 'Rails.application.routes.draw do'
|
84
|
-
existing_user_class = false
|
85
|
-
end
|
86
|
-
|
87
|
-
if parse_file_for_line(f, str)
|
88
|
-
say_status('skipped', "Routes already exist for #{user_class} at #{mount_path}")
|
89
|
-
else
|
90
|
-
insert_after_line(f, line, str)
|
91
|
-
|
92
|
-
if existing_user_class
|
93
|
-
scoped_routes = ''\
|
94
|
-
"as :#{user_class.underscore} do\n"\
|
95
|
-
" # Define routes for #{user_class} within this block.\n"\
|
96
|
-
" end\n"
|
97
|
-
insert_after_line(f, str, scoped_routes)
|
98
|
-
end
|
99
|
-
end
|
100
|
-
else
|
101
|
-
say_status('skipped', "config/routes.rb not found. Add \"mount_devise_token_auth_for '#{user_class}', at: '#{mount_path}'\" to your routes file.")
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
44
|
private
|
106
45
|
|
107
46
|
def self.next_migration_number(path)
|
108
47
|
Time.zone.now.utc.strftime('%Y%m%d%H%M%S')
|
109
48
|
end
|
110
49
|
|
111
|
-
def insert_after_line(filename, line, str)
|
112
|
-
gsub_file filename, /(#{Regexp.escape(line)})/mi do |match|
|
113
|
-
"#{match}\n #{str}"
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
def parse_file_for_line(filename, str)
|
118
|
-
match = false
|
119
|
-
|
120
|
-
File.open(File.join(destination_root, filename)) do |f|
|
121
|
-
f.each_line do |line|
|
122
|
-
match = line if line =~ /(#{Regexp.escape(str)})/mi
|
123
|
-
end
|
124
|
-
end
|
125
|
-
match
|
126
|
-
end
|
127
|
-
|
128
|
-
def is_rails_api?
|
129
|
-
fname = 'app/controllers/application_controller.rb'
|
130
|
-
line = 'class ApplicationController < ActionController::API'
|
131
|
-
parse_file_for_line(fname, line)
|
132
|
-
end
|
133
|
-
|
134
50
|
def json_supported_database?
|
135
51
|
(postgres? && postgres_correct_version?) || (mysql? && mysql_correct_version?)
|
136
52
|
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
module DeviseTokenAuth
|
2
|
+
module InstallGeneratorHelpers
|
3
|
+
class << self
|
4
|
+
def included(mod)
|
5
|
+
mod.class_eval do
|
6
|
+
source_root File.expand_path('templates', __dir__)
|
7
|
+
|
8
|
+
argument :user_class, type: :string, default: 'User'
|
9
|
+
argument :mount_path, type: :string, default: 'auth'
|
10
|
+
|
11
|
+
def create_initializer_file
|
12
|
+
copy_file('devise_token_auth.rb', 'config/initializers/devise_token_auth.rb')
|
13
|
+
end
|
14
|
+
|
15
|
+
def include_controller_concerns
|
16
|
+
fname = 'app/controllers/application_controller.rb'
|
17
|
+
line = 'include DeviseTokenAuth::Concerns::SetUserByToken'
|
18
|
+
|
19
|
+
if File.exist?(File.join(destination_root, fname))
|
20
|
+
if parse_file_for_line(fname, line)
|
21
|
+
say_status('skipped', 'Concern is already included in the application controller.')
|
22
|
+
elsif is_rails_api?
|
23
|
+
inject_into_file fname, after: "class ApplicationController < ActionController::API\n" do <<-'RUBY'
|
24
|
+
include DeviseTokenAuth::Concerns::SetUserByToken
|
25
|
+
RUBY
|
26
|
+
end
|
27
|
+
else
|
28
|
+
inject_into_file fname, after: "class ApplicationController < ActionController::Base\n" do <<-'RUBY'
|
29
|
+
include DeviseTokenAuth::Concerns::SetUserByToken
|
30
|
+
RUBY
|
31
|
+
end
|
32
|
+
end
|
33
|
+
else
|
34
|
+
say_status('skipped', "app/controllers/application_controller.rb not found. Add 'include DeviseTokenAuth::Concerns::SetUserByToken' to any controllers that require authentication.")
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def add_route_mount
|
39
|
+
f = 'config/routes.rb'
|
40
|
+
str = "mount_devise_token_auth_for '#{user_class}', at: '#{mount_path}'"
|
41
|
+
|
42
|
+
if File.exist?(File.join(destination_root, f))
|
43
|
+
line = parse_file_for_line(f, 'mount_devise_token_auth_for')
|
44
|
+
|
45
|
+
if line
|
46
|
+
existing_user_class = true
|
47
|
+
else
|
48
|
+
line = 'Rails.application.routes.draw do'
|
49
|
+
existing_user_class = false
|
50
|
+
end
|
51
|
+
|
52
|
+
if parse_file_for_line(f, str)
|
53
|
+
say_status('skipped', "Routes already exist for #{user_class} at #{mount_path}")
|
54
|
+
else
|
55
|
+
insert_after_line(f, line, str)
|
56
|
+
|
57
|
+
if existing_user_class
|
58
|
+
scoped_routes = ''\
|
59
|
+
"as :#{user_class.underscore} do\n"\
|
60
|
+
" # Define routes for #{user_class} within this block.\n"\
|
61
|
+
" end\n"
|
62
|
+
insert_after_line(f, str, scoped_routes)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
else
|
66
|
+
say_status('skipped', "config/routes.rb not found. Add \"mount_devise_token_auth_for '#{user_class}', at: '#{mount_path}'\" to your routes file.")
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def insert_after_line(filename, line, str)
|
73
|
+
gsub_file filename, /(#{Regexp.escape(line)})/mi do |match|
|
74
|
+
"#{match}\n #{str}"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def parse_file_for_line(filename, str)
|
79
|
+
match = false
|
80
|
+
|
81
|
+
File.open(File.join(destination_root, filename)) do |f|
|
82
|
+
f.each_line do |line|
|
83
|
+
match = line if line =~ /(#{Regexp.escape(str)})/mi
|
84
|
+
end
|
85
|
+
end
|
86
|
+
match
|
87
|
+
end
|
88
|
+
|
89
|
+
def is_rails_api?
|
90
|
+
fname = 'app/controllers/application_controller.rb'
|
91
|
+
line = 'class ApplicationController < ActionController::API'
|
92
|
+
parse_file_for_line(fname, line)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|