trestle-auth 0.2.3 → 0.4.1

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 trestle-auth might be problematic. Click here for more details.

Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/.browserslistrc +1 -0
  3. data/.gitignore +5 -0
  4. data/.rspec +1 -0
  5. data/.travis.yml +20 -3
  6. data/Gemfile +14 -0
  7. data/README.md +35 -8
  8. data/app/assets/bundle/trestle/auth/bundle.css +1 -0
  9. data/app/assets/bundle/trestle/auth/userbox.css +1 -0
  10. data/app/assets/stylesheets/trestle/_custom-auth.css +6 -0
  11. data/app/assets/stylesheets/trestle/auth.css +2 -0
  12. data/app/controllers/trestle/auth/sessions_controller.rb +4 -5
  13. data/app/helpers/trestle/auth/title_helper.rb +20 -0
  14. data/app/helpers/trestle/auth/user_helper.rb +2 -6
  15. data/app/views/layouts/trestle/auth.html.erb +11 -16
  16. data/app/views/trestle/auth/_userbox.html.erb +17 -6
  17. data/app/views/trestle/auth/sessions/_form.html.erb +32 -0
  18. data/app/views/trestle/auth/sessions/new.html.erb +8 -31
  19. data/bin/rails +20 -0
  20. data/config/{locale → locales}/en.yml +7 -0
  21. data/config/locales/es.yml +15 -0
  22. data/config/{locale → locales}/pl.yml +7 -0
  23. data/config/{locale → locales}/pt-BR.yml +7 -0
  24. data/config/locales/zh-CN.yml +8 -0
  25. data/config/routes.rb +8 -3
  26. data/frontend/_form.scss +79 -0
  27. data/frontend/_layout.scss +35 -0
  28. data/{app/assets/stylesheets/trestle/auth/_defaults.scss → frontend/_variables.scss} +2 -6
  29. data/frontend/index.scss +5 -0
  30. data/{app/assets/stylesheets/trestle/auth → frontend}/userbox.scss +14 -12
  31. data/gemfiles/rails-4.2.gemfile +18 -0
  32. data/gemfiles/rails-5.0.gemfile +18 -0
  33. data/gemfiles/rails-5.1.gemfile +18 -0
  34. data/gemfiles/rails-5.2.gemfile +18 -0
  35. data/gemfiles/rails-6.0.gemfile +18 -0
  36. data/lib/generators/trestle/auth/account/account_generator.rb +32 -0
  37. data/lib/generators/trestle/auth/account/templates/admin.rb.erb +47 -0
  38. data/lib/generators/trestle/auth/admin/admin_generator.rb +15 -1
  39. data/lib/generators/trestle/auth/admin/templates/admin.rb.erb +28 -5
  40. data/lib/generators/trestle/auth/install/install_generator.rb +39 -83
  41. data/lib/generators/trestle/auth/install/templates/basic.rb.erb +121 -0
  42. data/lib/generators/trestle/auth/install/templates/devise.rb.erb +87 -0
  43. data/lib/trestle/auth.rb +14 -8
  44. data/lib/trestle/auth/backends.rb +34 -0
  45. data/lib/trestle/auth/backends/base.rb +28 -0
  46. data/lib/trestle/auth/backends/basic.rb +72 -0
  47. data/lib/trestle/auth/backends/devise.rb +14 -0
  48. data/lib/trestle/auth/backends/warden.rb +53 -0
  49. data/lib/trestle/auth/configuration.rb +41 -6
  50. data/lib/trestle/auth/configuration/warden.rb +11 -0
  51. data/lib/trestle/auth/controller/authentication.rb +55 -0
  52. data/lib/trestle/auth/controller/locale.rb +18 -0
  53. data/lib/trestle/auth/controller/time_zone.rb +18 -0
  54. data/lib/trestle/auth/controller_methods.rb +3 -74
  55. data/lib/trestle/auth/engine.rb +1 -1
  56. data/lib/trestle/auth/model_methods.rb +2 -3
  57. data/lib/trestle/auth/version.rb +1 -1
  58. data/package.json +29 -0
  59. data/trestle-auth.gemspec +14 -10
  60. data/webpack.config.js +49 -0
  61. data/yarn.lock +4765 -0
  62. metadata +67 -31
  63. data/app/assets/javascripts/trestle/auth.js +0 -0
  64. data/app/assets/stylesheets/trestle/_custom-auth.scss +0 -4
  65. data/app/assets/stylesheets/trestle/auth.scss +0 -13
  66. data/app/assets/stylesheets/trestle/auth/_form.scss +0 -120
  67. data/app/assets/stylesheets/trestle/auth/_layout.scss +0 -20
