summaryrefslogtreecommitdiff
path: root/app/models/user_synced_attributes_metadata.rb
blob: 4cd0e3fb828fb776fa45a29d0714335b3e42aa7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# frozen_string_literal: true

class UserSyncedAttributesMetadata < ApplicationRecord
  belongs_to :user

  validates :user, presence: true

  SYNCABLE_ATTRIBUTES = %i[name email location].freeze

  def read_only?(attribute)
    sync_profile_from_provider? && synced?(attribute)
  end

  def read_only_attributes
    return [] unless sync_profile_from_provider?

    self.class.syncable_attributes.select { |key| synced?(key) }
  end

  def synced?(attribute)
    read_attribute("#{attribute}_synced")
  end

  def set_attribute_synced(attribute, value)
    write_attribute("#{attribute}_synced", value)
  end

  class << self
    def syncable_attributes
      if Gitlab.config.ldap.enabled && !Gitlab.config.ldap.sync_name
        SYNCABLE_ATTRIBUTES - %i[name]
      else
        SYNCABLE_ATTRIBUTES
      end
    end
  end

  private

  def sync_profile_from_provider?
    Gitlab::Auth::OAuth::Provider.sync_profile_from_provider?(provider)
  end
end