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