summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorRoeland Jago Douma <roeland@famdouma.nl>2015-03-13 12:29:13 +0100
committerRoeland Jago Douma <roeland@famdouma.nl>2015-06-03 21:21:54 +0200
commitfe1de11b23a33fce6587cf1eae869e3f409e4c68 (patch)
treebbf04806f640ee3bbce807ad8ba534829faaf5f7 /lib
parentd1f99f10036dd859a5fa36cddeb4663665a17c58 (diff)
downloadnextcloud-server-fe1de11b23a33fce6587cf1eae869e3f409e4c68.tar.gz
nextcloud-server-fe1de11b23a33fce6587cf1eae869e3f409e4c68.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.php80
-rw-r--r--lib/public/share.php14
2 files changed, 94 insertions, 0 deletions
diff --git a/lib/private/share/share.php b/lib/private/share/share.php
index e52704be3d8..12640c095fd 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,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.
@@ -2410,4 +2482,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