@@ -121,7 +121,11 @@ public ListResponse<IacResourceTypeResponse> listIacResourceTypes(ListIacResourc
121121
122122 @ Override
123123 public ListResponse <IacTemplateResponse > listIacTemplates (ListIacTemplatesCmd cmd ) {
124- Account caller = CallContext .current ().getCallingAccount ();
124+ CallContext currentCallContext = CallContext .current ();
125+ Account caller = currentCallContext .getCallingAccount ();
126+ if (cmd .getId () != null ) {
127+ checkCallerAccessToIacTemplate (currentCallContext , cmd .getId ());
128+ }
125129 long domainId = getBaseDomainIdToListIacTemplatesFrom (cmd .getDomainId (), caller );
126130 List <Long > domainIds = cmd .isRecursive () ? domainDao .getDomainAndChildrenIds (domainId ) : List .of (domainId );
127131 Long accountId = getAccountIdToListIacTemplatesFor (cmd .getAccountId (), cmd .getProjectId (), caller );
@@ -138,6 +142,35 @@ public ListResponse<IacTemplateResponse> listIacTemplates(ListIacTemplatesCmd cm
138142 return response ;
139143 }
140144
145+ private void checkCallerAccessToIacTemplate (CallContext callContext , long iacTemplateId ) {
146+ IacTemplateVO iacTemplate = iacTemplateDao .findById (iacTemplateId );
147+ if (iacTemplate == null ) {
148+ throw new InvalidParameterValueException ("Unable to find IaC template with the specified ID." );
149+ }
150+
151+ boolean hasAccess = false ;
152+ Account iacTemplateOwner = accountService .getActiveAccountById (iacTemplate .getAccountId ());
153+ if (iacTemplateOwner != null ) {
154+ try {
155+ accountService .checkAccess (callContext .getCallingUser (), iacTemplateOwner );
156+ hasAccess = true ;
157+ } catch (PermissionDeniedException ignored ) {}
158+ }
159+
160+ Account caller = callContext .getCallingAccount ();
161+ boolean isIacTemplateSharedWithCallingAccount = iacTemplate .getAccountMappings ()
162+ .stream ().anyMatch ((accountMap ) -> accountMap .getAccountId () == caller .getId ());
163+ boolean isIacTemplateSharedWithCallingAccountDomain = iacTemplate .getDomainMappings ()
164+ .stream ().anyMatch ((domainMap ) -> domainMap .getDomainId () == caller .getDomainId ());
165+ if (isIacTemplateSharedWithCallingAccount || isIacTemplateSharedWithCallingAccountDomain ) {
166+ hasAccess = true ;
167+ }
168+
169+ if (!hasAccess ) {
170+ throw new PermissionDeniedException (String .format ("Account [%s] does not have permission to operate over the requested IaC template." , caller .getAccountName ()));
171+ }
172+ }
173+
141174 Set <Long > getListOfSharedIacTemplatesIds (long domainId , Long accountId ) {
142175 Set <Long > sharedIacTemplateIds = new HashSet <>();
143176 iacTemplateAccountMapDao .listByAccountId (accountId )
@@ -295,7 +328,8 @@ private IacTemplate persistIacTemplate(BaseIacTemplateRegistrationCmd cmd, Accou
295328 if (iacTemplateUpdate ) {
296329 iacTemplateDomainMapDao .removeByIacTemplateId (iacTemplate .getId ());
297330 }
298- Set <Long > sharedDomainIds = cmd .isRecursiveDomains () ? getSharedDomainIdsRecursively (cmd .getSharedDomainIds ()) : cmd .getSharedDomainIds ();
331+ Set <Long > sharedDomainIds = BooleanUtils .toBoolean (cmd .isRecursiveDomains ()) ?
332+ getSharedDomainIdsRecursively (cmd .getSharedDomainIds ()) : cmd .getSharedDomainIds ();
299333 persistDomainMappings (sharedDomainIds , persistedTemplate .getId (), owner );
300334 }
301335
@@ -411,6 +445,7 @@ public IacTemplate findIacTemplateById(Long id) {
411445
412446 @ Override
413447 public void cleanUpAccountIacTemplates (long accountId ) {
448+ // not cleaning up relationships here?
414449 iacTemplateDao .removeByAccountId (accountId );
415450 }
416451
0 commit comments