|
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 |
|
@@ -3813,11 +3812,62 @@ else if (!template.isPublicTemplate() && caller.getType() != Account.Type.ADMIN) |
3813 | 3812 | } |
3814 | 3813 | } |
3815 | 3814 |
|
| 3815 | + applyPublicTemplateSharingRestrictions(sc, caller); |
| 3816 | + |
3816 | 3817 | return templateChecks(isIso, hypers, tags, name, keyword, hyperType, onlyReady, bootable, zoneId, showDomr, caller, |
3817 | 3818 | showRemovedTmpl, parentTemplateId, showUnique, searchFilter, sc); |
3818 | 3819 |
|
3819 | 3820 | } |
3820 | 3821 |
|
| 3822 | + /** |
| 3823 | + * If the caller is not a root admin, restricts the search to return only public templates from the domain which |
| 3824 | + * the caller belongs to and domains with the setting 'share.public.templates.with.other.domains' enabled. |
| 3825 | + */ |
| 3826 | + protected void applyPublicTemplateSharingRestrictions(SearchCriteria<TemplateJoinVO> sc, Account caller) { |
| 3827 | + if (caller.getType() == Account.Type.ADMIN) { |
| 3828 | + s_logger.debug(String.format("Account [%s] is a root admin. Therefore, it has access to all public templates.", caller)); |
| 3829 | + return; |
| 3830 | + } |
| 3831 | + |
| 3832 | + List<TemplateJoinVO> publicTemplates = _templateJoinDao.listPublicTemplates(); |
| 3833 | + |
| 3834 | + Set<Long> unsharableDomainIds = new HashSet<>(); |
| 3835 | + for (TemplateJoinVO template : publicTemplates) { |
| 3836 | + addDomainIdToSetIfDomainDoesNotShareTemplates(template.getDomainId(), caller, unsharableDomainIds); |
| 3837 | + } |
| 3838 | + |
| 3839 | + if (!unsharableDomainIds.isEmpty()) { |
| 3840 | + 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())); |
| 3841 | + sc.addAnd("domainId", SearchCriteria.Op.NOTIN, unsharableDomainIds.toArray()); |
| 3842 | + } |
| 3843 | + } |
| 3844 | + |
| 3845 | + /** |
| 3846 | + * Adds the provided domain ID to the set if the domain does not share templates with the account. That is, if: |
| 3847 | + * (1) the template does not belong to the domain of the account AND |
| 3848 | + * (2) the domain of the template has the setting 'share.public.templates.with.other.domains' disabled. |
| 3849 | + */ |
| 3850 | + protected void addDomainIdToSetIfDomainDoesNotShareTemplates(long domainId, Account account, Set<Long> unsharableDomainIds) { |
| 3851 | + if (domainId == account.getDomainId()) { |
| 3852 | + 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)); |
| 3853 | + return; |
| 3854 | + } |
| 3855 | + |
| 3856 | + if (unsharableDomainIds.contains(domainId)) { |
| 3857 | + s_logger.trace(String.format("Domain [%s] is already on the set of domains with unshared templates.", domainId)); |
| 3858 | + return; |
| 3859 | + } |
| 3860 | + |
| 3861 | + if (!checkIfDomainSharesTemplates(domainId)) { |
| 3862 | + 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())); |
| 3863 | + unsharableDomainIds.add(domainId); |
| 3864 | + } |
| 3865 | + } |
| 3866 | + |
| 3867 | + protected boolean checkIfDomainSharesTemplates(Long domainId) { |
| 3868 | + return QueryService.SharePublicTemplatesWithOtherDomains.valueIn(domainId); |
| 3869 | + } |
| 3870 | + |
3821 | 3871 | private Pair<List<TemplateJoinVO>, Integer> templateChecks(boolean isIso, List<HypervisorType> hypers, Map<String, String> tags, String name, String keyword, |
3822 | 3872 | HypervisorType hyperType, boolean onlyReady, Boolean bootable, Long zoneId, boolean showDomr, Account caller, |
3823 | 3873 | boolean showRemovedTmpl, Long parentTemplateId, Boolean showUnique, |
@@ -3947,27 +3997,9 @@ private Pair<List<TemplateJoinVO>, Integer> findTemplatesByIdOrTempZonePair(Pair |
3947 | 3997 | templates = _templateJoinDao.searchByTemplateZonePair(showRemoved, templateZonePairs); |
3948 | 3998 | } |
3949 | 3999 |
|
3950 | | - if(caller.getType() != Account.Type.ADMIN) { |
3951 | | - templates = applyPublicTemplateRestriction(templates, caller); |
3952 | | - count = templates.size(); |
3953 | | - } |
3954 | | - |
3955 | 4000 | return new Pair<List<TemplateJoinVO>, Integer>(templates, count); |
3956 | 4001 | } |
3957 | 4002 |
|
3958 | | - private List<TemplateJoinVO> applyPublicTemplateRestriction(List<TemplateJoinVO> templates, Account caller){ |
3959 | | - List<Long> unsharableDomainIds = templates.stream() |
3960 | | - .map(TemplateJoinVO::getDomainId) |
3961 | | - .distinct() |
3962 | | - .filter(domainId -> domainId != caller.getDomainId()) |
3963 | | - .filter(Predicate.not(QueryService.SharePublicTemplatesWithOtherDomains::valueIn)) |
3964 | | - .collect(Collectors.toList()); |
3965 | | - |
3966 | | - return templates.stream() |
3967 | | - .filter(Predicate.not(t -> unsharableDomainIds.contains(t.getDomainId()))) |
3968 | | - .collect(Collectors.toList()); |
3969 | | - } |
3970 | | - |
3971 | 4003 | @Override |
3972 | 4004 | public ListResponse<TemplateResponse> listIsos(ListIsosCmd cmd) { |
3973 | 4005 | Pair<List<TemplateJoinVO>, Integer> result = searchForIsosInternal(cmd); |
|
0 commit comments