From e6536c4db8a981287594027654ad7febb12683e7 Mon Sep 17 00:00:00 2001 From: ocket8888 Date: Wed, 25 Sep 2019 13:56:50 -0600 Subject: [PATCH 1/3] Added deprecation notices for /user/current/update --- docs/source/api/user_current_update.rst | 4 + traffic_ops/app/lib/API/User.pm | 117 ++++++++++++++++++++ traffic_ops/app/lib/MojoPlugins/Response.pm | 21 ++++ traffic_ops/app/lib/TrafficOpsRoutes.pm | 2 +- 4 files changed, 143 insertions(+), 1 deletion(-) diff --git a/docs/source/api/user_current_update.rst b/docs/source/api/user_current_update.rst index b61c244a69..135a6f5a03 100644 --- a/docs/source/api/user_current_update.rst +++ b/docs/source/api/user_current_update.rst @@ -107,5 +107,9 @@ Response Structure { "level": "success", "text": "User profile was successfully updated" + }, + { + "level": "warning", + "text": "This endpoint is deprecated, please use 'PUT /api/1.4/user/current' instead" } ]} diff --git a/traffic_ops/app/lib/API/User.pm b/traffic_ops/app/lib/API/User.pm index c500999089..51d09adcd2 100644 --- a/traffic_ops/app/lib/API/User.pm +++ b/traffic_ops/app/lib/API/User.pm @@ -599,6 +599,123 @@ sub current { } } +# Designated handler for the deprecated path to updating current users +sub user_current_update { + my $self = shift; + + my $alternative = "PUT /api/1.4/user/current"; + + my $user = $self->req->json->{user}; + if ( &is_ldap($self) ) { + return $self->with_deprecation( "Profile cannot be updated because '" . $user->{username} . "' is logged in as LDAP.", "error", 400, $alternative ); + } + + my $tenant_utils = Utils::Tenant->new($self); + my $tenants_data = $tenant_utils->create_tenants_data_from_db(); + if (!$tenant_utils->is_user_resource_accessible($tenants_data, $user->{"tenantId"})) { + return $self->with_deprecation("Invalid tenant. This tenant is not available to you for assignment.", "error", 400, $alternative); + } + + my $db_user; + + # Prevent these from getting updated + # Do not modify the localPasswd if it comes across as blank. + my $local_passwd = $user->{"localPasswd"}; + if ( defined($local_passwd) && ( $local_passwd eq '' ) ) { + delete( $user->{"localPasswd"} ); + } + + # Do not modify the confirmLocalPasswd if it comes across as blank. + my $confirm_local_passwd = $user->{"confirmLocalPasswd"}; + if ( defined($confirm_local_passwd) && ( $confirm_local_passwd eq '' ) ) { + delete( $user->{"confirmLocalPasswd"} ); + } + + my $current_user_id = $self->db->resultset('TmUser')->search( { username => $self->current_user()->{username} } )->get_column('id')->single; + + my ( $is_valid, $result ) = $self->is_valid( $user, $current_user_id ); + + if ($is_valid) { + my $username = $self->current_user()->{username}; + my $dbh = $self->db->resultset('TmUser')->find( { username => $username } ); + + # Updating a user implies it is no longer new + $db_user->{"new_user"} = 0; + + # These if "defined" checks allow for partial user updates, otherwise the entire + # user would need to be passed through. + if ( defined($local_passwd) && $local_passwd ne '' ) { + $db_user->{"local_passwd"} = Utils::Helper::hash_pass( $local_passwd ); + } + if ( defined($confirm_local_passwd) && $confirm_local_passwd ne '' ) { + $db_user->{"confirm_local_passwd"} = Utils::Helper::hash_pass( $confirm_local_passwd ); + } + if ( defined( $user->{"id"} ) ) { + $db_user->{"id"} = $user->{"id"}; + } + if ( defined( $user->{"username"} ) ) { + $db_user->{"username"} = $user->{"username"}; + } + if ( defined( $user->{"tenantId"} ) ) { + $db_user->{"tenant_id"} = $user->{"tenantId"}; + } + if ( defined( $user->{"public_ssh_key"} ) ) { + $db_user->{"public_ssh_key"} = $user->{"public_ssh_key"}; + } + if ( &is_admin($self) && defined( $user->{"role"} ) ) { + $db_user->{"role"} = $user->{"role"}; + } + if ( defined( $user->{"uid"} ) ) { + $db_user->{"uid"} = $user->{"uid"}; + } + if ( defined( $user->{"gid"} ) ) { + $db_user->{"gid"} = $user->{"gid"}; + } + if ( defined( $user->{"company"} ) ) { + $db_user->{"company"} = $user->{"company"}; + } + if ( defined( $user->{"email"} ) ) { + $db_user->{"email"} = $user->{"email"}; + } + if ( defined( $user->{"fullName"} ) ) { + $db_user->{"full_name"} = $user->{"fullName"}; + } + if ( defined( $user->{"newUser"} ) ) { + $db_user->{"new_user"} = $user->{"newUser"}; + } + if ( defined( $user->{"addressLine1"} ) ) { + $db_user->{"address_line1"} = $user->{"addressLine1"}; + } + if ( defined( $user->{"addressline2"} ) ) { + $db_user->{"address_line2"} = $user->{"addressLine2"}; + } + if ( defined( $user->{"city"} ) ) { + $db_user->{"city"} = $user->{"city"}; + } + if ( defined( $user->{"stateOrProvince"} ) ) { + $db_user->{"state_or_province"} = $user->{"stateOrProvince"}; + } + if ( defined( $user->{"phoneNumber"} ) ) { + $db_user->{"phone_number"} = $user->{"phoneNumber"}; + } + if ( defined( $user->{"postalCode"} ) ) { + $db_user->{"postal_code"} = $user->{"postalCode"}; + } + if ( defined( $user->{"country"} ) ) { + $db_user->{"country"} = $user->{"country"}; + } + # token is intended for new user registrations and on current user update, it should be cleared from the db + $db_user->{"token"} = undef; + # new_user flag is intended to identify new user registrations and on current user update, registration is complete + $db_user->{"new_user"} = 0; + $dbh->update($db_user); + return $self->with_deprecation("User profile was successfully updated", "success", 200, $alternative); + } + else { + return $self->with_deprecation($result, "error", 400, $alternative); + } +} + # Update sub update_current { my $self = shift; diff --git a/traffic_ops/app/lib/MojoPlugins/Response.pm b/traffic_ops/app/lib/MojoPlugins/Response.pm index 6067b6ac41..b678ac2108 100755 --- a/traffic_ops/app/lib/MojoPlugins/Response.pm +++ b/traffic_ops/app/lib/MojoPlugins/Response.pm @@ -119,6 +119,27 @@ sub register { } ); + $app->renderer->add_helper( + with_deprecation => sub { + my $self = shift || confess("Call on an instance of MojoPlugins::Response"); + my $alert = shift || confess("Please supply an alert string"); + my $level = shift || confess("Please supply an alert level such as 'error' or 'warning'"); + my $code = shift || confess("Please supply a response code e.g. 400"); + my $alternative = shift || confess("Please supply an alternative handler, like 'PUT /api/1.4/user/current'"); + my $response_object = shift; + + my $builder ||= MojoPlugins::Response::Builder->new($self, @_); + my @alerts_response = [{$LEVEL_KEY => $level, $TEXT_KEY => $alert}]; + push(@alerts_response, {$LEVEL_KEY => $WARNING_LEVEL, $TEXT_KEY => "This endpoint is deprecated, please use '" . $alternative . "' instead"}); + + if (defined($response_object)) { + return $self->render( $STATUS_KEY => $code, $JSON_KEY => { $ALERTS_KEY => \@alerts_response, $RESPONSE_KEY => $response_object } ); + } else { + return $self->render( $STATUS_KEY => $code, $JSON_KEY => { $ALERTS_KEY => \@alerts_response } ); + } + } + ); + # Alerts (500) $app->renderer->add_helper( internal_server_error => sub { diff --git a/traffic_ops/app/lib/TrafficOpsRoutes.pm b/traffic_ops/app/lib/TrafficOpsRoutes.pm index 5cc9482446..b099369a2e 100644 --- a/traffic_ops/app/lib/TrafficOpsRoutes.pm +++ b/traffic_ops/app/lib/TrafficOpsRoutes.pm @@ -851,7 +851,7 @@ sub api_routes { $r->put("/api/$version/user/current")->over( authenticated => 1, not_ldap => 1 )->to( 'User#update_current', namespace => $namespace ); # alternate current user routes - $r->post("/api/$version/user/current/update")->over( authenticated => 1, not_ldap => 1 )->to( 'User#update_current', namespace => $namespace ); + $r->post("/api/$version/user/current/update")->over( authenticated => 1, not_ldap => 1 )->to( 'User#user_current_update', namespace => $namespace ); # -- USERS: LOGIN # login w/ username and password From 653e1c62eaed5c8122023a49d1376f60fc214501 Mon Sep 17 00:00:00 2001 From: ocket8888 Date: Wed, 9 Oct 2019 13:32:27 -0600 Subject: [PATCH 2/3] fix sub-array in alerts object --- infrastructure/cdn-in-a-box/traffic_ops/Dockerfile | 2 +- traffic_ops/app/lib/MojoPlugins/Response.pm | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/infrastructure/cdn-in-a-box/traffic_ops/Dockerfile b/infrastructure/cdn-in-a-box/traffic_ops/Dockerfile index f26c64e67c..5418d7e591 100644 --- a/infrastructure/cdn-in-a-box/traffic_ops/Dockerfile +++ b/infrastructure/cdn-in-a-box/traffic_ops/Dockerfile @@ -105,5 +105,5 @@ ADD infrastructure/cdn-in-a-box/enroller/server_template.json \ ADD infrastructure/cdn-in-a-box/traffic_ops_data /traffic_ops_data -RUN chown -R trafops:trafops /opt/traffic_ops +# RUN chown -R trafops:trafops /opt/traffic_ops CMD /run.sh diff --git a/traffic_ops/app/lib/MojoPlugins/Response.pm b/traffic_ops/app/lib/MojoPlugins/Response.pm index b678ac2108..f963e67e2c 100755 --- a/traffic_ops/app/lib/MojoPlugins/Response.pm +++ b/traffic_ops/app/lib/MojoPlugins/Response.pm @@ -129,8 +129,7 @@ sub register { my $response_object = shift; my $builder ||= MojoPlugins::Response::Builder->new($self, @_); - my @alerts_response = [{$LEVEL_KEY => $level, $TEXT_KEY => $alert}]; - push(@alerts_response, {$LEVEL_KEY => $WARNING_LEVEL, $TEXT_KEY => "This endpoint is deprecated, please use '" . $alternative . "' instead"}); + my @alerts_response = ({$LEVEL_KEY => $level, $TEXT_KEY => $alert}, {$LEVEL_KEY => $WARNING_LEVEL, $TEXT_KEY => "This endpoint is deprecated, please use '" . $alternative . "' instead"}); if (defined($response_object)) { return $self->render( $STATUS_KEY => $code, $JSON_KEY => { $ALERTS_KEY => \@alerts_response, $RESPONSE_KEY => $response_object } ); From 75821e8fc1e25a5426ca1fda58dc2b31b0f3c9c1 Mon Sep 17 00:00:00 2001 From: ocket8888 Date: Fri, 11 Oct 2019 10:04:46 -0600 Subject: [PATCH 3/3] Removed unnecessary line in TO CiaB Dockerfile --- infrastructure/cdn-in-a-box/traffic_ops/Dockerfile | 1 - 1 file changed, 1 deletion(-) diff --git a/infrastructure/cdn-in-a-box/traffic_ops/Dockerfile b/infrastructure/cdn-in-a-box/traffic_ops/Dockerfile index 5418d7e591..5c4b756edf 100644 --- a/infrastructure/cdn-in-a-box/traffic_ops/Dockerfile +++ b/infrastructure/cdn-in-a-box/traffic_ops/Dockerfile @@ -105,5 +105,4 @@ ADD infrastructure/cdn-in-a-box/enroller/server_template.json \ ADD infrastructure/cdn-in-a-box/traffic_ops_data /traffic_ops_data -# RUN chown -R trafops:trafops /opt/traffic_ops CMD /run.sh