minimalist_authentication 3.2.1 → 3.2.3

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: 4b81e23298baa784d92be34aa1b6b372f585c5ea8bba51d953a85ee39ec44db5
4
- data.tar.gz: 2cad820418a3869931fad968063d3f97e7302686b1fd534daf3a1c9f0d5dda15
3
+ metadata.gz: 69d08aa192324c376e8a95622c7dbdad23b1d3b97c271a0dfaf23ea096c7a3ee
4
+ data.tar.gz: 8a28310be5faf089c7b2128624a186ee0d4afe4827bc8e81be26a12a6d69a2f6
5
5
  SHA512:
6
- metadata.gz: 3b5cd0f9f672f3084884b5385d333705d21d9835738e0b79ba8bc10853bc8bf5cbec88451ff07db26844a601fc69e685fe3f5e3961c5dbcd3616033d9cfe781a
7
- data.tar.gz: 183194ef85c0ed224f9550c95779ecc5d37b6437ea0b05cd2e2cc32c43785d6718639bc1e4b74997df0fedf93432366e7240bc1e1d6b440008210ce64f277134
6
+ metadata.gz: 5fd1cb1d8a7e659c2e29c6d395f86f70b87dc6e4f91e47b96fc233d70c8328537baee9b310dc3f60f92700c8455afb8cc1c28558692c2b3df08f420ec360ebf8
7
+ data.tar.gz: 93ecc3a00b2a1728f0974f8bda923d9177ae7d74fe03df9af66c0dc8d2c70032c9bfaccc65083507be82486d23d347316cda948d24b8b778f56ba4744e7cba47
data/README.md CHANGED
@@ -26,6 +26,13 @@ bin/rails generate model user active:boolean username:string password_digest:str
26
26
 
27
27
 
28
28
  ## Example
29
+ Create a Current class that inherits from ActiveSupport::CurrentAttributes with a user attribute (app/models/current.rb)
30
+ ```ruby
31
+ class Current < ActiveSupport::CurrentAttributes
32
+ attribute :user
33
+ end
34
+ ```
35
+
29
36
  Include MinimalistAuthentication::User in your user model (app/models/user.rb)
