Class: Spud::SpudUserModel

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/spud/spud_user_model.rb

Direct Known Subclasses

SpudUser

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.where_name_like(string) ⇒ Object

Returns an ActiveRecord::Relation performing a LIKE query against name columns



78
79
80
81
# File 'app/models/spud/spud_user_model.rb', line 78

def self.where_name_like(string)
  like = '%' + string + '%'
  return self.where('login like ? or concat(`first_name`, " ", `last_name`) like ?', like, like)
end

Instance Method Details

#can_view_app?(admin_application) ⇒ Boolean

Returns true if the user can view a spud app based on it’s key

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
# File 'app/models/spud/spud_user_model.rb', line 34

def can_view_app?(admin_application)
  if self.super_admin?
    return true
  else
    key = admin_application[:key]
    return self.permissions.find{ |p| p.apps.include?(key) }.present?
  end
end

#full_nameObject



12
13
14
15
16
17
18
19
20
21
22
# File 'app/models/spud/spud_user_model.rb', line 12

def full_name
  if first_name.blank? && last_name.blank?
    return self.
  end
  if self.first_name.blank?
    return self.last_name
  elsif self.last_name.blank?
    return self.first_name
  end
  return "#{self.first_name} #{self.last_name}"
end

#has_admin_rights?Boolean

Returns true if user can view at least one dashboard app

Returns:

  • (Boolean)


25
26
27
28
29
30
31
# File 'app/models/spud/spud_user_model.rb', line 25

def has_admin_rights?
  if self.super_admin?
    return true
  else
    return Spud::Core.admin_applications.find{ |app| self.can_view_app?(app) }.present?
  end
end

#has_any_permission?(*tags) ⇒ Boolean

Check if a user has at least one out of a given list of permissions

  • if one tag is supplied, return true if the tag matches

  • if multiple tags are supplied, return true if ANY tag matches

Returns:

  • (Boolean)


60
61
62
63
64
65
66
# File 'app/models/spud/spud_user_model.rb', line 60

def has_any_permission?(*tags)
  if self.super_admin?
    return true
  else
    return self.permissions.find{ |p| tags.include?(p.tag) }.present?
  end
end

#has_permission?(*tags) ⇒ Boolean

Check if a user has a given list of permissions

  • if one tag is supplied, return true if the tag matches

  • if multiple tags are supplied, return true if ALL tags match

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
# File 'app/models/spud/spud_user_model.rb', line 47

def has_permission?(*tags)
  if self.super_admin?
    return true
  else
    my_tags = self.permissions.collect(&:tag)
    return tags.find{ |tag| !my_tags.include?(tag) }.blank?
  end
end

#permissionsObject

Return a list of SpudPermission objects for the user’s SpudRole



69
70
71
72
73
74
75
# File 'app/models/spud/spud_user_model.rb', line 69

def permissions
  if !self.role
    return []
  else
    return self.role.permissions
  end
end