Github user nir-sopher commented on a diff in the pull request:

    
https://github.com/apache/incubator-trafficcontrol/pull/644#discussion_r124880183
  
    --- Diff: traffic_ops/app/lib/API/Tenant.pm ---
    @@ -131,21 +130,74 @@ sub update {
     
        my $is_active = $params->{active};
        
    -   if ( !$params->{active} && $self->isRootTenant($id)) {
    -           return $self->alert("Root user cannot be in-active.");
    +   if ( !$params->{active} && $tenant_utils->is_root_tenant($tenants_data, 
$id)) {
    +           return $self->alert("Root tenant cannot be in-active.");
    +   }
    +
    +   #this is a write operation, allowed only by parents of the tenant 
(which are the owners of the resource of type tenant) 
    +   my $current_resource_tenancy = $self->db->resultset('Tenant')->search( 
{ id => $id } )->get_column('parent_id')->single();
    +   if (!defined($current_resource_tenancy)) {
    +           #no parent - the tenant is its-own owner
    +           $current_resource_tenancy = $id;
        }
        
    +   if (!$tenant_utils->is_tenant_resource_accessible($tenants_data, 
$current_resource_tenancy)) {
    +           return $self->forbidden(); #Current owning tenant is not under 
user's tenancy
    +   }
     
    -   if ( !defined($params->{parentId}) && !isRootTenant($id) ) {
    -           return $self->alert("Only the \"root\" tenant can have no 
parent.");
    +   if (!$tenant_utils->is_tenant_resource_accessible($tenants_data, 
$params->{parentId})) {
    +           return $self->forbidden(); #Parent tenant to be set is not 
under user's tenancy
        }
    +
    +
    +   if ($params->{parentId} != $tenant->parent) {
    +           #parent replacement
    +           if (!defined($tenant_utils->get_tenant_by_id($tenants_data, 
$params->{parentId}))) {
    +                   return $self->alert("Parent tenant does not exists.");
    +           }
    +           my $parent_depth = 
$tenant_utils->get_tenant_heirarchy_depth($tenants_data, $params->{parentId});
    +           if (!defined($parent_depth))
    +           {
    +                   return $self->alert("Failed to retrieve parent tenant 
depth.");
    +           }
    +
    +           my $tenant_height = 
$tenant_utils->get_tenant_heirarchy_height($tenants_data, $id);
    +           if (!defined($tenant_height))
    +           {
    +                   return $self->alert("Failed to retrieve tenant 
height.");
    +           }
    +   
    +           if ($parent_depth+$tenant_height+1 > 
$tenant_utils->max_heirarchy_limit())
    --- End diff --
    
    Indeed, safety nets may cover up bugs, but not in this case - the operation 
will fail instead of crashing or getting stuck. Then we can debug it.
    Furthermore, we will have an error in the log pointing out the reason of 
the failure - what we will not have if we get into an infinite loop/recursion.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to