[Sharing 2.0] Add L10N instance to manager for translated errors

This commit is contained in:
Roeland Jago Douma 2016-01-05 12:50:00 +01:00
parent 527b434cd2
commit 26280e1f19
4 changed files with 65 additions and 28 deletions

View File

@ -39,7 +39,8 @@ class OCSShareWrapper {
\OC::$server->getSecureRandom(), \OC::$server->getSecureRandom(),
\OC::$server->getHasher(), \OC::$server->getHasher(),
\OC::$server->getMountManager(), \OC::$server->getMountManager(),
\OC::$server->getGroupManager() \OC::$server->getGroupManager(),
\OC::$server->getL10N('core')
), ),
\OC::$server->getGroupManager(), \OC::$server->getGroupManager(),
\OC::$server->getUserManager(), \OC::$server->getUserManager(),

View File

@ -268,9 +268,9 @@ class Share20OCS {
} }
$share->setPermissions( $share->setPermissions(
\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_READ |
\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_CREATE |
\OCP\Constants::PERMISSION_UPDATE \OCP\Constants::PERMISSION_UPDATE
); );
} else { } else {
$share->setPermissions(\OCP\Constants::PERMISSION_READ); $share->setPermissions(\OCP\Constants::PERMISSION_READ);
@ -303,7 +303,10 @@ class Share20OCS {
try { try {
$share = $this->shareManager->createShare($share); $share = $this->shareManager->createShare($share);
} catch (\Exception $e) { } catch (\OC\HintException $e) {
$code = $e->getCode() === 0 ? 403 : $e->getCode();
return new \OC_OCS_Result(null, $code, $e->getHint());
}catch (\Exception $e) {
return new \OC_OCS_Result(null, 403, $e->getMessage()); return new \OC_OCS_Result(null, 403, $e->getMessage());
} }

View File

@ -22,11 +22,15 @@ namespace OC\Share20;
use OCP\IConfig; use OCP\IConfig;
use OCP\IL10N;
use OCP\ILogger; use OCP\ILogger;
use OCP\Security\ISecureRandom; use OCP\Security\ISecureRandom;
use OCP\Security\IHasher; use OCP\Security\IHasher;
use OCP\Files\Mount\IMountManager; use OCP\Files\Mount\IMountManager;
use OCP\IGroupManager; use OCP\IGroupManager;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\IUser;
use OC\Share20\Exception\ShareNotFound; use OC\Share20\Exception\ShareNotFound;
@ -35,9 +39,7 @@ use OC\Share20\Exception\ShareNotFound;
*/ */
class Manager { class Manager {
/** /** @var IShareProvider[] */
* @var IShareProvider[]
*/
private $defaultProvider; private $defaultProvider;
/** @var ILogger */ /** @var ILogger */
@ -58,6 +60,9 @@ class Manager {
/** @var IGroupManager */ /** @var IGroupManager */
private $groupManager; private $groupManager;
/** @var IL10N */
private $l;
/** /**
* Manager constructor. * Manager constructor.
* *
@ -68,6 +73,7 @@ class Manager {
* @param IHasher $hasher * @param IHasher $hasher
* @param IMountManager $mountManager * @param IMountManager $mountManager
* @param IGroupManager $groupManager * @param IGroupManager $groupManager
* @param IL10N $l
*/ */
public function __construct( public function __construct(
ILogger $logger, ILogger $logger,
@ -76,7 +82,8 @@ class Manager {
ISecureRandom $secureRandom, ISecureRandom $secureRandom,
IHasher $hasher, IHasher $hasher,
IMountManager $mountManager, IMountManager $mountManager,
IGroupManager $groupManager IGroupManager $groupManager,
IL10N $l
) { ) {
$this->logger = $logger; $this->logger = $logger;
$this->config = $config; $this->config = $config;
@ -84,6 +91,7 @@ class Manager {
$this->hasher = $hasher; $this->hasher = $hasher;
$this->mountManager = $mountManager; $this->mountManager = $mountManager;
$this->groupManager = $groupManager; $this->groupManager = $groupManager;
$this->l = $l;
// TEMP SOLUTION JUST TO GET STARTED // TEMP SOLUTION JUST TO GET STARTED
$this->defaultProvider = $defaultProvider; $this->defaultProvider = $defaultProvider;
@ -191,6 +199,7 @@ class Manager {
* *
* @param \DateTime $expireDate The current expiration date (can be null) * @param \DateTime $expireDate The current expiration date (can be null)
* @return \DateTime|null The expiration date or null if $expireDate was null and it is not required * @return \DateTime|null The expiration date or null if $expireDate was null and it is not required
* @throws \OC\HintException
*/ */
protected function validateExpiredate($expireDate) { protected function validateExpiredate($expireDate) {
@ -201,7 +210,8 @@ class Manager {
$date = new \DateTime(); $date = new \DateTime();
$date->setTime(0, 0, 0); $date->setTime(0, 0, 0);
if ($date >= $expireDate) { if ($date >= $expireDate) {
throw new \InvalidArgumentException('Expiration date is in the past'); $message = $this->l->t('Expiration date is in the past');
throw new \OC\HintException($message, $message, 404);
} }
} }
@ -215,7 +225,8 @@ class Manager {
$date->setTime(0, 0, 0); $date->setTime(0, 0, 0);
$date->add(new \DateInterval('P' . $this->shareApiLinkDefaultExpireDays() . 'D')); $date->add(new \DateInterval('P' . $this->shareApiLinkDefaultExpireDays() . 'D'));
if ($date < $expireDate) { if ($date < $expireDate) {
throw new \InvalidArgumentException('Cannot set expiration date more than ' . $this->shareApiLinkDefaultExpireDays() . ' in the future'); $message = $this->l->t('Cannot set expiration date more than %s days in the future', [$this->shareApiLinkDefaultExpireDays()]);
throw new \OC\HintException($message, $message, 404);
} }
return $expireDate; return $expireDate;
@ -390,7 +401,7 @@ class Manager {
* For now ignore a set token. * For now ignore a set token.
*/ */
$share->setToken( $share->setToken(
$this->secureRandom->getMediumStrengthGenerator()->generate( $this->secureRandom->generate(
\OC\Share\Constants::TOKEN_LENGTH, \OC\Share\Constants::TOKEN_LENGTH,
\OCP\Security\ISecureRandom::CHAR_LOWER. \OCP\Security\ISecureRandom::CHAR_LOWER.
\OCP\Security\ISecureRandom::CHAR_UPPER. \OCP\Security\ISecureRandom::CHAR_UPPER.

View File

@ -23,6 +23,7 @@ namespace Test\Share20;
use OC\Share20\Manager; use OC\Share20\Manager;
use OC\Share20\Exception; use OC\Share20\Exception;
use OCP\IL10N;
use OCP\ILogger; use OCP\ILogger;
use OCP\IConfig; use OCP\IConfig;
use OC\Share20\IShareProvider; use OC\Share20\IShareProvider;
@ -63,6 +64,9 @@ class ManagerTest extends \Test\TestCase {
/** @var IGroupManager */ /** @var IGroupManager */
protected $groupManager; protected $groupManager;
/** @var IL10N */
protected $l;
public function setUp() { public function setUp() {
$this->logger = $this->getMock('\OCP\ILogger'); $this->logger = $this->getMock('\OCP\ILogger');
@ -73,6 +77,12 @@ class ManagerTest extends \Test\TestCase {
$this->mountManager = $this->getMock('\OCP\Files\Mount\IMountManager'); $this->mountManager = $this->getMock('\OCP\Files\Mount\IMountManager');
$this->groupManager = $this->getMock('\OCP\IGroupManager'); $this->groupManager = $this->getMock('\OCP\IGroupManager');
$this->l = $this->getMock('\OCP\IL10N');
$this->l->method('t')
->will($this->returnCallback(function($text, $parameters = []) {
return vsprintf($text, $parameters);
}));
$this->manager = new Manager( $this->manager = new Manager(
$this->logger, $this->logger,
$this->config, $this->config,
@ -80,7 +90,8 @@ class ManagerTest extends \Test\TestCase {
$this->secureRandom, $this->secureRandom,
$this->hasher, $this->hasher,
$this->mountManager, $this->mountManager,
$this->groupManager $this->groupManager,
$this->l
); );
} }
@ -126,7 +137,8 @@ class ManagerTest extends \Test\TestCase {
$this->secureRandom, $this->secureRandom,
$this->hasher, $this->hasher,
$this->mountManager, $this->mountManager,
$this->groupManager $this->groupManager,
$this->l
]) ])
->setMethods(['getShareById', 'deleteChildren']) ->setMethods(['getShareById', 'deleteChildren'])
->getMock(); ->getMock();
@ -216,7 +228,8 @@ class ManagerTest extends \Test\TestCase {
$this->secureRandom, $this->secureRandom,
$this->hasher, $this->hasher,
$this->mountManager, $this->mountManager,
$this->groupManager $this->groupManager,
$this->l,
]) ])
->setMethods(['getShareById']) ->setMethods(['getShareById'])
->getMock(); ->getMock();
@ -360,7 +373,8 @@ class ManagerTest extends \Test\TestCase {
$this->secureRandom, $this->secureRandom,
$this->hasher, $this->hasher,
$this->mountManager, $this->mountManager,
$this->groupManager $this->groupManager,
$this->l,
]) ])
->setMethods(['deleteShare']) ->setMethods(['deleteShare'])
->getMock(); ->getMock();
@ -569,7 +583,7 @@ class ManagerTest extends \Test\TestCase {
} }
/** /**
* @expectedException InvalidArgumentException * @expectedException \OC\HintException
* @expectedExceptionMessage Expiration date is in the past * @expectedExceptionMessage Expiration date is in the past
*/ */
public function testValidateExpiredateInPast() { public function testValidateExpiredateInPast() {
@ -594,10 +608,6 @@ class ManagerTest extends \Test\TestCase {
$this->invokePrivate($this->manager, 'validateExpiredate', [null]); $this->invokePrivate($this->manager, 'validateExpiredate', [null]);
} }
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Cannot set expiration date more than 3 in the future
*/
public function testValidateExpiredateEnforceToFarIntoFuture() { public function testValidateExpiredateEnforceToFarIntoFuture() {
// Expire date in the past // Expire date in the past
$future = new \DateTime(); $future = new \DateTime();
@ -609,7 +619,13 @@ class ManagerTest extends \Test\TestCase {
['core', 'shareapi_expire_after_n_days', '7', '3'], ['core', 'shareapi_expire_after_n_days', '7', '3'],
])); ]));
$this->invokePrivate($this->manager, 'validateExpiredate', [$future]); try {
$this->invokePrivate($this->manager, 'validateExpiredate', [$future]);
} catch (\OC\HintException $e) {
$this->assertEquals('Cannot set expiration date more than 3 days in the future', $e->getMessage());
$this->assertEquals('Cannot set expiration date more than 3 days in the future', $e->getHint());
$this->assertEquals(404, $e->getCode());
}
} }
public function testValidateExpiredateEnforceValid() { public function testValidateExpiredateEnforceValid() {
@ -1139,7 +1155,8 @@ class ManagerTest extends \Test\TestCase {
$this->secureRandom, $this->secureRandom,
$this->hasher, $this->hasher,
$this->mountManager, $this->mountManager,
$this->groupManager $this->groupManager,
$this->l,
]) ])
->setMethods(['isSharingDisabledForUser']) ->setMethods(['isSharingDisabledForUser'])
->getMock(); ->getMock();
@ -1167,7 +1184,8 @@ class ManagerTest extends \Test\TestCase {
$this->secureRandom, $this->secureRandom,
$this->hasher, $this->hasher,
$this->mountManager, $this->mountManager,
$this->groupManager $this->groupManager,
$this->l,
]) ])
->setMethods(['canShare']) ->setMethods(['canShare'])
->getMock(); ->getMock();
@ -1186,7 +1204,8 @@ class ManagerTest extends \Test\TestCase {
$this->secureRandom, $this->secureRandom,
$this->hasher, $this->hasher,
$this->mountManager, $this->mountManager,
$this->groupManager $this->groupManager,
$this->l,
]) ])
->setMethods(['canShare', 'generalCreateChecks', 'userCreateChecks', 'pathCreateChecks']) ->setMethods(['canShare', 'generalCreateChecks', 'userCreateChecks', 'pathCreateChecks'])
->getMock(); ->getMock();
@ -1247,7 +1266,8 @@ class ManagerTest extends \Test\TestCase {
$this->secureRandom, $this->secureRandom,
$this->hasher, $this->hasher,
$this->mountManager, $this->mountManager,
$this->groupManager $this->groupManager,
$this->l,
]) ])
->setMethods(['canShare', 'generalCreateChecks', 'groupCreateChecks', 'pathCreateChecks']) ->setMethods(['canShare', 'generalCreateChecks', 'groupCreateChecks', 'pathCreateChecks'])
->getMock(); ->getMock();
@ -1308,7 +1328,8 @@ class ManagerTest extends \Test\TestCase {
$this->secureRandom, $this->secureRandom,
$this->hasher, $this->hasher,
$this->mountManager, $this->mountManager,
$this->groupManager $this->groupManager,
$this->l,
]) ])
->setMethods([ ->setMethods([
'canShare', 'canShare',
@ -1448,7 +1469,8 @@ class ManagerTest extends \Test\TestCase {
$this->secureRandom, $this->secureRandom,
$this->hasher, $this->hasher,
$this->mountManager, $this->mountManager,
$this->groupManager $this->groupManager,
$this->l,
]) ])
->setMethods([ ->setMethods([
'canShare', 'canShare',