diff options
author | Michael Gapczynski <mtgap@owncloud.com> | 2012-08-01 17:52:29 -0400 |
---|---|---|
committer | Michael Gapczynski <mtgap@owncloud.com> | 2012-08-01 17:52:29 -0400 |
commit | 4933128850879f0e0590746b6742dafcf857e113 (patch) | |
tree | 9932c4f25e97d7fcc2f8f7165644212565a0247d | |
parent | 2201074e1f1e4ec3c2f60cac74d8168738d83650 (diff) | |
download | nextcloud-server-4933128850879f0e0590746b6742dafcf857e113.tar.gz nextcloud-server-4933128850879f0e0590746b6742dafcf857e113.zip |
Throw exceptions in share API for UI to display
-rw-r--r-- | core/ajax/share.php | 10 | ||||
-rw-r--r-- | core/js/share.js | 2 | ||||
-rw-r--r-- | lib/public/share.php | 43 |
3 files changed, 34 insertions, 21 deletions
diff --git a/core/ajax/share.php b/core/ajax/share.php index 6f5b927c779..f7a41aa3a4a 100644 --- a/core/ajax/share.php +++ b/core/ajax/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': diff --git a/core/js/share.js b/core/js/share.js index 5832acfd454..a3fe0f69732 100644 --- a/core/js/share.js +++ b/core/js/share.js @@ -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'); } }); }, diff --git a/lib/public/share.php b/lib/public/share.php index d9bcb8df81f..3cc755866e3 100644 --- a/lib/public/share.php +++ b/lib/public/share.php @@ -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) { |