3737import javax .inject .Inject ;
3838import javax .naming .ConfigurationException ;
3939
40- import org .apache .commons .codec .binary .Base64 ;
41- import org .apache .log4j .Logger ;
4240import org .apache .cloudstack .acl .ControlledEntity ;
4341import org .apache .cloudstack .acl .RoleType ;
4442import org .apache .cloudstack .acl .SecurityChecker ;
5452import org .apache .cloudstack .framework .config .dao .ConfigurationDao ;
5553import org .apache .cloudstack .managed .context .ManagedContextRunnable ;
5654import org .apache .cloudstack .region .gslb .GlobalLoadBalancerRuleDao ;
55+ import org .apache .commons .codec .binary .Base64 ;
56+ import org .apache .log4j .Logger ;
5757
5858import com .cloud .api .ApiDBUtils ;
5959import com .cloud .api .query .vo .ControlledViewEntity ;
@@ -379,6 +379,22 @@ public void checkAccess(Account caller, Domain domain) throws PermissionDeniedEx
379379
380380 @ Override
381381 public void checkAccess (Account caller , AccessType accessType , boolean sameOwner , ControlledEntity ... entities ) {
382+
383+ //check for the same owner
384+ Long ownerId = null ;
385+ ControlledEntity prevEntity = null ;
386+ if (sameOwner ) {
387+ for (ControlledEntity entity : entities ) {
388+ if (sameOwner ) {
389+ if (ownerId == null ) {
390+ ownerId = entity .getAccountId ();
391+ } else if (ownerId .longValue () != entity .getAccountId ()) {
392+ throw new PermissionDeniedException ("Entity " + entity + " and entity " + prevEntity + " belong to different accounts" );
393+ }
394+ prevEntity = entity ;
395+ }
396+ }
397+ }
382398
383399 if (caller .getId () == Account .ACCOUNT_ID_SYSTEM || isRootAdmin (caller .getType ())) {
384400 // no need to make permission checks if the system/root admin makes the call
@@ -389,13 +405,11 @@ public void checkAccess(Account caller, AccessType accessType, boolean sameOwner
389405 }
390406
391407 HashMap <Long , List <ControlledEntity >> domains = new HashMap <Long , List <ControlledEntity >>();
392- Long ownerId = null ;
393- ControlledEntity prevEntity = null ;
394408
395409 for (ControlledEntity entity : entities ) {
396410 long domainId = entity .getDomainId ();
397411 if (entity .getAccountId () != -1 && domainId == -1 ) { // If account exists domainId should too so calculate
398- // it. This condition might be hit for templates or entities which miss domainId in their tables
412+ // it. This condition might be hit for templates or entities which miss domainId in their tables
399413 Account account = ApiDBUtils .findAccountById (entity .getAccountId ());
400414 domainId = account != null ? account .getDomainId () : -1 ;
401415 }
@@ -421,15 +435,6 @@ public void checkAccess(Account caller, AccessType accessType, boolean sameOwner
421435 }
422436 }
423437
424- if (sameOwner ) {
425- if (ownerId == null ) {
426- ownerId = entity .getAccountId ();
427- } else if (ownerId .longValue () != entity .getAccountId ()) {
428- throw new PermissionDeniedException ("Entity " + entity + " and entity " + prevEntity + " belong to different accounts" );
429- }
430- prevEntity = entity ;
431- }
432-
433438 if (!granted ) {
434439 assert false : "How can all of the security checkers pass on checking this check: " + entity ;
435440 throw new PermissionDeniedException ("There's no way to confirm " + caller + " has access to " + entity );
0 commit comments