Throw exceptions in share API for UI to display

remotes/origin/stable45
Michael Gapczynski 13 years ago
parent 2201074e1f
commit 4933128850
  1. 10
      core/ajax/share.php
  2. 2
      core/js/share.js
  3. 43
      lib/public/share.php

@ -25,9 +25,13 @@ if (isset($_POST['action']) && isset($_POST['itemType']) && isset($_POST['item']
switch ($_POST['action']) {
case 'share':
if (isset($_POST['shareType']) && isset($_POST['shareWith']) && isset($_POST['permissions'])) {
$return = OCP\Share::share($_POST['itemType'], $_POST['item'], $_POST['shareType'], $_POST['shareWith'], $_POST['permissions']);
// TODO May need to return private link
($return) ? OC_JSON::success() : OC_JSON::error();
try {
OCP\Share::share($_POST['itemType'], $_POST['item'], $_POST['shareType'], $_POST['shareWith'], $_POST['permissions']);
// TODO May need to return private link
OC_JSON::success();
} catch (Exception $exception) {
OC_JSON::error(array('data' => array('message' => $exception->getMessage())));
}
}
break;
case 'unshare':

@ -47,7 +47,7 @@ OC.Share={
callback(result.data);
}
} else {
OC.dialogs.alert('Error', 'Error while sharing');
OC.dialogs.alert(result.data.message, 'Error while sharing');
}
});
},

@ -156,27 +156,32 @@ class Share {
switch ($shareType) {
case self::SHARE_TYPE_USER:
if ($shareWith == $uidOwner) {
\OC_Log::write('OCP\Share', 'Sharing '.$item.' failed, because the user '.$shareWith.' is the item owner', \OC_Log::ERROR);
return false;
$message = 'Sharing '.$item.' failed, because the user '.$shareWith.' is the item owner';
\OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
throw new \Exception($message);
}
if (!\OC_User::userExists($shareWith)) {
\OC_Log::write('OCP\Share', 'Sharing '.$item.' failed, because the user '.$shareWith.' does not exist', \OC_Log::ERROR);
return false;
$message = 'Sharing '.$item.' failed, because the user '.$shareWith.' does not exist';
\OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
throw new \Exception($message);
} else {
$inGroup = array_intersect(\OC_Group::getUserGroups($uidOwner), \OC_Group::getUserGroups($shareWith));
if (empty($inGroup)) {
\OC_Log::write('OCP\Share', 'Sharing '.$item.' failed, because the user '.$shareWith.' is not a member of any groups that '.$uidOwner.' is a member of', \OC_Log::ERROR);
return false;
$message = 'Sharing '.$item.' failed, because the user '.$shareWith.' is not a member of any groups that '.$uidOwner.' is a member of';
\OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
throw new \Exception($message);
}
}
break;
case self::SHARE_TYPE_GROUP:
if (!\OC_Group::groupExists($shareWith)) {
\OC_Log::write('OCP\Share', 'Sharing '.$item.' failed, because the group '.$shareWith.' does not exist', \OC_Log::ERROR);
return false;
$message = 'Sharing '.$item.' failed, because the group '.$shareWith.' does not exist';
\OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
throw new \Exception($message);
} else if (!\OC_Group::inGroup($uidOwner, $shareWith)) {
\OC_Log::write('OCP\Share', 'Sharing '.$item.' failed, because '.$uidOwner.' is not a member of the group '.$shareWith, \OC_Log::ERROR);
return false;
$message = 'Sharing '.$item.' failed, because '.$uidOwner.' is not a member of the group '.$shareWith;
\OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
throw new \Exception($message);
}
// Convert share with into an array with the keys group and users
$group = $shareWith;
@ -189,19 +194,22 @@ class Share {
return self::put($itemType, $item, $shareType, $shareWith, $uidOwner, $permissions);
case self::SHARE_TYPE_CONTACT:
if (!\OC_App::isEnabled('contacts')) {
\OC_Log::write('OCP\Share', 'Sharing '.$item.' failed, because the contacts app is not enabled', \OC_Log::ERROR);
$message = 'Sharing '.$item.' failed, because the contacts app is not enabled';
\OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
return false;
}
$vcard = \OC_Contacts_App::getContactVCard($shareWith);
if (!isset($vcard)) {
\OC_Log::write('OCP\Share', 'Sharing '.$item.' failed, because the contact does not exist', \OC_Log::ERROR);
return false;
$message = 'Sharing '.$item.' failed, because the contact does not exist';
\OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
throw new \Exception($message);
}
$details = OC_Contacts_VCard::structureContact($vcard);
// TODO Add ownCloud user to contacts vcard
if (!isset($details['EMAIL'])) {
\OC_Log::write('OCP\Share', 'Sharing '.$item.' failed, because no email address is associated with the contact', \OC_Log::ERROR);
return false;
$message = 'Sharing '.$item.' failed, because no email address is associated with the contact';
\OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
throw new \Exception($message);
}
return self::share($itemType, $item, self::SHARE_TYPE_EMAIL, $permissions);
break;
@ -211,8 +219,9 @@ class Share {
return false;
}
if (self::getItems($itemType, $item, $shareType, $shareWith, $uidOwner, self::FORMAT_NONE, null, 1)) {
\OC_Log::write('OCP\Share', 'Sharing '.$item.' failed, because this item is already shared with '.$shareWith, \OC_Log::ERROR);
return false;
$message = 'Sharing '.$item.' failed, because this item is already shared with '.$shareWith;
\OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
throw new \Exception($message);
}
if ($collectionTypes = self::getCollectionItemTypes($itemType)) {
foreach ($collectionTypes as $collectionType) {

Loading…
Cancel
Save