@@ -1,33 +1,56 @@
1
- Trestle.resource(:<%= plural_name %>, model: Trestle.config.auth.user_class, scope: Auth) do
1
+ Trestle.resource(:<%= plural_name %>, model: <%= model %>, scope: Auth) do
2
2
  menu do
3
3
  group :configuration, priority: :last do
4
- item :<%= plural_name %>, icon: "fa fa-users"
4
+ item :<%= plural_name %>, icon: "fas fa-users"
5
5
  end
6
6
  end
7
7
 
8
8
  table do
9
- column :avatar, header: false do |administrator|
10
- avatar_for(administrator)
9
+ column :avatar, header: false do |<%= parameter_name %>|
10
+ avatar_for(<%= parameter_name %>)
11
11
  end
12
12
  column :email, link: true
13
+ <%- unless devise? -%>
13
14
  column :first_name
14
15
  column :last_name
16
+ <%- end -%>
15
17
  actions do |a|
16
18
  a.delete unless a.instance == current_user
17
19
  end
18
20
  end
19
21
 
20
- form do |administrator|
22
+ form do |<%= parameter_name %>|
21
23
  text_field :email
22
24
 
25
+ <%- unless devise? -%>
23
26
  row do
24
27
  col(sm: 6) { text_field :first_name }
25
28
  col(sm: 6) { text_field :last_name }
26
29
  end
27
30
 
31
+ <%- end -%>
28
32
  row do
29
33
  col(sm: 6) { password_field :password }
30
34
  col(sm: 6) { password_field :password_confirmation }
31
35
  end
32
36
  end
37
+ <%- if devise? -%>
38
+
39
+ # Ignore the password parameters if they are blank
40
+ update_instance do |instance, attrs|
41
+ if attrs[:password].blank?
42
+ attrs.delete(:password)
43
+ attrs.delete(:password_confirmation) if attrs[:password_confirmation].blank?
44
+ end
45
+
46
+ instance.assign_attributes(attrs)
47
+ end
48
+
49
+ # Log the current user back in if their password was changed
50
+ after_action on: :update do
51
+ if instance == current_user && instance.encrypted_password_previously_changed?
52
+ login!(instance)
53
+ end
54
+ end if Devise.sign_in_after_reset_password
55
+ <%- end -%>
33
56
  end
@@ -6,101 +6,57 @@ module Trestle
6
6
 
7
7
  argument :model, type: :string, default: "Administrator"
8
8
 
