2020-02-28 13:11:56 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-02-28 13:11:56 +01:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-02-28 13:11:56 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-02-28 13:11:56 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Models\User;
|
2023-06-22 04:07:03 +02:00
|
|
|
use App\Models\CompanyUser;
|
|
|
|
use Illuminate\Http\Response;
|
2023-07-05 00:01:09 +02:00
|
|
|
use App\Transformers\UserTransformer;
|
2020-02-28 13:11:56 +01:00
|
|
|
use App\Transformers\CompanyUserTransformer;
|
2023-01-22 05:19:49 +01:00
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
2023-06-22 04:07:03 +02:00
|
|
|
use App\Http\Requests\CompanyUser\UpdateCompanyUserRequest;
|
|
|
|
use App\Http\Requests\CompanyUser\UpdateCompanyUserPreferencesRequest;
|
2020-02-28 13:11:56 +01:00
|
|
|
|
|
|
|
class CompanyUserController extends BaseController
|
|
|
|
{
|
|
|
|
protected $entity_type = CompanyUser::class;
|
|
|
|
|
|
|
|
protected $entity_transformer = CompanyUserTransformer::class;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @return void
|
2020-02-28 13:11:56 +01:00
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @return void
|
2020-02-28 13:11:56 +01:00
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-07-22 04:25:58 +02:00
|
|
|
public function store()
|
2020-02-28 13:11:56 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param int $id
|
|
|
|
* @return void
|
2020-02-28 13:11:56 +01:00
|
|
|
*/
|
|
|
|
public function show($id)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for editing the specified resource.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param int $id
|
|
|
|
* @return void
|
2020-02-28 13:11:56 +01:00
|
|
|
*/
|
|
|
|
public function edit($id)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @OA\Post(
|
|
|
|
* path="/api/v1/company_users",
|
|
|
|
* operationId="updateCompanyUser",
|
|
|
|
* tags={"company_user"},
|
|
|
|
* summary="Update a company user record",
|
|
|
|
* description="Attempts to update a company user record. A company user can modify only their settings fields. Full access for Admin users",
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="The Company User response",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2020-02-28 13:11:56 +01:00
|
|
|
* @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"),
|
|
|
|
* ),
|
|
|
|
* )
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param UpdateCompanyUserRequest $request
|
|
|
|
* @param User $user
|
|
|
|
* @return Response|mixed|void
|
2020-02-28 13:11:56 +01:00
|
|
|
*/
|
|
|
|
public function update(UpdateCompanyUserRequest $request, User $user)
|
|
|
|
{
|
2023-09-04 08:47:03 +02:00
|
|
|
/** @var \App\Models\User $auth_user */
|
|
|
|
$auth_user = auth()->user();
|
|
|
|
$company = $auth_user->company();
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2023-09-07 05:43:22 +02:00
|
|
|
$company_user = CompanyUser::query()->where('user_id', $user->id)->where('company_id',$company->id)->first();
|
2020-03-21 06:37:30 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $company_user) {
|
2021-01-24 23:24:13 +01:00
|
|
|
throw new ModelNotFoundException(ctrans('texts.company_user_not_found'));
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-04 08:47:03 +02:00
|
|
|
if ($auth_user->isAdmin()) {
|
2020-03-21 06:37:30 +01:00
|
|
|
$company_user->fill($request->input('company_user'));
|
|
|
|
} else {
|
2021-06-19 23:34:30 +02:00
|
|
|
$company_user->settings = $request->input('company_user')['settings'];
|
|
|
|
$company_user->notifications = $request->input('company_user')['notifications'];
|
2023-09-07 05:43:22 +02:00
|
|
|
|
|
|
|
if(isset($request->input('company_user')['react_settings'])) {
|
|
|
|
$company_user->react_settings = $request->input('company_user')['react_settings'];
|
|
|
|
}
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$company_user->save();
|
2020-02-28 13:11:56 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
return $this->itemResponse($company_user->fresh());
|
2020-02-28 13:11:56 +01:00
|
|
|
}
|
|
|
|
|
2023-06-22 04:07:03 +02:00
|
|
|
public function updatePreferences(UpdateCompanyUserPreferencesRequest $request, User $user)
|
|
|
|
{
|
2023-09-04 08:47:03 +02:00
|
|
|
/** @var \App\Models\User $auth_user */
|
|
|
|
$auth_user = auth()->user();
|
|
|
|
$company = $auth_user->company();
|
2023-08-04 10:13:26 +02:00
|
|
|
|
2023-09-04 08:47:03 +02:00
|
|
|
$company = $auth_user->company();
|
2023-06-22 04:07:03 +02:00
|
|
|
|
|
|
|
$company_user = CompanyUser::whereUserId($user->id)->whereCompanyId($company->id)->first();
|
|
|
|
|
|
|
|
if (! $company_user) {
|
|
|
|
throw new ModelNotFoundException(ctrans('texts.company_user_not_found'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-05 00:01:09 +02:00
|
|
|
$this->entity_type = User::class;
|
|
|
|
|
|
|
|
$this->entity_transformer = UserTransformer::class;
|
|
|
|
|
2023-06-22 04:07:03 +02:00
|
|
|
$company_user->react_settings = $request->react_settings;
|
|
|
|
$company_user->save();
|
|
|
|
|
2023-07-05 00:01:09 +02:00
|
|
|
return $this->itemResponse($user->fresh());
|
2023-06-22 04:07:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-28 13:11:56 +01:00
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param int $id
|
|
|
|
* @return void
|
2020-02-28 13:11:56 +01:00
|
|
|
*/
|
|
|
|
public function destroy($id)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
}
|