lib/scrivito/user.rb in scrivito_sdk-1.9.1 vs lib/scrivito/user.rb in scrivito_sdk-1.10.0.rc1

- old
+ new

@@ -29,113 +29,108 @@ # Permits the user to access the publishing history :read_history, ].freeze - class << self - # - # Defines a new user. - # - # @api public - # - # @param [String] id 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. - # - # @raise [Scrivito::ScrivitoError] if id is blank - # @raise [Scrivito::ScrivitoError] if id is more than 64 characters long - # - # @yieldparam [Scrivito::UserDefinition] user object to define rules on - # - # @see Scrivito::UserDefinition#can_always - # @see Scrivito::UserDefinition#can_never - # @see Scrivito::UserDefinition#description - # @see Scrivito::UserDefinition#restrict_obj_publish - # @see Scrivito::UserDefinition#suggest_users - # - # @example - # 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 - # - def define(id, &block) - assert_valid_id(id) - define_user(id, &block) - end + # Defines a new user. + # + # @api public + # + # @param [String] id 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. + # + # @raise [Scrivito::ScrivitoError] if id is blank + # @raise [Scrivito::ScrivitoError] if id is more than 64 characters long + # + # @yieldparam [Scrivito::UserDefinition] user object to define rules on + # + # @see Scrivito::UserDefinition#can_always + # @see Scrivito::UserDefinition#can_never + # @see Scrivito::UserDefinition#description + # @see Scrivito::UserDefinition#restrict_obj_publish + # @see Scrivito::UserDefinition#suggest_users + # + # @example + # 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 + # + def self.define(id, &block) + assert_valid_id(id) + define_user(id, &block) + end - # - # Returns an anonymous system user who can always create workspaces, can always read, write, - # publish, delete, and invite others to collaborate on any workspace. - # @example Check whether the user may publish a particular object: - # Scrivito::User.system_user.can_publish?(Obj.root) - # # => true - # - # @example Get the notification messages for publishing restrictions. An empty array indicates that no restrictions exist. - # Scrivito::User.system_user.restriction_messages_for(Obj.root) - # # => [] - # - # @api public - # @return [Scrivito::User] the system user - # - def system_user - define_user { |user| user.is_admin! } - end + # + # Returns an anonymous system user who can always create workspaces, can always read, write, + # publish, delete, and invite others to collaborate on any workspace. + # @example Check whether the user may publish a particular object: + # Scrivito::User.system_user.can_publish?(Obj.root) + # # => true + # + # @example Get the notification messages for publishing restrictions. An empty array indicates that no restrictions exist. + # Scrivito::User.system_user.restriction_messages_for(Obj.root) + # # => [] + # + # @api public + # @return [Scrivito::User] the system user + # + def self.system_user + define_user { |user| user.is_admin! } + end - def unknown_user(id) - new(id: id, explicit_rules: {}) - end + def self.unknown_user(id) + new(id: id, explicit_rules: {}) + end - def find(id) - if Configuration.find_user_proc - user = Scrivito::Configuration.find_user_proc.call(id) - assert_valid_user(user) - user - end + def self.find(id) + if Configuration.find_user_proc + user = Scrivito::Configuration.find_user_proc.call(id) + assert_valid_user(user) + user end + end - def mget(*ids) - ids.map(&method(:find)) - end + def self.mget(*ids) + ids.map(&method(:find)) + end - private + private_class_method def self.define_user(id = nil) + user_definition = UserDefinition.new(id) + yield user_definition if block_given? + user_definition.user + end - def define_user(id = nil) - user_definition = UserDefinition.new(id) - yield user_definition if block_given? - user_definition.user - end + private_class_method def self.assert_valid_id(id) + raise ScrivitoError.new('User id must be a string') unless id.to_s == id + raise ScrivitoError.new('User id can not be blank') if id.blank? + raise ScrivitoError.new('User id is too long (max length 64)') if id.length > 64 + end - def assert_valid_id(id) - raise ScrivitoError.new('User id must be a string') unless id.to_s == id - raise ScrivitoError.new('User id can not be blank') if id.blank? - raise ScrivitoError.new('User id is too long (max length 64)') if id.length > 64 + private_class_method def self.assert_valid_user(user) + unless user.is_a?(User) || user.nil? + raise ScrivitoError.new("Expected an instance of #{self} or nil, but got #{user.inspect}") end - - def assert_valid_user(user) - unless user.is_a?(User) || user.nil? - raise ScrivitoError.new("Expected an instance of #{self} or nil, but got #{user.inspect}") - end - end end attr_reader :id, :explicit_rules, :description_proc, :suggest_users_proc, :restriction_set def initialize(options) @@ -233,11 +228,10 @@ def as_json { id: id, description: description, - explicit_rules: explicit_rules_as_json, } end private @@ -260,12 +254,8 @@ rescue StandardError => e message = %{Method `suggest_users' of the user "#{id}" raised an error on input "#{input}"} Warning.error(message, e) nil - end - - def explicit_rules_as_json - @explicit_rules.map { |(adverb, verb, _)| "#{adverb}-#{verb}" } end end end