|
29 | 29 | import java.util.Map; |
30 | 30 | import java.util.Set; |
31 | 31 | import java.util.UUID; |
32 | | -import java.util.function.Predicate; |
33 | 32 | import java.util.stream.Collectors; |
34 | 33 | import java.util.stream.Stream; |
35 | 34 |
|
@@ -3823,11 +3822,62 @@ else if (!template.isPublicTemplate() && caller.getType() != Account.Type.ADMIN) |
3823 | 3822 | } |
3824 | 3823 | } |
3825 | 3824 |
|
| 3825 | + applyPublicTemplateSharingRestrictions(sc, caller); |
| 3826 | + |
3826 | 3827 | return templateChecks(isIso, hypers, tags, name, keyword, hyperType, onlyReady, bootable, zoneId, showDomr, caller, |
3827 | 3828 | showRemovedTmpl, parentTemplateId, showUnique, searchFilter, sc); |
3828 | 3829 |
|
3829 | 3830 | } |
3830 | 3831 |
|
| 3832 | + /** |
| 3833 | + * If the caller is not a root admin, restricts the search to return only public templates from the domain which |
| 3834 | + * the caller belongs to and domains with the setting 'share.public.templates.with.other.domains' enabled. |
| 3835 | + */ |
| 3836 | + protected void applyPublicTemplateSharingRestrictions(SearchCriteria<TemplateJoinVO> sc, Account caller) { |
| 3837 | + if (caller.getType() == Account.Type.ADMIN) { |
| 3838 | + s_logger.debug(String.format("Account [%s] is a root admin. Therefore, it has access to all public templates.", caller)); |
| 3839 | + return; |
| 3840 | + } |
| 3841 | + |
| 3842 | + List<TemplateJoinVO> publicTemplates = _templateJoinDao.listPublicTemplates(); |
| 3843 | + |
| 3844 | + Set<Long> unsharableDomainIds = new HashSet<>(); |
| 3845 | + for (TemplateJoinVO template : publicTemplates) { |
| 3846 | + addDomainIdToSetIfDomainDoesNotShareTemplates(template.getDomainId(), caller, unsharableDomainIds); |
| 3847 | + } |
| 3848 | + |
| 3849 | + if (!unsharableDomainIds.isEmpty()) { |
| 3850 | + s_logger.info(String.format("The public templates belonging to the domains [%s] will not be listed to account [%s] as they have the configuration [%s] marked as 'false'.", unsharableDomainIds, caller, QueryService.SharePublicTemplatesWithOtherDomains.key())); |
| 3851 | + sc.addAnd("domainId", SearchCriteria.Op.NOTIN, unsharableDomainIds.toArray()); |
| 3852 | + } |
| 3853 | + } |
| 3854 | + |
| 3855 | + /** |
| 3856 | + * Adds the provided domain ID to the set if the domain does not share templates with the account. That is, if: |
| 3857 | + * (1) the template does not belong to the domain of the account AND |
| 3858 | + * (2) the domain of the template has the setting 'share.public.templates.with.other.domains' disabled. |
| 3859 | + */ |
| 3860 | + protected void addDomainIdToSetIfDomainDoesNotShareTemplates(long domainId, Account account, Set<Long> unsharableDomainIds) { |
| 3861 | + if (domainId == account.getDomainId()) { |
| 3862 | + s_logger.trace(String.format("Domain [%s] will not be added to the set of domains with unshared templates since the account [%s] belongs to it.", domainId, account)); |
| 3863 | + return; |
| 3864 | + } |
| 3865 | + |
| 3866 | + if (unsharableDomainIds.contains(domainId)) { |
| 3867 | + s_logger.trace(String.format("Domain [%s] is already on the set of domains with unshared templates.", domainId)); |
| 3868 | + return; |
| 3869 | + } |
| 3870 | + |
| 3871 | + if (!checkIfDomainSharesTemplates(domainId)) { |
| 3872 | + s_logger.debug(String.format("Domain [%s] will be added to the set of domains with unshared templates as configuration [%s] is false.", domainId, QueryService.SharePublicTemplatesWithOtherDomains.key())); |
| 3873 | + unsharableDomainIds.add(domainId); |
| 3874 | + } |
| 3875 | + } |
| 3876 | + |
| 3877 | + protected boolean checkIfDomainSharesTemplates(Long domainId) { |
| 3878 | + return QueryService.SharePublicTemplatesWithOtherDomains.valueIn(domainId); |
| 3879 | + } |
| 3880 | + |
3831 | 3881 | private Pair<List<TemplateJoinVO>, Integer> templateChecks(boolean isIso, List<HypervisorType> hypers, Map<String, String> tags, String name, String keyword, |
3832 | 3882 | HypervisorType hyperType, boolean onlyReady, Boolean bootable, Long zoneId, boolean showDomr, Account caller, |
3833 | 3883 | boolean showRemovedTmpl, Long parentTemplateId, Boolean showUnique, |
@@ -3957,27 +4007,9 @@ private Pair<List<TemplateJoinVO>, Integer> findTemplatesByIdOrTempZonePair(Pair |
3957 | 4007 | templates = _templateJoinDao.searchByTemplateZonePair(showRemoved, templateZonePairs); |
3958 | 4008 | } |
3959 | 4009 |
|
3960 | | - if(caller.getType() != Account.Type.ADMIN) { |
3961 | | - templates = applyPublicTemplateRestriction(templates, caller); |
3962 | | - count = templates.size(); |
3963 | | - } |
3964 | | - |
3965 | 4010 | return new Pair<List<TemplateJoinVO>, Integer>(templates, count); |
3966 | 4011 | } |
3967 | 4012 |
|
3968 | | - private List<TemplateJoinVO> applyPublicTemplateRestriction(List<TemplateJoinVO> templates, Account caller){ |
3969 | | - List<Long> unsharableDomainIds = templates.stream() |
3970 | | - .map(TemplateJoinVO::getDomainId) |
3971 | | - .distinct() |
3972 | | - .filter(domainId -> domainId != caller.getDomainId()) |
3973 | | - .filter(Predicate.not(QueryService.SharePublicTemplatesWithOtherDomains::valueIn)) |
3974 | | - .collect(Collectors.toList()); |
3975 | | - |
3976 | | - return templates.stream() |
3977 | | - .filter(Predicate.not(t -> unsharableDomainIds.contains(t.getDomainId()))) |
3978 | | - .collect(Collectors.toList()); |
3979 | | - } |
3980 | | - |
3981 | 4013 | @Override |
3982 | 4014 | public ListResponse<TemplateResponse> listIsos(ListIsosCmd cmd) { |
3983 | 4015 | Pair<List<TemplateJoinVO>, Integer> result = searchForIsosInternal(cmd); |
|
0 commit comments