Completed
Push — master ( 8932ec...0de15a )
by Joas
14:56
created

Hooks::post_deleteUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 12
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
/**
3
 * @author Björn Schießle <[email protected]>
4
 * @author Morris Jobke <[email protected]>
5
 * @author Robin McCorkell <[email protected]>
6
 * @author Roeland Jago Douma <[email protected]>
7
 *
8
 * @copyright Copyright (c) 2016, ownCloud, Inc.
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
namespace OC\Share;
26
27
class Hooks extends \OC\Share\Constants {
28
29
	/**
30
	 * remember which targets need to be updated in the post addToGroup Hook
31
	 * @var array
32
	 */
33
	private static $updateTargets = array();
34
35
	/**
36
	 * Function that is called before a user is added to a group.
37
	 * check if we need to create a unique target for the user
38
	 * @param array $arguments
39
	 */
40
	public static function pre_addToGroup($arguments) {
41
		$currentUser = \OC::$server->getUserSession()->getUser();
42
		$currentUserID = is_null($currentUser) ? '' : $currentUser->getUID();
43
44
		// setup filesystem for added user if it isn't the current user
45
		if($currentUserID !== $arguments['uid']) {
46
			\OC_Util::tearDownFS();
47
			\OC_Util::setupFS($arguments['uid']);
48
		}
49
50
		/** @var \OC\DB\Connection $db */
51
		$db = \OC::$server->getDatabaseConnection();
52
53
		$insert = $db->createQueryBuilder();
54
55
		$select = $db->createQueryBuilder();
56
		// Find the group shares and check if the user needs a unique target
57
		$select->select('*')
58
			->from('`*PREFIX*share`')
59
			->where($select->expr()->andX(
60
				$select->expr()->eq('`share_type`', ':shareType'),
61
				$select->expr()->eq('`share_with`', ':shareWith')
62
			))
63
			->setParameter('shareType', self::SHARE_TYPE_GROUP)
64
			->setParameter('shareWith', $arguments['gid']);
65
66
		$result = $select->execute();
67
68
		while ($item = $result->fetch()) {
69
70
			$itemTarget = Helper::generateTarget(
71
				$item['item_type'],
72
				$item['item_source'],
73
				self::SHARE_TYPE_USER,
74
				$arguments['uid'],
75
				$item['uid_owner'],
76
				null,
77
				$item['parent']
78
			);
79
80
			if ($item['item_type'] === 'file' || $item['item_type'] === 'folder') {
81
				$fileTarget = Helper::generateTarget(
82
					$item['item_type'],
83
					$item['file_target'],
84
					self::SHARE_TYPE_USER,
85
					$arguments['uid'],
86
					$item['uid_owner'],
87
					null,
88
					$item['parent']
89
				);
90
			} else {
91
				$fileTarget = null;
92
			}
93
94
95
			// Insert an extra row for the group share if the item or file target is unique for this user
96
			if (
97
				($fileTarget === null && $itemTarget != $item['item_target'])
98
				|| ($fileTarget !== null && $fileTarget !== $item['file_target'])
99
			) {
100
				self::$updateTargets[$arguments['gid']][] = [
101
					'`item_type`' => $insert->expr()->literal($item['item_type']),
102
					'`item_source`' => $insert->expr()->literal($item['item_source']),
103
					'`item_target`' => $insert->expr()->literal($itemTarget),
104
					'`file_target`' => $insert->expr()->literal($fileTarget),
105
					'`parent`' => $insert->expr()->literal($item['id']),
106
					'`share_type`' => $insert->expr()->literal(self::$shareTypeGroupUserUnique),
107
					'`share_with`' => $insert->expr()->literal($arguments['uid']),
108
					'`uid_owner`' => $insert->expr()->literal($item['uid_owner']),
109
					'`permissions`' => $insert->expr()->literal($item['permissions']),
110
					'`stime`' => $insert->expr()->literal($item['stime']),
111
					'`file_source`' => $insert->expr()->literal($item['file_source']),
112
				];
113
			}
114
		}
115
116
		// re-setup old filesystem state
117
		if($currentUserID !== $arguments['uid']) {
118
			\OC_Util::tearDownFS();
119
			if($currentUserID !== '') {
120
				\OC_Util::setupFS($currentUserID);
121
			}
122
		}
123
	}
124
125
	/**
126
	 * Function that is called after a user is added to a group.
127
	 * add unique target for the user if needed
128
	 * @param array $arguments
129
	 */
130
	public static function post_addToGroup($arguments) {
131
		/** @var \OC\DB\Connection $db */
132
		$db = \OC::$server->getDatabaseConnection();
133
134
		$insert = $db->createQueryBuilder();
135
		$insert->insert('`*PREFIX*share`');
136
137
		if (isset(self::$updateTargets[$arguments['gid']])) {
138
			foreach (self::$updateTargets[$arguments['gid']] as $newTarget) {
139
				$insert->values($newTarget);
140
				$insert->execute();
141
			}
142
			unset(self::$updateTargets[$arguments['gid']]);
143
		}
144
	}
145
146
	/**
147
	 * Function that is called after a user is removed from a group. Shares are cleaned up.
148
	 * @param array $arguments
149
	 */
150
	public static function post_removeFromGroup($arguments) {
151
		$sql = 'SELECT `id`, `share_type` FROM `*PREFIX*share`'
152
			.' WHERE (`share_type` = ? AND `share_with` = ?) OR (`share_type` = ? AND `share_with` = ?)';
153
		$result = \OC_DB::executeAudited($sql, array(self::SHARE_TYPE_GROUP, $arguments['gid'],
154
			self::$shareTypeGroupUserUnique, $arguments['uid']));
155
		while ($item = $result->fetchRow()) {
156
			if ($item['share_type'] == self::SHARE_TYPE_GROUP) {
157
				// Delete all reshares by this user of the group share
158
				Helper::delete($item['id'], true, $arguments['uid']);
159
			} else {
160
				Helper::delete($item['id']);
161
			}
162
		}
163
	}
164
165
	/**
166
	 * Function that is called after a group is removed. Cleans up the shares to that group.
167
	 * @param array $arguments
168
	 */
169
	public static function post_deleteGroup($arguments) {
170
		$sql = 'SELECT `id` FROM `*PREFIX*share` WHERE `share_type` = ? AND `share_with` = ?';
171
		$result = \OC_DB::executeAudited($sql, array(self::SHARE_TYPE_GROUP, $arguments['gid']));
172
		while ($item = $result->fetchRow()) {
173
			Helper::delete($item['id']);
174
		}
175
	}
176
177
}
178