Skip to content

Commit

Permalink
Translate OCS Share API error messages
Browse files Browse the repository at this point in the history
For #22209
  • Loading branch information
rullzer committed Apr 25, 2016
1 parent dc5c570 commit 5e9b618
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 63 deletions.
4 changes: 3 additions & 1 deletion apps/files_sharing/api/ocssharewrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ private function getShare20OCS() {
\OC::$server->getRequest(),
\OC::$server->getRootFolder(),
\OC::$server->getURLGenerator(),
\OC::$server->getUserSession()->getUser());
\OC::$server->getUserSession()->getUser(),
\OC::$server->getL10N('files_sharing')
);
}

public function getAllShares() {
Expand Down
63 changes: 34 additions & 29 deletions apps/files_sharing/api/share20ocs.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

use OCP\Files\NotFoundException;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\IUserManager;
use OCP\IRequest;
use OCP\IURLGenerator;
Expand Down Expand Up @@ -54,6 +55,8 @@ class Share20OCS {
private $urlGenerator;
/** @var IUser */
private $currentUser;
/** @var IL10N */
private $l;

/**
* Share20OCS constructor.
Expand All @@ -73,7 +76,8 @@ public function __construct(
IRequest $request,
IRootFolder $rootFolder,
IURLGenerator $urlGenerator,
IUser $currentUser
IUser $currentUser,
IL10N $l10n
) {
$this->shareManager = $shareManager;
$this->userManager = $userManager;
Expand All @@ -82,6 +86,7 @@ public function __construct(
$this->rootFolder = $rootFolder;
$this->urlGenerator = $urlGenerator;
$this->currentUser = $currentUser;
$this->l = $l10n;
}

/**
Expand Down Expand Up @@ -162,13 +167,13 @@ protected function formatShare(\OCP\Share\IShare $share) {
*/
public function getShare($id) {
if (!$this->shareManager->shareApiEnabled()) {
return new \OC_OCS_Result(null, 404, 'Share API is disabled');
return new \OC_OCS_Result(null, 404, $this->l->t('Share API is disabled'));
}

try {
$share = $this->getShareById($id);
} catch (ShareNotFound $e) {
return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.');
return new \OC_OCS_Result(null, 404, $this->l->t('Wrong share ID, share doesn\'t exist'));
}

if ($this->canAccessShare($share)) {
Expand All @@ -180,7 +185,7 @@ public function getShare($id) {
}
}

return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.');
return new \OC_OCS_Result(null, 404, $this->l->t('Wrong share ID, share doesn\'t exist'));
}

/**
Expand All @@ -191,17 +196,17 @@ public function getShare($id) {
*/
public function deleteShare($id) {
if (!$this->shareManager->shareApiEnabled()) {
return new \OC_OCS_Result(null, 404, 'Share API is disabled');
return new \OC_OCS_Result(null, 404, $this->l->t('Share API is disabled'));
}

try {
$share = $this->getShareById($id);
} catch (ShareNotFound $e) {
return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.');
return new \OC_OCS_Result(null, 404, $this->l->t('Wrong share ID, share doesn\'t exist'));
}

if (!$this->canAccessShare($share)) {
return new \OC_OCS_Result(null, 404, 'could not delete share');
return new \OC_OCS_Result(null, 404, $this->l->t('Could not delete share'));
}

$this->shareManager->deleteShare($share);
Expand All @@ -216,20 +221,20 @@ public function createShare() {
$share = $this->shareManager->newShare();

if (!$this->shareManager->shareApiEnabled()) {
return new \OC_OCS_Result(null, 404, 'Share API is disabled');
return new \OC_OCS_Result(null, 404, $this->l->t('Share API is disabled'));
}

// Verify path
$path = $this->request->getParam('path', null);
if ($path === null) {
return new \OC_OCS_Result(null, 404, 'please specify a file or folder path');
return new \OC_OCS_Result(null, 404, $this->l->t('Please specify a file or folder path'));
}

$userFolder = $this->rootFolder->getUserFolder($this->currentUser->getUID());
try {
$path = $userFolder->get($path);
} catch (\OCP\Files\NotFoundException $e) {
return new \OC_OCS_Result(null, 404, 'wrong path, file/folder doesn\'t exist');
return new \OC_OCS_Result(null, 404, $this->l->t('Wrong path, file/folder doesn\'t exist'));
}

$share->setNode($path);
Expand Down Expand Up @@ -270,25 +275,25 @@ public function createShare() {
if ($shareType === \OCP\Share::SHARE_TYPE_USER) {
// Valid user is required to share
if ($shareWith === null || !$this->userManager->userExists($shareWith)) {
return new \OC_OCS_Result(null, 404, 'please specify a valid user');
return new \OC_OCS_Result(null, 404, $this->l->t('Please specify a valid user'));
}
$share->setSharedWith($shareWith);
$share->setPermissions($permissions);
} else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
if (!$this->shareManager->allowGroupSharing()) {
return new \OC_OCS_Result(null, 404, 'group sharing is disabled by the administrator');
return new \OC_OCS_Result(null, 404, $this->l->t('Group sharing is disabled by the administrator'));
}

// Valid group is required to share
if ($shareWith === null || !$this->groupManager->groupExists($shareWith)) {
return new \OC_OCS_Result(null, 404, 'please specify a valid group');
return new \OC_OCS_Result(null, 404, $this->l->t('Please specify a valid group'));
}
$share->setSharedWith($shareWith);
$share->setPermissions($permissions);
} else if ($shareType === \OCP\Share::SHARE_TYPE_LINK) {
//Can we even share links?
if (!$this->shareManager->shareApiAllowLinks()) {
return new \OC_OCS_Result(null, 404, 'public link sharing is disabled by the administrator');
return new \OC_OCS_Result(null, 404, $this->l->t('Public link sharing is disabled by the administrator'));
}

/*
Expand All @@ -304,12 +309,12 @@ public function createShare() {
if ($publicUpload === 'true') {
// Check if public upload is allowed
if (!$this->shareManager->shareApiLinkAllowPublicUpload()) {
return new \OC_OCS_Result(null, 403, 'public upload disabled by the administrator');
return new \OC_OCS_Result(null, 403, $this->l->t('Public upload disabled by the administrator'));
}

// Public upload can only be set for folders
if ($path instanceof \OCP\Files\File) {
return new \OC_OCS_Result(null, 404, 'public upload is only possible for public shared folders');
return new \OC_OCS_Result(null, 404, $this->l->t('Public upload is only possible for publicly shared folders'));
}

$share->setPermissions(
Expand All @@ -336,19 +341,19 @@ public function createShare() {
$expireDate = $this->parseDate($expireDate);
$share->setExpirationDate($expireDate);
} catch (\Exception $e) {
return new \OC_OCS_Result(null, 404, 'Invalid Date. Format must be YYYY-MM-DD.');
return new \OC_OCS_Result(null, 404, $this->l->t('Invalid date, date format must be YYYY-MM-DD'));
}
}

} else if ($shareType === \OCP\Share::SHARE_TYPE_REMOTE) {
if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) {
return new \OC_OCS_Result(null, 403, 'Sharing '.$path->getPath().' failed, because the backend does not allow shares from type '.$shareType);
return new \OC_OCS_Result(null, 403, $this->l->t('Sharing %s failed because the back end does not allow shares from type %s', [$path->getPath(), $shareType]));
}

$share->setSharedWith($shareWith);
$share->setPermissions($permissions);
} else {
return new \OC_OCS_Result(null, 400, "unknown share type");
return new \OC_OCS_Result(null, 400, $this->l->t('Unknown share type'));
}

$share->setShareType($shareType);
Expand Down Expand Up @@ -397,7 +402,7 @@ private function getSharedWithMe($node = null) {
*/
private function getSharesInDir($folder) {
if (!($folder instanceof \OCP\Files\Folder)) {
return new \OC_OCS_Result(null, 400, "not a directory");
return new \OC_OCS_Result(null, 400, $this->l->t('Not a directory'));
}

$nodes = $folder->getDirectoryListing();
Expand Down Expand Up @@ -450,7 +455,7 @@ public function getShares() {
try {
$path = $userFolder->get($path);
} catch (\OCP\Files\NotFoundException $e) {
return new \OC_OCS_Result(null, 404, 'wrong path, file/folder doesn\'t exist');
return new \OC_OCS_Result(null, 404, $this->l->t('Wrong path, file/folder doesn\'t exist'));
}
}

Expand Down Expand Up @@ -498,17 +503,17 @@ public function getShares() {
*/
public function updateShare($id) {
if (!$this->shareManager->shareApiEnabled()) {
return new \OC_OCS_Result(null, 404, 'Share API is disabled');
return new \OC_OCS_Result(null, 404, $this->l->t('Share API is disabled'));
}

try {
$share = $this->getShareById($id);
} catch (ShareNotFound $e) {
return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.');
return new \OC_OCS_Result(null, 404, $this->l->t('Wrong share ID, share doesn\'t exist'));
}

if (!$this->canAccessShare($share)) {
return new \OC_OCS_Result(null, 404, 'wrong share Id, share doesn\'t exist.');
return new \OC_OCS_Result(null, 404, $this->l->t('Wrong share ID, share doesn\'t exist'));
}

$permissions = $this->request->getParam('permissions', null);
Expand Down Expand Up @@ -538,16 +543,16 @@ public function updateShare($id) {
if ($newPermissions !== null &&
$newPermissions !== \OCP\Constants::PERMISSION_READ &&
$newPermissions !== (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE)) {
return new \OC_OCS_Result(null, 400, 'can\'t change permission for public link share');
return new \OC_OCS_Result(null, 400, $this->l->t('Can\'t change permissions for public share links'));
}

if ($newPermissions === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE)) {
if (!$this->shareManager->shareApiLinkAllowPublicUpload()) {
return new \OC_OCS_Result(null, 403, 'public upload disabled by the administrator');
return new \OC_OCS_Result(null, 403, $this->l->t('Public upload disabled by the administrator'));
}

if (!($share->getNode() instanceof \OCP\Files\Folder)) {
return new \OC_OCS_Result(null, 400, "public upload is only possible for public shared folders");
return new \OC_OCS_Result(null, 400, $this->l->t('Public upload is only possible for publicly shared folders'));
}
}

Expand Down Expand Up @@ -575,7 +580,7 @@ public function updateShare($id) {
} else {
// For other shares only permissions is valid.
if ($permissions === null) {
return new \OC_OCS_Result(null, 400, 'Wrong or no update parameter given');
return new \OC_OCS_Result(null, 400, $this->l->t('Wrong or no update parameter given'));
} else {
$permissions = (int)$permissions;
$share->setPermissions($permissions);
Expand All @@ -594,7 +599,7 @@ public function updateShare($id) {
}

if ($share->getPermissions() & ~$maxPermissions) {
return new \OC_OCS_Result(null, 404, 'Cannot increase permissions');
return new \OC_OCS_Result(null, 404, $this->l->t('Cannot increase permissions'));
}
}
}
Expand Down
15 changes: 11 additions & 4 deletions apps/files_sharing/tests/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,21 @@ private function createRequest(array $data) {
private function createOCS($request, $userId) {
$currentUser = \OC::$server->getUserManager()->get($userId);

$l = $this->getMock('\OCP\IL10N');
$l->method('t')
->will($this->returnCallback(function($text, $parameters = []) {
return vsprintf($text, $parameters);
}));

return new \OCA\Files_Sharing\API\Share20OCS(
$this->shareManager,
\OC::$server->getGroupManager(),
\OC::$server->getUserManager(),
$request,
\OC::$server->getRootFolder(),
\OC::$server->getURLGenerator(),
$currentUser
$currentUser,
$l
);
}

Expand Down Expand Up @@ -665,7 +672,7 @@ function testGetShareFromFolderWithFile() {

$this->assertFalse($result->succeeded());
$this->assertEquals(400, $result->getStatusCode());
$this->assertEquals('not a directory', $result->getMeta()['message']);
$this->assertEquals('Not a directory', $result->getMeta()['message']);

$this->shareManager->deleteShare($share1);
}
Expand Down Expand Up @@ -935,7 +942,7 @@ function testGetShareFromUnknownId() {

$this->assertEquals(404, $result->getStatusCode());
$meta = $result->getMeta();
$this->assertEquals('wrong share ID, share doesn\'t exist.', $meta['message']);
$this->assertEquals('Wrong share ID, share doesn\'t exist', $meta['message']);
}

/**
Expand Down Expand Up @@ -1404,7 +1411,7 @@ public function testPublicLinkExpireDate($date, $valid) {
if ($valid === false) {
$this->assertFalse($result->succeeded());
$this->assertEquals(404, $result->getStatusCode());
$this->assertEquals('Invalid Date. Format must be YYYY-MM-DD.', $result->getMeta()['message']);
$this->assertEquals('Invalid date, date format must be YYYY-MM-DD', $result->getMeta()['message']);
return;
}

Expand Down
Loading

0 comments on commit 5e9b618

Please sign in to comment.