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



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

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

.current=(user) ⇒ Object



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

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

.guest(options = {}) ⇒ Object



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

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)


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

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)


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

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

#able_to_edit_or_publish_content?Boolean

Returns:

  • (Boolean)


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

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

#able_to_modify?(object) ⇒ Boolean

Returns:

  • (Boolean)


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

def able_to_modify?(object)
  case object
    when Section
      object.with_ancestors.any? { |section| modifiable_sections.include?(section) }
    when Page, Link
      object.section.with_ancestors.any? { |section| modifiable_sections.include?(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)


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

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)


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

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

#disableObject



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

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



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

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

#enableObject



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

def enable
  self.expires_at = nil
end

#enable!Object



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

def enable!
  enable
  save!
end

#expired?Boolean

Returns:

  • (Boolean)


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

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.



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

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

#full_nameObject



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

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

#full_name_with_loginObject



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

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

#guest?Boolean

Returns:

  • (Boolean)


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

def guest?
  !!@guest
end

#modifiable_sectionsObject



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

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



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

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

#viewable_sectionsObject



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

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