From c23b9b147d5e997e9aa3a40489517dca6b61b6d2 Mon Sep 17 00:00:00 2001 From: = Date: Wed, 3 Mar 2021 18:22:14 +1100 Subject: [PATCH 1/2] Fixes for attaching a user to different companies --- app/Http/Controllers/UserController.php | 2 +- app/Models/CompanyUser.php | 2 +- app/Repositories/UserRepository.php | 6 ++--- .../portal/ninja2020/auth/login.blade.php | 2 ++ .../auth/passwords/request.blade.php | 2 ++ .../ninja2020/auth/passwords/reset.blade.php | 2 ++ routes/api.php | 4 ++-- tests/Feature/UserTest.php | 24 ++++++++++++++++--- 8 files changed, 34 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 87de725daaf8..d91c9d5a3af2 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -467,7 +467,7 @@ class UserController extends BaseController public function destroy(DestroyUserRequest $request, User $user) { /* If the user passes the company user we archive the company user */ - $user = $this->user_repo->destroy($request->all(), $user); + $user = $this->user_repo->delete($request->all(), $user); event(new UserWasDeleted($user, auth()->user(), auth()->user()->company, Ninja::eventVars())); diff --git a/app/Models/CompanyUser.php b/app/Models/CompanyUser.php index 20089288b5d4..26f21eccbd1d 100644 --- a/app/Models/CompanyUser.php +++ b/app/Models/CompanyUser.php @@ -49,7 +49,7 @@ class CompanyUser extends Pivot 'shop_restricted', ]; - protected $touches = []; + protected $touches = ['user']; public function getEntityType() { diff --git a/app/Repositories/UserRepository.php b/app/Repositories/UserRepository.php index 74dd0fe05b4f..bf1c96592180 100644 --- a/app/Repositories/UserRepository.php +++ b/app/Repositories/UserRepository.php @@ -141,9 +141,9 @@ class UserRepository extends BaseRepository event(new UserWasDeleted($user, auth()->user(), $company, Ninja::eventVars())); - $user->is_deleted = true; - $user->save(); - $user->delete(); + // $user->is_deleted = true; + // $user->save(); + // $user->delete(); return $user->fresh(); diff --git a/resources/views/portal/ninja2020/auth/login.blade.php b/resources/views/portal/ninja2020/auth/login.blade.php index b19a36b08023..5af31e2a8b0b 100644 --- a/resources/views/portal/ninja2020/auth/login.blade.php +++ b/resources/views/portal/ninja2020/auth/login.blade.php @@ -8,11 +8,13 @@ @section('body')
+ @if(!\App\Models\Account::first()->isPaid()) + @endif
diff --git a/resources/views/portal/ninja2020/auth/passwords/request.blade.php b/resources/views/portal/ninja2020/auth/passwords/request.blade.php index 580f228e5c25..0f9e0daef726 100644 --- a/resources/views/portal/ninja2020/auth/passwords/request.blade.php +++ b/resources/views/portal/ninja2020/auth/passwords/request.blade.php @@ -3,11 +3,13 @@ @section('body')
+ @if(!\App\Models\Account::first()->isPaid()) + @endif
diff --git a/resources/views/portal/ninja2020/auth/passwords/reset.blade.php b/resources/views/portal/ninja2020/auth/passwords/reset.blade.php index 7ce9b644f547..dac4a8f0c890 100644 --- a/resources/views/portal/ninja2020/auth/passwords/reset.blade.php +++ b/resources/views/portal/ninja2020/auth/passwords/reset.blade.php @@ -3,11 +3,13 @@ @section('body')
+ @if(!\App\Models\Account::first()->isPaid()) + @endif
diff --git a/routes/api.php b/routes/api.php index 4348c24b679d..944b436e32b0 100644 --- a/routes/api.php +++ b/routes/api.php @@ -160,8 +160,8 @@ Route::group(['middleware' => ['api_db', 'token_auth', 'locale'], 'prefix' => 'a Route::get('users', 'UserController@index'); Route::put('users/{user}', 'UserController@update')->middleware('password_protected'); Route::post('users', 'UserController@store')->middleware('password_protected'); - Route::post('users/{user}/attach_to_company', 'UserController@attach')->middleware('password_protected'); - Route::delete('users/{user}/detach_from_company', 'UserController@detach')->middleware('password_protected'); + //Route::post('users/{user}/attach_to_company', 'UserController@attach')->middleware('password_protected'); + Route::delete('users/{user}/', 'UserController@detach')->middleware('password_protected'); Route::post('users/bulk', 'UserController@bulk')->name('users.bulk')->middleware('password_protected'); Route::post('/user/{user}/reconfirm', 'UserController@reconfirm')->middleware('password_protected'); diff --git a/tests/Feature/UserTest.php b/tests/Feature/UserTest.php index d00430c629fd..69d0f1077bc2 100644 --- a/tests/Feature/UserTest.php +++ b/tests/Feature/UserTest.php @@ -21,6 +21,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Routing\Middleware\ThrottleRequests; use Illuminate\Support\Facades\Session; +use Illuminate\Validation\ValidationException; use Tests\MockAccountData; use Tests\TestCase; @@ -33,6 +34,8 @@ class UserTest extends TestCase use MockAccountData; use DatabaseTransactions; + private $default_email = 'attach@gmail.com'; + public function setUp() :void { parent::setUp(); @@ -45,6 +48,8 @@ class UserTest extends TestCase Model::reguard(); + $this->withoutExceptionHandling(); + $this->withoutMiddleware( ThrottleRequests::class, PasswordProtection::class @@ -97,13 +102,23 @@ class UserTest extends TestCase $user = UserFactory::create($this->account->id); $user->first_name = 'Test'; $user->last_name = 'Palloni'; + $user->email = $this->default_email; $user->save(); + $data = $user->toArray(); + + try { $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, 'X-API-PASSWORD' => 'ALongAndBriliantPassword', - ])->post('/api/v1/users/'.$this->encodePrimaryKey($user->id).'/attach_to_company?include=company_user'); + ])->post('/api/v1/users?include=company_user', $data); + + } catch (ValidationException $e) { + $message = json_decode($e->validator->getMessageBag(), 1); + nlog($message); + $this->assertNotNull($message); + } $response->assertStatus(200); @@ -114,7 +129,7 @@ class UserTest extends TestCase 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, 'X-API-PASSWORD' => 'ALongAndBriliantPassword', - ])->delete('/api/v1/users/'.$this->encodePrimaryKey($user->id).'/detach_from_company?include=company_user'); + ])->delete('/api/v1/users/'.$this->encodePrimaryKey($user->id).'?include=company_user'); $response->assertStatus(200); @@ -153,12 +168,15 @@ class UserTest extends TestCase $new_user = UserFactory::create($this->account->id); $new_user->first_name = 'Test'; $new_user->last_name = 'Palloni'; + $new_user->email = $this->default_email; $new_user->save(); + $data = $new_user->toArray(); + $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $company_token->token, - ])->post('/api/v1/users/'.$this->encodePrimaryKey($new_user->id).'/attach_to_company?include=company_user'); + ])->post('/api/v1/users?include=company_user', $data); $response->assertStatus(200); From bfbde2ba51979c6c335bb0259395086ef3ee9885 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 3 Mar 2021 21:04:10 +1100 Subject: [PATCH 2/2] Fixes --- VERSION.txt | 2 +- app/DataMapper/CompanySettings.php | 2 +- .../Controllers/OneTimeTokenController.php | 5 +- app/Http/Controllers/UserController.php | 73 ------------------- .../ValidationRules/User/AttachableUser.php | 1 - app/Utils/HtmlEngine.php | 2 +- config/ninja.php | 2 +- resources/lang/en/texts.php | 4 +- .../portal/ninja2020/auth/login.blade.php | 2 +- .../auth/passwords/request.blade.php | 2 +- .../ninja2020/auth/passwords/reset.blade.php | 2 +- routes/api.php | 3 +- tests/Feature/UserTest.php | 2 +- 13 files changed, 14 insertions(+), 88 deletions(-) diff --git a/VERSION.txt b/VERSION.txt index 0c5d745ebb0d..63f23b12f92a 100644 --- a/VERSION.txt +++ b/VERSION.txt @@ -1 +1 @@ -5.1.13 \ No newline at end of file +5.1.14 \ No newline at end of file diff --git a/app/DataMapper/CompanySettings.php b/app/DataMapper/CompanySettings.php index 1c6bc259125d..76485942ea71 100644 --- a/app/DataMapper/CompanySettings.php +++ b/app/DataMapper/CompanySettings.php @@ -614,8 +614,8 @@ class CompanySettings extends BaseSettings '$invoice.po_number', '$invoice.date', '$invoice.due_date', - '$invoice.balance_due', '$invoice.total', + '$invoice.balance_due', ], 'quote_details' => [ '$quote.number', diff --git a/app/Http/Controllers/OneTimeTokenController.php b/app/Http/Controllers/OneTimeTokenController.php index 17d1a7da7ed3..34b6ca8fb974 100644 --- a/app/Http/Controllers/OneTimeTokenController.php +++ b/app/Http/Controllers/OneTimeTokenController.php @@ -23,8 +23,7 @@ class OneTimeTokenController extends BaseController { private $contexts = [ - 'stripe_connect_test' => 'https://connect.stripe.com/oauth/authorize?response_type=code&client_id=ca_J2FhIhcf9GT5BlWUNeQ1FhnZACaYZrOI&scope=read_write -', + 'stripe_connect_test' => 'https://connect.stripe.com/oauth/authorize?response_type=code&client_id=ca_J2FhIhcf9GT5BlWUNeQ1FhnZACaYZrOI&scope=read_write', 'stripe_connect' => 'https://connect.stripe.com/oauth/authorize?response_type=code&client_id=ca_J2Fh2tZfMlaaItUfbUwBBx4JPss8jCz9&scope=read_write' ]; @@ -93,7 +92,7 @@ class OneTimeTokenController extends BaseController Auth::login($user, true); - Cache::forget($request->input('hash')); + // Cache::forget($request->input('hash')); $this->sendTo($data['context']); diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index d91c9d5a3af2..5a00afa0ce4b 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -554,79 +554,6 @@ class UserController extends BaseController return $this->listResponse(User::withTrashed()->whereIn('id', $return_user_collection)); } - /** - * Attach an existing user to a company. - * - * @OA\Post( - * path="/api/v1/users/{user}/attach_to_company", - * operationId="attachUser", - * tags={"users"}, - * summary="Attach an existing user to a company", - * description="Attach an existing user to a company", - * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), - * @OA\Parameter(ref="#/components/parameters/X-Api-Token"), - * @OA\Parameter(ref="#/components/parameters/X-Requested-With"), - * @OA\Parameter(ref="#/components/parameters/include"), - * @OA\Parameter( - * name="user", - * in="path", - * description="The user hashed_id", - * example="FD767dfd7", - * required=true, - * @OA\Schema( - * type="string", - * format="string", - * ), - * ), - * @OA\RequestBody( - * description="The company user object", - * required=true, - * @OA\JsonContent(ref="#/components/schemas/CompanyUser"), - * ), - * @OA\Response( - * response=200, - * description="Returns the saved User object", - * @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"), - * @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"), - * @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"), - * @OA\JsonContent(ref="#/components/schemas/CompanyUser"), - * ), - * @OA\Response( - * response=422, - * description="Validation error", - * @OA\JsonContent(ref="#/components/schemas/ValidationError"), - * - * ), - * @OA\Response( - * response="default", - * description="Unexpected Error", - * @OA\JsonContent(ref="#/components/schemas/Error"), - * ), - * ) - * @param AttachCompanyUserRequest $request - * @param User $user - * @return Response|mixed - */ - public function attach(AttachCompanyUserRequest $request, User $user) - { - $company = auth()->user()->company(); - - $user->companies()->attach( - $company->id, - array_merge( - $request->all(), - [ - 'account_id' => $company->account->id, - 'notifications' => CompanySettings::notificationDefaults(), - ] - ) - ); - - $ct = CreateCompanyToken::dispatchNow($company, $user, 'User token created by'.auth()->user()->present()->name()); - - return $this->itemResponse($user->fresh()); - } - /** * Detach an existing user to a company. * diff --git a/app/Http/ValidationRules/User/AttachableUser.php b/app/Http/ValidationRules/User/AttachableUser.php index 6c572cc32f78..226d67b702cb 100644 --- a/app/Http/ValidationRules/User/AttachableUser.php +++ b/app/Http/ValidationRules/User/AttachableUser.php @@ -61,7 +61,6 @@ class AttachableUser implements Rule ->where('user_id', $user->id) ->where('account_id',$user->account_id) ->where('company_id', auth()->user()->company()->id) - ->withTrashed() ->exists(); if($user_already_attached) diff --git a/app/Utils/HtmlEngine.php b/app/Utils/HtmlEngine.php index 11dcbce10247..c21f2c608f51 100644 --- a/app/Utils/HtmlEngine.php +++ b/app/Utils/HtmlEngine.php @@ -249,7 +249,7 @@ class HtmlEngine $data['$client.balance'] = ['value' => Number::formatMoney($this->client->balance, $this->client), 'label' => ctrans('texts.account_balance')]; $data['$client_balance'] = ['value' => Number::formatMoney($this->client->balance, $this->client), 'label' => ctrans('texts.account_balance')]; - $data['$paid_to_date'] = ['value' => Number::formatMoney($this->client->paid_to_date, $this->client), 'label' => ctrans('texts.paid_to_date')]; + $data['$paid_to_date'] = ['value' => Number::formatMoney($this->entity->paid_to_date, $this->client), 'label' => ctrans('texts.paid_to_date')]; $data['$contact.full_name'] = ['value' => $this->contact->present()->name(), 'label' => ctrans('texts.name')]; $data['$contact.email'] = ['value' => $this->contact->email, 'label' => ctrans('texts.email')]; diff --git a/config/ninja.php b/config/ninja.php index 40e4e421927e..db75a9773830 100644 --- a/config/ninja.php +++ b/config/ninja.php @@ -13,7 +13,7 @@ return [ 'require_https' => env('REQUIRE_HTTPS', true), 'app_url' => rtrim(env('APP_URL', ''), '/'), 'app_domain' => env('APP_DOMAIN', ''), - 'app_version' => '5.1.13', + 'app_version' => '5.1.14', 'minimum_client_version' => '5.0.16', 'terms_version' => '1.0.1', 'api_secret' => env('API_SECRET', false), diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index 7699ff50fa1c..937cd80f21da 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -3968,8 +3968,8 @@ $LANG = array( 'list_of_recurring_invoices' => 'List of recurring invoices', 'details_of_recurring_invoice' => 'Here are some details about recurring invoice', 'cancellation' => 'Cancellation', - 'about_cancellation' => 'In case you want to stop the recurring invoice,\n please click the request the cancellation.', - 'cancellation_warning' => 'Warning! You are requesting a cancellation of this service.\n Your service may be cancelled with no further notification to you.', + 'about_cancellation' => 'In case you want to stop the recurring invoice, please click the request the cancellation.', + 'cancellation_warning' => 'Warning! You are requesting a cancellation of this service. Your service may be cancelled with no further notification to you.', 'cancellation_pending' => 'Cancellation pending, we\'ll be in touch!', 'list_of_payments' => 'List of payments', 'payment_details' => 'Details of the payment', diff --git a/resources/views/portal/ninja2020/auth/login.blade.php b/resources/views/portal/ninja2020/auth/login.blade.php index 5af31e2a8b0b..038b8f002acf 100644 --- a/resources/views/portal/ninja2020/auth/login.blade.php +++ b/resources/views/portal/ninja2020/auth/login.blade.php @@ -8,7 +8,7 @@ @section('body')
- @if(!\App\Models\Account::first()->isPaid()) + @if(\App\Models\Account::count() > 0 && !\App\Models\Account::first()->isPaid())