Class: Scrivito::User

Inherits:
Object
  • Object
show all
Defined in:
lib/scrivito/user.rb

Constant Summary collapse

VERBS =

Valid action verbs for the explicit rules.

[
  :create,
  :delete,
  :invite_to,
  :publish,
  :read,
  :write,
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.define(id) {|user| ... } ⇒ Object

Defines a new user.

Examples:

Scrivito::User.define('alice') do |user|
  user.description { 'Alice Almighty' }

  user.can_always(:read, :workspace)
  user.can_always(:write, :workspace)
  user.can_always(:publish, :workspace, 'You can always publish workspaces.')
end

Scrivito::User.define('bob') do |user|
  user.description('Bob Doe')

  user.can_never(:create, :workspace, 'You are not allowed to create workspaces.')
  user.can_always(:read, :workspace)

  user.restrict_obj_publish(using: :_obj_class) do |obj_class|
    if obj_class.name == 'BlogPost'
      false
    else
      'You are not allowed to publish blog posts.'
    end
  end
end

Parameters:

  • id (String)

    the unique, unalterable id of the user. The user id is used to associate the user with the corresponding CMS resources. It will be persisted in the CMS.

Yield Parameters:

Raises:

See Also:



64
65
66
67
# File 'lib/scrivito/user.rb', line 64

def define(id, &block)
  assert_valid_id(id)
  define_user(id, &block)
end

.system_userScrivito::User

Returns an anonymous system user, who can always create workspaces, can always read, write, publish, delete and invite to any workspace.

Returns:



76
77
78
# File 'lib/scrivito/user.rb', line 76

def system_user
  define_user { |user| user.is_admin! }
end

Instance Method Details

#can_publish?(obj) ⇒ Boolean

Verifies if the User is able to publish changes to a certain Obj

Parameters:

  • obj (BasicObj)

    the obj that should be published

Returns:

  • (Boolean)

    true if the user is allowed to publish otherwise false



154
155
156
# File 'lib/scrivito/user.rb', line 154

def can_publish?(obj)
  restriction_messages_for(obj).empty?
end

#restriction_messages_for(obj) ⇒ Array<String>

Checks if the User is able to publish changes and returns the message specified in a Scrivito::UserDefinition#restrict_obj_publish callback if they are not If the user can publish the obj an empty array is returned

Parameters:

  • obj (BasicObj)

    the obj that should be published

Returns:

  • (Array<String>)

    Hints why the user can’t publish



167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/scrivito/user.rb', line 167

def restriction_messages_for(obj)
  assert_restrictions_applicable(obj)

  return [] if can_always?(:publish, :workspace)

  if obj.modification == Modification::EDITED
    base_revision_obj = obj.in_revision(obj.revision.workspace.base_revision)

    restriction_set.restriction_messages_for(obj) |
      restriction_set.restriction_messages_for(base_revision_obj)
  else
    restriction_set.restriction_messages_for(obj)
  end
end