Skip to content

Commit 8dad579

Browse files
committed
feat: added requested changes from review
1 parent a41c970 commit 8dad579

12 files changed

Lines changed: 51 additions & 42 deletions

File tree

app/models/user.rb

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
class User < ApplicationRecord
44
include NamespaceParent
55

6-
GHOST_USERNAME = 'ghost'
7-
GHOST_EMAIL = 'ghost@code0.tech'
6+
USER_TYPES = {
7+
regular: 0,
8+
ghost: 1,
9+
}.freeze
810

911
has_secure_password
1012

13+
enum :user_type, USER_TYPES
14+
1115
validates :username, length: { maximum: 50 },
1216
presence: true,
1317
allow_blank: false,
@@ -34,15 +38,8 @@ class User < ApplicationRecord
3438

3539
has_one_attached :avatar
3640

37-
before_destroy :prevent_destroy_ghost_user, prepend: true
38-
before_destroy :reassign_authored_audit_events_to_ghost_user
39-
4041
def self.ghost
41-
find_by!(username: GHOST_USERNAME)
42-
end
43-
44-
def ghost?
45-
username == GHOST_USERNAME
42+
find_by!(user_type: :ghost)
4643
end
4744

4845
def mfa_enabled?
@@ -82,23 +79,6 @@ def validate_mfa!(mfa)
8279
generates_token_for :password_reset, expires_in: 15.minutes do
8380
password_digest&.last(20)
8481
end
85-
86-
private
87-
88-
def prevent_destroy_ghost_user
89-
return unless ghost?
90-
91-
errors.add(:base, :invalid, message: 'Cannot delete ghost user')
92-
throw :abort
93-
end
94-
95-
def reassign_authored_audit_events_to_ghost_user
96-
ghost_user = self.class.ghost
97-
98-
authored_audit_events.find_each do |audit_event|
99-
audit_event.update!(author: ghost_user)
100-
end
101-
end
10282
end
10383

10484
User.prepend_extensions

app/policies/user_policy.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
class UserPolicy < BasePolicy
44
condition(:user_is_self) { subject.id == user&.id }
55
condition(:user_is_admin) { user&.admin? || false }
6+
condition(:user_is_regular) { subject.regular? }
67
condition(:admin_status_visible) { ApplicationSetting.current[:admin_status_visible] }
78

89
rule { ~anonymous }.enable :read_user
@@ -12,11 +13,12 @@ class UserPolicy < BasePolicy
1213
enable :read_user_identity
1314
enable :update_attachment_avatar
1415
enable :read_email
15-
enable :delete_user
1616
enable :read_admin_status
1717
enable :read_mfa_status
1818
end
1919

20+
rule { user_is_admin & user_is_regular }.enable :delete_user
21+
2022
rule { admin_status_visible & ~anonymous }.enable :read_admin_status
2123

2224
rule { user_is_self }.policy do

app/services/users/delete_service.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ def execute
1818

1919
transactional do |t|
2020
namespace = user.namespace
21-
audit_author_id = user == current_authentication.user ? User.ghost.id : current_authentication.user.id
21+
ghost_user = User.ghost
22+
audit_author_id = user == current_authentication.user ? ghost_user.id : current_authentication.user.id
2223

24+
user.authored_audit_events.update_all(author_id: ghost_user.id) # rubocop:disable Rails/SkipsModelValidations
2325
user.destroy
2426

2527
if user.persisted?

db/fixtures/01_application_settings.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
# frozen_string_literal: true
22

3-
User.seed_once :username do |u|
4-
u.username = User::GHOST_USERNAME
5-
u.email = User::GHOST_EMAIL
6-
u.password = SecureRandom.hex
7-
u.admin = false
8-
end
9-
103
ApplicationSetting.seed_once :setting do |s|
114
s.setting = :user_registration_enabled
125
s.value = false

db/fixtures/02_users.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
User.seed_once :user_type do |u|
4+
u.username = 'ghost'
5+
u.email = 'ghost@code0.tech'
6+
u.password = SecureRandom.hex
7+
u.admin = false
8+
u.user_type = :ghost
9+
end

db/fixtures/production/01_users.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
return unless User.where.not(username: User::GHOST_USERNAME).none?
3+
return unless User.regular.none?
44

55
initial_root_email = ENV.fetch('INITIAL_ROOT_MAIL', nil)
66
initial_root_password = ENV.fetch('INITIAL_ROOT_PASSWORD', SecureRandom.hex)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
class AddUserTypeToUsers < Code0::ZeroTrack::Database::Migration[1.0]
4+
def change
5+
add_column :users, :user_type, :integer, default: 0, null: false
6+
end
7+
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
68074a1fb7f8bdf3087c3b6823eaef35175c480f05d4bc73c190ef7b2811cf68

db/structure.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,7 @@ CREATE TABLE users (
11651165
email_verified_at timestamp with time zone,
11661166
readme text,
11671167
blocked_at timestamp with time zone,
1168+
user_type integer DEFAULT 0 NOT NULL,
11681169
CONSTRAINT check_11461c37fb CHECK ((char_length(readme) <= 5000)),
11691170
CONSTRAINT check_3bedaaa612 CHECK ((char_length(email) <= 255)),
11701171
CONSTRAINT check_56606ce552 CHECK ((char_length(username) <= 50)),

spec/factories/users.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
blocked_at { Time.zone.now }
2323
end
2424

25+
trait :ghost do
26+
user_type { :ghost }
27+
end
28+
2529
trait :with_namespace do
2630
after :build, &:ensure_namespace
2731
end

0 commit comments

Comments
 (0)