diff options
author | Roeland Jago Douma <roeland@famdouma.nl> | 2015-03-13 12:29:13 +0100 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2015-03-30 14:00:50 +0200 |
commit | b0aa17b13fe504445a3108e46a56031ca4b73bc6 (patch) | |
tree | f1f9f08495bda85bcb713416fc08679aedf4f05a /lib | |
parent | 00b2be11dda5934998636ae774d29a16bd6d1f10 (diff) | |
download | nextcloud-server-b0aa17b13fe504445a3108e46a56031ca4b73bc6.tar.gz nextcloud-server-b0aa17b13fe504445a3108e46a56031ca4b73bc6.zip |
OCS Fixes to allow setting of password without removing additional settings
- Added setPassword to share.php
- Fixed OCS API call
- Added unit tests
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/share/share.php | 80 | ||||
-rw-r--r-- | lib/public/share.php | 14 |
2 files changed, 94 insertions, 0 deletions
diff --git a/lib/private/share/share.php b/lib/private/share/share.php index 90f3f28f2ee..38fd34e9760 100644 --- a/lib/private/share/share.php +++ b/lib/private/share/share.php @@ -37,6 +37,10 @@ namespace OC\Share; +use OCP\IUserSession; +use OC\DB\Connection; +use OCP\IConfig; + /** * This class provides the ability for apps to share their content between users. * Apps must create a backend class that implements OCP\Share_Backend and register it with this class. @@ -1151,6 +1155,74 @@ class Share extends \OC\Share\Constants { } /** + * Retrieve the owner of a connection + * + * @param Connection $connection + * @param int $shareId + * @throws \Exception + * @return string uid of share owner + */ + private static function getShareOwner(Connection $connection, $shareId) { + $qb = $connection->createQueryBuilder(); + + $qb->select('`uid_owner`') + ->from('`*PREFIX*share`') + ->where($qb->expr()->eq('`id`', $shareId)); + $result = $qb->execute(); + $result = $result->fetch(); + + if (empty($result)) { + throw new \Exception('Share not found'); + } + + return $result['uid_owner']; + } + + /** + * Set expiration date for a share + * + * @param IUserSession $userSession + * @param Connection $connection + * @param IConfig $config + * @param int $shareId + * @param string $password + * @throws \Exception + * @return boolean + */ + public static function setPassword(IUserSession $userSession, + Connection $connection, + IConfig $config, + $shareId, $password) { + $user = $userSession->getUser(); + if (is_null($user)) { + throw new \Exception("User not logged in"); + } + + $uid = self::getShareOwner($connection, $shareId); + + if ($uid !== $user->getUID()) { + throw new \Exception('Cannot update share of a different user'); + } + + if ($password === '') { + $password = null; + } + + //If passwords are enforced the password can't be null + if (self::enforcePassword($config) && is_null($password)) { + throw new \Exception('Cannot remove password'); + } + + $qb = $connection->createQueryBuilder(); + $qb->update('`*PREFIX*share`') + ->set('`share_with`', is_null($password) ? 'NULL' : $qb->expr()->literal(\OC::$server->getHasher()->hash($password))) + ->where($qb->expr()->eq('`id`', $shareId)); + $qb->execute(); + + return true; + } + + /** * Checks whether a share has expired, calls unshareItem() if yes. * @param array $item Share data (usually database row) * @return boolean True if item was expired, false otherwise. @@ -2429,4 +2501,12 @@ class Share extends \OC\Share\Constants { return false; } + /** + * @param IConfig $config + * @return bool + */ + public static function enforcePassword(IConfig $config) { + $enforcePassword = $config->getAppValue('core', 'shareapi_enforce_links_password', 'no'); + return ($enforcePassword === "yes") ? true : false; + } } diff --git a/lib/public/share.php b/lib/public/share.php index d27c1825d62..fcdcc6a6d2c 100644 --- a/lib/public/share.php +++ b/lib/public/share.php @@ -318,6 +318,20 @@ class Share extends \OC\Share\Constants { } /** + * Set expiration date for a share + * @param int $shareId + * @param string $password + * @return boolean + */ + public static function setPassword($shareId, $password) { + $userSession = \OC::$server->getUserSession(); + $connection = \OC::$server->getDatabaseConnection(); + $config = \OC::$server->getConfig(); + return \OC\Share\Share::setPassword($userSession, $connection, $config, $shareId, $password); + } + + + /** * Get the backend class for the specified item type * @param string $itemType * @return Share_Backend |