Skip to content

Commit

Permalink
Merge pull request #1307 from nextcloud/search-users
Browse files Browse the repository at this point in the history
fix #881 - respect autocompletion restrictions from share settings
  • Loading branch information
dartcafe authored Dec 28, 2020
2 parents 419462e + f3b16b9 commit e84ca86
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 134 deletions.
29 changes: 12 additions & 17 deletions lib/Model/Circle.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,23 @@

namespace OCA\Polls\Model;

use OCP\App\IAppManager;
use OCA\Circles\Api\v1\Circles;

use \OCA\Circles\Model\Circle as CirclesCircle;
use OCA\Polls\Exceptions\CirclesNotEnabledException;

class Circle extends UserGroupClass {
public const TYPE = 'circle';
public const ICON = 'icon-circles';

/** @var CirclesCircle */
private $circle;

public function __construct(
$id
) {
parent::__construct($id, self::TYPE);
if (\OC::$server->getAppManager()->isEnabledForUser('circles')) {
if (self::isEnabled()) {
$this->icon = self::ICON;
$this->circle = Circles::detailsCircle($id);
$this->displayName = $this->circle->getName();
Expand All @@ -49,26 +51,19 @@ public function __construct(
}

public static function isEnabled(): bool {
return \OC::$server->getAppManager()->isEnabledForUser('circles');
}

public static function listRaw(string $query = '') {
$circles = [];
if (\OC::$server->getAppManager()->isEnabledForUser('circles')) {
$circles = Circles::listCircles(\OCA\Circles\Model\Circle::CIRCLES_ALL, $query);
}

return $circles;
return self::getContainer()->query(IAppManager::class)->isEnabledForUser('circles');
}

/**
* @return Circle[]
*/
public static function search(string $query = '', $skip = []) {
public static function search(string $query = '', $skip = []): array {
$circles = [];
foreach (self::listRaw($query) as $circle) {
if (!in_array($circle->getUniqueId(), $skip)) {
$circles[] = new self($circle->getUniqueId());
if (self::isEnabled()) {
foreach (Circles::listCircles(CirclesCircle::CIRCLES_ALL, $query) as $circle) {
if (!in_array($circle->getUniqueId(), $skip)) {
$circles[] = new self($circle->getUniqueId());
}
}
}

Expand All @@ -80,7 +75,7 @@ public static function search(string $query = '', $skip = []) {
*/
public function getMembers() {
$members = [];
if (\OC::$server->getAppManager()->isEnabledForUser('circles')) {
if (self::isEnabled()) {
foreach (Circles::detailsCircle($this->id)->getMembers() as $circleMember) {
if ($circleMember->getType() === Circles::TYPE_USER) {
$members[] = new User($circleMember->getUserId());
Expand Down
70 changes: 37 additions & 33 deletions lib/Model/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

namespace OCA\Polls\Model;

use OCP\App\IAppManager;
use OCP\Contacts\IManager as IContactsManager;
use OCA\Polls\Exceptions\MultipleContactsFound;
use OCA\Polls\Exceptions\ContactsNotEnabledExceptions;

Expand All @@ -37,10 +39,15 @@ public function __construct(
$id
) {
parent::__construct($id, self::TYPE);
$this->icon = self::ICON;
$this->getContact();
if (self::isEnabled()) {
$this->icon = self::ICON;
$this->getContact();
} else {
throw new ContactsNotEnabledExceptions();
}
}


/**
* * must use displayName for contact's user id, because contact id
* * is not accessable outside the owners's scope
Expand All @@ -51,10 +58,6 @@ public function getPublicId(): string {
return $this->displayName;
}

public static function isEnabled(): bool {
return \OC::$server->getAppManager()->isEnabledForUser('contacts');
}

/**
* We just need the contact's UID, so make sure, the any prefix is removed
*/
Expand Down Expand Up @@ -84,49 +87,50 @@ private function loadContact(): void {
}

private function getContact(): void {
if (\OC::$server->getAppManager()->isEnabledForUser('contacts')) {
$this->resolveContactId();
$this->loadContact();
$this->resolveContactId();
$this->loadContact();

$this->id = $this->contact['UID'];
$this->displayName = isset($this->contact['FN']) ? $this->contact['FN'] : $this->displayName;
$this->emailAddress = isset($this->contact['EMAIL'][0]) ? $this->contact['EMAIL'][0] : $this->emailAddress;
$this->organisation = isset($this->contact['ORG']) ? $this->contact['ORG'] : '';
$this->categories = isset($this->contact['CATEGORIES']) ? explode(',', $this->contact['CATEGORIES']) : [];
$this->id = $this->contact['UID'];
$this->displayName = isset($this->contact['FN']) ? $this->contact['FN'] : $this->displayName;
$this->emailAddress = isset($this->contact['EMAIL'][0]) ? $this->contact['EMAIL'][0] : $this->emailAddress;
$this->organisation = isset($this->contact['ORG']) ? $this->contact['ORG'] : '';
$this->categories = isset($this->contact['CATEGORIES']) ? explode(',', $this->contact['CATEGORIES']) : [];


if (isset($this->contact['CATEGORIES'])) {
$this->categories = explode(',', $this->contact['CATEGORIES']);
} else {
$this->categories = [];
}
if (isset($this->contact['CATEGORIES'])) {
$this->categories = explode(',', $this->contact['CATEGORIES']);
} else {
$this->categories = [];
}

$description = $this->categories;
$description = $this->categories;

if (isset($this->contact['ORG'])) {
array_unshift($description, $this->organisation);
}
if (isset($this->contact['ORG'])) {
array_unshift($description, $this->organisation);
}

if (count($description) > 0) {
$this->description = implode(", ", $description);
} else {
$this->description = \OC::$server->getL10N('polls')->t('Contact');
}
if (count($description) > 0) {
$this->description = implode(", ", $description);
} else {
throw new ContactsNotEnabledExceptions();
$this->description = \OC::$server->getL10N('polls')->t('Contact');
}
}

public static function isEnabled(): bool {
return self::getContainer()->query(IAppManager::class)->isEnabledForUser('contacts');
}

/**
* * List all contacts with email adresses
* * excluding contacts from localSystemBook
*
* @param string[] $queryRange
*/
public static function listRaw(string $query = '', array $queryRange = ['FN', 'EMAIL', 'ORG', 'CATEGORIES']) {
private static function listRaw(string $query = '', array $queryRange = ['FN', 'EMAIL', 'ORG', 'CATEGORIES']): array {
$contacts = [];
if (\OC::$server->getAppManager()->isEnabledForUser('contacts')) {
foreach (\OC::$server->getContactsManager()->search($query, $queryRange) as $contact) {

if (self::isEnabled()) {
foreach (self::getContainer()->query(IContactsManager::class)->search($query, $queryRange) as $contact) {
if (!array_key_exists('isLocalSystemBook', $contact) && array_key_exists('EMAIL', $contact)) {
$contacts[] = $contact;
}
Expand All @@ -138,7 +142,7 @@ public static function listRaw(string $query = '', array $queryRange = ['FN', 'E
/**
* @return Contact[]
*/
public static function search(string $query = '', $queryRange = ['FN', 'EMAIL', 'ORG', 'CATEGORIES']) {
public static function search(string $query = '', $queryRange = ['FN', 'EMAIL', 'ORG', 'CATEGORIES']): array {
$contacts = [];
foreach (self::listRaw($query, $queryRange) as $contact) {
$contacts[] = new Self($contact['UID']);
Expand Down
69 changes: 37 additions & 32 deletions lib/Model/ContactGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

namespace OCA\Polls\Model;

use OCP\App\IAppManager;
use OCP\Contacts\IManager as IContactsManager;

class ContactGroup extends UserGroupClass {
public const TYPE = 'contactGroup';
public const ICON = 'icon-group';
Expand All @@ -32,8 +35,12 @@ public function __construct(
$id
) {
parent::__construct($id, self::TYPE);
$this->icon = self::ICON;
$this->description = \OC::$server->getL10N('polls')->t('Contact group');
if (self::isEnabled()) {
$this->icon = self::ICON;
$this->description = \OC::$server->getL10N('polls')->t('Contact group');
} else {
throw new ContactsNotEnabledExceptions();
}
}

/**
Expand All @@ -46,11 +53,34 @@ public function getDisplayName(): string {
return $this->displayName;
}

public static function listRaw($query = '') {
/**
* Get a list of contacts group members
* @return Contact[]
*/
public function getMembers() {
$contacts = [];

foreach (self::getContainer()->query(IContactsManager::class)->search($this->id, ['CATEGORIES']) as $contact) {
if (array_key_exists('EMAIL', $contact)) {
$contacts[] = new Contact($contact['UID']);
}
}

return $contacts;
}

public static function isEnabled(): bool {
return self::getContainer()->query(IAppManager::class)->isEnabledForUser('contacts');
}

/**
* @return ContactGroup[]
*/
public static function search(string $query = ''): array {
$contactGroups = [];
if (\OC::$server->getContactsManager()->isEnabled()) {
// find contact, which are member of the requested Group
foreach (\OC::$server->getContactsManager()->search($query, ['CATEGORIES']) as $contact) {
if (self::isEnabled() && $query) {
// foreach (\OC::$server->getContactsManager()->search($query, ['CATEGORIES']) as $contact) {
foreach (self::getContainer()->query(IContactsManager::class)->search($query, ['CATEGORIES']) as $contact) {
// get all groups from the found contact and explode to array
$temp = explode(',', $contact['CATEGORIES']);
foreach ($temp as $contactGroup) {
Expand All @@ -59,36 +89,11 @@ public static function listRaw($query = '') {
}
}
}
}
return array_unique($contactGroups);
}

/**
* @return ContactGroup[]
*/
public static function search(string $query = '') {
$contactGroups = [];
if (\OC::$server->getContactsManager()->isEnabled() && $query) {
foreach (self::listRaw($query) as $contactGroup) {
foreach (array_unique($contactGroups) as $contactGroup) {
$contactGroups[] = new self($contactGroup);
}
}
return $contactGroups;
}

/**
* Get a list of contacts group members
* @return Contact[]
*/
public function getMembers() {
$contacts = [];
if (\OC::$server->getContactsManager()->isEnabled()) {
foreach (\OC::$server->getContactsManager()->search($this->id, ['CATEGORIES']) as $contact) {
if (array_key_exists('EMAIL', $contact)) {
$contacts[] = new Contact($contact['UID']);
}
}
}
return $contacts;
}
}
9 changes: 5 additions & 4 deletions lib/Model/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@ class Email extends UserGroupClass {
public const ICON = 'icon-mail';

public function __construct(
$id
$id,
$displayName = ''
) {
parent::__construct($id, self::TYPE);
$this->description = \OC::$server->getL10N('polls')->t('External Email');
$this->icon = self::ICON;
$this->emailAddress = $id;
if ($displayName) {
$this->displayName = $displayName;
}
}

/**
* @return string
*/
public function getDisplayName(): string {
if (!$this->displayName) {
return $this->id;
Expand Down
30 changes: 14 additions & 16 deletions lib/Model/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

namespace OCA\Polls\Model;

use OCP\IGroupManager;
use OCP\IGroup;

class Group extends UserGroupClass {
Expand All @@ -38,8 +39,7 @@ public function __construct(
) {
parent::__construct($id, self::TYPE);
$this->icon = self::ICON;

$this->group = \OC::$server->getGroupManager()->get($this->id);
$this->group = self::getContainer()->query(IGroupManager::class)->get($this->id);
$this->description = \OC::$server->getL10N('polls')->t('Group');
try {
// since NC19
Expand All @@ -50,8 +50,16 @@ public function __construct(
}
}

public static function listRaw(string $query = '') {
return \OC::$server->getGroupManager()->search($query);
/**
* @return User[]
*/
public function getMembers(): array {
$members = [];

foreach (array_keys(self::getContainer()->query(IGroupManager::class)->displayNamesInGroup($this->id)) as $member) {
$members[] = new User($member);
}
return $members;
}

/**
Expand All @@ -61,22 +69,12 @@ public static function listRaw(string $query = '') {
*/
public static function search(string $query = '', array $skip = []): array {
$groups = [];
foreach (self::listRaw($query) as $group) {

foreach (self::getContainer()->query(IGroupManager::class)->search($query) as $group) {
if (!in_array($group->getGID(), $skip)) {
$groups[] = new Self($group->getGID());
}
}
return $groups;
}

/**
* @return User[]
*/
public function getMembers(): array {
$members = [];
foreach (array_keys(\OC::$server->getGroupManager()->displayNamesInGroup($this->id)) as $member) {
$members[] = new User($member);
}
return $members;
}
}
Loading

0 comments on commit e84ca86

Please sign in to comment.