diff options
-rw-r--r-- | js/circles.app.actions.js | 5 | ||||
-rw-r--r-- | js/circles.app.navigation.js | 9 | ||||
-rw-r--r-- | js/circles.app.results.members.js | 17 | ||||
-rw-r--r-- | lib/Api/v1/Circles.php | 2 | ||||
-rw-r--r-- | lib/Controller/MembersController.php | 46 | ||||
-rw-r--r-- | lib/Exceptions/EmailAccountInvalidFormatException.php | 32 | ||||
-rw-r--r-- | lib/Service/MembersService.php | 47 |
7 files changed, 149 insertions, 9 deletions
diff --git a/js/circles.app.actions.js b/js/circles.app.actions.js index 4317df2f..bb429983 100644 --- a/js/circles.app.actions.js +++ b/js/circles.app.actions.js @@ -96,6 +96,11 @@ var actions = { }, + validateEmail: function (email) { + var re = /^(([^<>()\[\]\\.,;:\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,}))$/; + return re.test(email); + }, + selectCircle: function (circle_id) { curr.searchUser = ''; elements.addMember.val(''); diff --git a/js/circles.app.navigation.js b/js/circles.app.navigation.js index 83090fd3..3dfc5e5e 100644 --- a/js/circles.app.navigation.js +++ b/js/circles.app.navigation.js @@ -91,8 +91,13 @@ var nav = { } }); } else { - api.addMember(curr.circle, elements.addMember.val(), - resultMembers.addMemberResult); + if (actions.validateEmail(elements.addMember.val())) { + api.addEmail(curr.circle, elements.addMember.val(), + resultMembers.addEmailResult); + } else { + api.addMember(curr.circle, elements.addMember.val(), + resultMembers.addMemberResult); + } } } }); diff --git a/js/circles.app.results.members.js b/js/circles.app.results.members.js index 4569553f..95d26d00 100644 --- a/js/circles.app.results.members.js +++ b/js/circles.app.results.members.js @@ -100,6 +100,23 @@ var resultMembers = { }, + addEmailResult: function (result) { + + if (result.status === 1) { + OCA.notification.onSuccess( + t('circles', "The email address '{email}' was added to the circle", + {email: result.email})); + + nav.displayMembers(result.members); + return; + } + OCA.notification.onFail( + t('circles', "The email address '{email}' could not be added to the circle", + {email: result.email}) + + ': ' + ((result.error) ? result.error : t('circles', 'no error message'))); + }, + + inviteMemberResult: function (result) { if (result.status === 1) { diff --git a/lib/Api/v1/Circles.php b/lib/Api/v1/Circles.php index 7185611d..77f035dc 100644 --- a/lib/Api/v1/Circles.php +++ b/lib/Api/v1/Circles.php @@ -245,7 +245,7 @@ class Circles { $c = self::getContainer(); return $c->query('MembersService') - ->addMember($circleUniqueId, $userId); + ->addLocalMember($circleUniqueId, $userId); } diff --git a/lib/Controller/MembersController.php b/lib/Controller/MembersController.php index 1877c31f..0002fb9f 100644 --- a/lib/Controller/MembersController.php +++ b/lib/Controller/MembersController.php @@ -26,6 +26,7 @@ namespace OCA\Circles\Controller; +use OCA\Circles\Model\Member; use OCP\AppFramework\Http\DataResponse; class MembersController extends BaseController { @@ -40,10 +41,10 @@ class MembersController extends BaseController { * * @return DataResponse */ - public function add($uniqueId, $name) { + public function addLocalMember($uniqueId, $name) { try { - $data = $this->membersService->addMember($uniqueId, $name); + $data = $this->membersService->addLocalMember($uniqueId, $name); } catch (\Exception $e) { return $this->fail( [ @@ -70,6 +71,41 @@ class MembersController extends BaseController { * @NoAdminRequired * @NoSubAdminRequired * + * @param string $uniqueId + * @param string $email + * + * @return DataResponse + */ + public function addEmailAddress($uniqueId, $email) { + + try { + $data = $this->membersService->addEmailAddress($uniqueId, $email); + } catch (\Exception $e) { + return $this->fail( + [ + 'circle_id' => $uniqueId, + 'user_id' => $email, + 'name' => $this->miscService->getDisplayName($email, true), + 'error' => $e->getMessage() + ] + ); + } + + return $this->success( + [ + 'circle_id' => $uniqueId, + 'user_id' => $email, + 'name' => $this->miscService->getDisplayName($email, true), + 'members' => $data + ] + ); + } + + + /** + * @NoAdminRequired + * @NoSubAdminRequired + * * @param $uniqueId * @param string $name * @@ -114,7 +150,7 @@ class MembersController extends BaseController { public function level($uniqueId, $member, $level) { try { - $data = $this->membersService->levelMember($uniqueId, $member, $level); + $data = $this->membersService->levelLocalMember($uniqueId, $member, $level); } catch (\Exception $e) { return $this->fail( @@ -149,10 +185,10 @@ class MembersController extends BaseController { * * @return DataResponse */ - public function remove($uniqueId, $member) { + public function removeLocalMember($uniqueId, $member) { try { - $data = $this->membersService->removeMember($uniqueId, $member); + $data = $this->membersService->removeMember($uniqueId, $member, Member::TYPE_USER); } catch (\Exception $e) { return $this->fail( diff --git a/lib/Exceptions/EmailAccountInvalidFormatException.php b/lib/Exceptions/EmailAccountInvalidFormatException.php new file mode 100644 index 00000000..4d97556b --- /dev/null +++ b/lib/Exceptions/EmailAccountInvalidFormatException.php @@ -0,0 +1,32 @@ +<?php +/** + * Circles - Bring cloud-users closer together. + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Maxence Lange <maxence@pontapreta.net> + * @copyright 2017 + * @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\Circles\Exceptions; + +class EmailAccountInvalidFormatException extends \Exception { + +} + diff --git a/lib/Service/MembersService.php b/lib/Service/MembersService.php index 877d4869..289bb10f 100644 --- a/lib/Service/MembersService.php +++ b/lib/Service/MembersService.php @@ -31,6 +31,7 @@ use OC\User\NoUserException; use OCA\Circles\Db\CirclesRequest; use OCA\Circles\Db\MembersRequest; use OCA\Circles\Exceptions\CircleTypeNotValidException; +use OCA\Circles\Exceptions\EmailAccountInvalidFormatException; use OCA\Circles\Exceptions\GroupDoesNotExistException; use OCA\Circles\Exceptions\MemberAlreadyExistsException; use OCA\Circles\Exceptions\MemberDoesNotExistException; @@ -105,7 +106,7 @@ class MembersService { * @return array * @throws \Exception */ - public function addMember($circleUniqueId, $name) { + public function addLocalMember($circleUniqueId, $name) { try { $circle = $this->circlesRequest->getCircle($circleUniqueId, $this->userId); @@ -134,6 +135,50 @@ class MembersService { /** * @param string $circleUniqueId + * @param string $email + * + * @return array + * @throws \Exception + */ + public function addEmailAddress($circleUniqueId, $email) { + + $this->miscService->log('___' . $email); + if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { + throw new EmailAccountInvalidFormatException( + $this->l10n->t('Email format is not valid') + ); + } + + try { + $circle = $this->circlesRequest->getCircle($circleUniqueId, $this->userId); + $circle->getHigherViewer() + ->hasToBeModerator(); + } catch (\Exception $e) { + throw $e; + } + + try { + $member = $this->getFreshNewMember($circleUniqueId, $email, Member::TYPE_MAIL); + } catch (\Exception $e) { + throw $e; + } + + $this->miscService->log('___' . json_encode($member)); + +// +// $member->inviteToCircle($circle->getType()); +// $this->membersRequest->updateMember($member); +// +// $this->eventsService->onMemberNew($circle, $member); +// + return $this->membersRequest->getMembers( + $circle->getUniqueId(), $circle->getHigherViewer() + ); + } + + + /** + * @param string $circleUniqueId * @param string $groupId * * @return array |