Commits

Olemis Lang committed 1fe8942

BH RPC : Special treatment for product owner

Comments (0)

Files changed (1)

trunk/bhrpc/product.py

 """
 
 from trac.core import Component, implements, TracError
+from trac.perm import PermissionSystem
 from trac.resource import ResourceNotFound, Neighborhood
 from trac.util.translation import _
 
-from multiproduct.env import ProductEnvironment
+from multiproduct.env import ProductEnvironment, lookup_product_env
 from multiproduct.model import Product
 
 from tracrpc.api import IXMLRPCHandler
                       (None, str, str),
                       (None, str, str, str),
                       (None, str, str, str, str),), self.update)
+        yield (None, ((None, str, str),), self.chown)
         if not isinstance(self.env, ProductEnvironment):
             yield ('PRODUCT_CREATE', ((None, str, str),), self.create)
 
         """ Create a new product . """
 
         owner = owner or req.authname
+        if not owner or owner == 'anonymous':
+            raise TracError("Product owner must be specified")
         req.perm(Neighborhood('global', None)).require('PRODUCT_CREATE')
         product = Product(self.env)
         product.update_field_dict({'prefix':prefix, 'name':name, 'owner':owner,
-                              'description' : description})
+                              'description':description})
         product.insert()
 
-    def update(self, req, prefix, name=None, description=None, owner=None):
+    def update(self, req, prefix, name=None, description=None):
         """ Update products with given values. """
 
         req.perm(Neighborhood('product', prefix)).require('PRODUCT_MODIFY')
         product = Product(self.env, {'prefix' : prefix})
         l = locals()
-        changes = dict(l[k] for k in ('name', 'description', 'owner') if l[k])
+        changes = dict(l[k] for k in ('name', 'description') if l[k])
         product.update_field_dict(changes)
         product.update()
+
+    def chown(self, req, prefix, owner):
+        """ Transfer product ownership to a given user """
+
+        if not owner or owner == 'anonymous':
+            raise TracError("Product owner must be specified")
+        product = Product(self.env, {'prefix' : prefix})
+        if product.owner == owner:
+            self.log.warning("Product %s owner retained as %s", prefix, owner)
+        elif product.owner != req.authname:
+            req.perm.require('TRAC_ADMIN')
+        # Ensure previous owner retains admin privileges in product context
+        if product.owner:
+            permsys = PermissionSystem(lookup_product_env(self.env, prefix))
+            permsys.grant_permission(product.owner, 'PRODUCT_ADMIN')
+        product.update_field_dict({'owner' : owner})
+        product.update()