trestle-auth 0.2.4 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
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 +34 -7
  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 +3 -1
  14. data/app/helpers/trestle/auth/user_helper.rb +2 -6
  15. data/app/views/layouts/trestle/auth.html.erb +11 -8
  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/{locale → locales}/zh-CN.yml +0 -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 +45 -82
  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 +58 -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 +66 -32
  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 -138
  67. data/app/assets/stylesheets/trestle/auth/_layout.scss +0 -20
@@ -6,102 +6,65 @@ 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) }
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"
23
11
 
24
- # Specify the Trestle admin for managing administrator users.
25
- #
26
- config.auth.user_admin = -> { :"auth/#{model.underscore.pluralize}" }
12
+ source_root File.expand_path("../templates", __FILE__)
27
13
 
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
- # }
39
-
40
- # Customize the method for finding a user given an ID from the session.
41
- # The block should return an instance of the auth user class, or nil.
42
- #
43
- # config.auth.find_user = ->(id) {
44
- # User.find_by(id: id)
45
- # }
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
46
19
 
47
- # Customize the rendering of user avatars. Can be disabled by setting to false.
48
- # Defaults to the Gravatar based on the user's email address.
49
- #
50
- # config.auth.avatar = ->(user) {
51
- # image_tag(user.avatar_url, alt: user.name)
52
- # }
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
53
25
 
54
- # Customize the method for determining the user's locale.
55
- # Defaults to user.locale (if the method is defined).
56
- #
57
- # config.auth.locale = ->(user) {
58
- # user.locale if user.respond_to?(:locale)
59
- # }
26
+ def generate_model
27
+ generate "trestle:auth:model", model unless devise?
28
+ end
60
29
 
61
- # Customize the method for determining the user's time zone.
62
- # Defaults to user.time_zone (if the method is defined).
63
- #
64
- # config.auth.time_zone = ->(user) {
65
- # user.time_zone if user.respond_to?(:time_zone)
66
- # }
30
+ def generate_admin
31
+ generate "trestle:auth:admin", model, ("--devise" if devise?)
32
+ end
67
33
 
68
- # Enable or disable remember me functionality. Defaults to true.
69
- #
70
- # config.auth.remember.enabled = false
34
+ def generate_account
35
+ generate "trestle:auth:account", model, ("--devise" if devise?) unless options[:skip_account]
36
+ end
71
37
 
72
- # Specify remember me expiration time. Defaults to 2 weeks.
73
- #
74
- # config.auth.remember.for = 30.days
38
+ def devise?
39
+ options[:devise]
40
+ end
75
41
 
76
- # Customize the method for authenticating a user given a remember token.
77
- #
78
- # config.auth.remember.authenticate = ->(token) {
79
- # User.authenticate_with_remember_token(token)
80
- # }
42
+ def configuration_template
43
+ devise? ? "devise.rb.erb" : "basic.rb.erb"
44
+ end
81
45
 
82
- # Customize the method for remembering a user.
83
- #
84
- # config.auth.remember.remember_me, ->(user) { user.remember_me! }
46
+ private
47
+ def format_configuration(source)
48
+ "\n#{source.indent(2)}\n"
49
+ end
85
50
 
86
- # Customize the method for forgetting a user.
87
- #
88
- # config.auth.remember.forget_me, ->(user) { user.forget_me! }
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")
89
54
 
90
- # Customize the method for generating the remember cookie.
91
- #
92
- # config.auth.remember.cookie, ->(user) {
93
- # { value: user.remember_token, expires: user.remember_token_expires_at }
94
- # }
95
- RUBY
96
- end
55
+ content = capturable_erb(path).tap do |erb|
56
+ erb.filename = path
57
+ end.result(context)
97
58
  end
98
59
 
99
- def generate_model
100
- generate "trestle:auth:model", model
101
- end
60
+ def capturable_erb(path)
61
+ match = ERB.version.match(/(\d+\.\d+\.\d+)/)
102
62
 
103
- def generate_admin
104
- generate "trestle:auth:admin", model
63
+ if match && match[1] >= "2.2.0" # Ruby 2.6+
64
+ CapturableERB.new(::File.binread(path), trim_mode: "-", eoutvar: "@output_buffer")
65
+ else
66
+ CapturableERB.new(::File.binread(path), nil, "-", "@output_buffer")
67
+ end
105
68
  end
106
69
  end
107
70
  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
data/lib/trestle/auth.rb CHANGED
@@ -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
@@ -0,0 +1,28 @@
1
+ module Trestle
2
+ module Auth
3
+ module Backends
4
+ class Base
5
+ attr_reader :controller, :request, :session, :cookies
6
+
7
+ def initialize(controller:, request:, session:, cookies:)
8
+ @controller, @request, @session, @cookies = controller, request, session, cookies
9
+ end
10
+
11
+ # Default params scope to use for the login form.
12
+ def scope
13
+ :user
14
+ end
15
+
16
+ # Stores the previous return location in the session to return to after logging in.
17
+ def store_location(url)
18
+ session[:trestle_return_to] = url
19
+ end
20
+
21
+ # Returns (and deletes) the previously stored return location from the session.
22
+ def previous_location
23
+ session.delete(:trestle_return_to)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end