9
- def insert_configuration
10
- inject_into_file "config/initializers/trestle.rb", before: /^end/ do
11
- <<-RUBY.strip_heredoc.indent(2)
12
-
13
- # == Authentication Options
14
- #
15
- # Specify the user class to be used by trestle-auth.
16
- #
17
- config.auth.user_class = -> { #{model} }
18
-
19
- # Specify the scope for valid admin users.
20
- # Defaults to config.auth.user_class (unscoped).
21
- #
22
- # config.auth.user_scope = -> { User.where(admin: true) }
23
-
24
- # Specify the Trestle admin for managing administrator users.
25
- #
26
- config.auth.user_admin = -> { :"auth/#{model.underscore.pluralize}" }
27
-
28
- # Specify the parameter (along with a password) to be used to
29
- # authenticate an administrator. Defaults to :email.
30
- #
31
- # config.auth.authenticate_with = :login
32
-
33
- # Customize the method for authenticating a user given login parameters.
34
- # The block should return an instance of the auth user class, or nil.
35
- #
36
- # config.auth.authenticate = ->(params) {
37
- # User.authenticate(params[:login], params[:password])
38
- # }
9
+ class_option :devise, type: :boolean, default: false, desc: "Setup trestle-auth with Devise integration"
10
+ class_option :skip_account, type: :boolean, default: false, desc: "Skip creation of the current account admin"
39
11
 
40
- # Customize the rendering of user avatars. Can be disabled by setting to false.
41
- # Defaults to the Gravatar based on the user's email address.
42
- #
43
- # config.auth.avatar = ->(user) {
44
- # image_tag(user.avatar_url, alt: user.name)
45
- # }
12
+ source_root File.expand_path("../templates", __FILE__)
46
13
 
47
- # Customize the method for determining the user's locale.
48
- # Defaults to user.locale (if the method is defined).
49
- #
50
- # config.auth.locale = ->(user) {
51
- # user.locale if user.respond_to?(:locale)
52
- # }
53
-
54
- # Customize the method for determining the user's time zone.
55
- # Defaults to user.time_zone (if the method is defined).
56
- #
57
- # config.auth.time_zone = ->(user) {
58
- # user.time_zone if user.respond_to?(:time_zone)
59
- # }
60
-
61
- # Enable or disable remember me functionality. Defaults to true.
62
- #
63
- # config.auth.remember.enabled = false
14
+ def check_trestle_installed
15
+ unless ::File.exist?("config/initializers/trestle.rb")
16
+ raise Thor::Error, "The file config/initializers/trestle.rb does not appear to exist. Please run `trestle:install` first."
17
+ end
18
+ end
64
19
 
65
- # Specify remember me expiration time. Defaults to 2 weeks.
66
- #
67
- # config.auth.remember.for = 30.days
20
+ def insert_configuration
21
+ inject_into_file "config/initializers/trestle.rb", before: /^end/ do
22
+ format_configuration(template_content(configuration_template))
23
+ end
24
+ end
68
25
 
69
- # Customize the method for authenticating a user given a remember token.
70
- #
71
- # config.auth.remember.authenticate = ->(token) {
72
- # User.authenticate_with_remember_token(token)
73
- # }
26
+ def generate_model
27
+ generate "trestle:auth:model", model unless devise?
28
+ end
74
29
 
75
- # Customize the method for finding a user given an ID from the session.
76
- #
77
- # config.auth.remember.find_user = ->(id) {
78
- # User.find_by(id: id)
79
- # }
30
+ def generate_admin
31
+ generate "trestle:auth:admin", model, ("--devise" if devise?)
32
+ end
80
33
 
81
- # Customize the method for remembering a user.
82
- #
83
- # config.auth.remember.remember_me, ->(user) { user.remember_me! }
34
+ def generate_account
35
+ generate "trestle:auth:account", model, ("--devise" if devise?) unless options[:skip_account]
36
+ end
84
37
 
85
- # Customize the method for forgetting a user.
86
- #
87
- # config.auth.remember.forget_me, ->(user) { user.forget_me! }
38
+ def devise?
39
+ options[:devise]
40
+ end
88
41
 
89
- # Customize the method for generating the remember cookie.
90
- #
91
- # config.auth.remember.cookie, ->(user) {
92
- # { value: user.remember_token, expires: user.remember_token_expires_at }
93
- # }
94
- RUBY
95
- end
42
+ def configuration_template
43
+ devise? ? "devise.rb.erb" : "basic.rb.erb"
96
44
  end
97
45
 
98
- def generate_model
99
- generate "trestle:auth:model", model
46
+ private
47
+ def format_configuration(source)
48
+ "\n#{source.indent(2)}\n"
100
49
  end
101
50
 
102
- def generate_admin
103
- generate "trestle:auth:admin", model
51
+ def template_content(path, options={})
52
+ path = File.expand_path(find_in_source_paths(path.to_s))
53
+ context = options.delete(:context) || instance_eval("binding")
54
+
55
+ capturable_erb = CapturableERB.new(::File.binread(path), trim_mode: "-", eoutvar: "@output_buffer")
56
+
57
+ content = capturable_erb.tap do |erb|
58
+ erb.filename = path
59
+ end.result(context)
104
60
  end
105
61
  end
106
62
  end
@@ -0,0 +1,121 @@
1
+ # == Authentication Options
2
+ #
3
+ # Specify the user class to be used by trestle-auth.
4
+ #
5
+ config.auth.user_class = -> { <%= model %> }
6
+
7
+ # Specify the scope for valid admin users.
8
+ # Defaults to config.auth.user_class (unscoped).
9
+ #
10
+ # config.auth.user_scope = -> { User.where(admin: true) }
11
+
12
+ # Specify the Trestle admin for managing the current user (My Account).
13
+ #
14
+ config.auth.user_admin = -> { :"auth/account" }
15
+
16
+ # Specify the parameter (along with a password) to be used to
17
+ # authenticate an administrator. Defaults to :email.
18
+ #
19
+ # config.auth.authenticate_with = :login
20
+
21
+ # Customize the method for authenticating a user given login parameters.
22
+ # The block should return an instance of the auth user class, or nil.
23
+ #
24
+ # config.auth.authenticate = ->(params) {
25
+ # User.authenticate(params[:login], params[:password])
26
+ # }
27
+
28
+ # Customize the method for finding a user given an ID from the session.
29
+ # The block should return an instance of the auth user class, or nil.
30
+ #
31
+ # config.auth.find_user = ->(id) {
32
+ # User.find_by(id: id)
33
+ # }
34
+
35
+ # Customize the rendering of user avatars. Can be disabled by setting to false.
36
+ # Defaults to the Gravatar based on the user's email address.
37
+ #
38
+ # config.auth.avatar = ->(user) {
39
+ # avatar(fallback: user.initials) do
40
+ # image_tag(user.avatar_url, alt: user.name) if user.avatar_url?
41
+ # end
42
+ # }
43
+
44
+ # Customize the rendering of the current user's name in the main header.
45
+ # Defaults to the user's #first_name and #last_name (last name in bold),
46
+ # with a fallback to `display(user)` if those methods aren't defined.
47
+ #
48
+ # config.auth.format_user_name = ->(user) {
49
+ # content_tag(:strong, user.full_name)
50
+ # }
51
+
52
+ # Customize the method for determining the user's locale.
53
+ # Defaults to user.locale (if the method is defined).
54
+ #
55
+ # config.auth.locale = ->(user) {
56
+ # user.locale if user.respond_to?(:locale)
57
+ # }
58
+
59
+ # Customize the method for determining the user's time zone.
60
+ # Defaults to user.time_zone (if the method is defined).
61
+ #
62
+ # config.auth.time_zone = ->(user) {
63
+ # user.time_zone if user.respond_to?(:time_zone)
64
+ # }
65
+
66
+ # Specify the redirect location after a successful login.
67
+ # Defaults to the main Trestle admin path.
68
+ #
69
+ # config.auth.redirect_on_login = -> {
70
+ # if admin = Trestle.lookup(Trestle.config.auth.user_admin)
71
+ # admin.instance_path(current_user)
72
+ # else
73
+ # Trestle.config.path
74
+ # end
75
+ # }
76
+
77
+ # Specify the redirect location after logging out.
78
+ # Defaults to the trestle-auth new login path.
79
+ #
80
+ # config.auth.redirect_on_logout = -> { "/" }
81
+
82
+ # Enable or disable the built-in login/logout form and actions. Defaults to true.
83
+ # You may wish to disable these if you are using a custom backend and
84
+ # handling authentication entirely within your main application.
85
+ #
86
+ # config.auth.enable_login = true
87
+ # config.auth.enable_logout = true
88
+
89
+ # Specify the logo used on the login form.
90
+ # If not specified, will fall back to config.site_logo,
91
+ # config.site_logo_small or config.site_title.
92
+ #
93
+ # config.auth.logo = "auth-logo.png"
94
+
95
+ # Enable or disable remember me functionality. Defaults to true.
96
+ #
97
+ # config.auth.remember.enabled = false
98
+
99
+ # Specify remember me expiration time. Defaults to 2 weeks.
100
+ #
101
+ # config.auth.remember.for = 30.days
102
+
103
+ # Customize the method for authenticating a user given a remember token.
104
+ #
105
+ # config.auth.remember.authenticate = ->(token) {
106
+ # User.authenticate_with_remember_token(token)
107
+ # }
108
+
109
+ # Customize the method for remembering a user.
110
+ #
111
+ # config.auth.remember.remember_me = ->(user) { user.remember_me! }
112
+
113
+ # Customize the method for forgetting a user.
114
+ #
115
+ # config.auth.remember.forget_me = ->(user) { user.forget_me! }
116
+
117
+ # Customize the method for generating the remember cookie.
118
+ #
119
+ # config.auth.remember.cookie = ->(user) {
120
+ # { value: user.remember_token, expires: user.remember_token_expires_at }
121
+ # }
@@ -0,0 +1,87 @@
1
+ # == Authentication Options
2
+ #
3
+ # Set the authentication backend to use Devise.
4
+ #
5
+ config.auth.backend = :devise
6
+
7
+ # Specify the Devise/Warden mapping/scope.
8
+ #
9
+ config.auth.warden.scope = :<%= model.underscore.singularize %>
10
+
11
+ # Specify the user class to be used by trestle-auth.
12
+ #
13
+ config.auth.user_class = -> { <%= model %> }
14
+
15
+ # Specify the Trestle admin for managing the current user (My Account).
16
+ #
17
+ config.auth.user_admin = -> { :"auth/account" }
18
+
19
+ # Customize the rendering of user avatars. Can be disabled by setting to false.
20
+ # Defaults to the Gravatar based on the user's email address.
21
+ #
22
+ # config.auth.avatar = ->(user) {
23
+ # avatar(fallback: user.initials) do
24
+ # image_tag(user.avatar_url, alt: user.name) if user.avatar_url?
25
+ # end
26
+ # }
27
+
28
+ # Customize the rendering of the current user's name in the main header.
29
+ # Defaults to the user's #first_name and #last_name (last name in bold),
30
+ # with a fallback to `display(user)` if those methods aren't defined.
31
+ #
32
+ # config.auth.format_user_name = ->(user) {
33
+ # content_tag(:strong, user.full_name)
34
+ # }
35
+
36
+ # Customize the method for determining the user's locale.
37
+ # Defaults to user.locale (if the method is defined).
38
+ #
39
+ # config.auth.locale = ->(user) {
40
+ # user.locale if user.respond_to?(:locale)
41
+ # }
42
+
43
+ # Customize the method for determining the user's time zone.
44
+ # Defaults to user.time_zone (if the method is defined).
45
+ #
46
+ # config.auth.time_zone = ->(user) {
47
+ # user.time_zone if user.respond_to?(:time_zone)
48
+ # }
49
+
50
+ # Specify the redirect location after a successful login.
51
+ # Defaults to the main Trestle admin path.
52
+ #
53
+ # config.auth.redirect_on_login = -> {
54
+ # if admin = Trestle.lookup(Trestle.config.auth.user_admin)
55
+ # admin.instance_path(current_user)
56
+ # else
57
+ # Trestle.config.path
58
+ # end
59
+ # }
60
+
61
+ # Specify the redirect location after logging out.
62
+ # Defaults to the trestle-auth new login path.
63
+ #
64
+ # config.auth.redirect_on_logout = -> { "/" }
65
+
66
+ # Enable or disable the built-in login/logout form and actions. Defaults to true.
67
+ # You may wish to disable these if you are using a custom backend and
68
+ # handling authentication entirely within your main application.
69
+ #
70
+ # config.auth.enable_login = true
71
+ # config.auth.enable_logout = true
72
+
73
+ # Specify the path to redirect to when login is required.
74
+ # Defaults to the trestle-auth login page. You may wish to change
75
+ # this if you have also disabled the login form/action above.
76
+ #
77
+ # config.auth.login_url = -> { "/users/sign_in" }
78
+
79
+ # Specify the logo used on the login form.
80
+ # If not specified, will fall back to config.site_logo,
81
+ # config.site_logo_small or config.site_title.
82
+ #
83
+ # config.auth.logo = "auth-logo.png"
84
+
85
+ # Enable or disable remember me functionality. Defaults to true.
86
+ #
87
+ # config.auth.remember.enabled = false
@@ -1,19 +1,25 @@
1
- require "trestle/auth/version"
1
+ require_relative "auth/version"
2
2
 
3
3
  require "trestle"
4
4
 
5
5
  module Trestle
6
6
  module Auth
7
- extend ActiveSupport::Autoload
7
+ require_relative "auth/backends"
8
+ require_relative "auth/configuration"
9
+ require_relative "auth/constraint"
10
+ require_relative "auth/model_methods"
11
+ require_relative "auth/null_user"
8
12
 
9
- autoload :Configuration
10
- autoload :Constraint
11
- autoload :ControllerMethods
12
- autoload :ModelMethods
13
- autoload :NullUser
13
+ module Controller
14
+ require_relative "auth/controller/authentication"
15
+ require_relative "auth/controller/locale"
16
+ require_relative "auth/controller/time_zone"
17
+ end
18
+
19
+ require_relative "auth/controller_methods"
14
20
  end
15
21
 
16
22
  Configuration.option :auth, Auth::Configuration.new
17
23
  end
18
24
 
19
- require "trestle/auth/engine" if defined?(Rails)
25
+ require_relative "auth/engine" if defined?(Rails)
@@ -0,0 +1,34 @@
1
+ module Trestle
2
+ module Auth
3
+ module Backends
4
+ extend ActiveSupport::Autoload
5
+
6
+ require_relative "backends/base"
7
+
8
+ autoload :Basic
9
+ autoload :Devise
10
+ autoload :Warden
11
+
12
+ def self.lookup(backend)
13
+ case backend
14
+ when Class
15
+ backend
16
+ else
17
+ registry.fetch(backend) { raise ArgumentError, "Invalid authentication backend: #{backend.inspect}" }
18
+ end
19
+ end
20
+
21
+ def self.registry
22
+ @registry ||= {}
23
+ end
24
+
25
+ def self.register(name, klass)
26
+ registry[name] = klass
27
+ end
28
+
29
+ register(:basic, Basic)
30
+ register(:devise, Devise)
31
+ register(:warden, Warden)
32
+ end
33
+ end
34
+ end