Class: Cms::ExternalUser

Inherits:
PersistentUser show all
Defined in:
app/models/cms/external_user.rb

Overview

Represents a user that has been authenticated from an external data source. Typical use case might be:

“‘

# Assumes there is an external Crm tool that we look up username/passwords from.
if(SouthparkCrm::Client.authenticate(params[:login], params[:password]))
 user = Cms::ExternalUser.authenticate('stan.marsh', 'southpark-crm')
 user.authorize(Cms::UsersService::GROUP_CMS_ADMIN)

end “‘

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PersistentUser

able_to_edit_or_publish_content, #active_for_authentication?, #cas_extra_attributes=, current, current=, #disable, #disable!, #enable, #enable!, #expired?, #expires_at_formatted, #full_name, #group_codes, #group_codes=, guest, #guest?, permitted_params

Methods included from DefaultAccessible

#non_permitted_params, #permitted_params

Class Method Details

.authenticate(login, source, info = {}) ⇒ Cms::ExternalUser

Returns an authenticated external user. If this is the first time this account has been logged in, this will create a new User row as a side effect. Otherwise, it returns the existing user account.

Parameters:

  • login (String)
  • source (String)

    Used for documentation purposes to determine where User accounts were granted permission from.

  • info (Hash) (defaults to: {})

    (Optional) Additional user attributes to assign to user. Can be core user fields (:first_name) or :external_data.

Returns:

  • (Cms::ExternalUser)

    An ExternalUser record which has been persisted in the database.



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/models/cms/external_user.rb', line 27

def authenticate(, source, info={})
  info = work_around_rails_4_serialization_bug(info)
  criteria = {login: , source: source}
  existing = Cms::ExternalUser.where(criteria).first
  if existing
    existing.update(info)
    return existing
  end
  criteria.merge!(info)
  new_user = Cms::ExternalUser.create!(criteria)
  new_user.groups << Cms::Group.guest if Cms::Group.guest
  new_user
end

.work_around_rails_4_serialization_bug(info) ⇒ Object

Rails 4.0.2 bug: If some value (even {}) for external_data is not specified, then a serialization error occurs.



42
43
44
# File 'app/models/cms/external_user.rb', line 42

def work_around_rails_4_serialization_bug(info)
  {external_data: {}}.merge(info)
end

Instance Method Details

#authorize(*group_codes) ⇒ Object

Authorize this particular user to be part of one or more groups. This will overwrite any previous group membership. Typically this would be called after authenticating a user.

Parameters:

  • group_codes (Array<String>)

    One or more group codes



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

def authorize(*group_codes)
  new_groups = group_codes.collect { |code| Cms::Group.with_code(code).first }
  self.groups = new_groups
end

#password_changeable?Boolean

Determines if this User can have their password changed.

Returns:

  • (Boolean)


48
49
50
# File 'app/models/cms/external_user.rb', line 48

def password_changeable?
  false
end