Merge pull request #5018 from turbo124/v5-develop

Fixes for attaching a user to different companies
This commit is contained in:
David Bomba 2021-03-03 21:11:12 +11:00 committed by GitHub
commit 9b3ea22c22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 41 additions and 91 deletions

View File

@ -1 +1 @@
5.1.13 5.1.14

View File

@ -614,8 +614,8 @@ class CompanySettings extends BaseSettings
'$invoice.po_number', '$invoice.po_number',
'$invoice.date', '$invoice.date',
'$invoice.due_date', '$invoice.due_date',
'$invoice.balance_due',
'$invoice.total', '$invoice.total',
'$invoice.balance_due',
], ],
'quote_details' => [ 'quote_details' => [
'$quote.number', '$quote.number',

View File

@ -23,8 +23,7 @@ class OneTimeTokenController extends BaseController
{ {
private $contexts = [ 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' '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); Auth::login($user, true);
Cache::forget($request->input('hash')); // Cache::forget($request->input('hash'));
$this->sendTo($data['context']); $this->sendTo($data['context']);

View File

@ -467,7 +467,7 @@ class UserController extends BaseController
public function destroy(DestroyUserRequest $request, User $user) public function destroy(DestroyUserRequest $request, User $user)
{ {
/* If the user passes the company user we archive the company 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())); event(new UserWasDeleted($user, auth()->user(), auth()->user()->company, Ninja::eventVars()));
@ -554,79 +554,6 @@ class UserController extends BaseController
return $this->listResponse(User::withTrashed()->whereIn('id', $return_user_collection)); 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. * Detach an existing user to a company.
* *

View File

@ -61,7 +61,6 @@ class AttachableUser implements Rule
->where('user_id', $user->id) ->where('user_id', $user->id)
->where('account_id',$user->account_id) ->where('account_id',$user->account_id)
->where('company_id', auth()->user()->company()->id) ->where('company_id', auth()->user()->company()->id)
->withTrashed()
->exists(); ->exists();
if($user_already_attached) if($user_already_attached)

View File

@ -49,7 +49,7 @@ class CompanyUser extends Pivot
'shop_restricted', 'shop_restricted',
]; ];
protected $touches = []; protected $touches = ['user'];
public function getEntityType() public function getEntityType()
{ {

View File

@ -141,9 +141,9 @@ class UserRepository extends BaseRepository
event(new UserWasDeleted($user, auth()->user(), $company, Ninja::eventVars())); event(new UserWasDeleted($user, auth()->user(), $company, Ninja::eventVars()));
$user->is_deleted = true; // $user->is_deleted = true;
$user->save(); // $user->save();
$user->delete(); // $user->delete();
return $user->fresh(); return $user->fresh();

View File

@ -253,7 +253,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['$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.full_name'] = ['value' => $this->contact->present()->name(), 'label' => ctrans('texts.name')];
$data['$contact.email'] = ['value' => $this->contact->email, 'label' => ctrans('texts.email')]; $data['$contact.email'] = ['value' => $this->contact->email, 'label' => ctrans('texts.email')];

View File

@ -13,7 +13,7 @@ return [
'require_https' => env('REQUIRE_HTTPS', true), 'require_https' => env('REQUIRE_HTTPS', true),
'app_url' => rtrim(env('APP_URL', ''), '/'), 'app_url' => rtrim(env('APP_URL', ''), '/'),
'app_domain' => env('APP_DOMAIN', ''), 'app_domain' => env('APP_DOMAIN', ''),
'app_version' => '5.1.13', 'app_version' => '5.1.14',
'minimum_client_version' => '5.0.16', 'minimum_client_version' => '5.0.16',
'terms_version' => '1.0.1', 'terms_version' => '1.0.1',
'api_secret' => env('API_SECRET', false), 'api_secret' => env('API_SECRET', false),

View File

@ -3968,8 +3968,8 @@ $LANG = array(
'list_of_recurring_invoices' => 'List of recurring invoices', 'list_of_recurring_invoices' => 'List of recurring invoices',
'details_of_recurring_invoice' => 'Here are some details about recurring invoice', 'details_of_recurring_invoice' => 'Here are some details about recurring invoice',
'cancellation' => 'Cancellation', 'cancellation' => 'Cancellation',
'about_cancellation' => 'In case you want to stop the recurring invoice,\n please click the request the cancellation.', '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.\n Your service may be cancelled with no further notification to you.', '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!', 'cancellation_pending' => 'Cancellation pending, we\'ll be in touch!',
'list_of_payments' => 'List of payments', 'list_of_payments' => 'List of payments',
'payment_details' => 'Details of the payment', 'payment_details' => 'Details of the payment',

View File

@ -8,11 +8,13 @@
@section('body') @section('body')
<div class="grid lg:grid-cols-3"> <div class="grid lg:grid-cols-3">
@if(\App\Models\Account::count() > 0 && !\App\Models\Account::first()->isPaid())
<div class="hidden lg:block col-span-1 bg-red-100 h-screen"> <div class="hidden lg:block col-span-1 bg-red-100 h-screen">
<img src="https://www.invoiceninja.com/wp-content/uploads/2018/04/bg-home2018b.jpg" <img src="https://www.invoiceninja.com/wp-content/uploads/2018/04/bg-home2018b.jpg"
class="w-full h-screen object-cover" class="w-full h-screen object-cover"
alt="Background image"> alt="Background image">
</div> </div>
@endif
<div class="col-span-2 h-screen flex"> <div class="col-span-2 h-screen flex">
<div class="m-auto md:w-1/2 lg:w-1/4"> <div class="m-auto md:w-1/2 lg:w-1/4">
<div class="flex flex-col"> <div class="flex flex-col">

View File

@ -3,11 +3,13 @@
@section('body') @section('body')
<div class="grid lg:grid-cols-3"> <div class="grid lg:grid-cols-3">
@if(\App\Models\Account::count() > 0 && !\App\Models\Account::first()->isPaid())
<div class="hidden lg:block col-span-1 bg-red-100 h-screen"> <div class="hidden lg:block col-span-1 bg-red-100 h-screen">
<img src="https://www.invoiceninja.com/wp-content/uploads/2018/04/bg-home2018b.jpg" <img src="https://www.invoiceninja.com/wp-content/uploads/2018/04/bg-home2018b.jpg"
class="w-full h-screen object-cover" class="w-full h-screen object-cover"
alt="Background image"> alt="Background image">
</div> </div>
@endif
<div class="col-span-2 h-screen flex"> <div class="col-span-2 h-screen flex">
<div class="m-auto w-1/2 md:w-1/3 lg:w-1/4"> <div class="m-auto w-1/2 md:w-1/3 lg:w-1/4">
<div class="flex flex-col"> <div class="flex flex-col">

View File

@ -3,11 +3,13 @@
@section('body') @section('body')
<div class="grid lg:grid-cols-3"> <div class="grid lg:grid-cols-3">
@if(\App\Models\Account::count() > 0 && !\App\Models\Account::first()->isPaid())
<div class="hidden lg:block col-span-1 bg-red-100 h-screen"> <div class="hidden lg:block col-span-1 bg-red-100 h-screen">
<img src="https://www.invoiceninja.com/wp-content/uploads/2018/04/bg-home2018b.jpg" <img src="https://www.invoiceninja.com/wp-content/uploads/2018/04/bg-home2018b.jpg"
class="w-full h-screen object-cover" class="w-full h-screen object-cover"
alt="Background image"> alt="Background image">
</div> </div>
@endif
<div class="col-span-2 h-screen flex"> <div class="col-span-2 h-screen flex">
<div class="m-auto w-1/2 md:w-1/3 lg:w-1/4"> <div class="m-auto w-1/2 md:w-1/3 lg:w-1/4">
<div class="flex flex-col"> <div class="flex flex-col">

View File

@ -160,8 +160,9 @@ Route::group(['middleware' => ['api_db', 'token_auth', 'locale'], 'prefix' => 'a
Route::get('users', 'UserController@index'); Route::get('users', 'UserController@index');
Route::put('users/{user}', 'UserController@update')->middleware('password_protected'); Route::put('users/{user}', 'UserController@update')->middleware('password_protected');
Route::post('users', 'UserController@store')->middleware('password_protected'); Route::post('users', 'UserController@store')->middleware('password_protected');
Route::post('users/{user}/attach_to_company', 'UserController@attach')->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::delete('users/{user}/detach_from_company', 'UserController@detach')->middleware('password_protected');
Route::post('users/bulk', 'UserController@bulk')->name('users.bulk')->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'); Route::post('/user/{user}/reconfirm', 'UserController@reconfirm')->middleware('password_protected');

View File

@ -21,6 +21,7 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Routing\Middleware\ThrottleRequests; use Illuminate\Routing\Middleware\ThrottleRequests;
use Illuminate\Support\Facades\Session; use Illuminate\Support\Facades\Session;
use Illuminate\Validation\ValidationException;
use Tests\MockAccountData; use Tests\MockAccountData;
use Tests\TestCase; use Tests\TestCase;
@ -33,6 +34,8 @@ class UserTest extends TestCase
use MockAccountData; use MockAccountData;
use DatabaseTransactions; use DatabaseTransactions;
private $default_email = 'attach@gmail.com';
public function setUp() :void public function setUp() :void
{ {
parent::setUp(); parent::setUp();
@ -45,6 +48,8 @@ class UserTest extends TestCase
Model::reguard(); Model::reguard();
$this->withoutExceptionHandling();
$this->withoutMiddleware( $this->withoutMiddleware(
ThrottleRequests::class, ThrottleRequests::class,
PasswordProtection::class PasswordProtection::class
@ -97,13 +102,23 @@ class UserTest extends TestCase
$user = UserFactory::create($this->account->id); $user = UserFactory::create($this->account->id);
$user->first_name = 'Test'; $user->first_name = 'Test';
$user->last_name = 'Palloni'; $user->last_name = 'Palloni';
$user->email = $this->default_email;
$user->save(); $user->save();
$data = $user->toArray();
try {
$response = $this->withHeaders([ $response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'), 'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token, 'X-API-TOKEN' => $this->token,
'X-API-PASSWORD' => 'ALongAndBriliantPassword', '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); $response->assertStatus(200);
@ -153,12 +168,15 @@ class UserTest extends TestCase
$new_user = UserFactory::create($this->account->id); $new_user = UserFactory::create($this->account->id);
$new_user->first_name = 'Test'; $new_user->first_name = 'Test';
$new_user->last_name = 'Palloni'; $new_user->last_name = 'Palloni';
$new_user->email = $this->default_email;
$new_user->save(); $new_user->save();
$data = $new_user->toArray();
$response = $this->withHeaders([ $response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'), 'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $company_token->token, '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); $response->assertStatus(200);