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.

[
  # Lets the user create new working copies.
  :create,

  # Makes the content of the working copy visible to the user.
  :read,

  # Lets the user modify the content of the working copy.
  # Also lets the user rename the working copy.
  :write,

  # Permits the user to delete the working copy.
  :delete,

  # Permits the user to invite other users to collaborate on the working copy.
  :invite_to,

  # Permits the user to publish the working copy.
  :publish,
].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_definition|
  user_definition.description { 'Alice Almighty' }

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

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

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

  user_definition.restrict_obj_publish(using: :_obj_class) do |obj_class|
    if obj_class == '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 is persisted in the CMS.

Yield Parameters:

Raises:

See Also:



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

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 others to collaborate on any workspace.

Examples:

Check whether the user may publish a particular object:

Scrivito::User.system_user.can_publish?(Obj.root)
# => true

Get the notification messages for publishing restrictions. An empty array indicates that no restrictions exist.

Scrivito::User.system_user.restriction_messages_for(Obj.root)
# => []

Returns:



95
96
97
# File 'lib/scrivito/user.rb', line 95

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

Instance Method Details

#can_publish?(obj) ⇒ Boolean

Checks whether the User may publish changes to a specific Obj.

Parameters:

Returns:

  • (Boolean)

    true if the user is allowed to publish the object, otherwise false



180
181
182
# File 'lib/scrivito/user.rb', line 180

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

#restriction_messages_for(obj) ⇒ Array<String>

Checks whether the User may publish changes to an Obj and returns the message specified in a Scrivito::UserDefinition#restrict_obj_publish callback if they may not. If the user may publish the CMS object, an empty array is returned.

Parameters:

Returns:

  • (Array<String>)

    Hints on why the user cannot publish the object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/scrivito/user.rb', line 193

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