Commit 01328670 by Dudás Ádám

common, vm: make possibile to specify parent activity for operation

parent 407db0d3
...@@ -25,10 +25,12 @@ class Operation(object): ...@@ -25,10 +25,12 @@ class Operation(object):
""" """
skip_checks = kwargs.setdefault('system', False) skip_checks = kwargs.setdefault('system', False)
user = kwargs.setdefault('user', None) user = kwargs.setdefault('user', None)
parent_activity = kwargs.pop('parent_activity', None)
if not skip_checks: if not skip_checks:
self.check_auth(user) self.check_auth(user)
self.check_precond() self.check_precond()
return self.create_activity(user=user) return self.create_activity(parent=parent_activity, user=user)
def _exec_op(self, activity, user, **kwargs): def _exec_op(self, activity, user, **kwargs):
"""Execute the operation inside the specified activity's context. """Execute the operation inside the specified activity's context.
...@@ -65,11 +67,14 @@ class Operation(object): ...@@ -65,11 +67,14 @@ class Operation(object):
"""Execute the operation (synchronously). """Execute the operation (synchronously).
Anticipated keyword arguments: Anticipated keyword arguments:
* user: The User invoking the operation. If this argument is not * parent_activity: Parent activity for the operation. If this argument
present, it'll be provided with a default value of None. is present, the operation's activity will be created
as a child activity of it.
* system: Indicates that the operation is invoked by the system, not a * system: Indicates that the operation is invoked by the system, not a
User. If this argument is present and has a value of True, User. If this argument is present and has a value of True,
then authorization checks are skipped. then authorization checks are skipped.
* user: The User invoking the operation. If this argument is not
present, it'll be provided with a default value of None.
""" """
activity = self.__prelude(kwargs) activity = self.__prelude(kwargs)
return self._exec_op(activity=activity, **kwargs) return self._exec_op(activity=activity, **kwargs)
...@@ -82,7 +87,7 @@ class Operation(object): ...@@ -82,7 +87,7 @@ class Operation(object):
raise PermissionDenied("%s doesn't have the required permissions." raise PermissionDenied("%s doesn't have the required permissions."
% user) % user)
def create_activity(self, user): def create_activity(self, parent, user):
raise NotImplementedError raise NotImplementedError
def on_abort(self, activity, error): def on_abort(self, activity, error):
......
...@@ -39,9 +39,22 @@ class InstanceOperation(Operation): ...@@ -39,9 +39,22 @@ class InstanceOperation(Operation):
super(InstanceOperation, self).check_auth(user=user) super(InstanceOperation, self).check_auth(user=user)
def create_activity(self, user): def create_activity(self, parent, user):
return InstanceActivity.create(code_suffix=self.activity_code_suffix, if parent:
instance=self.instance, user=user) if parent.instance != self.instance:
raise ValueError("The instance associated with the specified "
"parent activity does not match the instance "
"bound to the operation.")
if parent.user != user:
raise ValueError("The user associated with the specified "
"parent activity does not match the user "
"provided as parameter.")
return parent.create_sub(code_suffix=self.activity_code_suffix)
else:
return InstanceActivity.create(
code_suffix=self.activity_code_suffix, instance=self.instance,
user=user)
def register_instance_operation(op_cls, op_id=None): def register_instance_operation(op_cls, op_id=None):
...@@ -420,7 +433,19 @@ class NodeOperation(Operation): ...@@ -420,7 +433,19 @@ class NodeOperation(Operation):
super(NodeOperation, self).__init__(subject=node) super(NodeOperation, self).__init__(subject=node)
self.node = node self.node = node
def create_activity(self, user): def create_activity(self, parent, user):
if parent:
if parent.node != self.node:
raise ValueError("The node associated with the specified "
"parent activity does not match the node "
"bound to the operation.")
if parent.user != user:
raise ValueError("The user associated with the specified "
"parent activity does not match the user "
"provided as parameter.")
return parent.create_sub(code_suffix=self.activity_code_suffix)
else:
return NodeActivity.create(code_suffix=self.activity_code_suffix, return NodeActivity.create(code_suffix=self.activity_code_suffix,
node=self.node, user=user) node=self.node, user=user)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment