diff options
author | Maxence Lange <maxence@nextcloud.com> | 2017-07-12 10:59:57 +0300 |
---|---|---|
committer | Maxence Lange <maxence@nextcloud.com> | 2017-07-12 10:59:57 +0300 |
commit | 4cd7fb6a4507b862a6a6ff3ab3cf94a81cfd79d7 (patch) | |
tree | 15225de9ab39ebd91568fedf8a1d222b875afe03 | |
parent | ab70b9a81c4d3e117014a9aac1ed4d116ec82090 (diff) |
replacing CircleMapper per CircleRequestBuilder
Signed-off-by: Maxence Lange <maxence@nextcloud.com>
-rw-r--r-- | lib/Api/Circles.php | 2 | ||||
-rw-r--r-- | lib/AppInfo/Application.php | 17 | ||||
-rw-r--r-- | lib/Controller/BaseController.php | 1 | ||||
-rw-r--r-- | lib/Db/CirclesMapper.php | 49 | ||||
-rw-r--r-- | lib/Db/CirclesRequest.php | 56 | ||||
-rw-r--r-- | lib/Db/CirclesRequestBuilder.php | 313 | ||||
-rw-r--r-- | lib/Db/CoreRequestBuilder.php | 69 | ||||
-rw-r--r-- | lib/Db/MembersRequest.php | 34 | ||||
-rw-r--r-- | lib/Db/MembersRequestBuilder.php | 17 | ||||
-rw-r--r-- | lib/Model/Circle.php | 2 | ||||
-rw-r--r-- | lib/Service/CirclesService.php | 35 | ||||
-rw-r--r-- | lib/Service/FederatedService.php | 8 | ||||
-rw-r--r-- | lib/Service/GroupsService.php | 38 | ||||
-rw-r--r-- | lib/Service/MembersService.php | 63 |
14 files changed, 574 insertions, 130 deletions
diff --git a/lib/Api/Circles.php b/lib/Api/Circles.php index 79bbb889..ed0f60a9 100644 --- a/lib/Api/Circles.php +++ b/lib/Api/Circles.php @@ -40,7 +40,7 @@ class Circles { * * Returns details on the circle. If the current user is a member, the members list will be * return as well. - * @deprecated 12.0.1 + * @deprecated 13.0.0 * @param $circleId * * @return Circle diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 0c0d0ee3..8572fc5b 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -134,6 +134,7 @@ class Application extends App { return new MembersService( $c->query('UserId'), $c->query('L10N'), $c->query('UserManager'), $c->query('ConfigService'), $c->query('DatabaseService'), + $c->query('CirclesRequest'), $c->query('MembersRequest'), $c->query('EventsService'), $c->query('MiscService') ); } @@ -143,7 +144,8 @@ class Application extends App { 'GroupsService', function(IAppContainer $c) { return new GroupsService( $c->query('UserId'), $c->query('L10N'), $c->query('GroupManager'), - $c->query('DatabaseService'), $c->query('MembersRequest'), $c->query('MiscService') + $c->query('DatabaseService'), $c->query('CirclesRequest'), + $c->query('MembersRequest'), $c->query('MiscService') ); } ); @@ -287,7 +289,8 @@ class Application extends App { 'CirclesRequest', function(IAppContainer $c) { return new CirclesRequest( $c->query('L10N'), $c->query('ServerContainer') - ->getDatabaseConnection(), $c->query('MiscService') + ->getDatabaseConnection(), $c->query('MembersRequest'), + $c->query('MiscService') ); } ); @@ -325,7 +328,7 @@ class Application extends App { return new CirclesMapper( $c->query('UserId'), $c->query('ServerContainer') ->getDatabaseConnection(), $c->query('L10N'), - $c->query('MiscService') + $c->query('MembersRequest'), $c->query('MiscService') ); } ); @@ -450,11 +453,11 @@ class Application extends App { ->t('Circles'); return [ - 'id' => $this->appName, + 'id' => $this->appName, 'order' => 5, - 'href' => $urlGen->linkToRoute('circles.Navigation.navigate'), - 'icon' => $urlGen->imagePath($this->appName, 'circles.svg'), - 'name' => $navName + 'href' => $urlGen->linkToRoute('circles.Navigation.navigate'), + 'icon' => $urlGen->imagePath($this->appName, 'circles.svg'), + 'name' => $navName ]; } ); diff --git a/lib/Controller/BaseController.php b/lib/Controller/BaseController.php index b2c2cfb1..77f4da0c 100644 --- a/lib/Controller/BaseController.php +++ b/lib/Controller/BaseController.php @@ -117,6 +117,7 @@ class BaseController extends Controller { * @return DataResponse */ protected function fail($data) { + $this->miscService->log(json_encode($data)); return new DataResponse( array_merge($data, array('status' => 0)), Http::STATUS_NON_AUTHORATIVE_INFORMATION diff --git a/lib/Db/CirclesMapper.php b/lib/Db/CirclesMapper.php index 1077eb85..81b02732 100644 --- a/lib/Db/CirclesMapper.php +++ b/lib/Db/CirclesMapper.php @@ -48,13 +48,19 @@ class CirclesMapper extends Mapper { /** @var L10N */ private $l10n; + /** @var MembersRequest */ + private $membersRequest; + /** @var MiscService */ private $miscService; - public function __construct($userId, IDBConnection $db, $l10n, $miscService) { + public function __construct( + $userId, IDBConnection $db, $l10n, MembersRequest $membersRequest, $miscService + ) { parent::__construct($db, self::TABLENAME, 'OCA\Circles\Db\Circles'); $this->userId = $userId; $this->l10n = $l10n; + $this->membersRequest = $membersRequest; $this->miscService = $miscService; } @@ -70,6 +76,8 @@ class CirclesMapper extends Mapper { * * @return Circle[] * @throws ConfigNoCircleAvailable + * + * @deprecated - a virer */ public function findCirclesByUser($userId, $type, $name = '', $level = 0, $circleId = -1) { @@ -342,23 +350,30 @@ class CirclesMapper extends Mapper { * @return Circle * @throws CircleDoesNotExistException * @throws ConfigNoCircleAvailable + * + * @deprecated Should use CirclesRequest->getCircle() */ - public function getDetailsFromCircle($circleId, $userId) { - - try { - $result = $this->findCirclesByUser($userId, Circle::CIRCLES_ALL, '', 0, $circleId); - } catch (ConfigNoCircleAvailable $e) { - throw $e; - } - - if (sizeof($result) !== 1) { - throw new CircleDoesNotExistException( - $this->l10n->t("The circle does not exist or is hidden") - ); - } - - return $result[0]; - } +// public function getDetailsFromCircle($circleId, $userId) { +// +// try { +// $result = $this->findCirclesByUser($userId, Circle::CIRCLES_ALL, '', 0, $circleId); +// } catch (ConfigNoCircleAvailable $e) { +// throw $e; +// } +// +// if (sizeof($result) !== 1) { +// throw new CircleDoesNotExistException( +// $this->l10n->t("The circle does not exist or is hidden") +// ); +// } +// +// $circle = $result[0]; +// $circle->setGroupViewer( +// $this->membersRequest->forceGetHigherLevelGroupFromUser($circleId, $userId) +// ); +// +// return $circle; +// } /** diff --git a/lib/Db/CirclesRequest.php b/lib/Db/CirclesRequest.php index 69fb97ea..38865aa9 100644 --- a/lib/Db/CirclesRequest.php +++ b/lib/Db/CirclesRequest.php @@ -47,7 +47,7 @@ class CirclesRequest extends CirclesRequestBuilder { * returns data of a circle from its Id. * * WARNING: This function does not filters data regarding the current user/viewer. - * In case of interaction with users, Please use getGroup() instead. + * In case of interaction with users, Please use getCircle() instead. * * @param int $circleId * @@ -56,6 +56,7 @@ class CirclesRequest extends CirclesRequestBuilder { */ public function forceGetCircle($circleId) { $qb = $this->getCirclesSelectSql(); + $this->limitToId($qb, $circleId); $cursor = $qb->execute(); @@ -70,6 +71,39 @@ class CirclesRequest extends CirclesRequestBuilder { } + public function getCircles($userId, $type = 0, $name = '', $level = 0) { + if ($type === 0) { + $type = Circle::CIRCLES_ALL; + } + + $qb = $this->getCirclesSelectSql(); + $this->joinMembers($qb, 'c.id'); + + $this->limitToUserId($qb, $userId); + $this->limitToLevel($qb, $level, 'm', 'g'); + $this->limitRegardingCircleType($qb, $userId, -1, $type, $name); + + $this->leftJoinUserIdAsViewer($qb, $userId); + $this->leftJoinOwner($qb); + + $result = []; + $cursor = $qb->execute(); + + while ($data = $cursor->fetch()) { + if ($name === '' || stripos($data['name'], $name) !== false) { + $result[] = $this->parseCirclesSelectSql($data); + +// $circle = Circle::fromArray($this->l10n, $data); + // $this->fillCircleUserIdAndOwner($circle, $data); + // $result[] = $circle; + } + } + $cursor->closeCursor(); + + return $result; + } + + /** * * @param int $circleId @@ -78,30 +112,30 @@ class CirclesRequest extends CirclesRequestBuilder { * @return Circle * @throws CircleDoesNotExistException */ - // TODO: Filters data public function getCircle($circleId, $userId) { $qb = $this->getCirclesSelectSql(); $this->limitToId($qb, $circleId); - $this->leftJoinUserIdAsViewer($qb, $userId); + $this->leftJoinUserIdAsViewer($qb, $userId); + $this->leftJoinOwner($qb); -// $this->leftjoinOwner($qb); -// $this->buildWithMemberLevel($qb, 'u.level', $level); -// $this->buildWithCircleId($qb, 'c.id', $circleId); -// $this->buildWithOrXTypes($qb, $userId, $type, $name, $circleId); + // If we need the viewer to be at least member of the circle + // $this->limitToLevel($qb, Member::LEVEL_MEMBER, 'u'); + $this->limitRegardingCircleType($qb, $userId, $circleId, Circle::CIRCLES_ALL, ''); $cursor = $qb->execute(); $data = $cursor->fetch(); if ($data === false || $data === null) { - throw new CircleDoesNotExistException( - $this->l10n->t('Circle not found') - ); + throw new CircleDoesNotExistException($this->l10n->t('Circle not found')); } $circle = $this->parseCirclesSelectSql($data); -// if ($circle->getUser()->getLevel) + $circle->setGroupViewer( + $this->membersRequest->forceGetHigherLevelGroupFromUser($circleId, $userId) + ); + return $circle; } diff --git a/lib/Db/CirclesRequestBuilder.php b/lib/Db/CirclesRequestBuilder.php index a12f7244..0556e9a1 100644 --- a/lib/Db/CirclesRequestBuilder.php +++ b/lib/Db/CirclesRequestBuilder.php @@ -29,14 +29,37 @@ namespace OCA\Circles\Db; use Doctrine\DBAL\Query\QueryBuilder; +use OC\L10N\L10N; +use OCA\Circles\Exceptions\ConfigNoCircleAvailable; use OCA\Circles\Model\Circle; use OCA\Circles\Model\FederatedLink; use OCA\Circles\Model\Member; use OCA\Circles\Model\SharingFrame; +use OCA\Circles\Service\MiscService; use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\IDBConnection; class CirclesRequestBuilder extends CoreRequestBuilder { + + /** @var MembersRequest */ + protected $membersRequest; + + /** + * CirclesRequestBuilder constructor. + * + * {@inheritdoc} + * @param MembersRequest $membersRequest + */ + public function __construct( + L10N $l10n, IDBConnection $connection, MembersRequest $membersRequest, + MiscService $miscService + ) { + parent::__construct($l10n, $connection, $miscService); + $this->membersRequest = $membersRequest; + } + + /** * Join the Circles table * @@ -52,6 +75,226 @@ class CirclesRequestBuilder extends CoreRequestBuilder { /** + * Join the Circles table + * + * @param IQueryBuilder $qb + * @param string $field + */ + protected function joinGroups(& $qb, $field) { + $expr = $qb->expr(); + + $qb->from(self::TABLE_GROUPS, 'g') + ->andWhere($expr->eq('g.circle_id', $field)); + } + + + /** + * Link to member (userId) of circle + * + * @param IQueryBuilder $qb + * @param string $field + */ + protected function joinMembers(& $qb, $field) { + $expr = $qb->expr(); + + $qb->from(self::TABLE_MEMBERS, 'm') + ->andWhere($expr->eq('m.circle_id', $field)); + } + + + /** + * Limit the search to a userId, using also the group table from NC + * + * @param IQueryBuilder $qb + * @param $userId + */ + protected function limitToUserId(IQueryBuilder &$qb, $userId) { + $expr = $qb->expr(); + + $pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : ''; + + $this->joinGroups($qb, $pf . 'id'); + $this->joinNCGroupAndUser($qb, 'g.group_id', 'm.user_id'); + $qb->andWhere( + $expr->orX( + $expr->eq('m.user_id', $qb->createNamedParameter($userId)), + $expr->eq('ncgu.uid', $qb->createNamedParameter($userId)) + ) + ); + } + + + /** + * @param IQueryBuilder $qb + * @param $circleId + * @param $userId + * @param $type + * @param $name + * + * @return array + * @throws ConfigNoCircleAvailable + */ + protected function limitRegardingCircleType(IQueryBuilder &$qb, $userId, $circleId, $type, $name + ) { + $orTypes = $this->generateLimit($qb, $circleId, $userId, $type, $name); + if (sizeof($orTypes) === 0) { + throw new ConfigNoCircleAvailable( + $this->l10n->t( + 'You cannot use the Circles Application until your administrator has allowed at least one type of circles' + ) + ); + } + + $orXTypes = $qb->expr() + ->orX(); + foreach ($orTypes as $orType) { + $orXTypes->add($orType); + } + + $qb->andWhere($orXTypes); + + + } + + + /** + * @param IQueryBuilder $qb + * @param $circleId + * @param $userId + * @param $type + * @param $name + * + * @return array + */ + private function generateLimit(IQueryBuilder &$qb, $circleId, $userId, $type, $name) { + $orTypes = []; + array_push($orTypes, $this->generateLimitPersonal($qb, $userId, $type)); + array_push($orTypes, $this->generateLimitHidden($qb, $circleId, $type, $name)); + array_push($orTypes, $this->generateLimitPrivate($qb, $type)); + array_push($orTypes, $this->generateLimitPublic($qb, $type)); + + return array_filter($orTypes); + } + + + /** + * @param IQueryBuilder $qb + * @param int|string $userId + * @param int $type + * + * @return \OCP\DB\QueryBuilder\ICompositeExpression + */ + private function generateLimitPersonal(IQueryBuilder $qb, $userId, $type) { + if (!(Circle::CIRCLES_PERSONAL & (int)$type)) { + return null; + } + $expr = $qb->expr(); + + /** @noinspection PhpMethodParametersCountMismatchInspection */ + return $expr->andX( + $expr->eq('c.type', $qb->createNamedParameter(Circle::CIRCLES_PERSONAL)), + $expr->eq('o.user_id', $qb->createNamedParameter((string)$userId)) + ); + } + + + /** + * @param IQueryBuilder $qb + * @param int $circleId + * @param int $type + * @param string $name + * + * @return string + */ + private function generateLimitHidden(IQueryBuilder $qb, $circleId, $type, $name) { + if (!(Circle::CIRCLES_HIDDEN & (int)$type)) { + return null; + } + $expr = $qb->expr(); + + /** @noinspection PhpMethodParametersCountMismatchInspection */ + $sqb = $expr->andX( + $expr->eq('c.type', $qb->createNamedParameter(Circle::CIRCLES_HIDDEN)), + $expr->orX( + $expr->gte( + 'u.level', $qb->createNamedParameter(Member::LEVEL_MEMBER) + ), + // TODO: Replace search on CircleID By a search on UniqueID + $expr->eq('c.id', $qb->createNamedParameter($circleId)), + $expr->eq('c.name', $qb->createNamedParameter($name)) + ) + ); + + return $sqb; + } + + + /** + * @param IQueryBuilder $qb + * @param int $type + * + * @return string + */ + private function generateLimitPrivate(IQueryBuilder $qb, $type) { + if (!(Circle::CIRCLES_PRIVATE & (int)$type)) { + return null; + } + + return $qb->expr() + ->eq( + 'c.type', + $qb->createNamedParameter(Circle::CIRCLES_PRIVATE) + ); + } + + + /** + * @param IQueryBuilder $qb + * @param int $type + * + * @return string + */ + private function generateLimitPublic(IQueryBuilder $qb, $type) { + if (!(Circle::CIRCLES_PUBLIC & (int)$type)) { + return null; + } + + return $qb->expr() + ->eq( + 'c.type', + $qb->createNamedParameter(Circle::CIRCLES_PUBLIC) + ); + } + + + /** + * @param Circle $circle + * + * @deprecated + * + * do nothing. + */ + protected function filterCircleRegardingViewer(Circle $circle) { +// if ($circle->getHigherViewer() +// ->getLevel() < Member::LEVEL_MODERATOR +// ) { +// } +// $members = $circle->getMembers(); +// +// foreach ($members as $member) { +// $member->setNote('ok'); +// } +//// $circle->setMembers($members); +// +// +// foreach ($members as $member) { +// $this->miscService->log('note: ' . $member->getNote()); +// } + + + } + + /** * add a request to the members list, using the current user ID. * will returns level and stuff. * @@ -67,23 +310,23 @@ class CirclesRequestBuilder extends CoreRequestBuilder { $expr = $qb->expr(); $pf = $this->default_select_alias . '.'; - $qb->selectAlias('u.user_id', 'viewer_userid'); - $qb->selectAlias('u.level', 'viewer_level'); /** @noinspection PhpMethodParametersCountMismatchInspection */ - $qb->leftJoin( - $this->default_select_alias, CoreRequestBuilder::TABLE_MEMBERS, 'u', - $expr->andX( - $expr->eq($pf . 'id', 'u.circle_id'), - $expr->eq('u.user_id', $qb->createNamedParameter($userId)) - ) - ); + $qb->selectAlias('u.user_id', 'viewer_userid') + ->selectAlias('u.status', 'viewer_status') + ->selectAlias('u.level', 'viewer_level') + ->leftJoin( + $this->default_select_alias, CoreRequestBuilder::TABLE_MEMBERS, 'u', + $expr->andX( + $expr->eq($pf . 'id', 'u.circle_id'), + $expr->eq('u.user_id', $qb->createNamedParameter($userId)) + ) + ); } /** - * @param IQueryBuilder $qb + * Left Join members table to get the owner of the circle. * - * @deprecated - * never used in fact. + * @param IQueryBuilder $qb */ protected function leftJoinOwner(IQueryBuilder & $qb) { @@ -95,13 +338,16 @@ class CirclesRequestBuilder extends CoreRequestBuilder { $pf = $this->default_select_alias . '.'; /** @noinspection PhpMethodParametersCountMismatchInspection */ - $qb->leftJoin( - $this->default_select_alias, MembersMapper::TABLENAME, 'o', - $expr->andX( - $expr->eq($pf . 'id', 'o.circle_id'), - $expr->eq('o.level', $qb->createNamedParameter(Member::LEVEL_OWNER)) - ) - ); + $qb->selectAlias('o.user_id', 'owner_userid') + ->selectAlias('o.status', 'owner_status') + ->selectAlias('o.level', 'owner_level') + ->leftJoin( + $this->default_select_alias, CoreRequestBuilder::TABLE_MEMBERS, 'o', + $expr->andX( + $expr->eq($pf . 'id', 'o.circle_id'), + $expr->eq('o.level', $qb->createNamedParameter(Member::LEVEL_OWNER)) + ) + ); } @@ -218,15 +464,17 @@ class CirclesRequestBuilder extends CoreRequestBuilder { $qb = $this->dbConnection->getQueryBuilder(); /** @noinspection PhpMethodParametersCountMismatchInspection */ - $qb->select( - 'c.id', 'c.unique_id', 'c.name', 'c.description', 'c.settings', 'c.type', 'c.creation' - ) - ->from('circles_circles', 'c'); + $qb + ->selectDistinct('c.unique_id') + ->addSelect( + 'c.id', 'c.name', 'c.description', 'c.settings', 'c.type', 'c.creation' + )->from(CoreRequestBuilder::TABLE_CIRCLES, 'c'); $this->default_select_alias = 'c'; return $qb; } + /** * @param array $data * @@ -265,11 +513,30 @@ class CirclesRequestBuilder extends CoreRequestBuilder { if (key_exists('viewer_level', $data)) { $user = new Member($this->l10n); + $user->setStatus($data['viewer_status']); + $user->setCircleId($circle->getId()); $user->setUserId($data['viewer_userid']); $user->setLevel($data['viewer_level']); $circle->setViewer($user); } + if (key_exists('owner_level', $data)) { + $owner = new Member($this->l10n); + $owner->setStatus($data['owner_status']); + $owner->setCircleId($circle->getId()); + $owner->setUserId($data['owner_userid']); + $owner->setLevel($data['owner_level']); + $circle->setOwner($owner); + } + +// if (key_exists('group_level', $data)) +// { +// $group = new Member($this->l10n); +// $group->setGroupId($data['group_id']); +// $group->setLevel($data['group_level']); +// $circle->setGroupViewer($group); +// } + return $circle; } diff --git a/lib/Db/CoreRequestBuilder.php b/lib/Db/CoreRequestBuilder.php index 30b37522..f70d5b30 100644 --- a/lib/Db/CoreRequestBuilder.php +++ b/lib/Db/CoreRequestBuilder.php @@ -21,6 +21,7 @@ class CoreRequestBuilder { const TABLE_MEMBERS = 'circles_members'; const TABLE_GROUPS = 'circles_groups'; + const NC_TABLE_GROUP_USER = 'group_user'; /** @var IDBConnection */ protected $dbConnection; @@ -36,7 +37,7 @@ class CoreRequestBuilder { /** - * CirclesRequest constructor. + * RequestBuilder constructor. * * @param L10N $l10n * @param IDBConnection $connection @@ -109,11 +110,29 @@ class CoreRequestBuilder { * * @param IQueryBuilder $qb * @param integer $level + * @param string $pf + * @param string $pf2 */ - protected function limitToLevel(IQueryBuilder &$qb, $level) { + protected function limitToLevel(IQueryBuilder &$qb, $level, $pf = '', $pf2 = '') { $expr = $qb->expr(); - $pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : ''; - $qb->andWhere($expr->gte($pf . 'level', $qb->createNamedParameter($level))); + if ($pf === '') { + $pf = + ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : ''; + } else { + $pf .= '.'; + } + + if ($pf2 === '') { + $qb->andWhere($expr->gte($pf . 'level', $qb->createNamedParameter($level))); + } else { + $pf2 .= '.'; + $qb->andWhere( + $expr->orX( + $expr->gte($pf . 'level', $qb->createNamedParameter($level)), + $expr->gte($pf2 . 'level', $qb->createNamedParameter($level)) + ) + ); + } } @@ -128,4 +147,46 @@ class CoreRequestBuilder { $qb->andWhere($expr->eq($pf . $field, $qb->createNamedParameter($value))); } + + /** + * link to the groupId/UserId of the NC DB. + * + * @param IQueryBuilder $qb + * @param string $userId + */ + protected function limitToNCGroupUser(IQueryBuilder $qb, $userId) { + $expr = $qb->expr(); + $pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : ''; + + $qb->from(self::NC_TABLE_GROUP_USER, 'ncgu'); + $qb->andWhere( + $expr->andX( + $expr->eq($pf . 'group_id', 'ncgu.gid'), + $expr->eq( + 'ncgu.uid', + $qb->createNamedParameter($userId) + ) + ) + ); + } + + + /** + * link to the groupId/UserId of the NC DB. + * + * @param IQueryBuilder $qb + * @param $fieldGroup + * @param $fieldUser + */ + protected function joinNCGroupAndUser(IQueryBuilder $qb, $fieldGroup, $fieldUser) { + $expr = $qb->expr(); + + $qb->from(self::NC_TABLE_GROUP_USER, 'ncgu'); + $qb->andWhere( + $expr->andX( + $expr->eq('ncgu.gid', $fieldGroup), + $expr->eq('ncgu.uid', $fieldUser) + ) + ); + } }
\ No newline at end of file diff --git a/lib/Db/MembersRequest.php b/lib/Db/MembersRequest.php index 5f4478b6..721e5546 100644 --- a/lib/Db/MembersRequest.php +++ b/lib/Db/MembersRequest.php @@ -41,7 +41,6 @@ class MembersRequest extends MembersRequestBuilder { } - /** * forceGetGroup(); * @@ -75,11 +74,40 @@ class MembersRequest extends MembersRequestBuilder { } - // TODO - returns data of a group from a Viewer POV - public function getGroup($circleId, $groupId, $viewer) { + /** + * return the higher level group linked to a circle, that include the userId. + * + * WARNING: This function does not filters data regarding the current user/viewer. + * In case of interaction with users, Please don't use this. + * + * @param int $circleId + * @param string $userId + * + * @return Member + */ + public function forceGetHigherLevelGroupFromUser($circleId, $userId) { + $qb = $this->getGroupsSelectSql(); + + $this->limitToCircleId($qb, $circleId); + $this->limitToNCGroupUser($qb, $userId); + + /** @var Member $group */ + $group = null; + $cursor = $qb->execute(); + while ($data = $cursor->fetch()) { + $entry = Member::fromArray($this->l10n, $data); + if ($group === null || $entry->getLevel() > $group->getLevel()) { + $group = $entry; + } + } + $cursor->closeCursor(); + + return $group; } + + /** * @param int $circleId * @param Member $viewer diff --git a/lib/Db/MembersRequestBuilder.php b/lib/Db/MembersRequestBuilder.php index 90d5dc1a..5fa12a95 100644 --- a/lib/Db/MembersRequestBuilder.php +++ b/lib/Db/MembersRequestBuilder.php @@ -30,23 +30,14 @@ namespace OCA\Circles\Db; use OCP\DB\QueryBuilder\IQueryBuilder; use Doctrine\DBAL\Query\QueryBuilder; +use OCP\Diagnostics\IQuery; class MembersRequestBuilder extends CoreRequestBuilder { -// /** -// * Limit the request to a minimum member level. -// * -// * @param IQueryBuilder $qb -// * @param integer $level -// */ -// protected function limitToMemberLevel(IQueryBuilder & $qb, $level) { -// $pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : ''; -// $qb->andWhere( -// $qb->expr() -// ->gte($pf . 'level', $qb->createNamedParameter($level)) -// ); -// } + + + /** diff --git a/lib/Model/Circle.php b/lib/Model/Circle.php index 7735224e..bdf06315 100644 --- a/lib/Model/Circle.php +++ b/lib/Model/Circle.php @@ -72,6 +72,8 @@ class Circle extends BaseCircle implements \JsonSerializable { 'name' => $this->getName(), 'owner' => $this->getOwner(), 'user' => $this->getViewer(), + 'group' => $this->getGroupViewer(), + 'viewer' => $this->getHigherViewer(), 'description' => $this->getDescription(), 'settings' => $this->getSettings(), 'type' => $this->getTypeString(), diff --git a/lib/Service/CirclesService.php b/lib/Service/CirclesService.php index 41117134..cb33d0c5 100644 --- a/lib/Service/CirclesService.php +++ b/lib/Service/CirclesService.php @@ -172,7 +172,7 @@ class CirclesService { } $data = []; - $result = $this->dbCircles->findCirclesByUser($this->userId, $type, $name, $level); + $result = $this->circlesRequest->getCircles($this->userId, $type, $name, $level); foreach ($result as $item) { $data[] = $item; } @@ -188,15 +188,16 @@ class CirclesService { * * @return Circle * @throws \Exception -] */ + ] */ public function detailsCircle($circleId) { try { - $circle = $this->dbCircles->getDetailsFromCircle($circleId, $this->userId); - if ($circle->getViewer() + $circle = $this->circlesRequest->getCircle($circleId, $this->userId); + if ($circle->getHigherViewer() ->isLevel(Member::LEVEL_MEMBER) ) { - $members = $this->dbMembers->getMembersFromCircle($circleId, $circle->getViewer()); + $members = + $this->dbMembers->getMembersFromCircle($circleId, $circle->getHigherViewer()); $circle->setMembers($members); $this->detailsCircleLinkedGroups($circle); @@ -209,10 +210,12 @@ class CirclesService { return $circle; } + private function detailsCircleLinkedGroups(Circle &$circle) { $groups = []; if ($this->configService->isLinkedGroupsAllowed()) { - $groups = $this->membersRequest->getGroups($circle->getId(), $circle->getViewer()); + $groups = + $this->membersRequest->getGroups($circle->getId(), $circle->getHigherViewer()); } $circle->setGroups($groups); @@ -246,8 +249,8 @@ class CirclesService { public function settingsCircle($circleId, $settings) { try { - $circle = $this->dbCircles->getDetailsFromCircle($circleId, $this->userId); - $circle->getViewer() + $circle = $this->circlesRequest->getCircle($circleId, $this->userId); + $circle->getHigherViewer() ->hasToBeOwner(); $ak = array_keys($settings); @@ -275,7 +278,7 @@ class CirclesService { public function joinCircle($circleId) { try { - $circle = $this->dbCircles->getDetailsFromCircle($circleId, $this->userId); + $circle = $this->circlesRequest->getCircle($circleId, $this->userId); try { $member = $this->dbMembers->getMemberFromCircle($circle->getId(), $this->userId); @@ -307,8 +310,8 @@ class CirclesService { public function leaveCircle($circleId) { try { - $circle = $this->dbCircles->getDetailsFromCircle($circleId, $this->userId); - $member = $this->dbMembers->getMemberFromCircle($circle->getId(), $this->userId, false); + $circle = $this->circlesRequest->getCircle($circleId, $this->userId); + $member = $circle->getViewer(); if (!$member->isAlmostMember()) { $member->hasToBeMember(); @@ -318,6 +321,7 @@ class CirclesService { $member->setStatus(Member::STATUS_NONMEMBER); $member->setLevel(Member::LEVEL_NONE); $this->dbMembers->editMember($member); + $this->eventsService->onMemberLeaving($circle, $member); } catch (\Exception $e) { throw $e; @@ -337,10 +341,13 @@ class CirclesService { public function removeCircle($circleId) { try { - $member = $this->dbMembers->getMemberFromCircle($circleId, $this->userId, false); - $member->hasToBeOwner(); +// $member = $this->dbMembers->getMemberFromCircle($circleId, $this->userId, false); +// $member->hasToBeOwner(); + + $circle = $this->circlesRequest->getCircle($circleId, $this->userId); + $circle->getHigherViewer() + ->hasToBeOwner(); - $circle = $this->dbCircles->getDetailsFromCircle($circleId, $this->userId); $this->eventsService->onCircleDestruction($circle); $this->dbMembers->removeAllFromCircle($circleId); diff --git a/lib/Service/FederatedService.php b/lib/Service/FederatedService.php index e87da4ac..f91a26fc 100644 --- a/lib/Service/FederatedService.php +++ b/lib/Service/FederatedService.php @@ -197,7 +197,7 @@ class FederatedService { $link = $this->circlesRequest->getLinkFromId($linkId); $circle = $this->circlesRequest->getCircle($link->getCircleId(), $this->userId); $circle->hasToBeFederated(); - $circle->getViewer() + $circle->getHigherViewer() ->hasToBeAdmin(); $link->hasToBeValidStatusUpdate($status); @@ -273,7 +273,7 @@ class FederatedService { list($remoteCircle, $remoteAddress) = explode('@', $remote, 2); $circle = $this->circlesService->detailsCircle($circleId); - $circle->getViewer() + $circle->getHigherViewer() ->hasToBeAdmin(); $circle->hasToBeFederated(); $circle->cantBePersonal(); @@ -306,7 +306,7 @@ class FederatedService { * @return string */ private function generateLinkRemoteURL($remote) { -// $this->allowNonSSLLink(); + $this->allowNonSSLLink(); if ($this->localTest === false && strpos($remote, 'https') !== 0) { $remote = 'https://' . $remote; } @@ -321,7 +321,7 @@ class FederatedService { * @return string */ private function generatePayloadDeliveryURL($remote) { -// $this->allowNonSSLLink(); + $this->allowNonSSLLink(); if ($this->localTest === false && strpos($remote, 'https') !== 0) { $remote = 'https://' . $remote; } diff --git a/lib/Service/GroupsService.php b/lib/Service/GroupsService.php index a8bdd7d9..2ad3246a 100644 --- a/lib/Service/GroupsService.php +++ b/lib/Service/GroupsService.php @@ -27,8 +27,8 @@ namespace OCA\Circles\Service; -use OC\User\NoUserException; use OCA\Circles\Db\CirclesMapper; +use OCA\Circles\Db\CirclesRequest; use OCA\Circles\Db\MembersMapper; use OCA\Circles\Db\MembersRequest; use OCA\Circles\Exceptions\CircleTypeNotValid; @@ -37,10 +37,9 @@ use OCA\Circles\Exceptions\GroupDoesNotExistException; use OCA\Circles\Exceptions\MemberAlreadyExistsException; use OCA\Circles\Exceptions\MemberDoesNotExistException; use OCA\Circles\Model\Circle; -use \OCA\Circles\Model\Member; +use OCA\Circles\Model\Member; use OCP\IGroupManager; use OCP\IL10N; -use OCP\IUserManager; class GroupsService { @@ -53,8 +52,12 @@ class GroupsService { /** @var IGroupManager */ private $groupManager; + /** @var CirclesRequest */ + private $circlesRequest; + /** @var MembersRequest */ private $membersRequest; + /** @var CirclesMapper */ private $dbCircles; @@ -70,17 +73,19 @@ class GroupsService { * @param string $userId * @param IL10N $l10n * @param IGroupManager $groupManager - * @param DatabaseService $databaseService , + * @param DatabaseService $databaseService + * @param CirclesRequest $circlesRequest * @param MembersRequest $membersRequest * @param MiscService $miscService */ public function __construct( $userId, IL10N $l10n, IGroupManager $groupManager, DatabaseService $databaseService, - MembersRequest $membersRequest, MiscService $miscService + CirclesRequest $circlesRequest, MembersRequest $membersRequest, MiscService $miscService ) { $this->userId = $userId; $this->l10n = $l10n; $this->groupManager = $groupManager; + $this->circlesRequest = $circlesRequest; $this->membersRequest = $membersRequest; $this->miscService = $miscService; @@ -99,9 +104,9 @@ class GroupsService { public function linkGroup($circleId, $groupId) { try { - $circle = $this->dbCircles->getDetailsFromCircle($circleId, $this->userId); - $this->dbMembers->getMemberFromCircle($circleId, $this->userId) - ->hasToBeAdmin(); + $circle = $this->circlesRequest->getCircle($circleId, $this->userId); + $circle->getHigherViewer() + ->hasToBeAdmin(); $group = $this->getFreshNewMember($circleId, $groupId); } catch (\Exception $e) { @@ -112,7 +117,7 @@ class GroupsService { $this->membersRequest->editGroup($group); // $this->eventsService->onMemberNew($circle, $group); - return $this->membersRequest->getGroups($circleId, $circle->getViewer()); + return $this->membersRequest->getGroups($circleId, $circle->getHigherViewer()); } @@ -162,7 +167,7 @@ class GroupsService { $level = (int)$level; try { - $circle = $this->dbCircles->getDetailsFromCircle($circleId, $this->userId); + $circle = $this->circlesRequest->getCircle($circleId, $this->userId); if ($circle->getType() === Circle::CIRCLES_PERSONAL) { throw new CircleTypeNotValid( $this->l10n->t('You cannot edit level in a personal circle') @@ -182,7 +187,7 @@ class GroupsService { // $this->eventsService->onMemberLevel($circle, $member); } - return $this->membersRequest->getGroups($circle->getId(), $circle->getViewer()); + return $this->membersRequest->getGroups($circle->getId(), $circle->getHigherViewer()); } catch (\Exception $e) { throw $e; } @@ -224,17 +229,18 @@ class GroupsService { */ public function unlinkGroup($circleId, $groupId) { try { - $isMod = $this->dbMembers->getMemberFromCircle($circleId, $this->userId); - $isMod->hasToBeAdmin(); + $circle = $this->circlesRequest->getCircle($circleId, $this->userId); + $circle->getHigherViewer() + ->hasToBeAdmin(); $group = $this->membersRequest->forceGetGroup($circleId, $groupId); $group->cantBeOwner(); - $isMod->hasToBeHigherLevel($group->getLevel()); + $circle->getHigherViewer() + ->hasToBeHigherLevel($group->getLevel()); $group->setLevel(Member::LEVEL_NONE); $this->membersRequest->editGroup($group); - $circle = $this->dbCircles->getDetailsFromCircle($circleId, $this->userId); // $this->eventsService->onMemberLeaving($circle, $member); @@ -242,7 +248,7 @@ class GroupsService { throw $e; } - return $this->membersRequest->getGroups($circle->getId(), $circle->getViewer()); + return $this->membersRequest->getGroups($circle->getId(), $circle->getHigherViewer()); } diff --git a/lib/Service/MembersService.php b/lib/Service/MembersService.php index 68044a80..e028161a 100644 --- a/lib/Service/MembersService.php +++ b/lib/Service/MembersService.php @@ -29,7 +29,9 @@ namespace OCA\Circles\Service; use OC\User\NoUserException; use OCA\Circles\Db\CirclesMapper; +use OCA\Circles\Db\CirclesRequest; use OCA\Circles\Db\MembersMapper; +use OCA\Circles\Db\MembersRequest; use OCA\Circles\Exceptions\CircleTypeNotValid; use OCA\Circles\Exceptions\GroupDoesNotExistException; use OCA\Circles\Exceptions\MemberAlreadyExistsException; @@ -59,18 +61,39 @@ class MembersService { /** @var MembersMapper */ private $dbMembers; + /** @var CirclesRequest */ + private $circlesRequest; + + /** @var MembersRequest */ + private $membersRequest; + /** @var EventsService */ private $eventsService; /** @var MiscService */ private $miscService; + /** + * MembersService constructor. + * + * @param $userId + * @param IL10N $l10n + * @param IUserManager $userManager + * @param ConfigService $configService + * @param DatabaseService $databaseService + * @param CirclesRequest $circlesRequest + * @param MembersRequest $membersRequest + * @param EventsService $eventsService + * @param MiscService $miscService + */ public function __construct( $userId, IL10N $l10n, IUserManager $userManager, ConfigService $configService, DatabaseService $databaseService, + CirclesRequest $circlesRequest, + MembersRequest $membersRequest, EventsService $eventsService, MiscService $miscService ) { @@ -78,6 +101,8 @@ class MembersService { $this->l10n = $l10n; $this->userManager = $userManager; $this->configService = $configService; + $this->circlesRequest = $circlesRequest; + $this->membersRequest = $membersRequest; $this->eventsService = $eventsService; $this->miscService = $miscService; @@ -96,9 +121,9 @@ class MembersService { public function addMember($circleId, $name) { try { - $circle = $this->dbCircles->getDetailsFromCircle($circleId, $this->userId); - $this->dbMembers->getMemberFromCircle($circleId, $this->userId) - ->hasToBeModerator(); + $circle = $this->circlesRequest->getCircle($circleId, $this->userId); + $circle->getHigherViewer() + ->hasToBeModerator(); } catch (\Exception $e) { throw $e; } @@ -108,12 +133,13 @@ class MembersService { } catch (\Exception $e) { throw $e; } + $member->inviteToCircle($circle->getType()); $this->dbMembers->editMember($member); $this->eventsService->onMemberNew($circle, $member); - return $this->dbMembers->getMembersFromCircle($circleId, $circle->getViewer()); + return $this->dbMembers->getMembersFromCircle($circleId, $circle->getHigherViewer()); } @@ -127,9 +153,9 @@ class MembersService { public function importMembersFromGroup($circleId, $groupId) { try { - $circle = $this->dbCircles->getDetailsFromCircle($circleId, $this->userId); - $this->dbMembers->getMemberFromCircle($circleId, $this->userId) - ->hasToBeModerator(); + $circle = $this->circlesRequest->getCircle($circleId, $this->userId); + $circle->getHigherViewer() + ->hasToBeModerator(); } catch (\Exception $e) { throw $e; } @@ -154,7 +180,7 @@ class MembersService { } } - return $this->dbMembers->getMembersFromCircle($circleId, $circle->getViewer()); + return $this->dbMembers->getMembersFromCircle($circleId, $circle->getHigherViewer()); } @@ -172,8 +198,10 @@ class MembersService { public function getMember($circleId, $userId) { try { - $this->dbMembers->getMemberFromCircle($circleId, $this->userId) - ->hasToBeMember(); + $this->circlesRequest->getCircle($circleId, $this->userId) + ->getHigherViewer() + ->hasToBeMember(); + $member = $this->dbMembers->getMemberFromCircle($circleId, $userId); } catch (\Exception $e) { throw $e; @@ -244,7 +272,7 @@ class MembersService { $level = (int)$level; try { - $circle = $this->dbCircles->getDetailsFromCircle($circleId, $this->userId); + $circle = $this->circlesRequest->getCircle($circleId, $this->userId); if ($circle->getType() === Circle::CIRCLES_PERSONAL) { throw new CircleTypeNotValid( $this->l10n->t('You cannot edit level in a personal circle') @@ -262,7 +290,7 @@ class MembersService { $this->eventsService->onMemberLevel($circle, $member); } - return $this->dbMembers->getMembersFromCircle($circleId, $circle->getViewer()); + return $this->dbMembers->getMembersFromCircle($circleId, $circle->getHigherViewer()); } catch (\Exception $e) { throw $e; } @@ -329,13 +357,15 @@ class MembersService { public function removeMember($circleId, $name) { try { - $isMod = $this->dbMembers->getMemberFromCircle($circleId, $this->userId); - $isMod->hasToBeModerator(); + $circle = $this->circlesRequest->getCircle($circleId, $this->userId); + $circle->getHigherViewer() + ->hasToBeModerator(); $member = $this->dbMembers->getMemberFromCircle($circleId, $name); $member->cantBeOwner(); - $isMod->hasToBeHigherLevel($member->getLevel()); + $circle->getHigherViewer() + ->hasToBeHigherLevel($member->getLevel()); } catch (\Exception $e) { throw $e; } @@ -344,10 +374,9 @@ class MembersService { $member->setLevel(Member::LEVEL_NONE); $this->dbMembers->editMember($member); - $circle = $this->dbCircles->getDetailsFromCircle($circleId, $this->userId); $this->eventsService->onMemberLeaving($circle, $member); - return $this->dbMembers->getMembersFromCircle($circleId, $circle->getViewer()); + return $this->dbMembers->getMembersFromCircle($circleId, $circle->getHigherViewer()); } |