Class: CamaleonCms::UserDecorator

Inherits:
ApplicationDecorator
  • Object
show all
Includes:
CustomFieldsConcern
Defined in:
app/decorators/camaleon_cms/user_decorator.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CustomFieldsConcern

#render_fields, #the_field, #the_field_grouped, #the_fields, #the_fields_grouped, #the_json_field, #the_json_fields

Class Method Details

.object_class_nameObject



79
80
81
# File 'app/decorators/camaleon_cms/user_decorator.rb', line 79

def self.object_class_name
  'CamaleonCms::User'
end

Instance Method Details

#may_edit_credentials?(other_user) ⇒ Boolean

Whether this user may edit +other_user+'s login credential (password) or recovery identifiers (email, username). Only an admin may edit an admin's account: a :manage, :users holder who could reset an admin's password would sign in as them, and one who could repoint an admin's email would hijack a password-reset link — either is a path to superadmin that would make role_grantor?'s guard of the privileged set moot (H10). Editing one's own account, or any non-admin, is unrestricted here; the controller's :manage, :users (or self) authorization still applies first.

Returns:

  • (Boolean)


73
74
75
76
77
# File 'app/decorators/camaleon_cms/user_decorator.rb', line 73

def may_edit_credentials?(other_user)
  return true unless other_user&.admin?

  admin?
end

#role_grantor?(other_user, new_role = nil) ⇒ Boolean

Whether this user may set another user's role. Only an admin may grant the admin role (the one User#admin? tests) or change the role of a user who is already an admin — so holding :manage, :users is neither a path to minting an admin (escalation) nor to stripping one (H10).

Returns:

  • (Boolean)


60
61
62
63
64
65
# File 'app/decorators/camaleon_cms/user_decorator.rb', line 60

def role_grantor?(other_user, new_role = nil)
  return false unless h.can?(:manage, :users) && (other_user.nil? || id != other_user.id)
  return admin? if new_role.to_s == 'admin' || other_user&.role.to_s == 'admin'

  true
end

#the_admin_profile_urlObject

return the url for the profile in the admin module



47
48
49
50
# File 'app/decorators/camaleon_cms/user_decorator.rb', line 47

def the_admin_profile_url
  args = h.cama_current_site_host_port({})
  h.cama_admin_profile_url(object.id, args)
end

#the_avatar(default_avatar = nil) ⇒ Object

return the avatar for this user, default: assets/admin/img/no_image.jpg



22
23
24
25
26
# File 'app/decorators/camaleon_cms/user_decorator.rb', line 22

def the_avatar(default_avatar = nil)
  return object.get_meta('avatar') if avatar_exists?

  default_avatar || h.asset_url('camaleon_cms/admin/img/no_image.jpg')
end

#the_contentsObject

return all contents created by this user in current site



53
54
55
# File 'app/decorators/camaleon_cms/user_decorator.rb', line 53

def the_contents
  h.current_site.posts.where(user_id: object.id)
end

#the_nameObject

return the fullname



12
13
14
# File 'app/decorators/camaleon_cms/user_decorator.rb', line 12

def the_name
  object.fullname
end

#the_roleObject

return the role title of this user for current site



17
18
19
# File 'app/decorators/camaleon_cms/user_decorator.rb', line 17

def the_role
  object.get_role(h.current_site).try(:decorate).try(:the_title) || ''
end

#the_sloganObject

return the slogan for this user, default: Hello World



29
30
31
# File 'app/decorators/camaleon_cms/user_decorator.rb', line 29

def the_slogan
  object.get_meta('slogan', 'Hello World')
end

#the_url(*args) ⇒ Object

return front url for this user



34
35
36
37
38
39
40
41
42
43
44
# File 'app/decorators/camaleon_cms/user_decorator.rb', line 34

def the_url(*args)
  args = args.extract_options!
  args[:label] = I18n.t('routes.profile', default: 'profile')
  args[:user_id] = the_id
  args[:user_name] = the_name.parameterize
  args[:user_name] = the_username if args[:user_name].blank?
  args[:locale] = get_locale unless args.include?(:locale)
  args[:format] = args[:format] || 'html'
  as_path = args.delete(:as_path)
  h.cama_url_to_fixed("cama_profile_#{as_path.present? ? 'path' : 'url'}", args)
end

#the_usernameObject

return the identifier



7
8
9
# File 'app/decorators/camaleon_cms/user_decorator.rb', line 7

def the_username
  object.username
end