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)


111
112
113
114
115
116
# File 'app/models/user.rb', line 111

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)


157
158
159
# File 'app/models/user.rb', line 157

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

#able_to_edit_or_publish_content?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'app/models/user.rb', line 165

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

#able_to_modify?(object) ⇒ Boolean

Returns:

  • (Boolean)


139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'app/models/user.rb', line 139

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)


161
162
163
# File 'app/models/user.rb', line 161

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

#able_to_view?(object) ⇒ Boolean

Determine if this user has permission to view the specific object. Permissions

are always tied to a specific section. This method can take different input parameters
and will attempt to determine the relevant section to check.

Expects object to be of type:

1. Section - Will check the user's groups to see if any of those groups can view this section.
2. Path - Will look up the section based on the path, then check it.  (Note that section paths are not currently unique, so this will check the first one it finds).
3. Other - Assumes it has a section attribute and will call that and check the return value.

Returns: true if the user can view this object, false otherwise. Raises: ActiveRecord::RecordNotFound if a path to a not existent section is passed in.

Returns:

  • (Boolean)


128
129
130
131
132
133
134
135
136
137
# File 'app/models/user.rb', line 128

def able_to_view?(object)
  section = object
  if object.is_a?(String)
     section = Section.find_by_path(object)
     raise ActiveRecord::RecordNotFound.new("Could not find section with path = '#{object}'") unless section
  elsif !object.is_a?(Section)
    section = object.section
  end
  viewable_sections.include?(section) || cms_access?
end

#cms_access?Boolean

Determines if this user should have access to the CMS administration tools. Can be overridden by specific users (like GuestUser) which may not need to check the database for that information.

Returns:

  • (Boolean)


43
44
45
# File 'app/models/user.rb', line 43

def cms_access?
  groups.cms_access.count > 0 
end

#disableObject



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

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



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

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

#enableObject



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

def enable
  self.expires_at = nil
end

#enable!Object



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

def enable!
  enable
  save!
end

#expired?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'app/models/user.rb', line 62

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.



93
94
95
# File 'app/models/user.rb', line 93

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

#full_nameObject



75
76
77
# File 'app/models/user.rb', line 75

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

#full_name_or_loginObject



83
84
85
86
87
88
89
# File 'app/models/user.rb', line 83

def 
  if full_name.strip.blank?
    
  else
    full_name
  end
end

#full_name_with_loginObject



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

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

#guest?Boolean

Returns:

  • (Boolean)


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

def guest?
  !!@guest
end

#modifiable_sectionsObject



105
106
107
# File 'app/models/user.rb', line 105

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



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

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

#viewable_sectionsObject



101
102
103
# File 'app/models/user.rb', line 101

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