Class: User

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Cms::Authentication::Model
Defined in:
app/models/user.rb

Direct Known Subclasses

GuestUser

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Cms::Authentication::Model

included

Class Method Details

.currentObject



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

def self.current
  Thread.current[:cms_user]
end

.current=(user) ⇒ Object



29
30
31
# File 'app/models/user.rb', line 29

def self.current=(user)
  Thread.current[:cms_user] = user
end

.guest(options = {}) ⇒ Object



33
34
35
# File 'app/models/user.rb', line 33

def self.guest(options = {})
  GuestUser.new(options)
end

Instance Method Details

#able_to?(*required_permissions) ⇒ Boolean

Expects a list of names of Permissions true if the user has any of the permissions

Returns:

  • (Boolean)


97
98
99
100
101
102
# File 'app/models/user.rb', line 97

def able_to?(*required_permissions)
  perms = required_permissions.map(&:to_sym)
  permissions.any? do |p| 
    perms.include?(p.name.to_sym) 
  end
end

#able_to_edit?(object) ⇒ Boolean

Expects node to be a Section, Page or Link Returns true if the specified node, or any of its ancestor sections, is editable by any of the user’s ‘CMS User’ groups.

Returns:

  • (Boolean)


131
132
133
# File 'app/models/user.rb', line 131

def able_to_edit?(object)    
  able_to?(:edit_content) && able_to_modify?(object)
end

#able_to_edit_or_publish_content?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'app/models/user.rb', line 139

def able_to_edit_or_publish_content?
  able_to?(:edit_content, :publish_content)
end

#able_to_modify?(object) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/models/user.rb', line 113

def able_to_modify?(object)
  case object
    when Section
      modifiable_sections.include?(object)
    when Page, Link
      modifiable_sections.include?(object.section)
    else
      if object.class.respond_to?(:connectable?) && object.class.connectable?
        object.connected_pages.all? { |page| able_to_modify?(page) }
      else
        true
      end
  end
end

#able_to_publish?(object) ⇒ Boolean

Returns:

  • (Boolean)


135
136
137
# File 'app/models/user.rb', line 135

def able_to_publish?(object)
  able_to?(:publish_content) && able_to_modify?(object)
end

#able_to_view?(object) ⇒ Boolean

Expects object to be an object or a section If it’s a section, that will be used If it’s not a section, it will call section on the object returns true if any of the sections of the groups the user is in matches the page’s section.

Returns:

  • (Boolean)


108
109
110
111
# File 'app/models/user.rb', line 108

def able_to_view?(object)
  section = object.is_a?(Section) ? object : object.section
  viewable_sections.include?(section) || groups.cms_access.count > 0
end

#disableObject



41
42
43
44
45
46
47
# File 'app/models/user.rb', line 41

def disable
  if self.class.count(:conditions => ["expires_at is null and id != ?", id]) > 0
    self.expires_at = Time.now - 1.minutes
  else
    false
  end
end

#disable!Object



49
50
51
52
53
54
# File 'app/models/user.rb', line 49

def disable!
  unless disable
    raise "You must have at least 1 enabled user"
  end
  save!
end

#enableObject



60
61
62
# File 'app/models/user.rb', line 60

def enable
  self.expires_at = nil
end

#enable!Object



64
65
66
67
# File 'app/models/user.rb', line 64

def enable!
  enable
  save!
end

#expired?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'app/models/user.rb', line 56

def expired?
  expires_at && expires_at <= Time.now
end

#expires_at_formattedObject

This is to show a formated date on the input form. I’m unsure that this is the best way to solve this, but it works.



79
80
81
# File 'app/models/user.rb', line 79

def expires_at_formatted
  expires_at ? (expires_at.strftime '%m/%d/%Y' ): nil
end

#full_nameObject



69
70
71
# File 'app/models/user.rb', line 69

def full_name
  [first_name, last_name].reject{|e| e.nil?}.join(" ")
end

#full_name_with_loginObject



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

def 
  "#{full_name} (#{})"
end

#guest?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'app/models/user.rb', line 37

def guest?
  !!@guest
end

#modifiable_sectionsObject



91
92
93
# File 'app/models/user.rb', line 91

def modifiable_sections
  @modifiable_sections ||= Section.find(:all, :include => {:groups => [:group_type, :users]}, :conditions => ["users.id = ? and group_types.cms_access = ?", id, true])
end

#permissionsObject



83
84
85
# File 'app/models/user.rb', line 83

def permissions
  @permissions ||= Permission.find(:all, :include => {:groups => :users}, :conditions => ["users.id = ?", id])
end

#viewable_sectionsObject



87
88
89
# File 'app/models/user.rb', line 87

def viewable_sections
  @viewable_sections ||= Section.find(:all, :include => {:groups => :users}, :conditions => ["users.id = ?", id])
end