Skip to content

Commit 315fff7

Browse files
Merge pull request #1078 from code0-tech/#923-user-deletion-fails
added ghost user
2 parents 4b00919 + fe64f9a commit 315fff7

20 files changed

Lines changed: 176 additions & 7 deletions

File tree

app/models/user.rb

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

6+
USER_TYPES = {
7+
regular: 0,
8+
ghost: 1,
9+
}.freeze
10+
611
has_secure_password
712

13+
enum :user_type, USER_TYPES
14+
815
validates :username, length: { maximum: 50 },
916
presence: true,
1017
allow_blank: false,
@@ -31,6 +38,10 @@ class User < ApplicationRecord
3138

3239
has_one_attached :avatar
3340

41+
def self.ghost
42+
find_by!(user_type: :ghost)
43+
end
44+
3445
def mfa_enabled?
3546
totp_secret != nil
3647
end

app/policies/global_policy.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
class GlobalPolicy < BasePolicy
44
condition(:organization_creation_restricted) { ApplicationSetting.current[:organization_creation_restricted] }
55
condition(:admin) { user&.admin }
6+
condition(:user_is_regular) { user&.regular? }
67

78
rule { ~anonymous }.enable :create_organization
89
rule { organization_creation_restricted & ~admin }.prevent :create_organization
@@ -15,6 +16,8 @@ class GlobalPolicy < BasePolicy
1516
enable :read_velorum_config
1617
end
1718

19+
rule { user_is_regular }.enable :create_user_session
20+
1821
rule { admin }.policy do
1922
enable :read_application_setting
2023
enable :update_application_setting

app/policies/user_policy.rb

Lines changed: 3 additions & 0 deletions
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(:subject_is_regular) { subject.regular? }
67
condition(:admin_status_visible) { ApplicationSetting.current[:admin_status_visible] }
78

89
rule { ~anonymous }.enable :read_user
@@ -17,6 +18,8 @@ class UserPolicy < BasePolicy
1718
enable :read_mfa_status
1819
end
1920

21+
rule { ~subject_is_regular }.prevent :delete_user
22+
2023
rule { admin_status_visible & ~anonymous }.enable :read_admin_status
2124

2225
rule { user_is_self }.policy do

app/services/users/delete_service.rb

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

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

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

2427
if user.persisted?
@@ -41,7 +44,7 @@ def execute
4144

4245
AuditService.audit(
4346
:user_deleted,
44-
author_id: current_authentication.user.id,
47+
author_id: audit_author_id,
4548
entity: user,
4649
target: AuditEvent::GLOBAL_TARGET,
4750
details: {}

app/services/users/identity/login_service.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ def execute
5050
details: user_session.errors)
5151
end
5252

53+
unless Ability.allowed?(Sagittarius::Authentication.new(:session, user_session), :create_user_session)
54+
logger.warn(
55+
message: 'User was not allowed to create user session',
56+
user_id: user.id,
57+
username: user.username
58+
)
59+
t.rollback_and_return! ServiceResponse.error(message: 'Not allowed to create user session',
60+
error_code: :invalid_login_data)
61+
end
62+
5363
AuditService.audit(
5464
:user_logged_in,
5565
author_id: user.id,

app/services/users/login_service.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ def execute
4545
details: user_session.errors)
4646
end
4747

48+
unless Ability.allowed?(Sagittarius::Authentication.new(:session, user_session), :create_user_session)
49+
logger.warn(message: 'User was not allowed to create user session', user_id: user.id, username: user.username)
50+
t.rollback_and_return! ServiceResponse.error(message: 'Not allowed to create user session',
51+
error_code: :invalid_login_data)
52+
end
53+
4854
AuditService.audit(
4955
:user_logged_in,
5056
author_id: user.id,

db/fixtures/02_users.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
u.readme = 'This user will hold the activity of the users that have been deleted'
10+
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.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

0 commit comments

Comments
 (0)