mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-11-01 20:17:35 -04:00 
			
		
		
		
	Merge pull request #5018 from turbo124/v5-develop
Fixes for attaching a user to different companies
This commit is contained in:
		
						commit
						9b3ea22c22
					
				| @ -1 +1 @@ | ||||
| 5.1.13 | ||||
| 5.1.14 | ||||
| @ -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', | ||||
|  | ||||
| @ -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']); | ||||
| 
 | ||||
|  | ||||
| @ -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())); | ||||
| 
 | ||||
| @ -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. | ||||
|      * | ||||
|  | ||||
| @ -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) | ||||
|  | ||||
| @ -49,7 +49,7 @@ class CompanyUser extends Pivot | ||||
|         'shop_restricted', | ||||
|     ]; | ||||
| 
 | ||||
|     protected $touches = []; | ||||
|     protected $touches = ['user']; | ||||
| 
 | ||||
|     public function getEntityType() | ||||
|     { | ||||
|  | ||||
| @ -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(); | ||||
|  | ||||
| @ -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['$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')]; | ||||
|  | ||||
| @ -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), | ||||
|  | ||||
| @ -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', | ||||
|  | ||||
| @ -8,11 +8,13 @@ | ||||
| 
 | ||||
| @section('body') | ||||
|     <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"> | ||||
|             <img src="https://www.invoiceninja.com/wp-content/uploads/2018/04/bg-home2018b.jpg" | ||||
|                  class="w-full h-screen object-cover" | ||||
|                  alt="Background image"> | ||||
|         </div> | ||||
|         @endif | ||||
|         <div class="col-span-2 h-screen flex"> | ||||
|             <div class="m-auto md:w-1/2 lg:w-1/4"> | ||||
|                 <div class="flex flex-col"> | ||||
|  | ||||
| @ -3,11 +3,13 @@ | ||||
| 
 | ||||
| @section('body') | ||||
|     <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"> | ||||
|             <img src="https://www.invoiceninja.com/wp-content/uploads/2018/04/bg-home2018b.jpg" | ||||
|                  class="w-full h-screen object-cover" | ||||
|                  alt="Background image"> | ||||
|         </div> | ||||
|         @endif | ||||
|         <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="flex flex-col"> | ||||
|  | ||||
| @ -3,11 +3,13 @@ | ||||
| 
 | ||||
| @section('body') | ||||
|     <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"> | ||||
|             <img src="https://www.invoiceninja.com/wp-content/uploads/2018/04/bg-home2018b.jpg" | ||||
|                  class="w-full h-screen object-cover" | ||||
|                  alt="Background image"> | ||||
|         </div> | ||||
|         @endif | ||||
|         <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="flex flex-col"> | ||||
|  | ||||
| @ -160,8 +160,9 @@ 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::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/bulk', 'UserController@bulk')->name('users.bulk')->middleware('password_protected'); | ||||
|     Route::post('/user/{user}/reconfirm', 'UserController@reconfirm')->middleware('password_protected'); | ||||
| 
 | ||||
|  | ||||
| @ -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); | ||||
| 
 | ||||
| @ -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); | ||||
| 
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user