summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorMorris Jobke <hey@morrisjobke.de>2015-06-09 17:09:16 +0200
committerMorris Jobke <hey@morrisjobke.de>2015-06-09 17:09:16 +0200
commit000f3a5f26cae244c7c8fe900d3f6103fe4fa87d (patch)
tree36e827999f9859a3e93cd7dff1be3f9543071a8a /lib
parent7dabbf93403c3123dcdb4bb7a0e36dcc441f0cdf (diff)
parent5865a3af8cedaac7fc9649cf17f660393ea90b9a (diff)
downloadnextcloud-server-000f3a5f26cae244c7c8fe900d3f6103fe4fa87d.tar.gz
nextcloud-server-000f3a5f26cae244c7c8fe900d3f6103fe4fa87d.zip
Merge pull request #16738 from rullzer/ocs_set_password_backport
[stable8] Set password properly in OCS Share API
Diffstat (limited to 'lib')
-rw-r--r--lib/private/share/share.php84
-rw-r--r--lib/public/share.php14
2 files changed, 98 insertions, 0 deletions
diff --git a/lib/private/share/share.php b/lib/private/share/share.php
index e52704be3d8..7c98d87cced 100644
--- a/lib/private/share/share.php
+++ b/lib/private/share/share.php
@@ -22,6 +22,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.
@@ -1136,6 +1140,78 @@ 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('`id` = :shareId')
+ ->setParameter(':shareId', $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`', ':pass')
+ ->where('`id` = :shareId')
+ ->setParameter(':pass', is_null($password) ? 'NULL' : \OC::$server->getHasher()->hash($password))
+ ->setParameter(':shareId', $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.
@@ -2410,4 +2486,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 60e5a6fd85b..08d57305649 100644
--- a/lib/public/share.php
+++ b/lib/public/share.php
@@ -309,6 +309,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