1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2025-02-07 01:54:16 +01:00
This commit is contained in:
babelouest 2014-05-09 13:54:45 -04:00
parent f699812f69
commit 5e95953799
3 changed files with 66 additions and 78 deletions

View File

@ -40,7 +40,7 @@ $principalBackend = new OC_Connector_Sabre_Principal();
$addressbookbackends = array(); $addressbookbackends = array();
$addressbookbackends[] = new OCA\Contacts\Backend\Database(\OCP\User::getUser()); $addressbookbackends[] = new OCA\Contacts\Backend\Database(\OCP\User::getUser());
$carddavBackend = new OCA\Contacts\CardDAV\Backend(array('local', 'shared')); $carddavBackend = new OCA\Contacts\CardDAV\Backend(array('local', 'shared', 'ldap'));
$requestBackend = new OC_Connector_Sabre_Request(); $requestBackend = new OC_Connector_Sabre_Request();
// Root nodes // Root nodes

View File

@ -36,9 +36,7 @@ abstract class AbstractBackend {
* @method array|null getAddressBook(string $addressbookid, array $options = array()) * @method array|null getAddressBook(string $addressbookid, array $options = array())
* @method array getContacts(string $addressbookid, array $options = array()) * @method array getContacts(string $addressbookid, array $options = array())
* @method array|null getContact(string $addressbookid, mixed $id, array $options = array()) * @method array|null getContact(string $addressbookid, mixed $id, array $options = array())
* * The following methods MAY be implemented:
* The following methods MAY be implemented but ONLY if the backend can actually perform the action
* as the existence of the methods is used to determine the backends capabilities:
* @method bool hasAddressBook(string $addressbookid) * @method bool hasAddressBook(string $addressbookid)
* @method bool updateAddressBook(string $addressbookid, array $updates, array $options = array()) * @method bool updateAddressBook(string $addressbookid, array $updates, array $options = array())
* @method string createAddressBook(array $properties, array $options = array()) * @method string createAddressBook(array $properties, array $options = array())
@ -53,26 +51,24 @@ abstract class AbstractBackend {
/** /**
* The name of the backend. * The name of the backend.
*
* @var string * @var string
*/ */
public $name; public $name;
/** /**
* The current user. * The current usert.
*
* @var string * @var string
*/ */
public $userid; public $userid;
protected $possibleContactCapabilities = array( protected $possibleContactPermissions = array(
\OCP\PERMISSION_CREATE => 'createContact', \OCP\PERMISSION_CREATE => 'createContact',
\OCP\PERMISSION_READ => 'getContact', \OCP\PERMISSION_READ => 'getContact',
\OCP\PERMISSION_UPDATE => 'updateContact', \OCP\PERMISSION_UPDATE => 'updateContact',
\OCP\PERMISSION_DELETE => 'deleteContact', \OCP\PERMISSION_DELETE => 'deleteContact',
); );
protected $possibleAddressBookCapabilities = array( protected $possibleAddressBookPermissions = array(
\OCP\PERMISSION_CREATE => 'createAddressBook', \OCP\PERMISSION_CREATE => 'createAddressBook',
\OCP\PERMISSION_READ => 'getAddressBook', \OCP\PERMISSION_READ => 'getAddressBook',
\OCP\PERMISSION_UPDATE => 'updateAddressBook', \OCP\PERMISSION_UPDATE => 'updateAddressBook',
@ -88,17 +84,16 @@ abstract class AbstractBackend {
} }
/** /**
* @brief Get all capabilities for contacts based on what the backend implements. * @brief Get all possible permissions for contacts based on what the backend implements.
* @returns bitwise-or'ed actions
* *
* Returns the supported actions as an int to be * Returns the supported actions as an int to be
* compared with \OCP\PERMISSION_CREATE etc. * compared with \OCP\PERMISSION_CREATE etc.
* @see AbstractBackend::hasContactMethodFor()
* @returns bitwise-or'ed actions
*/ */
protected function getContactCapacilities() { protected function getContactPermissions() {
$permissions = 0; $permissions = 0;
foreach ($this->possibleContactCapabilities as $permission => $methodName) { foreach ($this->possibleContactPermissions as $permission => $methodName) {
if(method_exists($this, $methodName)) { if(method_exists($this, $methodName)) {
$permissions |= $permission; $permissions |= $permission;
} }
@ -110,18 +105,17 @@ abstract class AbstractBackend {
} }
/** /**
* @brief Get all capabilities for address books based on what the backend implements. * @brief Get all permissions for address book based on what the backend implements.
* @returns bitwise-or'ed actions
* *
* Returns the supported actions as int to be * Returns the supported actions as int to be
* compared with \OCP\PERMISSION_CREATE etc. * compared with \OCP\PERMISSION_CREATE etc.
* @see AbstractBackend::hasAddressBookMethodFor()
* @returns bitwise-or'ed actions
*/ */
protected function getAddressBookCapabilities() { protected function getAddressBookPermissions() {
$permissions = 0; $permissions = 0;
foreach ($this->possibleAddressBookCapabilities as $permission => $methodName) { foreach ($this->possibleAddressBookPermissions as $permission => $methodName) {
if (method_exists($this, $methodName)) { if (method_exists($this, $methodName)) {
$permissions |= $permission; $permissions |= $permission;
} }
@ -134,36 +128,34 @@ abstract class AbstractBackend {
/** /**
* @brief Check if backend implements action for contacts * @brief Check if backend implements action for contacts
* * @param $actions bitwise-or'ed actions
* Returns true if the backend supports an action for a given permission
* for example \OCP\PERMISSION_CREATE etc.
*
* @param $permission bitwise-or'ed actions
* @returns boolean * @returns boolean
*
* Returns the supported actions as int to be
* compared with \OCP\PERMISSION_CREATE etc.
*/ */
public function hasContactMethodFor($permission) { public function hasContactMethodFor($permission) {
return (bool)($this->getContactCapacilities() & $permission); return (bool)($this->getContactPermissions() & $permission);
} }
/** /**
* @brief Check if backend implements action for contacts * @brief Check if backend implements action for contacts
* * @param $actions bitwise-or'ed actions
* Returns true if the backend supports an action for a given permission
* for example \OCP\PERMISSION_CREATE etc.
*
* @param $permission bitwise-or'ed actions
* @returns boolean * @returns boolean
*
* Returns the supported actions as int to be
* compared with \OCP\PERMISSION_CREATE etc.
*/ */
public function hasAddressBookMethodFor($permission) { public function hasAddressBookMethodFor($permission) {
return (bool)($this->getAddressBookCapabilities() & $permission); return (bool)($this->getAddressBookPermissions() & $permission);
} }
/** /**
* @brief Check if the backend has the address book * Check if the backend has the address book
* *
* This can be reimplemented in the backend to improve performance. * This can be reimplemented in the backend to improve performance.
* *
@ -177,8 +169,7 @@ abstract class AbstractBackend {
} }
/** /**
* @brief Returns the number of contacts in an address book. * Returns the number of contacts in an address book.
*
* Implementations can choose to override this method to either * Implementations can choose to override this method to either
* get the result more effectively or to return null if the backend * get the result more effectively or to return null if the backend
* cannot determine the number. * cannot determine the number.
@ -193,13 +184,12 @@ abstract class AbstractBackend {
} }
/** /**
* @brief Returns the list of addressbooks for a specific user. * Returns the list of addressbooks for a specific user.
* *
* The returned arrays MUST contain a unique 'id' for the * The returned arrays MUST contain a unique 'id' for the
* backend a string 'displayname' and an integer * backend and a 'displayname', and it MAY contain a
* 'permissions', and it MAY contain a string 'description'. * 'description'.
* *
* @see AbstractBackend::getAddressBook()
* @param array $options - Optional (backend specific options) * @param array $options - Optional (backend specific options)
* @return array * @return array
*/ */
@ -211,8 +201,8 @@ abstract class AbstractBackend {
* The returned array MUST contain string: 'displayname',string: 'backend' * The returned array MUST contain string: 'displayname',string: 'backend'
* and integer: 'permissions' value using there ownCloud CRUDS constants * and integer: 'permissions' value using there ownCloud CRUDS constants
* (which MUST be at least \OCP\PERMISSION_READ). * (which MUST be at least \OCP\PERMISSION_READ).
* The backend MAY also add the optional entries 'description' and 'owner', * Currently the only ones supported are 'displayname' and
* and can add additional entries if needed internally. * 'description', but backends can implement additional.
* *
* @param string $addressBookId * @param string $addressBookId
* @param array $options - Optional (backend specific options) * @param array $options - Optional (backend specific options)
@ -225,10 +215,9 @@ abstract class AbstractBackend {
* *
* The $properties array contains the changes to be made. * The $properties array contains the changes to be made.
* *
* The backend MUST NOT implement this method if it doesn't support updating, * Currently the only ones supported are 'displayname' and
* and the implementation is commented out here intentionally! * 'description', but backends can implement additional.
* *
* @see AbstractBackend::getAddressBook()
* @param string $addressBookId * @param string $addressBookId
* @param array $properties * @param array $properties
* @param array $options - Optional (backend specific options) * @param array $options - Optional (backend specific options)
@ -239,7 +228,7 @@ abstract class AbstractBackend {
/** /**
* Creates a new address book * Creates a new address book
* *
* Backends that doesn't support adding address books MUST NOT implement this method. * Classes that doesn't support adding address books MUST NOT implement this method.
* *
* Currently the only ones supported are 'displayname' and * Currently the only ones supported are 'displayname' and
* 'description', but backends can implement additional. * 'description', but backends can implement additional.
@ -254,7 +243,7 @@ abstract class AbstractBackend {
/** /**
* Deletes an entire address book and all its contents * Deletes an entire address book and all its contents
* *
* Backends that doesn't support deleting address books MUST NOT implement this method. * Classes that doesn't support deleting address books MUST NOT implement this method.
* *
* @param string $addressBookId * @param string $addressBookId
* @param array $options - Optional (backend specific options) * @param array $options - Optional (backend specific options)
@ -290,18 +279,11 @@ abstract class AbstractBackend {
* Returns all contacts for a specific addressbook id. * Returns all contacts for a specific addressbook id.
* *
* The returned array MUST contain the unique ID a string value 'id', a string * The returned array MUST contain the unique ID a string value 'id', a string
* value 'displayname' and an integer 'permissions' value using there * value 'displayname', a string value 'owner' and an integer 'permissions' value using there
* ownCloud CRUDS constants (which MUST be at least \OCP\PERMISSION_READ), and SHOULD * ownCloud CRUDS constants (which MUST be at least \OCP\PERMISSION_READ), and SHOULD
* contain the properties of the contact formatted as a vCard 3.0 * contain the properties of the contact formatted as a vCard 3.0
* https://tools.ietf.org/html/rfc2426 mapped to 'carddata' or as an * https://tools.ietf.org/html/rfc2426 mapped to 'carddata' or as an
* \OCA\Contacts\VObject\VCard object mapped to 'vcard'. * \OCA\Contacts\VObject\VCard object mapped to 'vcard'.
* If the backend supports different ownerships it can add an 'owner' entry.
*
* NOTE: To improve performance $options can contain the boolean value 'omitdata'
* that if true indicates that the backend SHOULD NOT add 'vcard' or 'carddata'.
*
* NOTE: If the backend doesn't support fetching in batches 'offset' and 'limit'
* MUST be ignored and all contacts MUST be returned.
* *
* Example: * Example:
* *
@ -310,6 +292,9 @@ abstract class AbstractBackend {
* 1 => array('id' => 'bbcca2d1535', 'owner' => 'bar', 'permissions' => 32, 'displayname' => 'Jane Doe', 'carddata' => $data) * 1 => array('id' => 'bbcca2d1535', 'owner' => 'bar', 'permissions' => 32, 'displayname' => 'Jane Doe', 'carddata' => $data)
* ); * );
* *
* For contacts that contain loads of data, the 'carddata' or 'vcard' MAY be omitted
* as it can be fetched later.
*
* The following options are supported in the $options array: * The following options are supported in the $options array:
* *
* - 'limit': An integer value for the number of contacts to fetch in each call. * - 'limit': An integer value for the number of contacts to fetch in each call.
@ -343,7 +328,7 @@ abstract class AbstractBackend {
* @param VCard $contact * @param VCard $contact
* @param array $options - Optional options * @param array $options - Optional options
* @return string|bool The identifier for the new contact or false on error. * @return string|bool The identifier for the new contact or false on error.
public function createContact($addressBookId, $contact, array $options = array()); public function createContact($addressbookid, $contact, array $options = array());
*/ */
/** /**
@ -356,7 +341,7 @@ abstract class AbstractBackend {
* @param VCard $contact * @param VCard $contact
* @param array $options - Optional options * @param array $options - Optional options
* @return bool * @return bool
public function updateContact($addressBookId, $id, $carddata, array $options = array()); public function updateContact($addressbookid, $id, $carddata, array $options = array());
*/ */
/** /**
@ -368,7 +353,7 @@ abstract class AbstractBackend {
* @param mixed $id * @param mixed $id
* @param array $options - Optional options * @param array $options - Optional options
* @return bool * @return bool
public function deleteContact($addressBookId, $id, array $options = array()); public function deleteContact($addressbookid, $id, array $options = array());
*/ */
/** /**
@ -449,11 +434,10 @@ abstract class AbstractBackend {
*/ */
public function getPreferences($addressBookId) { public function getPreferences($addressBookId) {
$key = $this->combinedKey($addressBookId); $key = 'prefs_' . $this->combinedKey($addressBookId);
$key = 'prefs_' . $key;
$data = \OCP\Config::getUserValue($this->userid, 'contacts', $key, false); $data = \OCP\Config::getUserValue($this->userid, 'contacts', $key, false);
return $data ? json_decode($data) : array(); return $data ? json_decode($data, true) : array();
} }
/** /**
@ -462,10 +446,8 @@ abstract class AbstractBackend {
* @param array the preferences, format array('param1' => 'value', 'param2' => 'value') * @param array the preferences, format array('param1' => 'value', 'param2' => 'value')
* @return boolean * @return boolean
*/ */
public function setPreferences($addressBookId, array $params) { public function setPreferences($addressbookid, array $params) {
$key = 'prefs_' . $this->combinedKey($addressbookid);
$key = $this->combinedKey($addressBookId);
$key = 'prefs_' . $key;
$data = json_encode($params); $data = json_encode($params);
return $data return $data
@ -473,7 +455,11 @@ abstract class AbstractBackend {
: false; : false;
} }
public function getSearchProvider($addressbook) { public function removePreferences($addressbookid) {
} $key = $this->combinedKey($addressbookid);
$key = 'prefs_' . $key;
\OC_Preferences::deleteKey( $this->userid, 'contacts', $key );
}
} }

View File

@ -257,27 +257,28 @@ class Ldap extends AbstractBackend {
*/ */
public function getAddressBooksForUser(array $options = array()) { public function getAddressBooksForUser(array $options = array()) {
try { /*try {
if(!isset(self::$preparedQueries['addressbooksforuser'])) { if(!isset(self::$preparedQueries['addressbooksforuser'])) {
$sql = 'SELECT `configkey` from *PREFIX*preferences where `configkey` like ?'; $sql = 'SELECT `configkey` from *PREFIX*preferences where `configkey` like ?';
$configkeyPrefix = $this->name . "_%_uri"; $configkeyPrefix = $this->name . "_%_uri";
self::$preparedQueries['addressbooksforuser'] = \OCP\DB::prepare($sql); self::$preparedQueries['addressbooksforuser'] = \OCP\DB::prepare($sql);
error_log("ca farte ? ".$sql." ".$configkeyPrefix);
$result = self::$preparedQueries['addressbooksforuser']->execute(array($configkeyPrefix)); $result = self::$preparedQueries['addressbooksforuser']->execute(array($configkeyPrefix));
if (\OC_DB::isError($result)) { if (\OC_DB::isError($result)) {
\OCP\Util::write('contacts', __METHOD__. 'DB error: ' . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); \OCP\Util::write('contacts', __METHOD__. 'DB error: ' . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
return $this->addressbooks; return $this->addressbooks;
} }
$this->addressbooks = array();
while($row = $result->fetchRow()) {
$id = str_replace("_uri", "", str_replace($this->name."_", "", $row['configkey']));
$this->addressbooks[] = self::getAddressBook($id);
}
return $this->addressbooks;
} }
} catch(\Exception $e) { } catch(\Exception $e) {
\OC_Log::write('contacts', __METHOD__.' exception: ' . $e->getMessage(), \OCP\Util::ERROR); \OC_Log::write('contacts', __METHOD__.' exception: ' . $e->getMessage(), \OCP\Util::ERROR);
return $this->addressbooks; return $this->addressbooks;
}*/
$addressbookidList = $this->getAddressbookList();
$this->addressbooks = array();
foreach($addressbookidList as $addressbookid) {
$this->addressbooks[] = self::getAddressBook($addressbookid);
} }
return $this->addressbooks;
} }
@ -304,11 +305,11 @@ class Ldap extends AbstractBackend {
$preferences = self::getPreferences($addressbookid); $preferences = self::getPreferences($addressbookid);
if ($preferences != false) { if ($preferences != false) {
$current = array(); $current = array();
$current['id'] = $addressbookid; $current['id'] = (string)$addressbookid;
$current['displayname'] = $preferences['displayname']; $current['displayname'] = (string)$preferences['displayname'];
$current['description'] = $preferences['description']; $current['description'] = (string)$preferences['description'];
$current['owner'] = $this->userid; $current['owner'] = $this->userid;
$current['uri'] = $preferences['uri']; $current['uri'] = (string)$preferences['uri'];
$current['permissions'] = \OCP\PERMISSION_ALL; $current['permissions'] = \OCP\PERMISSION_ALL;
$current['lastmodified'] = self::lastModifiedAddressBook($addressbookid); $current['lastmodified'] = self::lastModifiedAddressBook($addressbookid);
return $current; return $current;
@ -431,7 +432,7 @@ class Ldap extends AbstractBackend {
//OCP\Util::writeLog('contacts_ldap', __METHOD__.' Connector OK', \OC_Log::DEBUG); //OCP\Util::writeLog('contacts_ldap', __METHOD__.' Connector OK', \OC_Log::DEBUG);
$info = self::ldapFindMultiple( $info = self::ldapFindMultiple(
$this->ldapParams['ldapbasednsearch'], $this->ldapParams['ldapbasednsearch'],
'(objectclass=person)', $this->ldapParams['ldapfilter'],
$this->connector->getLdapEntries(), $this->connector->getLdapEntries(),
isset($options['offset']) ? $options['offset'] : null, isset($options['offset']) ? $options['offset'] : null,
isset($options['limit']) ? $options['limit'] : null isset($options['limit']) ? $options['limit'] : null
@ -583,6 +584,7 @@ class Ldap extends AbstractBackend {
* @return bool * @return bool
*/ */
public function updateContact($addressbookid, $id, $carddata, array $options = array()) { public function updateContact($addressbookid, $id, $carddata, array $options = array()) {
error_log("goat power ! $addressbookid, $id, $carddata");
$vcard = \Sabre\VObject\Reader::read($carddata); $vcard = \Sabre\VObject\Reader::read($carddata);
if (!is_array($id)) { if (!is_array($id)) {