30
37
  ```ruby
31
38
  class User < ApplicationRecord
@@ -7,7 +7,7 @@ class EmailsController < ApplicationController
7
7
  if current_user.update(user_params)
8
8
  redirect_to update_redirect_path, notice: t(".notice")
9
9
  else
10
- render :edit, status: :unprocessable_entity
10
+ render :edit, status: :unprocessable_content
11
11
  end
12
12
  end
13
13
 
@@ -19,7 +19,7 @@ class PasswordResetsController < ApplicationController
19
19
  redirect_to new_session_path, notice: t(".notice", email:)
20
20
  else
21
21
  flash.now.alert = t(".alert")
22
- render :new, status: :unprocessable_entity
22
+ render :new, status: :unprocessable_content
23
23
  end
24
24
  end
25
25
 
@@ -17,7 +17,7 @@ class PasswordsController < ApplicationController
17
17
  if user.update(password_params)
18
18
  redirect_to new_session_path, notice: t(".notice")
19
19
  else
20
- render :edit, status: :unprocessable_entity
20
+ render :edit, status: :unprocessable_content
21
21
  end
22
22
  end
23
23
 
@@ -5,8 +5,11 @@ module MinimalistAuthentication
5
5
  extend ActiveSupport::Concern
6
6
 
7
7
  included do
8
- # Lock down everything by default
9
- # use skip_before_action to open up specific actions
8
+ # Loads the user object from the session and assigns it to Current.user
9
+ before_action :load_current_user
10
+
11
+ # Requires an authorized user for all actions
12
+ # Use skip_before_action to allow access to specific actions
10
13
  before_action :authorization_required
11
14
 
12
15
  helper MinimalistAuthentication::ApplicationHelper
@@ -14,35 +17,47 @@ module MinimalistAuthentication
14
17
  helper_method :current_user, :logged_in?, :authorized?
15
18
  end
16
19
 
17
- private
20
+ # Returns true if the user is logged in
21
+ # Override this method in your controller to customize authorization
22
+ def authorized?(_action = action_name, _resource = controller_name)
23
+ logged_in?
24
+ end
18
25
 
26
+ # Returns the current user from the client application Current class
19
27
  def current_user
20
- @current_user ||= find_session_user || MinimalistAuthentication.configuration.user_model.guest
28
+ ::Current.user
21
29
  end
22
30
 
23
- def find_session_user
24
- MinimalistAuthentication.configuration.user_model.find_enabled(session_user_id)
31
+ # Returns true if a current user is present, otherwise returns false
32
+ def logged_in?
33
+ current_user.present?
25
34
  end
26
35
 
27
- def session_user_id
28
- session[MinimalistAuthentication.configuration.session_key]
36
+ # Logs in a user by setting the session key and updating the Current user
37
+ # Should only be called after a successful authentication
38
+ def update_current_user(user)
39
+ reset_session
40
+ session[MinimalistAuthentication.session_key] = user.id
41
+ ::Current.user = user
29
42
  end
30
43
 
31
- def authorization_required
32
- authorized? || access_denied
44
+ private
45
+
46
+ def access_denied
47
+ store_location if request.get? && !logged_in?
48
+ redirect_to new_session_path
33
49
  end
34
50
 
35
- def authorized?(_action = action_name, _resource = controller_name)
36
- logged_in?
51
+ def authorization_required
52
+ authorized? || access_denied
37
53
  end
38
54
 
39
- def logged_in?
40
- !current_user.guest?
55
+ def find_session_user
56
+ MinimalistAuthentication.user_model.find_enabled(session[MinimalistAuthentication.session_key])
41
57
  end
42
58
 
43
- def access_denied
44
- store_location if request.get? && !logged_in?
45
- redirect_to new_session_path
59
+ def load_current_user
60
+ Current.user = find_session_user
46
61
  end
47
62
 
48
63
  def store_location
@@ -27,11 +27,17 @@ module MinimalistAuthentication
27
27
 
28
28
  def destroy
29
29
  reset_session
30
+ clear_site_data
30
31
  redirect_to logout_redirect_to, notice: t(".notice"), status: :see_other
31
32
  end
32
33
 
33
34
  private
34
35
 
36
+ # Sets a “Clear-Site-Data” header to clear the browser cache.
37
+ def clear_site_data
38
+ response.headers["Clear-Site-Data"] = '"cache","storage"'
39
+ end
40
+
35
41
  def user
36
42
  @user ||= MinimalistAuthentication.configuration.user_model.new
37
43
  end
@@ -42,9 +48,8 @@ module MinimalistAuthentication
42
48
 
43
49
  def log_in_user
44
50
  self.return_to = session["return_to"]
45
- reset_session
51
+ update_current_user(authenticated_user)
46
52
  authenticated_user.logged_in
47
- session[MinimalistAuthentication.configuration.session_key] = authenticated_user.id
48
53
  end
49
54
 
50
55
  def user_params
@@ -77,7 +82,7 @@ module MinimalistAuthentication
77
82
  def after_authentication_failure
78
83
  flash.now.alert = t(".alert", identifier:)
79
84
  user
80
- render :new, status: :unprocessable_entity
85
+ render :new, status: :unprocessable_content
81
86
  end
82
87
 
83
88
  def identifier
@@ -5,22 +5,22 @@ module MinimalistAuthentication
5
5
  PASSWORD = "test-password"
6
6
  PASSWORD_DIGEST = BCrypt::Password.create(PASSWORD, cost: BCrypt::Engine::MIN_COST)
7
7
 
8
- def login_as(user_fixture_name, password = PASSWORD)
9
- post session_path, params: { user: { email: users(user_fixture_name).email, password: } }
10
- end
11
-
12
8
  def current_user
13
9
  @current_user ||= load_user_from_session
14
10
  end
15
11
 
12
+ def login_as(user_fixture_name, password = PASSWORD)
13
+ post session_path, params: { user: { email: users(user_fixture_name).email, password: } }
14
+ end
15
+
16
16
  private
17
17
 
18
18
  def load_user_from_session
19
- MinimalistAuthentication.configuration.user_model.find(session_user_id) if session_user_id
19
+ MinimalistAuthentication.user_model.find(session_user_id) if session_user_id
20
20
  end
21
21
 
22
22
  def session_user_id
23
- @request.session[MinimalistAuthentication.configuration.session_key]
23
+ @request.session[MinimalistAuthentication.session_key]
24
24
  end
25
25
  end
26
26
  end
@@ -6,8 +6,6 @@ module MinimalistAuthentication
6
6
  module User
7
7
  extend ActiveSupport::Concern
8
8
 
9
- GUEST_USER_EMAIL = "guest"
10
-
11
9
  included do
12
10
  has_secure_password
13
11
 
@@ -54,11 +52,6 @@ module MinimalistAuthentication
54
52
  active(false)
55
53
  end
56
54
 
57
- # Returns a frozen user with the email set to GUEST_USER_EMAIL.
58
- def guest
59
- new(email: GUEST_USER_EMAIL).freeze
60
- end
61
-
62
55
  # Minimum password length
63
56
  def password_minimum = 12
64
57
  end
@@ -87,9 +80,14 @@ module MinimalistAuthentication
87
80
  authenticate(password)
88
81
  end
89
82
 
90
- # Check if user is a guest based on their email attribute
83
+ # Deprecated method to check if the user is a guest. Returns false because the guest user has been removed.
91
84
  def guest?
92
- email == GUEST_USER_EMAIL
85
+ MinimalistAuthentication.deprecator.warn(<<-MSG.squish)
86
+ Calling #guest? is deprecated. Use #MinimalistAuthentication::Controller#logged_in? to
87
+ check for the presence of a current_user instead.
88
+ MSG
89
+
90
+ false
93
91
  end
94
92
 
95
93
  # Returns true if the user is not active.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MinimalistAuthentication
4
- VERSION = "3.2.1"
4
+ VERSION = "3.2.3"
5
5
  end
@@ -12,7 +12,7 @@ require "minimalist_authentication/test_helper"
12
12
 
13
13
  module MinimalistAuthentication
14
14
  class << self
15
- delegate :user_model, to: :configuration
15
+ delegate :session_key, :user_model, to: :configuration
16
16
 
17
17
  def deprecator
18
18
  @deprecator ||= ActiveSupport::Deprecation.new("4.0", name)
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minimalist_authentication
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.1
4
+ version: 3.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Baldwin
8
8
  - Brightways Learning
9
- autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2024-11-22 00:00:00.000000000 Z
11
+ date: 1980-01-02 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: bcrypt
@@ -97,7 +96,6 @@ licenses:
97
96
  metadata:
98
97
  homepage_uri: https://github.com/wwidea/minimalist_authentication
99
98
  rubygems_mfa_required: 'true'
100
- post_install_message:
101
99
  rdoc_options: []
102
100
  require_paths:
103
101
  - lib
@@ -112,8 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
110
  - !ruby/object:Gem::Version
113
111
  version: '0'
114
112
  requirements: []
115
- rubygems_version: 3.5.11
116
- signing_key:
113
+ rubygems_version: 3.6.9
117
114
  specification_version: 4
118
115
  summary: A Rails authentication plugin that takes a minimalist approach.
119
116
  test_files: []