001
014
015 package com.liferay.portal.repository;
016
017 import com.liferay.portal.exception.NoSuchRepositoryException;
018 import com.liferay.portal.kernel.bean.BeanReference;
019 import com.liferay.portal.kernel.exception.PortalException;
020 import com.liferay.portal.kernel.repository.InvalidRepositoryIdException;
021 import com.liferay.portal.kernel.repository.LocalRepository;
022 import com.liferay.portal.kernel.repository.Repository;
023 import com.liferay.portal.kernel.repository.RepositoryFactory;
024 import com.liferay.portal.kernel.repository.RepositoryProvider;
025 import com.liferay.portal.kernel.util.StringBundler;
026 import com.liferay.portal.model.Group;
027 import com.liferay.portal.model.RepositoryEntry;
028 import com.liferay.portal.security.permission.ActionKeys;
029 import com.liferay.portal.security.permission.PermissionChecker;
030 import com.liferay.portal.security.permission.PermissionThreadLocal;
031 import com.liferay.portal.service.GroupLocalService;
032 import com.liferay.portal.service.RepositoryEntryLocalService;
033 import com.liferay.portal.service.RepositoryLocalService;
034 import com.liferay.portlet.documentlibrary.exception.NoSuchFileEntryException;
035 import com.liferay.portlet.documentlibrary.exception.NoSuchFileShortcutException;
036 import com.liferay.portlet.documentlibrary.exception.NoSuchFileVersionException;
037 import com.liferay.portlet.documentlibrary.exception.NoSuchFolderException;
038 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
039 import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
040 import com.liferay.portlet.documentlibrary.model.DLFileVersion;
041 import com.liferay.portlet.documentlibrary.model.DLFolder;
042 import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalService;
043 import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
044 import com.liferay.portlet.documentlibrary.service.DLFileShortcutLocalService;
045 import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalService;
046 import com.liferay.portlet.documentlibrary.service.DLFolderLocalService;
047 import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
048 import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
049
050 import java.util.ArrayList;
051 import java.util.List;
052
053
056 public class RepositoryProviderImpl implements RepositoryProvider {
057
058 @Override
059 public LocalRepository getFileEntryLocalRepository(long fileEntryId)
060 throws PortalException {
061
062 try {
063 return getLocalRepository(getFileEntryRepositoryId(fileEntryId));
064 }
065 catch (InvalidRepositoryIdException irie) {
066 StringBundler sb = new StringBundler(3);
067
068 sb.append("No FileEntry exists with the key {fileEntryId=");
069 sb.append(fileEntryId);
070 sb.append("}");
071
072 throw new NoSuchFileEntryException(sb.toString(), irie);
073 }
074 }
075
076 @Override
077 public Repository getFileEntryRepository(long fileEntryId)
078 throws PortalException {
079
080 try {
081 checkFileEntryPermissions(fileEntryId);
082
083 return getRepository(getFileEntryRepositoryId(fileEntryId));
084 }
085 catch (InvalidRepositoryIdException irie) {
086 StringBundler sb = new StringBundler(3);
087
088 sb.append("No FileEntry exists with the key {fileEntryId=");
089 sb.append(fileEntryId);
090 sb.append("}");
091
092 throw new NoSuchFileEntryException(sb.toString(), irie);
093 }
094 }
095
096 @Override
097 public LocalRepository getFileShortcutLocalRepository(long fileShortcutId)
098 throws PortalException {
099
100 try {
101 return getLocalRepository(
102 getFileShortcutRepositoryId(fileShortcutId));
103 }
104 catch (InvalidRepositoryIdException irie) {
105 StringBundler sb = new StringBundler(3);
106
107 sb.append("No FileShortcut exists with the key {fileShortcutId=");
108 sb.append(fileShortcutId);
109 sb.append("}");
110
111 throw new NoSuchFileShortcutException(sb.toString(), irie);
112 }
113 }
114
115 @Override
116 public Repository getFileShortcutRepository(long fileShortcutId)
117 throws PortalException {
118
119 try {
120 checkFileShortcutPermissions(fileShortcutId);
121
122 return getRepository(getFileShortcutRepositoryId(fileShortcutId));
123 }
124 catch (InvalidRepositoryIdException irie) {
125 StringBundler sb = new StringBundler(3);
126
127 sb.append("No FileShortcut exists with the key {fileShortcutId=");
128 sb.append(fileShortcutId);
129 sb.append("}");
130
131 throw new NoSuchFileShortcutException(sb.toString(), irie);
132 }
133 }
134
135 @Override
136 public LocalRepository getFileVersionLocalRepository(long fileVersionId)
137 throws PortalException {
138
139 try {
140 return getLocalRepository(
141 getFileVersionRepositoryId(fileVersionId));
142 }
143 catch (InvalidRepositoryIdException irie) {
144 StringBundler sb = new StringBundler(3);
145
146 sb.append("No FileVersion exists with the key {fileVersionId=");
147 sb.append(fileVersionId);
148 sb.append("}");
149
150 throw new NoSuchFileVersionException(sb.toString(), irie);
151 }
152 }
153
154 @Override
155 public Repository getFileVersionRepository(long fileVersionId)
156 throws PortalException {
157
158 try {
159 checkFileVersionPermissions(fileVersionId);
160
161 return getRepository(getFileVersionRepositoryId(fileVersionId));
162 }
163 catch (InvalidRepositoryIdException irie) {
164 StringBundler sb = new StringBundler(3);
165
166 sb.append("No FileVersion exists with the key {fileVersionId=");
167 sb.append(fileVersionId);
168 sb.append("}");
169
170 throw new NoSuchFileVersionException(sb.toString(), irie);
171 }
172 }
173
174 @Override
175 public LocalRepository getFolderLocalRepository(long folderId)
176 throws PortalException {
177
178 try {
179 return getLocalRepository(getFolderRepositoryId(folderId));
180 }
181 catch (InvalidRepositoryIdException irie) {
182 StringBundler sb = new StringBundler(3);
183
184 sb.append("No Folder exists with the key {folderId=");
185 sb.append(folderId);
186 sb.append("}");
187
188 throw new NoSuchFolderException(sb.toString(), irie);
189 }
190 }
191
192 @Override
193 public Repository getFolderRepository(long folderId)
194 throws PortalException {
195
196 try {
197 checkFolderPermissions(folderId);
198
199 return getRepository(getFolderRepositoryId(folderId));
200 }
201 catch (InvalidRepositoryIdException irie) {
202 StringBundler sb = new StringBundler(3);
203
204 sb.append("No Folder exists with the key {folderId=");
205 sb.append(folderId);
206 sb.append("}");
207
208 throw new NoSuchFolderException(sb.toString(), irie);
209 }
210 }
211
212 @Override
213 public List<LocalRepository> getGroupLocalRepositories(long groupId)
214 throws PortalException {
215
216 List<LocalRepository> localRepositories = new ArrayList<>();
217
218 List<Long> repositoryIds = getGroupRepositoryIds(groupId);
219
220 for (long repositoryId : repositoryIds) {
221 localRepositories.add(getLocalRepository(repositoryId));
222 }
223
224 return localRepositories;
225 }
226
227 @Override
228 public List<Repository> getGroupRepositories(long groupId)
229 throws PortalException {
230
231 List<Repository> repositories = new ArrayList<>();
232
233 List<Long> repositoryIds = getGroupRepositoryIds(groupId);
234
235 for (long repositoryId : repositoryIds) {
236 repositories.add(getRepository(repositoryId));
237 }
238
239 return repositories;
240 }
241
242 @Override
243 public LocalRepository getImageLocalRepository(long imageId)
244 throws PortalException {
245
246 return getLocalRepository(getImageRepositoryId(imageId));
247 }
248
249 @Override
250 public Repository getImageRepository(long imageId) throws PortalException {
251 return getRepository(getImageRepositoryId(imageId));
252 }
253
254 @Override
255 public LocalRepository getLocalRepository(long repositoryId)
256 throws PortalException {
257
258 LocalRepository localRepository =
259 repositoryFactory.createLocalRepository(repositoryId);
260
261 checkRepository(repositoryId);
262 checkRepositoryAccess(repositoryId);
263
264 return localRepository;
265 }
266
267 @Override
268 public Repository getRepository(long repositoryId) throws PortalException {
269 Repository repository = repositoryFactory.createRepository(
270 repositoryId);
271
272 checkRepository(repositoryId);
273 checkRepositoryAccess(repositoryId);
274
275 return repository;
276 }
277
278 protected void checkFileEntryPermissions(long fileEntryId)
279 throws PortalException {
280
281 DLFileEntry dlFileEntry = dlFileEntryLocalService.fetchDLFileEntry(
282 fileEntryId);
283
284 PermissionChecker permissionChecker =
285 PermissionThreadLocal.getPermissionChecker();
286
287 if ((dlFileEntry != null) && (permissionChecker != null)) {
288 DLFileEntryPermission.check(
289 permissionChecker, fileEntryId, ActionKeys.VIEW);
290 }
291 }
292
293 protected void checkFileShortcutPermissions(long fileShortcutId)
294 throws PortalException {
295
296 DLFileShortcut dlFileShortcut =
297 dlFileShortcutLocalService.fetchDLFileShortcut(fileShortcutId);
298
299 PermissionChecker permissionChecker =
300 PermissionThreadLocal.getPermissionChecker();
301
302 if ((dlFileShortcut != null) && (permissionChecker != null)) {
303 DLFileEntryPermission.check(
304 permissionChecker, dlFileShortcut.getToFileEntryId(),
305 ActionKeys.VIEW);
306 }
307 }
308
309 protected void checkFileVersionPermissions(long fileVersionId)
310 throws PortalException {
311
312 DLFileVersion dlFileVersion =
313 dlFileVersionLocalService.fetchDLFileVersion(fileVersionId);
314
315 PermissionChecker permissionChecker =
316 PermissionThreadLocal.getPermissionChecker();
317
318 if ((dlFileVersion != null) && (permissionChecker != null)) {
319 DLFileEntryPermission.check(
320 permissionChecker, dlFileVersion.getFileEntryId(),
321 ActionKeys.VIEW);
322 }
323 }
324
325 protected void checkFolderPermissions(long folderId)
326 throws PortalException {
327
328 DLFolder dlFolder = dlFolderLocalService.fetchDLFolder(folderId);
329
330 PermissionChecker permissionChecker =
331 PermissionThreadLocal.getPermissionChecker();
332
333 if ((dlFolder != null) && (permissionChecker != null)) {
334 DLFolderPermission.check(
335 permissionChecker, dlFolder, ActionKeys.VIEW);
336 }
337 }
338
339 protected void checkRepository(long repositoryId) throws PortalException {
340 Group group = groupLocalService.fetchGroup(repositoryId);
341
342 if (group != null) {
343 return;
344 }
345
346 try {
347 repositoryLocalService.getRepository(repositoryId);
348 }
349 catch (NoSuchRepositoryException nsre) {
350 throw new InvalidRepositoryIdException(nsre.getMessage());
351 }
352 }
353
354 protected void checkRepositoryAccess(long repositoryId)
355 throws PortalException {
356
357 Group group = groupLocalService.fetchGroup(repositoryId);
358
359 if (group != null) {
360 return;
361 }
362
363 try {
364 com.liferay.portal.model.Repository repository =
365 repositoryLocalService.fetchRepository(repositoryId);
366
367 PermissionChecker permissionChecker =
368 PermissionThreadLocal.getPermissionChecker();
369
370 if ((repository != null) && (permissionChecker != null)) {
371 try {
372 DLFolderPermission.check(
373 permissionChecker, repository.getGroupId(),
374 repository.getDlFolderId(), ActionKeys.VIEW);
375 }
376 catch (NoSuchFolderException nsfe) {
377 }
378
379 return;
380 }
381 }
382 catch (NoSuchRepositoryException nsre) {
383 throw new InvalidRepositoryIdException(nsre.getMessage());
384 }
385 }
386
387 protected long getFileEntryRepositoryId(long fileEntryId) {
388 DLFileEntry dlFileEntry = dlFileEntryLocalService.fetchDLFileEntry(
389 fileEntryId);
390
391 if (dlFileEntry != null) {
392 return dlFileEntry.getRepositoryId();
393 }
394
395 RepositoryEntry repositoryEntry =
396 repositoryEntryLocalService.fetchRepositoryEntry(fileEntryId);
397
398 if (repositoryEntry != null) {
399 return repositoryEntry.getRepositoryId();
400 }
401
402 throw new InvalidRepositoryIdException(
403 "No repository associated with file entry " + fileEntryId);
404 }
405
406 protected long getFileShortcutRepositoryId(long fileShortcutId) {
407 DLFileShortcut dlFileShortcut =
408 dlFileShortcutLocalService.fetchDLFileShortcut(fileShortcutId);
409
410 if (dlFileShortcut != null) {
411 return dlFileShortcut.getRepositoryId();
412 }
413
414 throw new InvalidRepositoryIdException(
415 "No repository associated with file shortcut " + fileShortcutId);
416 }
417
418 protected long getFileVersionRepositoryId(long fileVersionId) {
419 DLFileVersion dlFileVersion =
420 dlFileVersionLocalService.fetchDLFileVersion(fileVersionId);
421
422 if (dlFileVersion != null) {
423 return dlFileVersion.getRepositoryId();
424 }
425
426 RepositoryEntry repositoryEntry =
427 repositoryEntryLocalService.fetchRepositoryEntry(fileVersionId);
428
429 if (repositoryEntry != null) {
430 return repositoryEntry.getRepositoryId();
431 }
432
433 throw new InvalidRepositoryIdException(
434 "No repository associated with file version " + fileVersionId);
435 }
436
437 protected long getFolderRepositoryId(long folderId) {
438 DLFolder dlFolder = dlFolderLocalService.fetchDLFolder(folderId);
439
440 if (dlFolder != null) {
441 if (dlFolder.isMountPoint()) {
442 return dlFolder.getGroupId();
443 }
444 else {
445 return dlFolder.getRepositoryId();
446 }
447 }
448
449 RepositoryEntry repositoryEntry =
450 repositoryEntryLocalService.fetchRepositoryEntry(folderId);
451
452 if (repositoryEntry != null) {
453 return repositoryEntry.getRepositoryId();
454 }
455
456 throw new InvalidRepositoryIdException(
457 "No repository associated with folder " + folderId);
458 }
459
460 protected List<Long> getGroupRepositoryIds(long groupId) {
461 List<com.liferay.portal.model.Repository> repositories =
462 repositoryLocalService.getGroupRepositories(groupId);
463
464 List<Long> repositoryIds = new ArrayList<>(repositories.size() + 1);
465
466 for (com.liferay.portal.model.Repository repository : repositories) {
467 repositoryIds.add(repository.getRepositoryId());
468 }
469
470 repositoryIds.add(groupId);
471
472 return repositoryIds;
473 }
474
475 protected long getImageRepositoryId(long imageId) throws PortalException {
476 DLFileEntry dlFileEntry =
477 DLFileEntryServiceUtil.fetchFileEntryByImageId(imageId);
478
479 if (dlFileEntry != null) {
480 return dlFileEntry.getRepositoryId();
481 }
482
483 throw new InvalidRepositoryIdException(
484 "No repository associated with image " + imageId);
485 }
486
487 @BeanReference(type = DLFileEntryLocalService.class)
488 protected DLFileEntryLocalService dlFileEntryLocalService;
489
490 @BeanReference(type = DLFileShortcutLocalService.class)
491 protected DLFileShortcutLocalService dlFileShortcutLocalService;
492
493 @BeanReference(type = DLFileVersionLocalService.class)
494 protected DLFileVersionLocalService dlFileVersionLocalService;
495
496 @BeanReference(type = DLFolderLocalService.class)
497 protected DLFolderLocalService dlFolderLocalService;
498
499 @BeanReference(type = GroupLocalService.class)
500 protected GroupLocalService groupLocalService;
501
502 @BeanReference(type = RepositoryEntryLocalService.class)
503 protected RepositoryEntryLocalService repositoryEntryLocalService;
504
505 @BeanReference(type = RepositoryFactory.class)
506 protected RepositoryFactory repositoryFactory;
507
508 @BeanReference(type = RepositoryLocalService.class)
509 protected RepositoryLocalService repositoryLocalService;
510
511 }