]> source.dussan.org Git - nextcloud-server.git/commitdiff
[Share 2.0] Allow recipient to be passed in to getShareById
authorRoeland Jago Douma <rullzer@owncloud.com>
Fri, 29 Jan 2016 09:07:28 +0000 (10:07 +0100)
committerRoeland Jago Douma <rullzer@owncloud.com>
Tue, 2 Feb 2016 09:41:57 +0000 (10:41 +0100)
* This allows us to retrieve usergroup shares for a given id.
  If the user deleted a share or moved it this will be a different share

lib/private/share20/defaultshareprovider.php
lib/private/share20/manager.php
lib/public/share/imanager.php
lib/public/share/ishareprovider.php
tests/lib/share20/defaultshareprovidertest.php

index 0fa1552a1e7d2bf6b3ae7d834b1716cf6da2247b..261ba5d48847d11cb3e1f5db78794ef949363b4d 100644 (file)
@@ -448,13 +448,9 @@ class DefaultShareProvider implements IShareProvider {
        }
 
        /**
-        * Get share by id
-        *
-        * @param int $id
-        * @return \OCP\Share\IShare
-        * @throws ShareNotFound
+        * @inheritdoc
         */
-       public function getShareById($id) {
+       public function getShareById($id, $recipient = null) {
                $qb = $this->dbConn->getQueryBuilder();
 
                $qb->select('*')
@@ -463,12 +459,11 @@ class DefaultShareProvider implements IShareProvider {
                        ->andWhere(
                                $qb->expr()->in(
                                        'share_type',
-                                       [
-                                               $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER),
-                                               $qb->expr()->literal(\OCP\Share::SHARE_TYPE_GROUP),
-                                               $qb->expr()->literal(\OCP\Share::SHARE_TYPE_LINK),
-                                               $qb->expr()->literal(self::SHARE_TYPE_USERGROUP),
-                                       ]
+                                       $qb->createNamedParameter([
+                                               \OCP\Share::SHARE_TYPE_USER,
+                                               \OCP\Share::SHARE_TYPE_GROUP,
+                                               \OCP\Share::SHARE_TYPE_LINK,
+                                       ], IQueryBuilder::PARAM_INT_ARRAY)
                                )
                        );
                
@@ -486,6 +481,11 @@ class DefaultShareProvider implements IShareProvider {
                        throw new ShareNotFound();
                }
 
+               // If the recipient is set for a group share resolve to that user
+               if ($recipient !== null && $share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
+                       $share = $this->resolveGroupShare($share, $recipient);
+               }
+
                return $share;
        }
 
index e45fa4b40f9368965827ff2c2eeebd5ddb7520f0..3c65f67e486ba81562c0924ac513cd65e94043c0 100644 (file)
@@ -746,14 +746,9 @@ class Manager implements IManager {
        }
 
        /**
-        * Retrieve a share by the share id
-        *
-        * @param string $id
-        * @return Share
-        *
-        * @throws ShareNotFound
+        * @inheritdoc
         */
-       public function getShareById($id) {
+       public function getShareById($id, $recipient = null) {
                if ($id === null) {
                        throw new ShareNotFound();
                }
@@ -761,7 +756,7 @@ class Manager implements IManager {
                list($providerId, $id) = $this->splitFullId($id);
                $provider = $this->factory->getProvider($providerId);
 
-               $share = $provider->getShareById($id);
+               $share = $provider->getShareById($id, $recipient);
 
                return $share;
        }
index b2d9953e9eff4f704a33f11c0ab1674a0fa4df89..6919fea00de980c5a2b6e4c6bc4b3ade9ecc3d2b 100644 (file)
@@ -101,14 +101,18 @@ interface IManager {
        public function getSharedWith(IUser $user, $shareType, $node = null, $limit = 50, $offset = 0);
 
        /**
-        * Retrieve a share by the share id
+        * Retrieve a share by the share id.
+        * If the recipient is set make sure to retrieve the file for that user.
+        * This makes sure that if a user has moved/deleted a group share this
+        * is reflected.
         *
         * @param string $id
-        * @return Share
+        * @param IUser|null $recipient
+        * @return IShare
         * @throws ShareNotFound
         * @since 9.0.0
         */
-       public function getShareById($id);
+       public function getShareById($id, $recipient = null);
 
        /**
         * Get the share by token possible with password
index 8507462cbedd504e72e2bfaec3458d5a63b6edfe..9dc56dc37ad1424fb66811f75d2663b242ad9d26 100644 (file)
@@ -97,11 +97,12 @@ interface IShareProvider {
         * Get share by id
         *
         * @param int $id
+        * @param IUser|null $recipient
         * @return \OCP\Share\IShare
         * @throws ShareNotFound
         * @since 9.0.0
         */
-       public function getShareById($id);
+       public function getShareById($id, $recipient = null);
 
        /**
         * Get shares for a given path
index 28b57435e1da7a8dcb2c3b9158146f684533f51e..4145e9e5ec65afdccc23f881e3d307dabca8810b 100644 (file)
@@ -247,6 +247,44 @@ class DefaultShareProviderTest extends \Test\TestCase {
                $this->assertEquals('myTarget', $share->getTarget());
        }
 
+       public function testGetShareByIdUserGroupShare() {
+               $id = $this->addShareToDB(\OCP\Share::SHARE_TYPE_GROUP, 'group0', 'user0', 'user0', 'file', 42, 'myTarget', 31, null, null);
+               $this->addShareToDB(2, 'user1', 'user0', 'user0', 'file', 42, 'userTarget', 0, null, null, $id);
+
+               $user0 = $this->getMock('OCP\IUser');
+               $user0->method('getUID')->willReturn('user0');
+               $user1 = $this->getMock('OCP\IUser');
+               $user1->method('getUID')->willReturn('user1');
+
+               $group0 = $this->getMock('OCP\IGroup');
+               $group0->method('inGroup')->with($user1)->willReturn(true);
+
+               $node = $this->getMock('\OCP\Files\Folder');
+               $node->method('getId')->willReturn(42);
+
+               $this->rootFolder->method('getUserFolder')->with('user0')->will($this->returnSelf());
+               $this->rootFolder->method('getById')->willReturn([$node]);
+
+               $this->userManager->method('get')->will($this->returnValueMap([
+                       ['user0', $user0],
+                       ['user1', $user1],
+               ]));
+               $this->groupManager->method('get')->with('group0')->willReturn($group0);
+
+               $share = $this->provider->getShareById($id, $user1);
+
+               $this->assertEquals($id, $share->getId());
+               $this->assertEquals(\OCP\Share::SHARE_TYPE_GROUP, $share->getShareType());
+               $this->assertSame($group0, $share->getSharedWith());
+               $this->assertSame($user0, $share->getSharedBy());
+               $this->assertSame($user0, $share->getShareOwner());
+               $this->assertSame($node, $share->getNode());
+               $this->assertEquals(0, $share->getPermissions());
+               $this->assertEquals(null, $share->getToken());
+               $this->assertEquals(null, $share->getExpirationDate());
+               $this->assertEquals('userTarget', $share->getTarget());
+       }
+
        public function testGetShareByIdLinkShare() {
                $qb = $this->dbConn->getQueryBuilder();