2014-02-18 14:07:03 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Björn Schießle <schiessle@owncloud.com>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-01-12 14:02:16 +00:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2014-02-18 14:07:03 +00:00
|
|
|
*
|
2016-01-12 14:02:16 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
2015-03-26 10:44:34 +00:00
|
|
|
* @license AGPL-3.0
|
2014-02-18 14:07:03 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
2014-02-18 14:07:03 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2014-02-18 14:07:03 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-03-26 10:44:34 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2014-02-18 14:07:03 +00:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2014-02-18 14:07:03 +00:00
|
|
|
namespace OC\Share;
|
|
|
|
|
|
|
|
class Hooks extends \OC\Share\Constants {
|
2015-07-03 15:04:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* remember which targets need to be updated in the post addToGroup Hook
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $updateTargets = array();
|
|
|
|
|
2014-05-11 20:51:30 +00:00
|
|
|
/**
|
2014-02-18 14:07:03 +00:00
|
|
|
* Function that is called after a user is deleted. Cleans up the shares of that user.
|
2014-05-11 20:51:30 +00:00
|
|
|
* @param array $arguments
|
2014-02-18 14:07:03 +00:00
|
|
|
*/
|
|
|
|
public static function post_deleteUser($arguments) {
|
|
|
|
// Delete any items shared with the deleted user
|
|
|
|
$query = \OC_DB::prepare('DELETE FROM `*PREFIX*share`'
|
|
|
|
.' WHERE `share_with` = ? AND `share_type` = ? OR `share_type` = ?');
|
2015-04-18 14:17:15 +00:00
|
|
|
$query->execute(array($arguments['uid'], self::SHARE_TYPE_USER, self::$shareTypeGroupUserUnique));
|
2014-02-18 14:07:03 +00:00
|
|
|
// Delete any items the deleted user shared
|
|
|
|
$query = \OC_DB::prepare('SELECT `id` FROM `*PREFIX*share` WHERE `uid_owner` = ?');
|
|
|
|
$result = $query->execute(array($arguments['uid']));
|
|
|
|
while ($item = $result->fetchRow()) {
|
|
|
|
Helper::delete($item['id']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-03 15:04:05 +00:00
|
|
|
|
2014-02-18 14:07:03 +00:00
|
|
|
/**
|
2015-07-03 15:04:05 +00:00
|
|
|
* Function that is called before a user is added to a group.
|
|
|
|
* check if we need to create a unique target for the user
|
2014-05-11 20:51:30 +00:00
|
|
|
* @param array $arguments
|
2014-02-18 14:07:03 +00:00
|
|
|
*/
|
2015-07-03 15:04:05 +00:00
|
|
|
public static function pre_addToGroup($arguments) {
|
|
|
|
/** @var \OC\DB\Connection $db */
|
|
|
|
$db = \OC::$server->getDatabaseConnection();
|
|
|
|
|
|
|
|
$insert = $db->createQueryBuilder();
|
2014-08-26 22:30:13 +00:00
|
|
|
|
2015-07-03 15:04:05 +00:00
|
|
|
$select = $db->createQueryBuilder();
|
2014-02-18 14:07:03 +00:00
|
|
|
// Find the group shares and check if the user needs a unique target
|
2015-07-03 15:04:05 +00:00
|
|
|
$select->select('*')
|
|
|
|
->from('`*PREFIX*share`')
|
|
|
|
->where($select->expr()->andX(
|
|
|
|
$select->expr()->eq('`share_type`', ':shareType'),
|
|
|
|
$select->expr()->eq('`share_with`', ':shareWith')
|
|
|
|
))
|
|
|
|
->setParameter('shareType', self::SHARE_TYPE_GROUP)
|
|
|
|
->setParameter('shareWith', $arguments['gid']);
|
2014-08-26 22:30:13 +00:00
|
|
|
|
2015-07-03 15:04:05 +00:00
|
|
|
$result = $select->execute();
|
2014-08-26 22:30:13 +00:00
|
|
|
|
2015-07-03 15:04:05 +00:00
|
|
|
while ($item = $result->fetch()) {
|
|
|
|
|
|
|
|
$itemTarget = Helper::generateTarget(
|
|
|
|
$item['item_type'],
|
|
|
|
$item['item_source'],
|
|
|
|
self::SHARE_TYPE_USER,
|
|
|
|
$arguments['uid'],
|
|
|
|
$item['uid_owner'],
|
|
|
|
null,
|
|
|
|
$item['parent']
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($item['item_type'] === 'file' || $item['item_type'] === 'folder') {
|
|
|
|
$fileTarget = Helper::generateTarget(
|
|
|
|
$item['item_type'],
|
|
|
|
$item['file_target'],
|
|
|
|
self::SHARE_TYPE_USER,
|
|
|
|
$arguments['uid'],
|
|
|
|
$item['uid_owner'],
|
|
|
|
null,
|
|
|
|
$item['parent']
|
|
|
|
);
|
2014-02-18 14:07:03 +00:00
|
|
|
} else {
|
2015-07-03 15:04:05 +00:00
|
|
|
$fileTarget = null;
|
2014-02-18 14:07:03 +00:00
|
|
|
}
|
2014-08-26 22:30:13 +00:00
|
|
|
|
2015-07-03 15:04:05 +00:00
|
|
|
|
2014-02-18 14:07:03 +00:00
|
|
|
// Insert an extra row for the group share if the item or file target is unique for this user
|
2015-07-03 15:04:05 +00:00
|
|
|
if (
|
|
|
|
($fileTarget === null && $itemTarget != $item['item_target'])
|
|
|
|
|| ($fileTarget !== null && $fileTarget !== $item['file_target'])
|
|
|
|
) {
|
|
|
|
self::$updateTargets[$arguments['gid']][] = [
|
|
|
|
'`item_type`' => $insert->expr()->literal($item['item_type']),
|
|
|
|
'`item_source`' => $insert->expr()->literal($item['item_source']),
|
|
|
|
'`item_target`' => $insert->expr()->literal($itemTarget),
|
|
|
|
'`file_target`' => $insert->expr()->literal($fileTarget),
|
|
|
|
'`parent`' => $insert->expr()->literal($item['id']),
|
|
|
|
'`share_type`' => $insert->expr()->literal(self::$shareTypeGroupUserUnique),
|
|
|
|
'`share_with`' => $insert->expr()->literal($arguments['uid']),
|
|
|
|
'`uid_owner`' => $insert->expr()->literal($item['uid_owner']),
|
|
|
|
'`permissions`' => $insert->expr()->literal($item['permissions']),
|
|
|
|
'`stime`' => $insert->expr()->literal($item['stime']),
|
|
|
|
'`file_source`' => $insert->expr()->literal($item['file_source']),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function that is called after a user is added to a group.
|
|
|
|
* add unique target for the user if needed
|
|
|
|
* @param array $arguments
|
|
|
|
*/
|
|
|
|
public static function post_addToGroup($arguments) {
|
|
|
|
/** @var \OC\DB\Connection $db */
|
|
|
|
$db = \OC::$server->getDatabaseConnection();
|
|
|
|
|
|
|
|
$insert = $db->createQueryBuilder();
|
|
|
|
$insert->insert('`*PREFIX*share`');
|
|
|
|
|
|
|
|
if (isset(self::$updateTargets[$arguments['gid']])) {
|
|
|
|
foreach (self::$updateTargets[$arguments['gid']] as $newTarget) {
|
|
|
|
$insert->values($newTarget);
|
|
|
|
$insert->execute();
|
2014-02-18 14:07:03 +00:00
|
|
|
}
|
2015-07-03 15:04:05 +00:00
|
|
|
unset(self::$updateTargets[$arguments['gid']]);
|
2014-02-18 14:07:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function that is called after a user is removed from a group. Shares are cleaned up.
|
2014-05-11 20:51:30 +00:00
|
|
|
* @param array $arguments
|
2014-02-18 14:07:03 +00:00
|
|
|
*/
|
|
|
|
public static function post_removeFromGroup($arguments) {
|
|
|
|
$sql = 'SELECT `id`, `share_type` FROM `*PREFIX*share`'
|
|
|
|
.' WHERE (`share_type` = ? AND `share_with` = ?) OR (`share_type` = ? AND `share_with` = ?)';
|
|
|
|
$result = \OC_DB::executeAudited($sql, array(self::SHARE_TYPE_GROUP, $arguments['gid'],
|
|
|
|
self::$shareTypeGroupUserUnique, $arguments['uid']));
|
|
|
|
while ($item = $result->fetchRow()) {
|
|
|
|
if ($item['share_type'] == self::SHARE_TYPE_GROUP) {
|
|
|
|
// Delete all reshares by this user of the group share
|
|
|
|
Helper::delete($item['id'], true, $arguments['uid']);
|
|
|
|
} else {
|
|
|
|
Helper::delete($item['id']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function that is called after a group is removed. Cleans up the shares to that group.
|
2014-05-11 20:51:30 +00:00
|
|
|
* @param array $arguments
|
2014-02-18 14:07:03 +00:00
|
|
|
*/
|
|
|
|
public static function post_deleteGroup($arguments) {
|
|
|
|
$sql = 'SELECT `id` FROM `*PREFIX*share` WHERE `share_type` = ? AND `share_with` = ?';
|
|
|
|
$result = \OC_DB::executeAudited($sql, array(self::SHARE_TYPE_GROUP, $arguments['gid']));
|
|
|
|
while ($item = $result->fetchRow()) {
|
|
|
|
Helper::delete($item['id']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|