REST API: fix deleting a product/service.

Add a User parameter to the Product::delete() method to explicitly set
the user that performs the action instead of using a global variable.
In the calls to Product::delete() with a fetched object, remove the
deprecated parameter $id.
This commit is contained in:
Xebax 2016-06-17 00:50:07 +02:00
parent 58c25ff665
commit d7c8a466b1
5 changed files with 7 additions and 11 deletions

View File

@ -553,7 +553,7 @@ if (empty($reshook))
{ {
if (($object->type == Product::TYPE_PRODUCT && $user->rights->produit->supprimer) || ($object->type == Product::TYPE_SERVICE && $user->rights->service->supprimer)) if (($object->type == Product::TYPE_PRODUCT && $user->rights->produit->supprimer) || ($object->type == Product::TYPE_SERVICE && $user->rights->service->supprimer))
{ {
$result = $object->delete($object->id); $result = $object->delete($user);
} }
if ($result > 0) if ($result > 0)

View File

@ -306,7 +306,6 @@ class Products extends DolibarrApi
* *
* @param int $id Product ID * @param int $id Product ID
* @return array * @return array
* FIXME Deleting a product/service does not work because the Product::delete() method uses a global $user but it is not set.
*/ */
function delete($id) function delete($id)
{ {
@ -322,7 +321,7 @@ class Products extends DolibarrApi
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
} }
return $this->product->delete($id); return $this->product->delete(DolibarrApiAccess::$user);
} }
/** /**

View File

@ -878,18 +878,19 @@ class Product extends CommonObject
/** /**
* Delete a product from database (if not used) * Delete a product from database (if not used)
* *
* @param User $user Object user that ask to delete
* @param int $id Product id (usage of this is deprecated, delete should be called without parameters on a fetched object) * @param int $id Product id (usage of this is deprecated, delete should be called without parameters on a fetched object)
* @param int $notrigger Do not execute trigger * @param int $notrigger Do not execute trigger
* @return int < 0 if KO, 0 = Not possible, > 0 if OK * @return int < 0 if KO, 0 = Not possible, > 0 if OK
*/ */
function delete($id=0, $notrigger=0) function delete($user, $id=0, $notrigger=0)
{ {
// Deprecation warning // Deprecation warning
if ($id > 0) { if ($id > 0) {
dol_syslog(__METHOD__ . " with parameter is deprecated", LOG_WARNING); dol_syslog(__METHOD__ . " with parameter is deprecated", LOG_WARNING);
} }
global $conf,$user,$langs; global $conf,$langs;
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php'; require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
$error=0; $error=0;

View File

@ -841,10 +841,6 @@ function deleteProductOrService($authentication,$listofidstring)
$error=0; $error=0;
$fuser=check_authentication($authentication,$error,$errorcode,$errorlabel); $fuser=check_authentication($authentication,$error,$errorcode,$errorlabel);
// User must be defined to user authenticated
global $user;
$user=$fuser;
$listofid=explode(',',trim($listofidstring)); $listofid=explode(',',trim($listofidstring));
$listofiddeleted=array(); $listofiddeleted=array();
@ -873,7 +869,7 @@ function deleteProductOrService($authentication,$listofidstring)
} }
else else
{ {
$result=$newobject->delete(); $result=$newobject->delete($fuser);
if ($result <= 0) if ($result <= 0)
{ {
$error++; $error++;

View File

@ -232,7 +232,7 @@ class ProductTest extends PHPUnit_Framework_TestCase
$localobject=new Product($this->savdb); $localobject=new Product($this->savdb);
$result=$localobject->fetch($id); $result=$localobject->fetch($id);
$result=$localobject->delete($id); $result=$localobject->delete($user);
print __METHOD__." id=".$id." result=".$result."\n"; print __METHOD__." id=".$id." result=".$result."\n";
$this->assertLessThan($result, 0); $this->assertLessThan($result, 0);