|
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 |
|
@@ -3782,11 +3781,62 @@ else if (!template.isPublicTemplate() && caller.getType() != Account.Type.ADMIN) |
3782 | 3781 | } |
3783 | 3782 | } |
3784 | 3783 |
|
| 3784 | + applyPublicTemplateSharingRestrictions(sc, caller); |
| 3785 | + |
3785 | 3786 | return templateChecks(isIso, hypers, tags, name, keyword, hyperType, onlyReady, bootable, zoneId, showDomr, caller, |
3786 | 3787 | showRemovedTmpl, parentTemplateId, showUnique, searchFilter, sc); |
3787 | 3788 |
|
3788 | 3789 | } |
3789 | 3790 |
|
| 3791 | + /** |
| 3792 | + * If the caller is not a root admin, restricts the search to return only public templates from the domain which |
| 3793 | + * the caller belongs to and domains with the setting 'share.public.templates.with.other.domains' enabled. |
| 3794 | + */ |
| 3795 | + protected void applyPublicTemplateSharingRestrictions(SearchCriteria<TemplateJoinVO> sc, Account caller) { |
| 3796 | + if (caller.getType() == Account.Type.ADMIN) { |
| 3797 | + s_logger.debug(String.format("Account [%s] is a root admin. Therefore, it has access to all public templates.", caller)); |
| 3798 | + return; |
| 3799 | + } |
| 3800 | + |
| 3801 | + List<TemplateJoinVO> publicTemplates = _templateJoinDao.listPublicTemplates(); |
| 3802 | + |
| 3803 | + Set<Long> unsharableDomainIds = new HashSet<>(); |
| 3804 | + for (TemplateJoinVO template : publicTemplates) { |
| 3805 | + addDomainIdToSetIfDomainDoesNotShareTemplates(template.getDomainId(), caller, unsharableDomainIds); |
| 3806 | + } |
| 3807 | + |
| 3808 | + if (!unsharableDomainIds.isEmpty()) { |
| 3809 | + 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())); |
| 3810 | + sc.addAnd("domainId", SearchCriteria.Op.NOTIN, unsharableDomainIds.toArray()); |
| 3811 | + } |
| 3812 | + } |
| 3813 | + |
| 3814 | + /** |
| 3815 | + * Adds the provided domain ID the set if the domain does not share templates with the account. That is, if: |
| 3816 | + * (1) the template does not belong to the domain of the account AND |
| 3817 | + * (2) the domain of the template has the setting 'share.public.templates.with.other.domains' disabled. |
| 3818 | + */ |
| 3819 | + protected void addDomainIdToSetIfDomainDoesNotShareTemplates(long domainId, Account account, Set<Long> unsharableDomainIds) { |
| 3820 | + if (domainId == account.getDomainId()) { |
| 3821 | + 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)); |
| 3822 | + return; |
| 3823 | + } |
| 3824 | + |
| 3825 | + if (unsharableDomainIds.contains(domainId)) { |
| 3826 | + s_logger.trace(String.format("Domain [%s] is already on the set of domains with unshared templates.", domainId)); |
| 3827 | + return; |
| 3828 | + } |
| 3829 | + |
| 3830 | + if (!checkIfDomainSharesTemplates(domainId)) { |
| 3831 | + 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())); |
| 3832 | + unsharableDomainIds.add(domainId); |
| 3833 | + } |
| 3834 | + } |
| 3835 | + |
| 3836 | + protected boolean checkIfDomainSharesTemplates(Long domainId) { |
| 3837 | + return QueryService.SharePublicTemplatesWithOtherDomains.valueIn(domainId); |
| 3838 | + } |
| 3839 | + |
3790 | 3840 | private Pair<List<TemplateJoinVO>, Integer> templateChecks(boolean isIso, List<HypervisorType> hypers, Map<String, String> tags, String name, String keyword, |
3791 | 3841 | HypervisorType hyperType, boolean onlyReady, Boolean bootable, Long zoneId, boolean showDomr, Account caller, |
3792 | 3842 | boolean showRemovedTmpl, Long parentTemplateId, Boolean showUnique, |
@@ -3916,27 +3966,9 @@ private Pair<List<TemplateJoinVO>, Integer> findTemplatesByIdOrTempZonePair(Pair |
3916 | 3966 | templates = _templateJoinDao.searchByTemplateZonePair(showRemoved, templateZonePairs); |
3917 | 3967 | } |
3918 | 3968 |
|
3919 | | - if(caller.getType() != Account.Type.ADMIN) { |
3920 | | - templates = applyPublicTemplateRestriction(templates, caller); |
3921 | | - count = templates.size(); |
3922 | | - } |
3923 | | - |
3924 | 3969 | return new Pair<List<TemplateJoinVO>, Integer>(templates, count); |
3925 | 3970 | } |
3926 | 3971 |
|
3927 | | - private List<TemplateJoinVO> applyPublicTemplateRestriction(List<TemplateJoinVO> templates, Account caller){ |
3928 | | - List<Long> unsharableDomainIds = templates.stream() |
3929 | | - .map(TemplateJoinVO::getDomainId) |
3930 | | - .distinct() |
3931 | | - .filter(domainId -> domainId != caller.getDomainId()) |
3932 | | - .filter(Predicate.not(QueryService.SharePublicTemplatesWithOtherDomains::valueIn)) |
3933 | | - .collect(Collectors.toList()); |
3934 | | - |
3935 | | - return templates.stream() |
3936 | | - .filter(Predicate.not(t -> unsharableDomainIds.contains(t.getDomainId()))) |
3937 | | - .collect(Collectors.toList()); |
3938 | | - } |
3939 | | - |
3940 | 3972 | @Override |
3941 | 3973 | public ListResponse<TemplateResponse> listIsos(ListIsosCmd cmd) { |
3942 | 3974 | Pair<List<TemplateJoinVO>, Integer> result = searchForIsosInternal(cmd); |
|
0 commit comments