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