1 | <?php |
||
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) { |
||
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) { |
||
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) { |
||
176 | |||
177 | } |
||
178 |