Class: Scrivito::UserDefinition

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

Instance Method Summary collapse

Instance Method Details

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

Note:

Usually, the memberships of a workspace decide whether a user is allowed or not to perform a specific action. This method lets you 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 an action verb, the subject of the action, and an optional message.

Examples:

User can always read from, write to, 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. Currently, only :workspace is supported.

  • message (String) (defaults to: nil)

    an optional message to be displayed in the UI.

Raises:

See Also:



41
42
43
44
# File 'app/cms/scrivito/user_definition.rb', line 41

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

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

Note:

Usually, the memberships of a workspace decide whether a user is allowed or not to perform a specific action. This method lets you 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 an action verb, the subject of the action, and an optional message.

Examples:

User can never publish a workspace, even if they’re a workspace owner:

Scrivito::User.define('alice') do |user|
  user.can_never(:publish, :workspace, 'You cannot 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. Currently, only :workspace is supported.

  • message (String) (defaults to: nil)

    an optional message to be displayed in the UI.

Raises:

See Also:



79
80
81
82
# File 'app/cms/scrivito/user_definition.rb', line 79

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

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

Note:

The description is calculated “lazily”. The calculated description is cached.

Defines the user description to be displayed in the in-place UI, e.g. in the workspace menu.

Examples:

Displays the user alice as “alice” in the in-place UI:

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

Displays the user bob as “Bob Doe” in the in-place UI:

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)

    a proc to calculate the description, defaults to the user id.

See Also:



120
121
122
123
124
125
126
# File 'app/cms/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

Enables the user to create workspaces, to read from, write to, publish, delete workspaces, and to invite others to collaborate on any workspace.



90
91
92
# File 'app/cms/scrivito/user_definition.rb', line 90

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

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

Note:

The callback is only executed for Objs equipped with the attribute specified using the :using option. It is not executed for a Widget with this attribute.

Lets you restrict a user’s capability to publish a specific CMS object. Each registered callback may refer to one 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 == '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 wish to refer to 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 applies



199
200
201
# File 'app/cms/scrivito/user_definition.rb', line 199

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

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

Note:

Only the first 20 users returned are displayed in the in-place UI.

Note:

suggest_users_proc may also be invoked with an empty string.

Defines the proc for fetching users for autocompletion purposes in the in-place UI. User autocompletion is used in the settings dialog of a workspace. If the proc is not set, Scrivito::User.find is used to fetch the suggested users, assuming the input is part of 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 UI

Yield Parameters:

  • input (String)

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

Yield Returns:

  • (Array<Scrivito::User>)

    users that were found on account of the given input string



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

def suggest_users(&suggest_users_proc)
  @suggest_users_proc = suggest_users_proc
end

#ui_locale(language) ⇒ Object

Lets you specify the user’s in-place UI language.

Overrides auto-detection from browser language. Overrides the application-wide scrivito UI locale configuration.

Examples:

Have robert use a UI with German titles:

robert = Scrivito::User.define('robert') do |user|
  user.ui_locale('de')
end

Parameters:

  • language (String)

    a language code (e.g. ‘en’)

See Also:



220
221
222
# File 'app/cms/scrivito/user_definition.rb', line 220

def ui_locale(language)
  @ui_locale = language
end