Class: Scrivito::UserDefinition

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

Instance Method Summary collapse

Instance Method Details

#can_always(verb, subject, message = nil) ⇒ Object

Note:

Normally the memberships of a workspace decide whether a user is allowed or not to execute a specific action. This method allows to add an exception to this logic and thus should be used carefully.

Note:

By default all users are allowed to create a new workspace.

Adds an explicit rule, that allows the user to always execute an action. A rule consists of a verb of the action, the subject of the action and an optional message.

Examples:

User can always read, write and publish a workspace, ignoring the memberships

Scrivito::User.define('alice') do |user|
  user.can_always(:read, :workspace)
  user.can_always(:write, :workspace)
  user.can_always(:publish, :workspace, 'You can always publish a workspace')
end

Parameters:

  • verb (Symbol)

    the verb of the action (see Scrivito::User::VERBS).

  • subject (Symbol)

    the subject of the action. At the moment only :workspace is supported.

  • message (String) (defaults to: nil)

    optional message to be displayed in the UI.

Raises:

See Also:



40
41
42
43
# File 'lib/scrivito/user_definition.rb', line 40

def can_always(verb, subject, message = nil)
  assert_no_conflict(:can_never, verb, subject)
  @explicit_rules[[:can_always, verb, subject]] = message
end

#can_never(verb, subject, message = nil) ⇒ Object

Note:

Normally the memberships of a workspace decide whether a user is allowed or not to execute a specific action. This method allows to add an exception to this logic and thus should be used carefully.

Note:

By default all users are allowed to create a new workspace. Use this method to forbid a user to create a new workspace.

Adds an explicit rule, that forbids the user to execute an action. A rule consists of a verb of the action, the subject of the action and an optional message.

Examples:

User can never publish a workspace, even if she’s a workspace owner

Scrivito::User.define('alice') do |user|
  user.can_never(:publish, :workspace, 'You can not publish workspaces.')
end

User cannot create a workspace

Scrivito::User.define('bob') do |user|
  user.can_never(:create, :workspace)
end

Parameters:

  • verb (Symbol)

    the verb of the action (see Scrivito::User::VERBS).

  • subject (Symbol)

    the subject of the action. At the moment only :workspace is supported.

  • message (String) (defaults to: nil)

    optional message to be displayed in the UI.

Raises:

See Also:



78
79
80
81
# File 'lib/scrivito/user_definition.rb', line 78

def can_never(verb, subject, message = nil)
  assert_no_conflict(:can_always, verb, subject)
  @explicit_rules[[:can_never, verb, subject]] = message
end

#description(description_string = nil, &description_proc) ⇒ Object

Note:

The description is calculated “lazy”.

Note:

The calculated description will be cached.

Defines the user description to be displayed, when the user is shown in the in-place GUI.

Examples:

Display the user alice as “alice” in the in-place GUI

alice = Scrivito::User.define('alice')

Display the user bob as “Bob Doe” in the in-place GUI

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

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

Parameters:

  • description_string (String) (defaults to: nil)

    the description. Defaults to the the user id.

  • description_proc (Proc)

    proc to calculate the description. Defaults to the the user id.

See Also:



120
121
122
123
124
125
126
# File 'lib/scrivito/user_definition.rb', line 120

def description(description_string = nil, &description_proc)
  if description_string
    description { description_string }
  else
    @description_proc = description_proc
  end
end

#is_admin!Object

Make the user to be always able to create workspaces, to read, to write, to publish, to delete and to invite to any workspace.



89
90
91
# File 'lib/scrivito/user_definition.rb', line 89

def is_admin!
  User::VERBS.each { |verb| @explicit_rules[[:can_always, verb, :workspace]] ||= nil }
end

#restrict_obj_publish(options) {|attribute| ... } ⇒ Object

Note:

the callback is only called with Objs that have the attribute specified by the :using option and if it is not a Widget-attribute

Lets you restrict the rule of a user to publish a certain object. Each registered callback can access a certain attribute of an object. Multiple callbacks are possible

Examples:

class MyUserModel
  def to_scrivito_user
    Scrivito::User.define(id) do |user|
      user.restrict_obj_publish(using: :_path) do |path|
        if path.start_with?("/en")
          false
        else
          "You are only allowed to edit the English site"
        end
      end

      user.restrict_obj_publish(using: :_obj_class) do |obj_class|
        if obj_class.name == 'BlogPost'
          false
        else
          'You are only allowed to edit Blog Posts'
        end
      end
    end
  end
end

Parameters:

  • options (Hash)

Options Hash (options):

  • :using (Symbol)

    the attribute you need in the callback

Yields:

  • (attribute)

    the value of the specified attribute

Yield Returns:

  • (String, false)

    either return a message for the user or false if no restriction is needed



198
199
200
# File 'lib/scrivito/user_definition.rb', line 198

def restrict_obj_publish(options, &block)
  restriction_set.add(options, &block)
end

#suggest_users(&suggest_users_proc) {|input| ... } ⇒ Object

Note:

Only the first 20 of the returnes users will be displayed in the in-place GUI.

Note:

suggest_users_proc may also be invoked with an empty string.

Defines the proc for fetching users for the user autocompletion of the in-place GUI. The user autocompletion is for example used in the details dialog of a workspace. If the proc is not set, then Scrivito::User.find will be used to fetch the suggested users with input as the user id.

Examples:

class MyUserModel
  def to_scrivito_user
    Scrivito::User.define(id) do |user|
      user.suggest_users do |input|
        MyUserModel.find_by_prefix(input).map(&:to_scrivito_user)
      end
    end
  end
end

Parameters:

  • suggest_users_proc (Proc)

    proc for fetching users to be suggested in the in-place GUI

Yield Parameters:

  • input (String)

    an arbitrary string from the input field of a user autocompletion, e.g. the first letters of a user name

Yield Returns:

  • (Array<Scrivito::User>)

    users that were found for the given input string



155
156
157
# File 'lib/scrivito/user_definition.rb', line 155

def suggest_users(&suggest_users_proc)
  @suggest_users_proc = suggest_users_proc
end