Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add localusers contacts to core #8951

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,8 @@ Options -Indexes
<IfModule pagespeed_module>
ModPagespeed Off
</IfModule>

ErrorDocument 403 /core/templates/403.php
ErrorDocument 404 /core/templates/404.php
ErrorDocument 403 /core/templates/403.php
ErrorDocument 404 /core/templates/404.php
4 changes: 2 additions & 2 deletions db_structure.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1128,11 +1128,11 @@

<field>
<name>objid</name>
<type>integer</type>
<type>text</type>
<default>0</default>
<notnull>true</notnull>
<unsigned>true</unsigned>
<length>4</length>
<length>255</length>
</field>

<!-- Foreign Key vcategory::id -->
Expand Down
10 changes: 10 additions & 0 deletions lib/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ public static function init() {
self::registerPreviewHooks();
self::registerShareHooks();
self::registerLogRotate();
self::registerAddressBook();

//make sure temporary files are cleaned up
register_shutdown_function(array('OC_Helper', 'cleanTmp'));
Expand Down Expand Up @@ -970,6 +971,15 @@ protected static function tryBasicAuthLogin() {
return true;
}

private static function registerAddressBook(){
if(OC_Config::getValue('installed', false)){
$backend = new \OC\Contacts\Backend\LocaLusers(\OCP\User::getUser());
$addressBookInfo = $backend->getAddressBook(\OCP\User::getUser());
$addressBook = new \OCP\Contacts\AddressBook($backend, $addressBookInfo);
\OCP\Contacts::registerAddressBook($addressBook->getSearchProvider());
}
}

}

