Class: Organizations::OrganizationUser

Inherits:
ApplicationRecord show all
Includes:
ActiveModel::Dirty
Defined in:
app/models/organizations/organization_user.rb

Constant Summary

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

Instance 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 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

.create_organization_record_for(user_id, organization_id) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/models/organizations/organization_user.rb', line 46

def self.create_organization_record_for(user_id, organization_id)
  # we try to find a record if it exists.
  find_by(user_id: user_id, organization_id: organization_id) ||

    # If not, we try to create it with `upsert`.
    # We use upsert for these reasons:
    #    - No subtransactions
    #    - Due to the use of `on_duplicate: :skip`, we are essentially issuing a `ON CONFLICT DO NOTHING`.
    #       - Postgres will take care of skipping the record without errors if a similar record was created
    #         by then in another thread.
    #       - There is no explicit error being thrown because we said "ON CONFLICT DO NOTHING".
    #         With this we avoid both the problems with subtransactions that could arise when we upgrade Rails,
    #         see https://gitlab.com/gitlab-org/gitlab/-/issues/439567, and also with race conditions.

    upsert(
      { organization_id: organization_id, user_id: user_id, access_level: :default },
      unique_by: [:organization_id, :user_id],
      on_duplicate: :skip # Do not change access_level, could make :owner :default
    )
end

.home_organization_access_level(user_is_admin: false) ⇒ Object



38
39
40
41
42
43
44
# File 'app/models/organizations/organization_user.rb', line 38

def self.home_organization_access_level(user_is_admin: false)
  if user_is_admin
    :owner
  else
    :default
  end
end

.update_home_organization_record_for(user, user_is_admin:) ⇒ Object



29
30
31
32
33
34
35
36
# File 'app/models/organizations/organization_user.rb', line 29

def self.update_home_organization_record_for(user, user_is_admin:)
  find_or_initialize_by(
    user_id: user.id, organization_id: user.organization_id
  ).tap do |record|
    record.access_level = home_organization_access_level(user_is_admin: user_is_admin)
    record.save!
  end
end

Instance Method Details

#last_owner?Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
# File 'app/models/organizations/organization_user.rb', line 67

def last_owner?
  return false unless owner?

  # Try to keep the last active user as owner
  return other_owners.with_active_users.empty? if user.active?

  other_owners.empty?
end