Tenancy utils - is_root_tenant
Project: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/commit/40375e63 Tree: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/tree/40375e63 Diff: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/diff/40375e63 Branch: refs/heads/master Commit: 40375e630ccdd730737105dbf5b96d8b1737c713 Parents: ed323e8 Author: nir-sopher <[email protected]> Authored: Thu Jun 1 16:23:16 2017 +0300 Committer: Jeremy Mitchell <[email protected]> Committed: Tue Jul 18 12:12:31 2017 -0600 ---------------------------------------------------------------------- traffic_ops/app/lib/API/Tenant.pm | 18 +++++------------- traffic_ops/app/lib/UI/TenantUtils.pm | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/40375e63/traffic_ops/app/lib/API/Tenant.pm ---------------------------------------------------------------------- diff --git a/traffic_ops/app/lib/API/Tenant.pm b/traffic_ops/app/lib/API/Tenant.pm index c1974ca..71c8afc 100644 --- a/traffic_ops/app/lib/API/Tenant.pm +++ b/traffic_ops/app/lib/API/Tenant.pm @@ -27,17 +27,11 @@ use MojoPlugins::Response; my $finfo = __FILE__ . ":"; sub getTenantName { - my $self = shift; + my $self = shift; my $tenant_id = shift; return defined($tenant_id) ? $self->db->resultset('Tenant')->search( { id => $tenant_id } )->get_column('name')->single() : "n/a"; } -sub isRootTenant { - my $self = shift; - my $tenant_id = shift; - return !defined($self->db->resultset('Tenant')->search( { id => $tenant_id } )->get_column('parent_id')->single()); -} - sub index { my $self = shift; @@ -122,7 +116,9 @@ sub update { } } - if ( !defined( $params->{parentId}) && !$self->isRootTenant($id) ) { + my $tenantUtils = UI::TenantUtils->new($self); + + if ( !defined( $params->{parentId}) && !$tenantUtils->is_root_tenant($id) ) { # Cannot turn a simple tenant to a root tenant. # Practically there is no problem with doing so, but it is to risky to be done by mistake. return $self->alert("Parent Id is required."); @@ -134,14 +130,10 @@ sub update { my $is_active = $params->{active}; - if ( !$params->{active} && $self->isRootTenant($id)) { + if ( !$params->{active} && $tenantUtils->is_root_tenant($id)) { return $self->alert("Root tenant cannot be in-active."); } - if ( !defined($params->{parentId}) && !isRootTenant($id) ) { - return $self->alert("Only the \"root\" tenant can have no parent."); - } - #operation my $values = { name => $params->{name}, http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/40375e63/traffic_ops/app/lib/UI/TenantUtils.pm ---------------------------------------------------------------------- diff --git a/traffic_ops/app/lib/UI/TenantUtils.pm b/traffic_ops/app/lib/UI/TenantUtils.pm index 253c588..d40b3ba 100644 --- a/traffic_ops/app/lib/UI/TenantUtils.pm +++ b/traffic_ops/app/lib/UI/TenantUtils.pm @@ -56,6 +56,21 @@ sub get_hierarchic_tenants_list { return @result; } +sub is_root_tenant { + my $self = shift; + my $tenant_id = shift; + + if (!defined($tenant_id)) { + return 0; + } + + if (defined($self->{tenants_dict})) { + return !(defined($self->{tenants_dict}{$tenant_id}{parent})); + } + return !defined($self->{context}->db->resultset('Tenant')->search( { id => $tenant_id } )->get_column('parent_id')->single()); +} + + ############################################################## sub _init_tenants {
