Class: Cortex::User

Inherits:
ApplicationRecord show all
Includes:
HasFirstnameLastname, HasGravatar
Defined in:
app/models/cortex/user.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.anonymousObject



72
73
74
# File 'app/models/cortex/user.rb', line 72

def anonymous
  User.new
end

.authenticate(username, password) ⇒ Object



67
68
69
70
# File 'app/models/cortex/user.rb', line 67

def authenticate(username, password)
  user = User.find_by_email(username)
  user && user.valid_password?(password) ? user : nil
end

Instance Method Details

#active_tenantObject



21
22
23
# File 'app/models/cortex/user.rb', line 21

def active_tenant
  super || tenants.order(:name).first
end

#anonymous?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'app/models/cortex/user.rb', line 31

def anonymous?
  self.id == nil
end

#gravatarObject



48
49
50
# File 'app/models/cortex/user.rb', line 48

def gravatar
  "//www.gravatar.com/avatar/#{Digest::MD5.hexdigest email}"
end

#has_permission?(resource, permission) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/cortex/user.rb', line 35

def has_permission?(resource, permission)
  return true if self.is_superadmin?

  resource_class = resource.class
  allowed_perms = allowed_permissions(resource_class, permission)

  if resource_class == Cortex::ContentType
    allowed_perms.select { |perm| perm.resource_id == resource.id }.any?
  else
    allowed_perms.any?
  end
end

#referenced?Boolean

Returns:

  • (Boolean)


25
26
27
28
29
# File 'app/models/cortex/user.rb', line 25

def referenced?
  [ContentItem].find do |resource|
    true if resource.where(user: self).count > 0
  end
end

#tenants_with_childrenObject



52
53
54
# File 'app/models/cortex/user.rb', line 52

def tenants_with_children
  tenants.flat_map(&:self_and_descendants)
end