lib/scrivito/user.rb in scrivito_sdk-1.0.0 vs lib/scrivito/user.rb in scrivito_sdk-1.1.0.rc1
- old
+ new
@@ -6,16 +6,28 @@
#
# Valid action verbs for the explicit rules.
# @api public
#
VERBS = [
+ # 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,
- :read,
- :write,
].freeze
class << self
#
# Defines a new user.
@@ -103,10 +115,11 @@
yield user_definition if block_given?
user_definition.user
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
end
def assert_valid_user(user)
@@ -128,14 +141,19 @@
@explicit_rules.each_key { |rule| assert_valid_verb(rule.second) }
end
def can?(verb, workspace)
assert_valid_verb(verb)
- can_always?(verb, :workspace) ||
- verb == :create && can_create? ||
- verb == :read && can_read?(workspace) ||
- can_as_owner?(verb, workspace)
+
+ return true if can_always?(verb, :workspace)
+ return false if can_never?(verb, :workspace)
+
+ case verb
+ when :create then true
+ when :read then workspace.published? || owner_of?(workspace)
+ when :write, :delete, :invite_to, :publish then owner_of?(workspace)
+ end
end
def can_always?(verb, subject)
assert_valid_verb(verb)
@explicit_rules.has_key?([:can_always, verb, subject])
@@ -204,21 +222,9 @@
def system_user?
id.nil?
end
private
-
- def can_create?
- !can_never?(:create, :workspace)
- end
-
- def can_read?(workspace)
- workspace.published? || can_as_owner?(:read, workspace)
- end
-
- def can_as_owner?(verb, workspace)
- workspace.is_a?(Workspace) && owner_of?(workspace) && !can_never?(verb, :workspace)
- end
def calculate_description
description_proc ? description_proc.call : id
end