lib/scrivito/user.rb in scrivito_sdk-0.18.1 vs lib/scrivito/user.rb in scrivito_sdk-0.30.0.rc1

- old
+ new

@@ -44,21 +44,24 @@ # else # 'You are not allowed to publish blog posts.' # end # end # end - def define(id) + def define(id, &block) assert_valid_id(id) - user_definition = UserDefinition.new(id) - yield user_definition if block_given? - user_definition.user + define_user(id, &block) end - def anonymous_admin - explicit_rules = {} - VERBS.each { |verb| explicit_rules[[:can_always, verb, :workspace]] = nil } - new(id: nil, explicit_rules: explicit_rules) + # + # Returns an anonymous system user, who can always create workspaces, can always read, write, + # publish, delete and invite to any workspace. + # + # @api public + # @return [Scrivito::User] the system user + # + def system_user + define_user { |user| user.is_admin! } end def unknown_user(id) new(id: id, explicit_rules: {}) end @@ -71,10 +74,16 @@ end end private + def define_user(id = nil) + user_definition = UserDefinition.new(id) + yield user_definition if block_given? + user_definition.user + end + def assert_valid_id(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 @@ -97,11 +106,12 @@ @explicit_rules.each_key { |rule| assert_valid_verb(rule.second) } end def can?(verb, workspace) assert_valid_verb(verb) - can_always?(verb, :workspace) || owner_of?(workspace) && !can_never?(verb, :workspace) + verb == :read && workspace.published? || can_always?(verb, :workspace) || + owner_of?(workspace) && !can_never?(verb, :workspace) end def can_always?(verb, subject) assert_valid_verb(verb) @explicit_rules.has_key?([:can_always, verb, subject]) @@ -150,31 +160,39 @@ @description ||= calculate_description end def suggest_users(input) if suggest_users_proc - suggested_users = suggest_users_proc.call(input) + suggested_users = sandbox_suggest_user_proc(input) suggested_users.nil? ? [] : suggested_users else user = self.class.find(input) user ? [user] : [] end end - def as_json(options = nil) - { - id: id, - description: description, - } + # Per convention only anonymous admin has a nil id. + def system_user? + id.nil? end private def calculate_description description_proc ? description_proc.call : id end def assert_valid_verb(verb) raise ScrivitoError.new("Invalid verb '#{verb}'") unless VERBS.include?(verb) + end + + def sandbox_suggest_user_proc(input) + suggest_users_proc.call(input) + rescue Exception => e + Rails.logger.error(%{ +Method `suggest_users' of the user "#{id}" raised an error on input "#{input}": #{e} + #{e.backtrace.join("\n")} + }) + nil end end end