001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.repository;
016    
017    import com.liferay.portal.NoSuchRepositoryException;
018    import com.liferay.portal.kernel.bean.BeanReference;
019    import com.liferay.portal.kernel.cache.CacheRegistryItem;
020    import com.liferay.portal.kernel.exception.PortalException;
021    import com.liferay.portal.kernel.repository.InvalidRepositoryIdException;
022    import com.liferay.portal.kernel.repository.LocalRepository;
023    import com.liferay.portal.kernel.repository.Repository;
024    import com.liferay.portal.kernel.repository.RepositoryFactory;
025    import com.liferay.portal.kernel.repository.RepositoryProvider;
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.NoSuchFolderException;
035    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
036    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
037    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
038    import com.liferay.portlet.documentlibrary.model.DLFolder;
039    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalService;
040    import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
041    import com.liferay.portlet.documentlibrary.service.DLFileShortcutLocalService;
042    import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalService;
043    import com.liferay.portlet.documentlibrary.service.DLFolderLocalService;
044    import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
045    import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
046    
047    import java.util.ArrayList;
048    import java.util.List;
049    import java.util.Map;
050    import java.util.concurrent.ConcurrentHashMap;
051    
052    /**
053     * @author Iv??n Zaera
054     */
055    public class RepositoryProviderImpl
056            implements RepositoryProvider, CacheRegistryItem {
057    
058            @Override
059            public LocalRepository getFileEntryLocalRepository(long fileEntryId)
060                    throws PortalException {
061    
062                    return getLocalRepository(getFileEntryRepositoryId(fileEntryId));
063            }
064    
065            @Override
066            public Repository getFileEntryRepository(long fileEntryId)
067                    throws PortalException {
068    
069                    checkFileEntryPermissions(fileEntryId);
070    
071                    return getRepository(getFileEntryRepositoryId(fileEntryId));
072            }
073    
074            @Override
075            public LocalRepository getFileShortcutLocalRepository(long fileShortcutId)
076                    throws PortalException {
077    
078                    return getLocalRepository(getFileShortcutRepositoryId(fileShortcutId));
079            }
080    
081            @Override
082            public Repository getFileShortcutRepository(long fileShortcutId)
083                    throws PortalException {
084    
085                    checkFileShortcutPermissions(fileShortcutId);
086    
087                    return getRepository(getFileShortcutRepositoryId(fileShortcutId));
088            }
089    
090            @Override
091            public LocalRepository getFileVersionLocalRepository(long fileVersionId)
092                    throws PortalException {
093    
094                    return getLocalRepository(getFileVersionRepositoryId(fileVersionId));
095            }
096    
097            @Override
098            public Repository getFileVersionRepository(long fileVersionId)
099                    throws PortalException {
100    
101                    checkFileVersionPermissions(fileVersionId);
102    
103                    return getRepository(getFileVersionRepositoryId(fileVersionId));
104            }
105    
106            @Override
107            public LocalRepository getFolderLocalRepository(long folderId)
108                    throws PortalException {
109    
110                    return getLocalRepository(getFolderRepositoryId(folderId));
111            }
112    
113            @Override
114            public Repository getFolderRepository(long folderId)
115                    throws PortalException {
116    
117                    checkFolderPermissions(folderId);
118    
119                    return getRepository(getFolderRepositoryId(folderId));
120            }
121    
122            @Override
123            public List<LocalRepository> getGroupLocalRepositories(long groupId)
124                    throws PortalException {
125    
126                    List<LocalRepository> localRepositories = new ArrayList<>();
127    
128                    List<Long> repositoryIds = getGroupRepositoryIds(groupId);
129    
130                    for (long repositoryId : repositoryIds) {
131                            localRepositories.add(getLocalRepository(repositoryId));
132                    }
133    
134                    return localRepositories;
135            }
136    
137            @Override
138            public List<Repository> getGroupRepositories(long groupId)
139                    throws PortalException {
140    
141                    List<Repository> repositories = new ArrayList<>();
142    
143                    List<Long> repositoryIds = getGroupRepositoryIds(groupId);
144    
145                    for (long repositoryId : repositoryIds) {
146                            repositories.add(getRepository(repositoryId));
147                    }
148    
149                    return repositories;
150            }
151    
152            @Override
153            public LocalRepository getImageLocalRepository(long imageId)
154                    throws PortalException {
155    
156                    return getLocalRepository(getImageRepositoryId(imageId));
157            }
158    
159            @Override
160            public Repository getImageRepository(long imageId) throws PortalException {
161                    return getRepository(getImageRepositoryId(imageId));
162            }
163    
164            @Override
165            public LocalRepository getLocalRepository(long repositoryId)
166                    throws PortalException {
167    
168                    LocalRepository localRepository = _localRepositories.get(repositoryId);
169    
170                    if (localRepository != null) {
171                            return localRepository;
172                    }
173    
174                    localRepository = repositoryFactory.createLocalRepository(repositoryId);
175    
176                    checkRepository(repositoryId);
177                    checkRepositoryAccess(repositoryId);
178    
179                    _localRepositories.put(repositoryId, localRepository);
180    
181                    return localRepository;
182            }
183    
184            @Override
185            public String getRegistryName() {
186                    return RepositoryProviderImpl.class.getName();
187            }
188    
189            @Override
190            public Repository getRepository(long repositoryId) throws PortalException {
191                    Repository repository = _repositories.get(repositoryId);
192    
193                    if (repository != null) {
194                            return repository;
195                    }
196    
197                    repository = repositoryFactory.createRepository(repositoryId);
198    
199                    checkRepository(repositoryId);
200                    checkRepositoryAccess(repositoryId);
201    
202                    _repositories.put(repositoryId, repository);
203    
204                    return repository;
205            }
206    
207            @Override
208            public void invalidate() {
209                    _localRepositories.clear();
210                    _repositories.clear();
211            }
212    
213            @Override
214            public void invalidateRepository(long repositoryId) {
215                    _localRepositories.remove(repositoryId);
216    
217                    _repositories.remove(repositoryId);
218            }
219    
220            protected void checkFileEntryPermissions(long fileEntryId)
221                    throws PortalException {
222    
223                    DLFileEntry dlFileEntry = dlFileEntryLocalService.fetchDLFileEntry(
224                            fileEntryId);
225    
226                    PermissionChecker permissionChecker =
227                            PermissionThreadLocal.getPermissionChecker();
228    
229                    if ((dlFileEntry != null) && (permissionChecker != null)) {
230                            DLFileEntryPermission.check(
231                                    permissionChecker, fileEntryId, ActionKeys.VIEW);
232                    }
233            }
234    
235            protected void checkFileShortcutPermissions(long fileShortcutId)
236                    throws PortalException {
237    
238                    DLFileShortcut dlFileShortcut =
239                            dlFileShortcutLocalService.fetchDLFileShortcut(fileShortcutId);
240    
241                    PermissionChecker permissionChecker =
242                            PermissionThreadLocal.getPermissionChecker();
243    
244                    if ((dlFileShortcut != null) && (permissionChecker != null)) {
245                            DLFileEntryPermission.check(
246                                    permissionChecker, dlFileShortcut.getToFileEntryId(),
247                                    ActionKeys.VIEW);
248                    }
249            }
250    
251            protected void checkFileVersionPermissions(long fileVersionId)
252                    throws PortalException {
253    
254                    DLFileVersion dlFileVersion =
255                            dlFileVersionLocalService.fetchDLFileVersion(fileVersionId);
256    
257                    PermissionChecker permissionChecker =
258                            PermissionThreadLocal.getPermissionChecker();
259    
260                    if ((dlFileVersion != null) && (permissionChecker != null)) {
261                            DLFileEntryPermission.check(
262                                    permissionChecker, dlFileVersion.getFileEntryId(),
263                                    ActionKeys.VIEW);
264                    }
265            }
266    
267            protected void checkFolderPermissions(long folderId)
268                    throws PortalException {
269    
270                    DLFolder dlFolder = dlFolderLocalService.fetchDLFolder(folderId);
271    
272                    PermissionChecker permissionChecker =
273                            PermissionThreadLocal.getPermissionChecker();
274    
275                    if ((dlFolder != null) && (permissionChecker != null)) {
276                            DLFolderPermission.check(
277                                    permissionChecker, dlFolder, ActionKeys.VIEW);
278                    }
279            }
280    
281            protected void checkRepository(long repositoryId) throws PortalException {
282                    Group group = groupLocalService.fetchGroup(repositoryId);
283    
284                    if (group != null) {
285                            return;
286                    }
287    
288                    try {
289                            repositoryLocalService.getRepository(repositoryId);
290                    }
291                    catch (NoSuchRepositoryException nsre) {
292                            throw new InvalidRepositoryIdException(nsre.getMessage());
293                    }
294            }
295    
296            protected void checkRepositoryAccess(long repositoryId)
297                    throws PortalException {
298    
299                    Group group = groupLocalService.fetchGroup(repositoryId);
300    
301                    if (group != null) {
302                            return;
303                    }
304    
305                    try {
306                            com.liferay.portal.model.Repository repository =
307                                    repositoryLocalService.fetchRepository(repositoryId);
308    
309                            PermissionChecker permissionChecker =
310                                    PermissionThreadLocal.getPermissionChecker();
311    
312                            if ((repository != null) && (permissionChecker != null)) {
313                                    try {
314                                            DLFolderPermission.check(
315                                                    permissionChecker, repository.getGroupId(),
316                                                    repository.getDlFolderId(), ActionKeys.VIEW);
317                                    }
318                                    catch (NoSuchFolderException nsfe) {
319                                    }
320    
321                                    return;
322                            }
323                    }
324                    catch (NoSuchRepositoryException nsre) {
325                            throw new InvalidRepositoryIdException(nsre.getMessage());
326                    }
327            }
328    
329            protected long getFileEntryRepositoryId(long fileEntryId) {
330                    DLFileEntry dlFileEntry = dlFileEntryLocalService.fetchDLFileEntry(
331                            fileEntryId);
332    
333                    if (dlFileEntry != null) {
334                            return dlFileEntry.getRepositoryId();
335                    }
336    
337                    RepositoryEntry repositoryEntry =
338                            repositoryEntryLocalService.fetchRepositoryEntry(fileEntryId);
339    
340                    if (repositoryEntry != null) {
341                            return repositoryEntry.getRepositoryId();
342                    }
343    
344                    throw new InvalidRepositoryIdException(
345                            "No repository associated with file entry " + fileEntryId);
346            }
347    
348            protected long getFileShortcutRepositoryId(long fileShortcutId) {
349                    DLFileShortcut dlFileShortcut =
350                            dlFileShortcutLocalService.fetchDLFileShortcut(fileShortcutId);
351    
352                    if (dlFileShortcut != null) {
353                            return dlFileShortcut.getRepositoryId();
354                    }
355    
356                    throw new InvalidRepositoryIdException(
357                            "No repository associated with file shortcut " + fileShortcutId);
358            }
359    
360            protected long getFileVersionRepositoryId(long fileVersionId) {
361                    DLFileVersion dlFileVersion =
362                            dlFileVersionLocalService.fetchDLFileVersion(fileVersionId);
363    
364                    if (dlFileVersion != null) {
365                            return dlFileVersion.getRepositoryId();
366                    }
367    
368                    RepositoryEntry repositoryEntry =
369                            repositoryEntryLocalService.fetchRepositoryEntry(fileVersionId);
370    
371                    if (repositoryEntry != null) {
372                            return repositoryEntry.getRepositoryId();
373                    }
374    
375                    throw new InvalidRepositoryIdException(
376                            "No repository associated with file version " + fileVersionId);
377            }
378    
379            protected long getFolderRepositoryId(long folderId) {
380                    DLFolder dlFolder = dlFolderLocalService.fetchDLFolder(folderId);
381    
382                    if (dlFolder != null) {
383                            if (dlFolder.isMountPoint()) {
384                                    return dlFolder.getGroupId();
385                            }
386                            else {
387                                    return dlFolder.getRepositoryId();
388                            }
389                    }
390    
391                    RepositoryEntry repositoryEntry =
392                            repositoryEntryLocalService.fetchRepositoryEntry(folderId);
393    
394                    if (repositoryEntry != null) {
395                            return repositoryEntry.getRepositoryId();
396                    }
397    
398                    throw new InvalidRepositoryIdException(
399                            "No repository associated with folder " + folderId);
400            }
401    
402            protected List<Long> getGroupRepositoryIds(long groupId) {
403                    List<com.liferay.portal.model.Repository> repositories =
404                            repositoryLocalService.getGroupRepositories(groupId);
405    
406                    List<Long> repositoryIds = new ArrayList<>(repositories.size() + 1);
407    
408                    for (com.liferay.portal.model.Repository repository : repositories) {
409                            repositoryIds.add(repository.getRepositoryId());
410                    }
411    
412                    repositoryIds.add(groupId);
413    
414                    return repositoryIds;
415            }
416    
417            protected long getImageRepositoryId(long imageId) throws PortalException {
418                    DLFileEntry dlFileEntry =
419                            DLFileEntryServiceUtil.fetchFileEntryByImageId(imageId);
420    
421                    if (dlFileEntry != null) {
422                            return dlFileEntry.getRepositoryId();
423                    }
424    
425                    throw new InvalidRepositoryIdException(
426                            "No repository associated with image " + imageId);
427            }
428    
429            @BeanReference(type = DLFileEntryLocalService.class)
430            protected DLFileEntryLocalService dlFileEntryLocalService;
431    
432            @BeanReference(type = DLFileShortcutLocalService.class)
433            protected DLFileShortcutLocalService dlFileShortcutLocalService;
434    
435            @BeanReference(type = DLFileVersionLocalService.class)
436            protected DLFileVersionLocalService dlFileVersionLocalService;
437    
438            @BeanReference(type = DLFolderLocalService.class)
439            protected DLFolderLocalService dlFolderLocalService;
440    
441            @BeanReference(type = GroupLocalService.class)
442            protected GroupLocalService groupLocalService;
443    
444            @BeanReference(type = RepositoryEntryLocalService.class)
445            protected RepositoryEntryLocalService repositoryEntryLocalService;
446    
447            @BeanReference(type = RepositoryFactory.class)
448            protected RepositoryFactory repositoryFactory;
449    
450            @BeanReference(type = RepositoryLocalService.class)
451            protected RepositoryLocalService repositoryLocalService;
452    
453            private final Map<Long, LocalRepository> _localRepositories =
454                    new ConcurrentHashMap<>();
455            private final Map<Long, Repository> _repositories =
456                    new ConcurrentHashMap<>();
457    
458    }