Class: UserCustomAttribute

Inherits:
ApplicationRecord show all
Defined in:
app/models/user_custom_attribute.rb

Constant Summary collapse

BLOCKED_BY =
'blocked_by'
UNBLOCKED_BY =
'unblocked_by'
ARKOSE_RISK_BAND =
'arkose_risk_band'
ARKOSE_SESSION =
'arkose_session'
AUTO_BANNED_BY_ABUSE_REPORT_ID =
'auto_banned_by_abuse_report_id'
AUTO_BANNED_BY_SPAM_LOG_ID =
'auto_banned_by_spam_log_id'
TRUSTED_BY =
'trusted_by'
AUTO_BANNED_BY =
'auto_banned_by'
IDENTITY_VERIFICATION_PHONE_EXEMPT =
'identity_verification_phone_exempt'
IDENTITY_VERIFICATION_EXEMPT =
'identity_verification_exempt'
DELETED_OWN_ACCOUNT_AT =
'deleted_own_account_at'
SKIPPED_ACCOUNT_DELETION_AT =
'skipped_account_deletion_at'
DEEP_CLEAN_CI_USAGE_WHEN_BANNED =
'deep_clean_ci_usage_when_banned'
TOP_LEVEL_GROUP_LIMIT_EXEMPT =
'top_level_group_limit_exempt'

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from HasCheckConstraints

HasCheckConstraints::NOT_NULL_CHECK_PATTERN

Constants included from ResetOnColumnErrors

ResetOnColumnErrors::MAX_RESET_PERIOD

Class Method Summary collapse

Methods inherited from ApplicationRecord

===, cached_column_list, #create_or_load_association, current_transaction, declarative_enum, default_select_columns, delete_all_returning, #deleted_from_database?, id_in, id_not_in, iid_in, nullable_column?, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from Organizations::Sharding

#sharding_organization

Methods included from ResetOnColumnErrors

#reset_on_union_error, #reset_on_unknown_attribute_error

Methods included from Gitlab::SensitiveSerializableHash

#serializable_hash

Class Method Details

.sessionsObject



41
42
43
44
45
46
47
# File 'app/models/user_custom_attribute.rb', line 41

def sessions
  return none if blocked_users.empty?

  arkose_sessions
    .by_user_id(blocked_users.map(&:user_id))
    .select(:value)
end

.set_auto_banned_by(user:, reason:) ⇒ Object



65
66
67
# File 'app/models/user_custom_attribute.rb', line 65

def set_auto_banned_by(user:, reason:)
  upsert_custom_attribute(user_id: user.id, key: AUTO_BANNED_BY, value: reason)
end

.set_banned_by_abuse_report(abuse_report) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'app/models/user_custom_attribute.rb', line 49

def set_banned_by_abuse_report(abuse_report)
  return unless abuse_report

  upsert_custom_attribute(
    user_id: abuse_report.user.id,
    key: AUTO_BANNED_BY_ABUSE_REPORT_ID,
    value: abuse_report.id
  )
end

.set_banned_by_spam_log(spam_log) ⇒ Object



59
60
61
62
63
# File 'app/models/user_custom_attribute.rb', line 59

def set_banned_by_spam_log(spam_log)
  return unless spam_log

  upsert_custom_attribute(user_id: spam_log.user_id, key: AUTO_BANNED_BY_SPAM_LOG_ID, value: spam_log.id)
end

.set_deleted_own_account_at(user) ⇒ Object



79
80
81
82
83
# File 'app/models/user_custom_attribute.rb', line 79

def (user)
  return unless user

  upsert_custom_attribute(user_id: user.id, key: DELETED_OWN_ACCOUNT_AT, value: Time.zone.now.to_s)
end

.set_skipped_account_deletion_at(user) ⇒ Object



85
86
87
88
89
# File 'app/models/user_custom_attribute.rb', line 85

def (user)
  return unless user

  upsert_custom_attribute(user_id: user.id, key: SKIPPED_ACCOUNT_DELETION_AT, value: Time.zone.now.to_s)
end

.set_trusted_by(user:, trusted_by:) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'app/models/user_custom_attribute.rb', line 69

def set_trusted_by(user:, trusted_by:)
  return unless user && trusted_by

  upsert_custom_attribute(
    user_id: user.id,
    key: UserCustomAttribute::TRUSTED_BY,
    value: "#{trusted_by.username}/#{trusted_by.id}+#{Time.current}"
  )
end

.upsert_custom_attribute(user_id:, key:, value:) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/user_custom_attribute.rb', line 91

def upsert_custom_attribute(user_id:, key:, value:)
  return unless user_id && key && value

  custom_attribute = {
    user_id: user_id,
    key: key,
    value: value
  }

  upsert_custom_attributes([custom_attribute])
end

.upsert_custom_attributes(custom_attributes) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'app/models/user_custom_attribute.rb', line 31

def upsert_custom_attributes(custom_attributes)
  created_at = DateTime.now
  updated_at = DateTime.now

  custom_attributes.map! do |custom_attribute|
    custom_attribute.merge({ created_at: created_at, updated_at: updated_at })
  end
  upsert_all(custom_attributes, unique_by: [:user_id, :key])
end