mirror of
https://github.com/owncloudarchive/contacts.git
synced 2024-12-01 13:24:10 +01:00
Error and permission checking.
This commit is contained in:
parent
51940b01a7
commit
5905e6c314
@ -30,7 +30,11 @@ $id = $_POST['id'];
|
||||
if(!$id) {
|
||||
bailOut(OC_Contacts_App::$l10n->t('id is not set.'));
|
||||
}
|
||||
OC_Contacts_App::getAddressbook( $id ); // is owner access check
|
||||
|
||||
OC_Contacts_Addressbook::delete($id);
|
||||
try {
|
||||
OC_Contacts_Addressbook::delete($id);
|
||||
} catch(Exception $e) {
|
||||
bailOut($e->getMessage());
|
||||
}
|
||||
|
||||
OCP\JSON::success(array('data' => array( 'id' => $id )));
|
||||
|
@ -27,7 +27,7 @@ OCP\JSON::callCheck();
|
||||
|
||||
require_once __DIR__.'/../loghandler.php';
|
||||
|
||||
$id = isset($_POST['id'])?$_POST['id']:null;
|
||||
$id = isset($_POST['id']) ? $_POST['id'] : null;
|
||||
if(!$id) {
|
||||
bailOut(OC_Contacts_App::$l10n->t('id is not set.'));
|
||||
}
|
||||
@ -35,10 +35,7 @@ if(!$id) {
|
||||
try {
|
||||
OC_Contacts_VCard::delete($id);
|
||||
} catch(Exception $e) {
|
||||
$msg = $e->getMessage();
|
||||
OCP\Util::writeLog('contacts', __METHOD__.', exception: '.$msg,
|
||||
OCP\Util::DEBUG);
|
||||
OCP\Util::writeLog('contacts', __METHOD__.', id'.$id, OCP\Util::DEBUG);
|
||||
bailOut($msg);
|
||||
bailOut($e->getMessage());
|
||||
}
|
||||
|
||||
OCP\JSON::success(array('data' => array( 'id' => $id )));
|
||||
|
@ -13,7 +13,7 @@ OCP\User::checkLoggedIn();
|
||||
OCP\App::checkAppEnabled('contacts');
|
||||
|
||||
// Get active address books. This creates a default one if none exists.
|
||||
$ids = OC_Contacts_Addressbook::all(OCP\USER::getUser());
|
||||
$ids = OC_Contacts_Addressbook::activeIds(OCP\USER::getUser());
|
||||
$has_contacts = (count(OC_Contacts_VCard::all($ids, 0, 1)) > 0
|
||||
? true
|
||||
: false); // just to check if there are any contacts.
|
||||
|
@ -81,10 +81,14 @@ class OC_Contacts_Addressbook {
|
||||
if(is_null($uid)) {
|
||||
$uid = OCP\USER::getUser();
|
||||
}
|
||||
$activeaddressbooks = self::all($uid, true);
|
||||
|
||||
// query all addressbooks to force creation of default if it desn't exist.
|
||||
$activeaddressbooks = self::all($uid);
|
||||
$ids = array();
|
||||
foreach($activeaddressbooks as $addressbook) {
|
||||
$ids[] = $addressbook['id'];
|
||||
if($addressbook['active']) {
|
||||
$ids[] = $addressbook['id'];
|
||||
}
|
||||
}
|
||||
return $ids;
|
||||
}
|
||||
@ -117,13 +121,11 @@ class OC_Contacts_Addressbook {
|
||||
try {
|
||||
$stmt = OCP\DB::prepare( 'SELECT * FROM `*PREFIX*contacts_addressbooks` WHERE `id` = ?' );
|
||||
$result = $stmt->execute(array($id));
|
||||
return $result->fetchRow();
|
||||
} catch(Exception $e) {
|
||||
OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.', exception: '.$e->getMessage(), OCP\Util::ERROR);
|
||||
OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.', id: '.$id, OCP\Util::DEBUG);
|
||||
return false;
|
||||
}
|
||||
|
||||
return $result->fetchRow();
|
||||
}
|
||||
|
||||
@ -274,30 +276,53 @@ class OC_Contacts_Addressbook {
|
||||
/**
|
||||
* @brief removes an address book
|
||||
* @param integer $id
|
||||
* @return boolean
|
||||
* @return boolean true on success, otherwise an exception will be thrown
|
||||
*/
|
||||
public static function delete($id) {
|
||||
$addressbook = self::find($id);
|
||||
if ($addressbook['userid'] != OCP\User::getUser()) {
|
||||
$sharedAddressbook = OCP\Share::getItemSharedWithBySource('addressbook', $id);
|
||||
if (!$sharedAddressbook || !($sharedAddressbook['permissions'] & OCP\Share::PERMISSION_DELETE)) {
|
||||
return false;
|
||||
throw new Exception(
|
||||
OC_Contacts_App::$l10n->t(
|
||||
'You do not have the permissions to delete this addressbook.'
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
self::setActive($id, false);
|
||||
try {
|
||||
$stmt = OCP\DB::prepare( 'DELETE FROM `*PREFIX*contacts_addressbooks` WHERE `id` = ?' );
|
||||
$stmt->execute(array($id));
|
||||
} catch(Exception $e) {
|
||||
OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.', exception for '.$id.': '.$e->getMessage(), OCP\Util::ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
// First delete cards belonging to this addressbook.
|
||||
$cards = OC_Contacts_VCard::all($id);
|
||||
foreach($cards as $card){
|
||||
OC_Contacts_VCard::delete($card['id']);
|
||||
try {
|
||||
OC_Contacts_VCard::delete($card['id']);
|
||||
} catch(Exception $e) {
|
||||
OCP\Util::writeLog('contacts',
|
||||
__METHOD__.', exception deleting vCard '.$card['id'].': '
|
||||
. $e->getMessage(),
|
||||
OCP\Util::ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
$stmt = OCP\DB::prepare('DELETE FROM `*PREFIX*contacts_addressbooks` WHERE `id` = ?');
|
||||
$stmt->execute(array($id));
|
||||
} catch(Exception $e) {
|
||||
OCP\Util::writeLog('contacts',
|
||||
__METHOD__.', exception for ' . $id . ': '
|
||||
. $e->getMessage(),
|
||||
OCP\Util::ERROR);
|
||||
throw new Exception(
|
||||
OC_Contacts_App::$l10n->t(
|
||||
'There was an error deleting this addressbook.'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if(count(self::all(OCP\User::getUser())) == 0) {
|
||||
self::addDefault();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -383,22 +383,35 @@ class OC_Contacts_VCard {
|
||||
* @brief edits a card
|
||||
* @param integer $id id of card
|
||||
* @param OC_VObject $card vCard file
|
||||
* @return boolean
|
||||
* @return boolean true on success, otherwise an exception will be thrown
|
||||
*/
|
||||
public static function edit($id, OC_VObject $card){
|
||||
$oldcard = self::find($id);
|
||||
if (!$oldcard) {
|
||||
return false;
|
||||
OCP\Util::writeLog('contacts', __METHOD__.', id: '
|
||||
. $id . ' not found.', OCP\Util::DEBUG);
|
||||
throw new Exception(
|
||||
OC_Contacts_App::$l10n->t(
|
||||
'Could not find the vCard with ID.' . $id
|
||||
)
|
||||
);
|
||||
}
|
||||
if(is_null($card)) {
|
||||
return false;
|
||||
}
|
||||
// NOTE: Owner checks are being made in the ajax files, which should be done inside the lib files to prevent any redundancies with sharing checks
|
||||
// NOTE: Owner checks are being made in the ajax files, which should be done
|
||||
// inside the lib files to prevent any redundancies with sharing checks
|
||||
$addressbook = OC_Contacts_Addressbook::find($oldcard['addressbookid']);
|
||||
if ($addressbook['userid'] != OCP\User::getUser()) {
|
||||
$sharedContact = OCP\Share::getItemSharedWithBySource('contact', $id, OCP\Share::FORMAT_NONE, null, true);
|
||||
if (!$sharedContact || !($sharedContact['permissions'] & OCP\Share::PERMISSION_UPDATE)) {
|
||||
throw new Exception(OC_Contacts_App::$l10n->t('You do not have the permissions to edit this contact.'));
|
||||
$sharedContact = OCP\Share::getItemSharedWithBySource('contact',
|
||||
$id, OCP\Share::FORMAT_NONE, null, true);
|
||||
if (!$sharedContact
|
||||
|| !($sharedContact['permissions'] & OCP\Share::PERMISSION_UPDATE)) {
|
||||
throw new Exception(
|
||||
OC_Contacts_App::$l10n->t(
|
||||
'You do not have the permissions to edit this contact.'
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
OC_Contacts_App::loadCategoriesFromVCard($card);
|
||||
@ -458,15 +471,32 @@ class OC_Contacts_VCard {
|
||||
/**
|
||||
* @brief deletes a card
|
||||
* @param integer $id id of card
|
||||
* @return boolean
|
||||
* @return boolean true on success, otherwise an exception will be thrown
|
||||
*/
|
||||
public static function delete($id){
|
||||
$card = self::find($id);
|
||||
if (!$card) {
|
||||
return false;
|
||||
OCP\Util::writeLog('contacts', __METHOD__.', id: '
|
||||
. $id . ' not found.', OCP\Util::DEBUG);
|
||||
throw new Exception(
|
||||
OC_Contacts_App::$l10n->t(
|
||||
'Could not find the vCard with ID: ' . $id
|
||||
)
|
||||
);
|
||||
}
|
||||
$addressbook = OC_Contacts_Addressbook::find($card['addressbookid']);
|
||||
if(!$addressbook) {
|
||||
throw new Exception(
|
||||
OC_Contacts_App::$l10n->t(
|
||||
'Could not find the Addressbook with ID: '
|
||||
. $card['addressbookid']
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if ($addressbook['userid'] != OCP\User::getUser()) {
|
||||
OCP\Util::writeLog('contacts', __METHOD__.', '
|
||||
. $addressbook['userid'] . ' != ' . OCP\User::getUser(), OCP\Util::DEBUG);
|
||||
$sharedContact = OCP\Share::getItemSharedWithBySource('contact',
|
||||
$id, OCP\Share::FORMAT_NONE, null, true);
|
||||
if (!$sharedContact
|
||||
@ -489,7 +519,11 @@ class OC_Contacts_VCard {
|
||||
', exception: ' . $e->getMessage(), OCP\Util::ERROR);
|
||||
OCP\Util::writeLog('contacts', __METHOD__.', id: '
|
||||
. $id, OCP\Util::DEBUG);
|
||||
return false;
|
||||
throw new Exception(
|
||||
OC_Contacts_App::$l10n->t(
|
||||
'There was an error deleting this contact.'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
Loading…
Reference in New Issue
Block a user