@@ -104,12 +104,12 @@ protected WebhookResponse createWebhookResponse(WebhookJoinVO webhookVO) {
104104 return response ;
105105 }
106106
107- protected List <Long > getIdsOfAccessibleWebhooks (Account caller ) {
108- if (Account . Type . ADMIN . equals ( caller . getType ()) ) {
107+ protected List <Long > getIdsOfAccessibleWebhooks (Account caller , boolean isCallerRootAdmin ) {
108+ if (isCallerRootAdmin ) {
109109 return new ArrayList <>();
110110 }
111111 String domainPath = null ;
112- if (Account . Type . DOMAIN_ADMIN . equals (caller .getType ())) {
112+ if (accountManager . isDomainAdmin (caller .getId ())) {
113113 Domain domain = domainDao .findById (caller .getDomainId ());
114114 domainPath = domain .getPath ();
115115 }
@@ -118,7 +118,7 @@ protected List<Long> getIdsOfAccessibleWebhooks(Account caller) {
118118 }
119119
120120 protected ManagementServerHostVO basicWebhookDeliveryApiCheck (Account caller , final Long id , final Long webhookId ,
121- final Long managementServerId , final Date startDate , final Date endDate ) {
121+ final Long managementServerId , final Date startDate , final Date endDate , boolean isCallerRootAdmin ) {
122122 if (id != null ) {
123123 WebhookDeliveryVO webhookDeliveryVO = webhookDeliveryDao .findById (id );
124124 if (webhookDeliveryVO == null ) {
@@ -141,7 +141,7 @@ protected ManagementServerHostVO basicWebhookDeliveryApiCheck(Account caller, fi
141141 }
142142 ManagementServerHostVO managementServerHostVO = null ;
143143 if (managementServerId != null ) {
144- if (!Account . Type . ADMIN . equals ( caller . getType ()) ) {
144+ if (!isCallerRootAdmin ) {
145145 throw new PermissionDeniedException ("Invalid parameter specified" );
146146 }
147147 managementServerHostVO = managementServerHostDao .findById (managementServerId );
@@ -234,6 +234,8 @@ public ListResponse<WebhookResponse> listWebhooks(ListWebhooksCmd cmd) {
234234 final String name = cmd .getName ();
235235 final String keyword = cmd .getKeyword ();
236236 final String scopeStr = cmd .getScope ();
237+ final boolean isCallerRootAdmin = accountManager .isRootAdmin (caller .getId ());
238+ final boolean isCallerAdmin = isCallerRootAdmin || accountManager .isAdmin (caller .getId ());
237239 List <WebhookResponse > responsesList = new ArrayList <>();
238240 List <Long > permittedAccounts = new ArrayList <>();
239241 Ternary <Long , Boolean , Project .ListProjectResourcesCriteria > domainIdRecursiveListProject =
@@ -266,9 +268,8 @@ public ListResponse<WebhookResponse> listWebhooks(ListWebhooksCmd cmd) {
266268 throw new InvalidParameterValueException ("Invalid scope specified" );
267269 }
268270 }
269- if ((Webhook .Scope .Global .equals (scope ) && !Account .Type .ADMIN .equals (caller .getType ())) ||
270- (Webhook .Scope .Domain .equals (scope ) &&
271- !List .of (Account .Type .ADMIN , Account .Type .DOMAIN_ADMIN ).contains (caller .getType ()))) {
271+ if ((Webhook .Scope .Global .equals (scope ) && !isCallerRootAdmin ) ||
272+ (Webhook .Scope .Domain .equals (scope ) && !isCallerAdmin )) {
272273 throw new InvalidParameterValueException (String .format ("Scope %s can not be specified" , scope ));
273274 }
274275 Webhook .State state = null ;
@@ -315,16 +316,17 @@ public WebhookResponse createWebhook(CreateWebhookCmd cmd) throws CloudRuntimeEx
315316 final String scopeStr = cmd .getScope ();
316317 final String stateStr = cmd .getState ();
317318 Webhook .Scope scope = Webhook .Scope .Local ;
319+ final boolean isOwnerRootAdmin = accountManager .isRootAdmin (owner .getId ());
320+ final boolean isOwnerAdmin = isOwnerRootAdmin || accountManager .isAdmin (owner .getId ());
318321 if (StringUtils .isNotEmpty (scopeStr )) {
319322 try {
320323 scope = Webhook .Scope .valueOf (scopeStr );
321324 } catch (IllegalArgumentException iae ) {
322325 throw new InvalidParameterValueException ("Invalid scope specified" );
323326 }
324327 }
325- if ((Webhook .Scope .Global .equals (scope ) && !Account .Type .ADMIN .equals (owner .getType ())) ||
326- (Webhook .Scope .Domain .equals (scope ) &&
327- !List .of (Account .Type .ADMIN , Account .Type .DOMAIN_ADMIN ).contains (owner .getType ()))) {
328+ if ((Webhook .Scope .Global .equals (scope ) && !isOwnerRootAdmin ) ||
329+ (Webhook .Scope .Domain .equals (scope ) && !isOwnerAdmin )) {
328330 throw new InvalidParameterValueException (
329331 String .format ("Scope %s can not be specified for owner %s" , scope , owner .getName ()));
330332 }
@@ -345,9 +347,7 @@ public WebhookResponse createWebhook(CreateWebhookCmd cmd) throws CloudRuntimeEx
345347 }
346348 long domainId = owner .getDomainId ();
347349 Long cmdDomainId = cmd .getDomainId ();
348- if (cmdDomainId != null &&
349- List .of (Account .Type .ADMIN , Account .Type .DOMAIN_ADMIN ).contains (owner .getType ()) &&
350- Webhook .Scope .Domain .equals (scope )) {
350+ if (cmdDomainId != null && isOwnerAdmin && Webhook .Scope .Domain .equals (scope )) {
351351 domainId = cmdDomainId ;
352352 }
353353 WebhookVO webhook = new WebhookVO (name , description , state , domainId , owner .getId (), payloadUrl , secretKey ,
@@ -403,12 +403,13 @@ public WebhookResponse updateWebhook(UpdateWebhookCmd cmd) throws CloudRuntimeEx
403403 }
404404 }
405405 Account owner = accountManager .getAccount (webhook .getAccountId ());
406+ final boolean isOwnerRootAdmin = accountManager .isRootAdmin (owner .getId ());
407+ final boolean isOwnerAdmin = isOwnerRootAdmin || accountManager .isAdmin (owner .getId ());
406408 if (StringUtils .isNotEmpty (scopeStr )) {
407409 try {
408410 Webhook .Scope scope = Webhook .Scope .valueOf (scopeStr );
409- if ((Webhook .Scope .Global .equals (scope ) && !Account .Type .ADMIN .equals (owner .getType ())) ||
410- (Webhook .Scope .Domain .equals (scope ) &&
411- !List .of (Account .Type .ADMIN , Account .Type .DOMAIN_ADMIN ).contains (owner .getType ()))) {
411+ if ((Webhook .Scope .Global .equals (scope ) && !isOwnerRootAdmin ) ||
412+ (Webhook .Scope .Domain .equals (scope ) && !isOwnerAdmin )) {
412413 throw new InvalidParameterValueException (
413414 String .format ("Scope %s can not be specified for owner %s" , scope , owner .getName ()));
414415 }
@@ -464,21 +465,23 @@ public ListResponse<WebhookDeliveryResponse> listWebhookDeliveries(ListWebhookDe
464465 final Date startDate = cmd .getStartDate ();
465466 final Date endDate = cmd .getEndDate ();
466467 final String eventType = cmd .getEventType ();
468+ final boolean isCallerRootAdmin = accountManager .isRootAdmin (caller .getId ());
467469 List <WebhookDeliveryResponse > responsesList = new ArrayList <>();
468470 ManagementServerHostVO host = basicWebhookDeliveryApiCheck (caller , id , webhookId , managementServerId ,
469- startDate , endDate );
471+ startDate , endDate , isCallerRootAdmin );
470472
471473 Filter searchFilter = new Filter (WebhookDeliveryJoinVO .class , "id" , false , cmd .getStartIndex (),
472474 cmd .getPageSizeVal ());
473475 List <Long > webhookIds = new ArrayList <>();
474476 if (webhookId != null ) {
475477 webhookIds .add (webhookId );
476478 } else {
477- webhookIds .addAll (getIdsOfAccessibleWebhooks (caller ));
479+ webhookIds .addAll (getIdsOfAccessibleWebhooks (caller , isCallerRootAdmin ));
478480 }
479481 Pair <List <WebhookDeliveryJoinVO >, Integer > deliveriesAndCount =
480482 webhookDeliveryJoinDao .searchAndCountByListApiParameters (id , webhookIds ,
481- (host != null ? host .getMsid () : null ), keyword , startDate , endDate , eventType , searchFilter );
483+ (host != null ? host .getMsid () : null ), keyword , startDate , endDate , eventType , searchFilter ,
484+ isCallerRootAdmin );
482485 for (WebhookDeliveryJoinVO delivery : deliveriesAndCount .first ()) {
483486 WebhookDeliveryResponse response = createWebhookDeliveryResponse (delivery );
484487 responsesList .add (response );
@@ -498,9 +501,16 @@ public int deleteWebhookDelivery(DeleteWebhookDeliveryCmd cmd) throws CloudRunti
498501 final Date startDate = cmd .getStartDate ();
499502 final Date endDate = cmd .getEndDate ();
500503 ManagementServerHostVO host = basicWebhookDeliveryApiCheck (caller , id , webhookId , managementServerId ,
501- startDate , endDate );
502- int removed = webhookDeliveryDao .deleteByDeleteApiParams (id , webhookId ,
503- (host != null ? host .getMsid () : null ), startDate , endDate );
504+ startDate , endDate , false );
505+ final boolean isCallerRootAdmin = accountManager .isRootAdmin (caller .getId ());
506+ List <Long > webhookIds = new ArrayList <>();
507+ if (webhookId != null ) {
508+ webhookIds .add (webhookId );
509+ } else {
510+ webhookIds .addAll (getIdsOfAccessibleWebhooks (caller , isCallerRootAdmin ));
511+ }
512+ int removed = webhookDeliveryDao .deleteByDeleteApiParams (id , webhookIds ,
513+ (host != null ? host .getMsid () : null ), startDate , endDate , isCallerRootAdmin );
504514 logger .info ("{} webhook deliveries removed" , removed );
505515 return removed ;
506516 }
0 commit comments