Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/polls.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authordartcafe <github@dartcafe.de>2020-08-12 22:48:54 +0300
committerdartcafe <github@dartcafe.de>2020-08-12 22:48:54 +0300
commitab6c0fe12c00a569bbb4252c76cb37a924ef9100 (patch)
treefde18ce0d332de533fe26e635295e1f175f86882 /lib
parenta7d96426e31d8853a2def37c587b27fe1c1ff3f6 (diff)
added sharing to contact groups
Diffstat (limited to 'lib')
-rw-r--r--lib/Controller/OptionController.php16
-rw-r--r--lib/Controller/ShareController.php30
-rw-r--r--lib/Controller/SystemController.php300
-rw-r--r--lib/Exceptions/TooShortException.php40
-rw-r--r--lib/Exceptions/UsernameInvalidException.php40
-rw-r--r--lib/Service/MailService.php4
-rw-r--r--lib/Service/OptionService.php2
-rw-r--r--lib/Service/ShareService.php9
-rw-r--r--lib/Service/SystemService.php414
9 files changed, 591 insertions, 264 deletions
diff --git a/lib/Controller/OptionController.php b/lib/Controller/OptionController.php
index 9fc74889..3b1e7f1e 100644
--- a/lib/Controller/OptionController.php
+++ b/lib/Controller/OptionController.php
@@ -63,22 +63,6 @@ class OptionController extends Controller {
return new DataResponse(['options' => $this->optionService->list($pollId)], Http::STATUS_OK);
}
- //
- // /**
- // * Get all options specified by token
- // * Read all options of a poll based on a share token and return list as array
- // * @NoAdminRequired
- // * @PublicPage
- // * @param string $token
- // * @return DataResponse
- // */
- // public function listByToken($token) {
- // return new DataResponse($this->optionService->list(0, $token), Http::STATUS_OK);
- // }
-
-
-
-
/**
* Add a new option
* @NoAdminRequired
diff --git a/lib/Controller/ShareController.php b/lib/Controller/ShareController.php
index 0c50b4c7..9c566214 100644
--- a/lib/Controller/ShareController.php
+++ b/lib/Controller/ShareController.php
@@ -37,6 +37,7 @@ use OCP\AppFramework\Http\DataResponse;
use OCA\Polls\Service\ShareService;
use OCA\Polls\Service\MailService;
+use OCA\Polls\Service\SystemService;
class ShareController extends Controller {
@@ -46,22 +47,28 @@ class ShareController extends Controller {
/** @var MailService */
private $mailService;
+ /** @var SystemService */
+ private $systemService;
+
/**
* ShareController constructor.
* @param string $appName
* @param IRequest $request
* @param MailService $mailService
* @param ShareService $shareService
+ * @param SystemService $systemService
*/
public function __construct(
string $appName,
IRequest $request,
MailService $mailService,
- ShareService $shareService
+ ShareService $shareService,
+ SystemService $systemService
) {
parent::__construct($appName, $request);
$this->shareService = $shareService;
$this->mailService = $mailService;
+ $this->systemService = $systemService;
}
/**
@@ -182,4 +189,25 @@ class ShareController extends Controller {
return new DataResponse(['error' => $e], Http::STATUS_CONFLICT);
}
}
+
+ /**
+ * resolve Contact groupe to individual shares
+ * @NoAdminRequired
+ * @param string $token
+ * @return DataResponse
+ */
+ public function resolveContactGroup($token) {
+ $shares = [];
+ try {
+ $share = $this->shareService->get($token);
+ \OC::$server->getLogger()->alert('Suche nach Gruppe: ' . $share->getUserId());
+ foreach ($this->systemService->getContactsGroupMembers($share->getUserId()) as $member) {
+ $shares[] = $this->shareService->add($share->getpollId(), 'contact', $member['user'], $member['emailAddress']) ;
+ }
+
+ return new DataResponse(['shares' => $shares], Http::STATUS_OK);
+ } catch (Exception $e) {
+ return new DataResponse(['error' => $e], Http::STATUS_CONFLICT);
+ }
+ }
}
diff --git a/lib/Controller/SystemController.php b/lib/Controller/SystemController.php
index 67b9bbc2..45e5afba 100644
--- a/lib/Controller/SystemController.php
+++ b/lib/Controller/SystemController.php
@@ -23,293 +23,115 @@
namespace OCA\Polls\Controller;
+use Exception;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
+use OCA\Polls\Service\SystemService;
-use OCP\IGroupManager;
-use OCP\IUser;
-use OCP\IUserManager;
-use OCP\IConfig;
use OCP\IRequest;
-use OCA\Polls\Db\Share;
-use OCA\Polls\Db\ShareMapper;
-use OCA\Polls\Db\Vote;
-use OCA\Polls\Db\VoteMapper;
class SystemController extends Controller {
- /** @var string */
- private $userId;
-
- /** @var IConfig */
- private $systemConfig;
-
- /** @var IGroupManager */
- private $groupManager;
-
- /** @var IUserManager */
- private $userManager;
-
- /** @var VoteMapper */
- private $voteMapper;
-
- /** @var ShareMapper */
- private $shareMapper;
+ /** @var SystemService */
+ private $systemService;
/**
* SystemController constructor.
* @param string $appName
- * @param $userId
* @param IRequest $request
- * @param IConfig $systemConfig
- * @param IGroupManager $groupManager
- * @param IUserManager $userManager
- * @param VoteMapper $voteMapper
- * @param ShareMapper $shareMapper
+ * @param SystemService $systemService
*/
+
public function __construct(
string $appName,
- $userId,
IRequest $request,
- IConfig $systemConfig,
- IGroupManager $groupManager,
- IUserManager $userManager,
- VoteMapper $voteMapper,
- ShareMapper $shareMapper
+ SystemService $systemService
) {
parent::__construct($appName, $request);
- $this->voteMapper = $voteMapper;
- $this->shareMapper = $shareMapper;
- $this->userId = $userId;
- $this->systemConfig = $systemConfig;
- $this->groupManager = $groupManager;
- $this->userManager = $userManager;
+ $this->systemService = $systemService;
}
+ /**
+ * Get a list of users
+ * @NoAdminRequired
+ * @param string $query
+ * @param array $skipUsers - usernames to skip in return array
+ * @return DataResponse
+ */
+ public function getSiteUsers($query = '', $skipUsers = array()) {
+ return new DataResponse(['users' => $this->systemService->getSiteUsers($query, $skipUsers)], Http::STATUS_OK);
+ }
+
+ /**
+ * Get a list of user groups
+ * @NoAdminRequired
+ * @param string $query
+ * @param array $skipGroups - group names to skip in return array
+ * @return DataResponse
+ */
+ public function getSiteGroups($query = '', $skipGroups = array()) {
+ return new DataResponse(['groups' => $this->systemService->getSiteGroups($query, $skipGroups)], Http::STATUS_OK);
+ }
+
/**
- * Validate string as email address
+ * Get a list of contacts
* @NoAdminRequired
* @param string $query
- * @return bool
+ * @return DataResponse
*/
- private function isValidEmail($email) {
- return (!preg_match('/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/', $email)) ? false : true;
- }
+ public function getContacts($query = '') {
+ return new DataResponse(['contacts' => $this->systemService->getContacts($query)], Http::STATUS_OK);
+ }
/**
- * Get a list of NC users, groups and contacts
+ * Get a list of contact groups
+ * @NoAdminRequired
+ * @param string $query
+ * @return DataResponse
+ */
+ public function getContactsGroups($query = '') {
+ return new DataResponse(['contactGroups' => $this->systemService->getContactsGroups($query)], Http::STATUS_OK);
+ }
+
+
+ /**
+ * Get a combined list of NC users, groups and contacts
* @NoAdminRequired
- * @PublicPage
- * @NoCSRFRequired
* @param string $query
* @param bool $getGroups - search in groups
* @param bool $getUsers - search in site users
* @param bool $getContacts - search in contacs
+ * @param bool $getContactGroups - search in contacs
* @param array $skipGroups - group names to skip in return array
* @param array $skipUsers - user names to skip in return array
* @return DataResponse
*/
- public function getSiteUsersAndGroups($query = '', $getGroups = true, $getUsers = true, $getContacts = true, $getMail = false, $skipGroups = array(), $skipUsers = array()) {
- $list = array();
-
- if ($getMail && $this->isValidEmail($query)) {
- $list[] = [
- 'id' => '',
- 'user' => '',
- 'organisation' => '',
- 'displayName' => '',
- 'emailAddress' => $query,
- 'desc' => $query,
- 'type' => 'email',
- 'icon' => 'icon-mail',
- 'avatarURL' => '',
- 'avatar' => '',
- 'lastLogin' => '',
- 'cloudId' => ''
-
- ];
- }
-
- if ($getGroups) {
- foreach ($this->groupManager->search($query) as $group) {
- if (!in_array($group->getGID(), $skipGroups)) {
- $list[] = [
- 'id' => $group->getGID(),
- 'user' => $group->getGID(),
- 'organisation' => '',
- 'displayName' => $group->getGID(),
- 'emailAddress' => '',
- 'desc' => 'Group',
- 'type' => 'group',
- 'icon' => 'icon-group',
- 'avatarURL' => '',
- 'avatar' => '',
- 'lastLogin' => '',
- 'cloudId' => ''
-
- ];
- }
- }
- }
-
- if ($getUsers) {
- $users = $this->userManager->searchDisplayName($query);
- foreach ($users as $user) {
- if (!in_array($user->getUID(), $skipUsers) && $user->isEnabled()) {
- $list[] = [
- 'id' => $user->getUID(),
- 'user' => $user->getUID(),
- 'displayName' => $user->getDisplayName(),
- 'organisation' => '',
- 'emailAddress' => $user->getEMailAddress(),
- 'desc' => 'User',
- 'type' => 'user',
- 'icon' => 'icon-user',
- 'avatarURL' => '',
- 'avatar' => '',
- 'lastLogin' => $user->getLastLogin(),
- 'cloudId' => $user->getCloudId()
- ];
- }
- }
- }
-
- if ($getContacts && \OC::$server->getContactsManager()->isEnabled()) {
-
- foreach (\OC::$server->getContactsManager()->search($query, array('FN', 'EMAIL', 'ORG', 'CATEGORIES')) as $contact) {
- if (!array_key_exists('isLocalSystemBook', $contact) && array_key_exists('EMAIL', $contact)) {
-
- $emailAdresses = $contact['EMAIL'];
-
- if (!is_array($emailAdresses)) {
- $emailAdresses = array($emailAdresses);
- } else {
- // take the first eMail address for now
- $emailAdresses = array($emailAdresses[0]);
- }
-
- foreach ($emailAdresses as $emailAddress) {
- $list[] = [
- 'id' => $contact['UID'],
- 'user' => $contact['FN'],
- 'displayName' => $contact['FN'],
- 'organisation' => isset($contact['ORG']) ? $contact['ORG'] : '',
- 'emailAddress' => $emailAddress,
- 'desc' => 'Contact',
- 'type' => 'contact',
- 'icon' => 'icon-mail',
- 'avatarURL' => '',
- 'avatar' => '',
- 'lastLogin' => '',
- 'cloudId' => '',
- ];
- }
-
- }
- }
- }
-
- return new DataResponse([
- 'siteusers' => $list
- ], Http::STATUS_OK);
+ public function getSiteUsersAndGroups(
+ $query = '',
+ $getGroups = true,
+ $getUsers = true,
+ $getContacts = true,
+ $getContactGroups = true,
+ $getMail = false,
+ $skipGroups = array(),
+ $skipUsers = array()
+ ) {
+ return new DataResponse(['siteusers' => $this->systemService->getSiteUsersAndGroups(
+ $query, $getGroups, $getUsers, $getContacts, $getContactGroups, $getMail, $skipGroups, $skipUsers )], Http::STATUS_OK);
}
/**
* Validate it the user name is reservrd
* return false, if this username already exists as a user or as
* a participant of the poll
- * @NoCSRFRequired
* @NoAdminRequired
* @PublicPage
* @return DataResponse
*/
public function validatePublicUsername($pollId, $userName, $token) {
+ return new DataResponse(['result' => true, 'name' => $userName], Http::STATUS_OK);
- // return forbidden, if $pollId does not match the share's pollId, force int compare
- if (intval($this->shareMapper->findByToken($token)->getPollId()) !== intVal($pollId)) {
- return new DataResponse(['result' => false, 'error' => 'wrong token'], Http::STATUS_FORBIDDEN);
- }
-
- // return forbidden, if the length of the userame is lower than 3 characters
- if (strlen(trim($userName)) < 3) {
- return new DataResponse(['result' => false, 'error' => 'userName too short'], Http::STATUS_FORBIDDEN);
- }
-
- $list = array();
-
- // get all groups
- $groups = $this->groupManager->search('');
- foreach ($groups as $group) {
- $list[] = [
- 'id' => $group->getGID(),
- 'user' => $group->getGID(),
- 'type' => 'group',
- 'displayName' => $group->getGID(),
- ];
- }
-
- // get all users
- $users = $this->userManager->searchDisplayName('');
- foreach ($users as $user) {
- $list[] = [
- 'id' => $user->getUID(),
- 'user' => $user->getUID(),
- 'type' => 'user',
- 'displayName' => $user->getDisplayName(),
- ];
- }
-
- // get all participants
- $votes = $this->voteMapper->findParticipantsByPoll($pollId);
- foreach ($votes as $vote) {
- if ($vote->getUserId() !== '' && $vote->getUserId() !== null) {
- $list[] = [
- 'id' => $vote->getUserId(),
- 'user' => $vote->getUserId(),
- 'type' => 'participant',
- 'displayName' => $vote->getUserId(),
- ];
- }
- }
-
- // get all shares for this poll
- $shares = $this->shareMapper->findByPoll($pollId);
- foreach ($shares as $share) {
- if ($share->getUserId() !== '' && $share->getUserId() !== null) {
- $list[] = [
- 'id' => $share->getUserId(),
- 'user' => $share->getUserId(),
- 'type' => 'share',
- 'displayName' => $share->getUserId(),
- ];
- }
- }
-
- // check if the username is contained inside the generated list
- // return forbidden, if list contains requested username
- foreach ($list as $element) {
- if (strtolower(trim($userName)) === strtolower(trim($element['id'])) || strtolower(trim($userName)) === strtolower(trim($element['displayName']))) {
- return new DataResponse([
- 'result' => false
- ], Http::STATUS_FORBIDDEN);
- }
- }
-
- // return OK, if username is allowed
- return new DataResponse([
- 'result' => true,
- 'name' => $userName
- ], Http::STATUS_OK);
}
- // public function getDisplayName() {
- // $this->userManager = \OC::$server->getUserManager();
- //
- // if (\OC::$server->getUserManager()->get($this->userId) instanceof IUser) {
- // return \OC::$server->getUserManager()->get($this->userId)->getDisplayName();
- // } else {
- // return $this->userId;
- // }
- // }
}
diff --git a/lib/Exceptions/TooShortException.php b/lib/Exceptions/TooShortException.php
new file mode 100644
index 00000000..8b497d28
--- /dev/null
+++ b/lib/Exceptions/TooShortException.php
@@ -0,0 +1,40 @@
+<?php
+/**
+ * @copyright Copyright (c) 2020 René Gieling <github@dartcafe.de>
+ *
+ * @author René Gieling <github@dartcafe.de>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program 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 program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCA\Polls\Exceptions;
+
+use OCP\AppFramework\Http;
+
+class TooShortException extends \Exception {
+ /**
+ * TooShortException Constructor
+ * @param string $e exception message
+ */
+ public function __construct($e = 'String too short') {
+ parent::__construct($e);
+ }
+ public function getStatus() {
+ return Http::STATUS_FORBIDDEN;
+ }
+
+}
diff --git a/lib/Exceptions/UsernameInvalidException.php b/lib/Exceptions/UsernameInvalidException.php
new file mode 100644
index 00000000..4c7c4e1a
--- /dev/null
+++ b/lib/Exceptions/UsernameInvalidException.php
@@ -0,0 +1,40 @@
+<?php
+/**
+ * @copyright Copyright (c) 2020 René Gieling <github@dartcafe.de>
+ *
+ * @author René Gieling <github@dartcafe.de>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program 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 program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCA\Polls\Exceptions;
+
+use OCP\AppFramework\Http;
+
+class UsernameInvalidException extends \Exception {
+ /**
+ * UsernameInvalidException Constructor
+ * @param string $e exception message
+ */
+ public function __construct($e = 'Username not allowed') {
+ parent::__construct($e);
+ }
+ public function getStatus() {
+ return Http::STATUS_FORBIDDEN;
+ }
+
+}
diff --git a/lib/Service/MailService.php b/lib/Service/MailService.php
index 543bafa0..73ccbcfd 100644
--- a/lib/Service/MailService.php
+++ b/lib/Service/MailService.php
@@ -231,8 +231,8 @@ class MailService {
$recipients[] = array(
'userId' => $share->getUserId(),
- 'eMailAddress' => $contact['EMAIL'][0],
- 'displayName' => $contact['FN'],
+ 'eMailAddress' => $share->getUserEmail(),
+ 'displayName' => $share->getUserId(),
'language' => $defaultLang,
'link' => $this->urlGenerator->getAbsoluteURL(
$this->urlGenerator->linkToRoute(
diff --git a/lib/Service/OptionService.php b/lib/Service/OptionService.php
index c598073e..7208a154 100644
--- a/lib/Service/OptionService.php
+++ b/lib/Service/OptionService.php
@@ -56,7 +56,7 @@ class OptionService {
private $acl;
/**
- * OptionController constructor.
+ * OptionService constructor.
* @param OptionMapper $optionMapper
* @param Option $option
* @param PollMapper $pollMapper
diff --git a/lib/Service/ShareService.php b/lib/Service/ShareService.php
index 83f6233f..af069d6e 100644
--- a/lib/Service/ShareService.php
+++ b/lib/Service/ShareService.php
@@ -101,7 +101,9 @@ class ShareService {
* @return Share
*/
public function get($token) {
- return $this->shareMapper->findByToken($token);
+ $this->share = $this->shareMapper->findByToken($token);
+
+ return $this->share;
}
/**
@@ -115,15 +117,12 @@ class ShareService {
* @throws NotAuthorizedException
*/
public function add($pollId, $type, $userId, $userEmail = '') {
+ \OC::$server->getLogger()->alert('==== Start ');
if (!$this->acl->set($pollId)->getAllowEdit()) {
throw new NotAuthorizedException;
}
- if ($type === 'contact') {
- $type = 'external';
- }
-
$this->share = new Share();
$this->share->setType($type);
$this->share->setPollId($pollId);
diff --git a/lib/Service/SystemService.php b/lib/Service/SystemService.php
new file mode 100644
index 00000000..5932fae5
--- /dev/null
+++ b/lib/Service/SystemService.php
@@ -0,0 +1,414 @@
+<?php
+/**
+ * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com>
+ *
+ * @author René Gieling <github@dartcafe.de>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program 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 program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCA\Polls\Service;
+
+use Exception;
+use OCA\Polls\Exceptions\NotAuthorizedException;
+use OCA\Polls\Exceptions\TooShortException;
+use OCA\Polls\Exceptions\UsernameInvalidException;
+
+use OCP\IGroupManager;
+use OCP\IUser;
+use OCP\IUserManager;
+use OCP\IRequest;
+use OCA\Polls\Db\Share;
+use OCA\Polls\Db\ShareMapper;
+use OCA\Polls\Db\Vote;
+use OCA\Polls\Db\VoteMapper;
+
+class SystemService {
+
+ /** @var IGroupManager */
+ private $groupManager;
+
+ /** @var IUserManager */
+ private $userManager;
+
+ /** @var VoteMapper */
+ private $voteMapper;
+
+ /** @var ShareMapper */
+ private $shareMapper;
+
+ /**
+ * SystemService constructor.
+ * @param IGroupManager $groupManager
+ * @param IUserManager $userManager
+ * @param VoteMapper $voteMapper
+ * @param ShareMapper $shareMapper
+ */
+ public function __construct(
+ IGroupManager $groupManager,
+ IUserManager $userManager,
+ VoteMapper $voteMapper,
+ ShareMapper $shareMapper
+ ) {
+ $this->groupManager = $groupManager;
+ $this->userManager = $userManager;
+ $this->voteMapper = $voteMapper;
+ $this->shareMapper = $shareMapper;
+ }
+
+ /**
+ * Validate string as email address
+ * @NoAdminRequired
+ * @param string $query
+ * @return bool
+ */
+ private function isValidEmail($email) {
+ return (!preg_match('/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/', $email)) ? false : true;
+ }
+
+
+ /**
+ * Get a list of users
+ * @NoAdminRequired
+ * @param string $query
+ * @param array $skip - usernames to skip in return array
+ * @return Array
+ */
+ public function getSiteUsers($query = '', $skip = array()) {
+ $users = array();
+ foreach ($this->userManager->searchDisplayName($query) as $user) {
+ if (!in_array($user->getUID(), $skip) && $user->isEnabled()) {
+ $users[] = [
+ 'id' => $user->getUID(),
+ 'user' => $user->getUID(),
+ 'displayName' => $user->getDisplayName(),
+ 'organisation' => '',
+ 'emailAddress' => $user->getEMailAddress(),
+ 'desc' => 'User',
+ 'type' => 'user',
+ 'icon' => 'icon-user',
+ 'avatarURL' => '',
+ 'avatar' => '',
+ 'lastLogin' => $user->getLastLogin(),
+ 'cloudId' => $user->getCloudId()
+ ];
+ }
+ }
+ return $users;
+ }
+
+ /**
+ * Get a list of user groups
+ * @NoAdminRequired
+ * @param string $query
+ * @param array $skip - group names to skip in return array
+ * @return Array
+ */
+ public function getSiteGroups($query = '', $skip = array()) {
+ $groups = array();
+ foreach ($this->groupManager->search($query) as $group) {
+ if (!in_array($group->getGID(), $skip)) {
+ $groups[] = [
+ 'id' => $group->getGID(),
+ 'user' => $group->getGID(),
+ 'organisation' => '',
+ 'displayName' => $group->getGID(),
+ 'emailAddress' => '',
+ 'desc' => 'Group',
+ 'type' => 'group',
+ 'icon' => 'icon-group',
+ 'avatarURL' => '',
+ 'avatar' => '',
+ 'lastLogin' => '',
+ 'cloudId' => ''
+
+ ];
+ }
+ }
+ return $groups;
+ }
+
+ /**
+ * Get a list of contacts
+ * @NoAdminRequired
+ * @param string $query
+ * @return Array
+ */
+ public function getContacts($query = '') {
+ $contacts = array();
+ foreach (\OC::$server->getContactsManager()->search($query, array('FN', 'EMAIL', 'ORG', 'CATEGORIES')) as $contact) {
+ if (!array_key_exists('isLocalSystemBook', $contact) && array_key_exists('EMAIL', $contact)) {
+
+ $emailAdresses = $contact['EMAIL'];
+
+ if (!is_array($emailAdresses)) {
+ $emailAdresses = array($emailAdresses);
+ } else {
+ // take the first eMail address for now
+ $emailAdresses = array($emailAdresses[0]);
+ }
+
+ foreach ($emailAdresses as $emailAddress) {
+ $contacts[] = [
+ 'id' => $contact['UID'],
+ 'user' => $contact['FN'],
+ 'displayName' => $contact['FN'],
+ 'organisation' => isset($contact['ORG']) ? $contact['ORG'] : '',
+ 'emailAddress' => $emailAddress,
+ 'desc' => 'Contact',
+ 'type' => 'contact',
+ 'icon' => 'icon-mail',
+ 'avatarURL' => '',
+ 'avatar' => '',
+ 'lastLogin' => '',
+ 'cloudId' => '',
+ ];
+ }
+
+ }
+ }
+ return $contacts;
+ }
+
+ /**
+ * Get a list of contacts
+ * @NoAdminRequired
+ * @param string $query
+ * @return Array
+ */
+ public function getContactsGroupMembers($query = '') {
+ $contacts = array();
+ \OC::$server->getLogger()->alert('Suche nach Gruppe: ' . $query);
+ foreach (\OC::$server->getContactsManager()->search($query, array('CATEGORIES')) as $contact) {
+ if (
+ !array_key_exists('isLocalSystemBook', $contact)
+ && array_key_exists('EMAIL', $contact)
+ && in_array($query, explode(',', $contact['CATEGORIES']))
+ ) {
+ $emailAdresses = $contact['EMAIL'];
+
+ if (!is_array($emailAdresses)) {
+ $emailAdresses = array($emailAdresses);
+ } else {
+ // take the first eMail address for now
+ $emailAdresses = array($emailAdresses[0]);
+ }
+
+ foreach ($emailAdresses as $emailAddress) {
+ $contacts[] = [
+ 'id' => $contact['UID'],
+ 'user' => $contact['FN'],
+ 'displayName' => $contact['FN'],
+ 'organisation' => isset($contact['ORG']) ? $contact['ORG'] : '',
+ 'emailAddress' => $emailAddress,
+ 'desc' => 'Contact',
+ 'type' => 'contact',
+ 'icon' => 'icon-mail',
+ 'avatarURL' => '',
+ 'avatar' => '',
+ 'lastLogin' => '',
+ 'cloudId' => '',
+ ];
+ }
+ }
+ }
+ return $contacts;
+ }
+
+ /**
+ * Get a list of contact groups
+ * @NoAdminRequired
+ * @param string $query
+ * @return Array
+ */
+ public function getContactsGroups($query = '') {
+ $contactGroups = array();
+ $foundContacts = [];
+
+ foreach (\OC::$server->getContactsManager()->search($query, array('CATEGORIES')) as $contact) {
+
+ foreach (explode(',', $contact['CATEGORIES']) as $contactGroup) {
+ if (strpos($contactGroup, $query) === 0 && !in_array($contactGroup, $foundContacts)) {
+ $foundContacts[] = $contactGroup;
+ $contactGroups[] = [
+ 'id' => 'contactgroup_' + $contactGroup,
+ 'user' => $contactGroup,
+ 'displayName' => $contactGroup,
+ 'organisation' => '',
+ 'emailAddress' => '',
+ 'desc' => 'Contact Group',
+ 'type' => 'contactGroup',
+ 'icon' => 'icon-group',
+ 'avatarURL' => '',
+ 'avatar' => '',
+ 'lastLogin' => '',
+ 'cloudId' => '',
+ ];
+ };
+ }
+ }
+ return $contactGroups;
+ }
+
+
+ /**
+ * Get a combined list of NC users, groups and contacts
+ * @NoAdminRequired
+ * @param string $query
+ * @param bool $getGroups - search in groups
+ * @param bool $getUsers - search in site users
+ * @param bool $getContacts - search in contacs
+ * @param bool $getContactGroups - search in contacs
+ * @param array $skipGroups - group names to skip in return array
+ * @param array $skipUsers - user names to skip in return array
+ * @return Array
+ */
+ public function getSiteUsersAndGroups(
+ $query = '',
+ $getGroups = true,
+ $getUsers = true,
+ $getContacts = true,
+ $getContactGroups = true,
+ $getMail = false,
+ $skipGroups = array(),
+ $skipUsers = array()
+ ) {
+ $list = array();
+
+ if ($getMail && $this->isValidEmail($query)) {
+ $list[] = [
+ 'id' => '',
+ 'user' => '',
+ 'organisation' => '',
+ 'displayName' => '',
+ 'emailAddress' => $query,
+ 'desc' => $query,
+ 'type' => 'email',
+ 'icon' => 'icon-mail',
+ 'avatarURL' => '',
+ 'avatar' => '',
+ 'lastLogin' => '',
+ 'cloudId' => ''
+
+ ];
+ }
+ if ($getGroups) {
+ $list = array_merge($list, $this->getSiteGroups($query, $skipGroups));
+ }
+
+ if ($getUsers) {
+ $list = array_merge($list, $this->getSiteUsers($query, $skipUsers));
+ }
+
+ if (\OC::$server->getContactsManager()->isEnabled()) {
+ if ($getContacts) {
+ $list = array_merge($list, $this->getContacts($query, $skipUsers));
+ }
+
+ if ($getContacts) {
+ $list = array_merge($list, $this->getContactsGroups($query, $skipGroups));
+ }
+ }
+
+ return $list;
+ }
+
+ /**
+ * Validate it the user name is reservrd
+ * return false, if this username already exists as a user or as
+ * a participant of the poll
+ * @NoAdminRequired
+ * @return Boolean
+ * @throws NotAuthorizedException
+ * @throws TooShortException
+ * @throws UsernameInvalidException
+ */
+ public function validatePublicUsername($pollId, $userName, $token) {
+
+ // return forbidden, if $pollId does not match the share's pollId, force int compare
+ if (intval($this->shareMapper->findByToken($token)->getPollId()) !== intVal($pollId)) {
+ throw new NotAuthorizedException;
+ }
+
+ // return forbidden, if the length of the userame is lower than 3 characters
+ if (strlen(trim($userName)) < 3) {
+ return new TooShortException('Username must have at least 3 characters');
+ }
+
+ $list = array();
+
+ // get all groups
+ $groups = $this->groupManager->search('');
+ foreach ($groups as $group) {
+ $list[] = [
+ 'id' => $group->getGID(),
+ 'user' => $group->getGID(),
+ 'type' => 'group',
+ 'displayName' => $group->getGID(),
+ ];
+ }
+
+ // get all users
+ $users = $this->userManager->searchDisplayName('');
+ foreach ($users as $user) {
+ $list[] = [
+ 'id' => $user->getUID(),
+ 'user' => $user->getUID(),
+ 'type' => 'user',
+ 'displayName' => $user->getDisplayName(),
+ ];
+ }
+
+ // get all participants
+ $votes = $this->voteMapper->findParticipantsByPoll($pollId);
+ foreach ($votes as $vote) {
+ if ($vote->getUserId() !== '' && $vote->getUserId() !== null) {
+ $list[] = [
+ 'id' => $vote->getUserId(),
+ 'user' => $vote->getUserId(),
+ 'type' => 'participant',
+ 'displayName' => $vote->getUserId(),
+ ];
+ }
+ }
+
+ // get all shares for this poll
+ $shares = $this->shareMapper->findByPoll($pollId);
+ foreach ($shares as $share) {
+ if ($share->getUserId() !== '' && $share->getUserId() !== null) {
+ $list[] = [
+ 'id' => $share->getUserId(),
+ 'user' => $share->getUserId(),
+ 'type' => 'share',
+ 'displayName' => $share->getUserId(),
+ ];
+ }
+ }
+
+ // check if the username is contained inside the generated list
+ // return forbidden, if list contains requested username
+ foreach ($list as $element) {
+ if (strtolower(trim($userName)) === strtolower(trim($element['id'])) || strtolower(trim($userName)) === strtolower(trim($element['displayName']))) {
+ throw new UsernameInvalidException;
+ }
+ }
+
+ // return true, if username is allowed
+ return true;
+ }
+
+}