if (!function_exists('get_temp_dir')) {
Expand Down
226 changes: 226 additions & 0 deletions lib/private/contacts/backend/localusers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
<?php
/**
* ownCloud - ownCloud users backend for Contacts
*
* @author Tobia De Koninck
* @copyright 2014 Tobia De Koninck ([email protected])
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OC\Contacts\Backend;

use OC\Contacts\LocalUsersAddressbookProvider;

/**
* Contact backend for storing all the ownCloud users in this installation.
* Every user has *1* personal addressbook. The id of this addresbook is the
* userid of the owner.
*/
class LocalUsers {

public $name = 'localusers';

/**
* language object
* @var OC_L10N
*/
public static $l10n;

/**
* Defaults object
* @var OC_Defaults
*/
public static $defaults;

public function __construct($userid) {
self::$l10n = \OCP\Util::getL10N('contacts');
self::$defaults = new \OCP\Defaults();
$this->userid = $userid ? $userid : \OCP\User::getUser();
$this->cache = \OC::$server->getCache();
$this->tagManager = \OC::$server->getTagManager()->load('contact');

}

/**
* {@inheritdoc}
*/
public function getAddressBooksForUser(array $options = array()) {
return array($this->getAddressBook('localusers'));
}

/**
* {@inheritdoc}
* Only 1 addressbook for every user
*/
public function getAddressBook($addressBookId, array $options = array()) {

$this->tagManager->add('LocalUsers');

return array(
'id' => $addressBookId,
'displayname' => (string)self::$l10n->t('On this %s', array(self::$defaults->getName())),
'description' => (string)self::$l10n->t('On this %s', array(self::$defaults->getName())),
'lastmodified' => $this->lastModifiedAddressBook($addressBookId),
'permissions' => \OCP\PERMISSION_READ,
'backend' => $this->name
);
}

/**
* {@inheritdoc}
* There are as many contacts in this addressbook as in this ownCloud installation
*/
public function getContacts($addressBookId, array $options = array()) {
$users = \OCP\User::getUsers();
$contacts = array();
foreach($users as $userid){
$contacts[] = $this->getContact($addressBookId, $userid);
}
return $contacts;
}

/**
* {@inheritdoc}
* If your username is "admin" and you want to retrieve your own contact
* the params would be: $addressbookid = 'admin'; $id = 'admin';
* If your username is 'foo' and you want to retrieve the contact with
* ownCloud username 'bar' the params would be: $addressbookid = 'foo'; $id = 'bar';
*/
public function getContact($addressBookId, $id, array $options = array()) {
$contact = array();
$contact['permissions'] = \OCP\PERMISSION_READ;
$contact['id'] = $id;
$contact['addressbookid'] = $addressBookId;
$contact['displayname'] = \OCP\User::getDisplayname($id);
$contact['carddata'] = $this->carddata($id)->serialize();
$contact['lastmodified'] = time();
return $contact;
}

/**
* This is a hack so backends can have different search functions.
* @return \OC\Contacts\LocalUsersAddressbookProvider
*/
public function getSearchProvider($addressBook) {
return new LocalUsersAddressbookProvider($addressBook);
}

public function lastModifiedAddressBook($addressBookId) {
return time();
}

private function carddata($id){
$vcard = \Sabre\VObject\Component::create('VCARD');
$vcard->FN = \OCP\User::getDisplayName($user);
$now = new \DateTime('now');
$vcard->REV = $now->format(\DateTime::W3C);

$appinfo = \OCP\App::getAppInfo('contacts');
$prodid = '-//ownCloud//NONSGML ' . $appinfo['name'] . ' ' . $appversion.'//EN';
$vcard->PRODID = $prodid;
$vcard->add('IMPP', 'x-owncloud-handle:' . $id, array("X-SERVICE-TYPE" => array("owncloud-handle")));

// AVATAR
$localPath = \OCP\Config::getSystemValue('datadirectory') . '/' . $id . '/avatar.';
if (file_exists($localPath . 'png')){
$localPath .= 'png';
$photoExists = true;
} else if (file_exists($localPath . 'jpg')){
$localPath .= 'jpg';
$photoExists = true;
}

if($photoExists){
$image = new \OCP\Image();
$image->loadFromFile($localPath);
$vcard->add('PHOTO', $image->__toString(), array('ENCODING' => 'b','TYPE' => $image->mimeType()));
}
// END AVATAR

// LOCALUSERS GROUP
if(!isset($vcard->CATEGORIES)) {
$vcard->add('CATEGORIES');
}
$vcard->CATEGORIES->addGroup('LocalUsers');
$this->tagManager->tagAs($id, 'LocalUsers');
// END LOCLAUSERS GROUP

return $vcard;

}

/**
* ###########
* Everything below is copied from abstractbackend
* ###########
*/

public function isActive($addressBookId = null) {
return true; // TODO add check if localUsers is active
}

/**
* @brief Check if backend implements action for contacts
* @param $actions bitwise-or'ed actions
* @returns boolean
*
* Returns the supported actions as int to be
* compared with \OCP\PERMISSION_CREATE etc.
*/
public function hasContactMethodFor($permission) {

return (bool)($this->getContactPermissions() & $permission);

}

/**
* @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
* compared with \OCP\PERMISSION_CREATE etc.
*/
protected function getContactPermissions() {
$permissions = 0;

foreach ($this->possibleContactPermissions as $permission => $methodName) {
if(method_exists($this, $methodName)) {
$permissions |= $permission;
}

}

//\OCP\Util::writeLog('contacts', __METHOD__.', permissions' . $permissions, \OCP\Util::DEBUG);
return $permissions;
}

protected $possibleContactPermissions = array(
\OCP\PERMISSION_CREATE => 'createContact',
\OCP\PERMISSION_READ => 'getContact',
\OCP\PERMISSION_UPDATE => 'updateContact',
\OCP\PERMISSION_DELETE => 'deleteContact',
);

protected $possibleAddressBookPermissions = array(
\OCP\PERMISSION_CREATE => 'createAddressBook',
\OCP\PERMISSION_READ => 'getAddressBook',
\OCP\PERMISSION_UPDATE => 'updateAddressBook',
\OCP\PERMISSION_DELETE => 'deleteAddressBook',
);



}
Loading