Class: EgovUtils::Principal

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

Direct Known Subclasses

Group, User

Instance Method Summary collapse

Instance Method Details

#auth_sourceObject



16
17
18
# File 'app/models/egov_utils/principal.rb', line 16

def auth_source
  @auth_source ||= EgovUtils::AuthSource.new(provider) if provider.present?
end

#has_role?(role) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'app/models/egov_utils/principal.rb', line 12

def has_role?(role)
  Array(roles).include?(role)
end

#ldap?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'app/models/egov_utils/principal.rb', line 20

def ldap?
  !!auth_source
end

#ldap_dnObject

Raises:

  • (NotImplementedError)


24
25
26
# File 'app/models/egov_utils/principal.rb', line 24

def ldap_dn
  raise NotImplementedError
end

#ldap_domainObject



28
29
30
# File 'app/models/egov_utils/principal.rb', line 28

def ldap_domain
  ldap? && ldap_dn.scan(/dc=([^,]*)/i).flatten.join('.')
end

#organization_by_domain(ldap_domain = self.ldap_domain) ⇒ Object



32
33
34
# File 'app/models/egov_utils/principal.rb', line 32

def organization_by_domain(ldap_domain=self.ldap_domain)
  @organization_by_domain ||= EgovUtils::Organization.where(domain: ldap_domain).first if ldap_domain
end

#organization_idObject



40
41
42
# File 'app/models/egov_utils/principal.rb', line 40

def organization_id
  organization_by_domain.try(:id)
end

#organization_id_by_key(organization_key = self.organization_key) ⇒ Object



44
45
46
# File 'app/models/egov_utils/principal.rb', line 44

def organization_id_by_key(organization_key=self.organization_key)
  EgovUtils::Organization.find_by_key(organization_key).try(:id)
end

#organization_keyObject



36
37
38
# File 'app/models/egov_utils/principal.rb', line 36

def organization_key
  organization_by_domain.try(:key)
end

#organization_with_suborganizations_ids(organization_id = self.organization_id) ⇒ Object



55
56
57
58
59
# File 'app/models/egov_utils/principal.rb', line 55

def organization_with_suborganizations_ids(organization_id=self.organization_id)
  Rails.cache.fetch("organizations/#{organization_key}/org_with_suborgs_ids") do
    [organization_id] + ( organization_id && EgovUtils::Organization.where(superior_id: organization_id).collect(&:id) || [] )
  end
end

#organization_with_suborganizations_keys(organization_key = self.organization_key) ⇒ Object



48
49
50
51
52
53
# File 'app/models/egov_utils/principal.rb', line 48

def organization_with_suborganizations_keys(organization_key=self.organization_key)
  return [] unless organization_key
  Rails.cache.fetch("organizations/#{organization_key}/org_with_suborgs_keys") do
    [organization_key] + ( organization_key && EgovUtils::Organization.where(superior_id: organization_id_by_key(organization_key)).collect(&:key) || [] )
  end
end

#reload(*attrs) ⇒ Object



7
8
9
10
# File 'app/models/egov_utils/principal.rb', line 7

def reload(*attrs)
  @auth_source = nil
  super
end