Class: Cms::PersistentUser

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
DefaultAccessible
Defined in:
app/models/cms/persistent_user.rb

Overview

A parent class for users that need to be persisted in the CMS database.

Direct Known Subclasses

ExternalUser, User

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DefaultAccessible

non_permitted_params, permitted_params

Class Method Details

.able_to_edit_or_publish_contentActiveRelation<Cms::User>

Returns all users that can :edit_content or :publish_content permissions.

Returns:

  • (ActiveRelation<Cms::User>)

    A scope which will find users with the correct permissions.



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

def able_to_edit_or_publish_content
  where(["#{Permission.table_name}.name = ? OR #{Permission.table_name}.name = ?", "edit_content", "publish_content"]).includes({ :groups => :permissions }).references(:permissions)
end

.currentObject



41
42
43
# File 'app/models/cms/persistent_user.rb', line 41

def current
  Cms::UsersService.current
end

.current=(user) ⇒ Object



45
46
47
# File 'app/models/cms/persistent_user.rb', line 45

def current=(user)
  Cms::UsersService.current = user
end

.guest(options = {}) ⇒ Object

Return a GuestUser with the given values.



50
51
52
# File 'app/models/cms/persistent_user.rb', line 50

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

.permitted_paramsObject



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

def permitted_params
  super + [{ :group_ids => [] }]
end

Instance Method Details

#active_for_authentication?Boolean

Determines if this user should be authenticated. Hook for Devise.

Returns:

  • (Boolean)

    true if this user has not expired.



93
94
95
96
97
# File 'app/models/cms/persistent_user.rb', line 93

def active_for_authentication?
  is_active = !expired?
  logger.error "Expired User '#{}' failed to login. Account expired on #{expires_at_formatted}." unless is_active
  is_active
end

#cas_extra_attributes=(extra_attributes) ⇒ Object



55
56
57
58
59
# File 'app/models/cms/persistent_user.rb', line 55

def cas_extra_attributes=(extra_attributes)
  self.external_data = extra_attributes.to_json
  extra_attributes = {}.merge(extra_attributes).symbolize_keys
  Cms.user_cas_extra_attributes_setter.call self, extra_attributes
end

#disableObject



74
75
76
77
78
79
80
# File 'app/models/cms/persistent_user.rb', line 74

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

#disable!Object



82
83
84
85
86
87
# File 'app/models/cms/persistent_user.rb', line 82

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

#enableObject



110
111
112
# File 'app/models/cms/persistent_user.rb', line 110

def enable
  self.expires_at = nil
end

#enable!Object



114
115
116
117
# File 'app/models/cms/persistent_user.rb', line 114

def enable!
  enable
  save!
end

#expired?Boolean

Determines if this user has expired or has been disabled.

Returns:

  • (Boolean)


106
107
108
# File 'app/models/cms/persistent_user.rb', line 106

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

#expires_at_formattedObject

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



125
126
127
# File 'app/models/cms/persistent_user.rb', line 125

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

#full_nameObject



119
120
121
# File 'app/models/cms/persistent_user.rb', line 119

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

#group_codesObject



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

def group_codes
  groups.map &:code
end

#group_codes=(group_codes) ⇒ Object



65
66
67
# File 'app/models/cms/persistent_user.rb', line 65

def group_codes=(group_codes)
  self.groups = Cms::Group.with_code(group_codes)
end

#guest?Boolean

Determines if this user a Guest or not.

Returns:

  • (Boolean)


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

def guest?
  !!@guest
end

#password_changeable?Boolean

Determines if this User can have their password changed.

Returns:

  • (Boolean)


100
101
102
# File 'app/models/cms/persistent_user.rb', line 100

def password_changeable?
  true
end