Completed
Push — master ( 29f215...ede9e3 )
by Roeland
15:55
created
apps/files_sharing/lib/Controller/ShareesAPIController.php 1 patch
Indentation   +222 added lines, -222 removed lines patch added patch discarded remove patch
@@ -40,226 +40,226 @@
 block discarded – undo
40 40
 use OCP\Share\IManager;
41 41
 
42 42
 class ShareesAPIController extends OCSController {
43
-	/** @var IConfig */
44
-	protected $config;
45
-
46
-	/** @var IURLGenerator */
47
-	protected $urlGenerator;
48
-
49
-	/** @var IManager */
50
-	protected $shareManager;
51
-
52
-	/** @var bool */
53
-	protected $shareWithGroupOnly = false;
54
-
55
-	/** @var bool */
56
-	protected $shareeEnumeration = true;
57
-
58
-	/** @var int */
59
-	protected $offset = 0;
60
-
61
-	/** @var int */
62
-	protected $limit = 10;
63
-
64
-	/** @var array */
65
-	protected $result = [
66
-		'exact' => [
67
-			'users' => [],
68
-			'groups' => [],
69
-			'remotes' => [],
70
-			'remote_groups' => [],
71
-			'emails' => [],
72
-			'circles' => [],
73
-			'rooms' => [],
74
-		],
75
-		'users' => [],
76
-		'groups' => [],
77
-		'remotes' => [],
78
-		'remote_groups' => [],
79
-		'emails' => [],
80
-		'lookup' => [],
81
-		'circles' => [],
82
-		'rooms' => [],
83
-	];
84
-
85
-	protected $reachedEndFor = [];
86
-	/** @var ISearch */
87
-	private $collaboratorSearch;
88
-
89
-	/**
90
-	 * @param string $appName
91
-	 * @param IRequest $request
92
-	 * @param IConfig $config
93
-	 * @param IURLGenerator $urlGenerator
94
-	 * @param IManager $shareManager
95
-	 * @param ISearch $collaboratorSearch
96
-	 */
97
-	public function __construct(
98
-		string $appName,
99
-		IRequest $request,
100
-		IConfig $config,
101
-		IURLGenerator $urlGenerator,
102
-		IManager $shareManager,
103
-		ISearch $collaboratorSearch
104
-	) {
105
-		parent::__construct($appName, $request);
106
-
107
-		$this->config = $config;
108
-		$this->urlGenerator = $urlGenerator;
109
-		$this->shareManager = $shareManager;
110
-		$this->collaboratorSearch = $collaboratorSearch;
111
-	}
112
-
113
-	/**
114
-	 * @NoAdminRequired
115
-	 *
116
-	 * @param string $search
117
-	 * @param string $itemType
118
-	 * @param int $page
119
-	 * @param int $perPage
120
-	 * @param int|int[] $shareType
121
-	 * @param bool $lookup
122
-	 * @return DataResponse
123
-	 * @throws OCSBadRequestException
124
-	 */
125
-	public function search(string $search = '', string $itemType = null, int $page = 1, int $perPage = 200, $shareType = null, bool $lookup = true): DataResponse {
126
-
127
-		// only search for string larger than a given threshold
128
-		$threshold = (int)$this->config->getSystemValue('sharing.minSearchStringLength', 0);
129
-		if (strlen($search) < $threshold) {
130
-			return new DataResponse($this->result);
131
-		}
132
-
133
-		// never return more than the max. number of results configured in the config.php
134
-		$maxResults = (int)$this->config->getSystemValue('sharing.maxAutocompleteResults', 0);
135
-		if ($maxResults > 0) {
136
-			$perPage = min($perPage, $maxResults);
137
-		}
138
-		if ($perPage <= 0) {
139
-			throw new OCSBadRequestException('Invalid perPage argument');
140
-		}
141
-		if ($page <= 0) {
142
-			throw new OCSBadRequestException('Invalid page');
143
-		}
144
-
145
-		$shareTypes = [
146
-			Share::SHARE_TYPE_USER,
147
-		];
148
-
149
-		if ($itemType === null) {
150
-			throw new OCSBadRequestException('Missing itemType');
151
-		} elseif ($itemType === 'file' || $itemType === 'folder') {
152
-			if ($this->shareManager->allowGroupSharing()) {
153
-				$shareTypes[] = Share::SHARE_TYPE_GROUP;
154
-			}
155
-
156
-			if ($this->isRemoteSharingAllowed($itemType)) {
157
-				$shareTypes[] = Share::SHARE_TYPE_REMOTE;
158
-			}
159
-
160
-			if ($this->isRemoteGroupSharingAllowed($itemType)) {
161
-				$shareTypes[] = Share::SHARE_TYPE_REMOTE_GROUP;
162
-			}
163
-
164
-			if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_EMAIL)) {
165
-				$shareTypes[] = Share::SHARE_TYPE_EMAIL;
166
-			}
167
-
168
-			if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_ROOM)) {
169
-				$shareTypes[] = Share::SHARE_TYPE_ROOM;
170
-			}
171
-		} else {
172
-			$shareTypes[] = Share::SHARE_TYPE_GROUP;
173
-			$shareTypes[] = Share::SHARE_TYPE_EMAIL;
174
-		}
175
-
176
-		// FIXME: DI
177
-		if (\OC::$server->getAppManager()->isEnabledForUser('circles') && class_exists('\OCA\Circles\ShareByCircleProvider')) {
178
-			$shareTypes[] = Share::SHARE_TYPE_CIRCLE;
179
-		}
180
-
181
-		if (isset($_GET['shareType']) && is_array($_GET['shareType'])) {
182
-			$shareTypes = array_intersect($shareTypes, $_GET['shareType']);
183
-			sort($shareTypes);
184
-		} else if (is_numeric($shareType)) {
185
-			$shareTypes = array_intersect($shareTypes, [(int) $shareType]);
186
-			sort($shareTypes);
187
-		}
188
-
189
-		$this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
190
-		$this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
191
-		$this->limit = (int) $perPage;
192
-		$this->offset = $perPage * ($page - 1);
193
-
194
-		list($result, $hasMoreResults) = $this->collaboratorSearch->search($search, $shareTypes, $lookup, $this->limit, $this->offset);
195
-
196
-		// extra treatment for 'exact' subarray, with a single merge expected keys might be lost
197
-		if(isset($result['exact'])) {
198
-			$result['exact'] = array_merge($this->result['exact'], $result['exact']);
199
-		}
200
-		$this->result = array_merge($this->result, $result);
201
-		$response = new DataResponse($this->result);
202
-
203
-		if ($hasMoreResults) {
204
-			$response->addHeader('Link', $this->getPaginationLink($page, [
205
-				'search' => $search,
206
-				'itemType' => $itemType,
207
-				'shareType' => $shareTypes,
208
-				'perPage' => $perPage,
209
-			]));
210
-		}
211
-
212
-		return $response;
213
-	}
214
-
215
-	/**
216
-	 * Method to get out the static call for better testing
217
-	 *
218
-	 * @param string $itemType
219
-	 * @return bool
220
-	 */
221
-	protected function isRemoteSharingAllowed(string $itemType): bool {
222
-		try {
223
-			// FIXME: static foo makes unit testing unnecessarily difficult
224
-			$backend = \OC\Share\Share::getBackend($itemType);
225
-			return $backend->isShareTypeAllowed(Share::SHARE_TYPE_REMOTE);
226
-		} catch (\Exception $e) {
227
-			return false;
228
-		}
229
-	}
230
-
231
-	protected function isRemoteGroupSharingAllowed(string $itemType): bool {
232
-		try {
233
-			// FIXME: static foo makes unit testing unnecessarily difficult
234
-			$backend = \OC\Share\Share::getBackend($itemType);
235
-			return $backend->isShareTypeAllowed(Share::SHARE_TYPE_REMOTE_GROUP);
236
-		} catch (\Exception $e) {
237
-			return false;
238
-		}
239
-	}
240
-
241
-
242
-	/**
243
-	 * Generates a bunch of pagination links for the current page
244
-	 *
245
-	 * @param int $page Current page
246
-	 * @param array $params Parameters for the URL
247
-	 * @return string
248
-	 */
249
-	protected function getPaginationLink(int $page, array $params): string {
250
-		if ($this->isV2()) {
251
-			$url = $this->urlGenerator->getAbsoluteURL('/ocs/v2.php/apps/files_sharing/api/v1/sharees') . '?';
252
-		} else {
253
-			$url = $this->urlGenerator->getAbsoluteURL('/ocs/v1.php/apps/files_sharing/api/v1/sharees') . '?';
254
-		}
255
-		$params['page'] = $page + 1;
256
-		return '<' . $url . http_build_query($params) . '>; rel="next"';
257
-	}
258
-
259
-	/**
260
-	 * @return bool
261
-	 */
262
-	protected function isV2(): bool {
263
-		return $this->request->getScriptName() === '/ocs/v2.php';
264
-	}
43
+    /** @var IConfig */
44
+    protected $config;
45
+
46
+    /** @var IURLGenerator */
47
+    protected $urlGenerator;
48
+
49
+    /** @var IManager */
50
+    protected $shareManager;
51
+
52
+    /** @var bool */
53
+    protected $shareWithGroupOnly = false;
54
+
55
+    /** @var bool */
56
+    protected $shareeEnumeration = true;
57
+
58
+    /** @var int */
59
+    protected $offset = 0;
60
+
61
+    /** @var int */
62
+    protected $limit = 10;
63
+
64
+    /** @var array */
65
+    protected $result = [
66
+        'exact' => [
67
+            'users' => [],
68
+            'groups' => [],
69
+            'remotes' => [],
70
+            'remote_groups' => [],
71
+            'emails' => [],
72
+            'circles' => [],
73
+            'rooms' => [],
74
+        ],
75
+        'users' => [],
76
+        'groups' => [],
77
+        'remotes' => [],
78
+        'remote_groups' => [],
79
+        'emails' => [],
80
+        'lookup' => [],
81
+        'circles' => [],
82
+        'rooms' => [],
83
+    ];
84
+
85
+    protected $reachedEndFor = [];
86
+    /** @var ISearch */
87
+    private $collaboratorSearch;
88
+
89
+    /**
90
+     * @param string $appName
91
+     * @param IRequest $request
92
+     * @param IConfig $config
93
+     * @param IURLGenerator $urlGenerator
94
+     * @param IManager $shareManager
95
+     * @param ISearch $collaboratorSearch
96
+     */
97
+    public function __construct(
98
+        string $appName,
99
+        IRequest $request,
100
+        IConfig $config,
101
+        IURLGenerator $urlGenerator,
102
+        IManager $shareManager,
103
+        ISearch $collaboratorSearch
104
+    ) {
105
+        parent::__construct($appName, $request);
106
+
107
+        $this->config = $config;
108
+        $this->urlGenerator = $urlGenerator;
109
+        $this->shareManager = $shareManager;
110
+        $this->collaboratorSearch = $collaboratorSearch;
111
+    }
112
+
113
+    /**
114
+     * @NoAdminRequired
115
+     *
116
+     * @param string $search
117
+     * @param string $itemType
118
+     * @param int $page
119
+     * @param int $perPage
120
+     * @param int|int[] $shareType
121
+     * @param bool $lookup
122
+     * @return DataResponse
123
+     * @throws OCSBadRequestException
124
+     */
125
+    public function search(string $search = '', string $itemType = null, int $page = 1, int $perPage = 200, $shareType = null, bool $lookup = true): DataResponse {
126
+
127
+        // only search for string larger than a given threshold
128
+        $threshold = (int)$this->config->getSystemValue('sharing.minSearchStringLength', 0);
129
+        if (strlen($search) < $threshold) {
130
+            return new DataResponse($this->result);
131
+        }
132
+
133
+        // never return more than the max. number of results configured in the config.php
134
+        $maxResults = (int)$this->config->getSystemValue('sharing.maxAutocompleteResults', 0);
135
+        if ($maxResults > 0) {
136
+            $perPage = min($perPage, $maxResults);
137
+        }
138
+        if ($perPage <= 0) {
139
+            throw new OCSBadRequestException('Invalid perPage argument');
140
+        }
141
+        if ($page <= 0) {
142
+            throw new OCSBadRequestException('Invalid page');
143
+        }
144
+
145
+        $shareTypes = [
146
+            Share::SHARE_TYPE_USER,
147
+        ];
148
+
149
+        if ($itemType === null) {
150
+            throw new OCSBadRequestException('Missing itemType');
151
+        } elseif ($itemType === 'file' || $itemType === 'folder') {
152
+            if ($this->shareManager->allowGroupSharing()) {
153
+                $shareTypes[] = Share::SHARE_TYPE_GROUP;
154
+            }
155
+
156
+            if ($this->isRemoteSharingAllowed($itemType)) {
157
+                $shareTypes[] = Share::SHARE_TYPE_REMOTE;
158
+            }
159
+
160
+            if ($this->isRemoteGroupSharingAllowed($itemType)) {
161
+                $shareTypes[] = Share::SHARE_TYPE_REMOTE_GROUP;
162
+            }
163
+
164
+            if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_EMAIL)) {
165
+                $shareTypes[] = Share::SHARE_TYPE_EMAIL;
166
+            }
167
+
168
+            if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_ROOM)) {
169
+                $shareTypes[] = Share::SHARE_TYPE_ROOM;
170
+            }
171
+        } else {
172
+            $shareTypes[] = Share::SHARE_TYPE_GROUP;
173
+            $shareTypes[] = Share::SHARE_TYPE_EMAIL;
174
+        }
175
+
176
+        // FIXME: DI
177
+        if (\OC::$server->getAppManager()->isEnabledForUser('circles') && class_exists('\OCA\Circles\ShareByCircleProvider')) {
178
+            $shareTypes[] = Share::SHARE_TYPE_CIRCLE;
179
+        }
180
+
181
+        if (isset($_GET['shareType']) && is_array($_GET['shareType'])) {
182
+            $shareTypes = array_intersect($shareTypes, $_GET['shareType']);
183
+            sort($shareTypes);
184
+        } else if (is_numeric($shareType)) {
185
+            $shareTypes = array_intersect($shareTypes, [(int) $shareType]);
186
+            sort($shareTypes);
187
+        }
188
+
189
+        $this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
190
+        $this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
191
+        $this->limit = (int) $perPage;
192
+        $this->offset = $perPage * ($page - 1);
193
+
194
+        list($result, $hasMoreResults) = $this->collaboratorSearch->search($search, $shareTypes, $lookup, $this->limit, $this->offset);
195
+
196
+        // extra treatment for 'exact' subarray, with a single merge expected keys might be lost
197
+        if(isset($result['exact'])) {
198
+            $result['exact'] = array_merge($this->result['exact'], $result['exact']);
199
+        }
200
+        $this->result = array_merge($this->result, $result);
201
+        $response = new DataResponse($this->result);
202
+
203
+        if ($hasMoreResults) {
204
+            $response->addHeader('Link', $this->getPaginationLink($page, [
205
+                'search' => $search,
206
+                'itemType' => $itemType,
207
+                'shareType' => $shareTypes,
208
+                'perPage' => $perPage,
209
+            ]));
210
+        }
211
+
212
+        return $response;
213
+    }
214
+
215
+    /**
216
+     * Method to get out the static call for better testing
217
+     *
218
+     * @param string $itemType
219
+     * @return bool
220
+     */
221
+    protected function isRemoteSharingAllowed(string $itemType): bool {
222
+        try {
223
+            // FIXME: static foo makes unit testing unnecessarily difficult
224
+            $backend = \OC\Share\Share::getBackend($itemType);
225
+            return $backend->isShareTypeAllowed(Share::SHARE_TYPE_REMOTE);
226
+        } catch (\Exception $e) {
227
+            return false;
228
+        }
229
+    }
230
+
231
+    protected function isRemoteGroupSharingAllowed(string $itemType): bool {
232
+        try {
233
+            // FIXME: static foo makes unit testing unnecessarily difficult
234
+            $backend = \OC\Share\Share::getBackend($itemType);
235
+            return $backend->isShareTypeAllowed(Share::SHARE_TYPE_REMOTE_GROUP);
236
+        } catch (\Exception $e) {
237
+            return false;
238
+        }
239
+    }
240
+
241
+
242
+    /**
243
+     * Generates a bunch of pagination links for the current page
244
+     *
245
+     * @param int $page Current page
246
+     * @param array $params Parameters for the URL
247
+     * @return string
248
+     */
249
+    protected function getPaginationLink(int $page, array $params): string {
250
+        if ($this->isV2()) {
251
+            $url = $this->urlGenerator->getAbsoluteURL('/ocs/v2.php/apps/files_sharing/api/v1/sharees') . '?';
252
+        } else {
253
+            $url = $this->urlGenerator->getAbsoluteURL('/ocs/v1.php/apps/files_sharing/api/v1/sharees') . '?';
254
+        }
255
+        $params['page'] = $page + 1;
256
+        return '<' . $url . http_build_query($params) . '>; rel="next"';
257
+    }
258
+
259
+    /**
260
+     * @return bool
261
+     */
262
+    protected function isV2(): bool {
263
+        return $this->request->getScriptName() === '/ocs/v2.php';
264
+    }
265 265
 }
Please login to merge, or discard this patch.