2018-10-21 00:26:21 +02:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-05-11 05:32:07 +02: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)
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-05-11 05:32:07 +02:00
|
|
|
*/
|
2018-10-21 00:26:21 +02:00
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2023-04-05 03:18:10 +02:00
|
|
|
use App\Models\User;
|
|
|
|
use App\Utils\Ninja;
|
|
|
|
use App\Models\CompanyUser;
|
|
|
|
use App\Factory\UserFactory;
|
|
|
|
use App\Filters\UserFilters;
|
|
|
|
use Illuminate\Http\Response;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
2020-09-16 01:56:10 +02:00
|
|
|
use App\Events\User\UserWasCreated;
|
2021-01-14 04:44:52 +01:00
|
|
|
use App\Events\User\UserWasDeleted;
|
|
|
|
use App\Events\User\UserWasUpdated;
|
2023-04-05 03:18:10 +02:00
|
|
|
use App\Jobs\User\UserEmailChanged;
|
|
|
|
use App\Repositories\UserRepository;
|
|
|
|
use App\Transformers\UserTransformer;
|
|
|
|
use App\Jobs\Company\CreateCompanyToken;
|
2021-11-06 00:28:48 +01:00
|
|
|
use App\Http\Requests\User\BulkUserRequest;
|
2019-04-27 11:20:03 +02:00
|
|
|
use App\Http\Requests\User\EditUserRequest;
|
|
|
|
use App\Http\Requests\User\ShowUserRequest;
|
|
|
|
use App\Http\Requests\User\StoreUserRequest;
|
2023-04-05 03:18:10 +02:00
|
|
|
use App\Http\Requests\User\CreateUserRequest;
|
2019-04-27 11:20:03 +02:00
|
|
|
use App\Http\Requests\User\UpdateUserRequest;
|
2023-04-05 03:18:10 +02:00
|
|
|
use App\Http\Requests\User\DestroyUserRequest;
|
|
|
|
use App\Http\Requests\User\ReconfirmUserRequest;
|
|
|
|
use App\Http\Controllers\Traits\VerifiesUserEmail;
|
|
|
|
use App\Http\Requests\User\DetachCompanyUserRequest;
|
|
|
|
use App\Http\Requests\User\DisconnectUserMailerRequest;
|
2018-10-21 00:26:21 +02:00
|
|
|
|
2019-01-27 00:22:57 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Class UserController.
|
2019-01-27 00:22:57 +01:00
|
|
|
*/
|
2019-03-28 22:34:58 +01:00
|
|
|
class UserController extends BaseController
|
2018-10-21 00:26:21 +02:00
|
|
|
{
|
2018-10-24 12:24:09 +02:00
|
|
|
use VerifiesUserEmail;
|
2019-12-30 22:59:12 +01:00
|
|
|
use MakesHash;
|
2019-04-25 13:33:03 +02:00
|
|
|
|
|
|
|
protected $entity_type = User::class;
|
|
|
|
|
|
|
|
protected $entity_transformer = UserTransformer::class;
|
|
|
|
|
2019-06-06 06:51:28 +02:00
|
|
|
protected $user_repo;
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Constructor.
|
2019-06-11 07:28:24 +02:00
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param UserRepository $user_repo The user repo
|
2019-06-11 07:28:24 +02:00
|
|
|
*/
|
|
|
|
public function __construct(UserRepository $user_repo)
|
2019-03-28 22:34:58 +01:00
|
|
|
{
|
|
|
|
parent::__construct();
|
2018-10-21 00:26:21 +02:00
|
|
|
|
2019-06-06 06:51:28 +02:00
|
|
|
$this->user_repo = $user_repo;
|
2019-03-28 22:34:58 +01:00
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-04-25 13:33:03 +02:00
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param UserFilters $filters
|
|
|
|
* @return Response
|
2019-10-07 06:34:57 +02:00
|
|
|
*
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-10-07 06:34:57 +02:00
|
|
|
* @OA\Get(
|
|
|
|
* path="/api/v1/users",
|
|
|
|
* operationId="getUsers",
|
|
|
|
* tags={"users"},
|
|
|
|
* summary="Gets a list of users",
|
|
|
|
* description="Lists users, search and filters allow fine grained lists to be generated.
|
2023-02-20 09:52:50 +01:00
|
|
|
*
|
|
|
|
*Query parameters can be added to performed more fine grained filtering of the users, these are handled by the UserFilters class which defines the methods available",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2019-10-07 06:34:57 +02:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/include"),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="A list of users",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2019-10-07 06:34:57 +02: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/User"),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=422,
|
|
|
|
* description="Validation error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
2019-12-30 22:59:12 +01:00
|
|
|
* response="default",
|
2019-10-07 06:34:57 +02:00
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
2019-04-25 13:33:03 +02:00
|
|
|
*/
|
2019-06-11 05:20:23 +02:00
|
|
|
public function index(UserFilters $filters)
|
2019-04-25 13:33:03 +02:00
|
|
|
{
|
|
|
|
$users = User::filter($filters);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-04-25 13:33:03 +02:00
|
|
|
return $this->listResponse($users);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param CreateUserRequest $request
|
|
|
|
* @return Response
|
2019-10-07 06:34:57 +02:00
|
|
|
*
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
|
|
|
*
|
2019-10-07 06:34:57 +02:00
|
|
|
* @OA\Get(
|
|
|
|
* path="/api/v1/users/create",
|
|
|
|
* operationId="getUsersCreate",
|
|
|
|
* tags={"users"},
|
|
|
|
* summary="Gets a new blank User object",
|
|
|
|
* description="Returns a blank object with default values",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2019-10-07 06:34:57 +02:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/include"),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="A blank User object",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2019-10-07 06:34:57 +02: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/User"),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=422,
|
|
|
|
* description="Validation error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
|
|
|
|
*
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
2019-12-30 22:59:12 +01:00
|
|
|
* response="default",
|
2019-10-07 06:34:57 +02:00
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
2019-04-25 13:33:03 +02:00
|
|
|
*/
|
2019-04-27 11:20:03 +02:00
|
|
|
public function create(CreateUserRequest $request)
|
2019-04-25 13:33:03 +02:00
|
|
|
{
|
2022-06-27 08:37:18 +02:00
|
|
|
$user = UserFactory::create(auth()->user()->account_id);
|
2019-04-27 11:20:03 +02:00
|
|
|
|
|
|
|
return $this->itemResponse($user);
|
2019-04-25 13:33:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param StoreUserRequest $request
|
|
|
|
* @return Response
|
2019-10-07 06:34:57 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @OA\Post(
|
|
|
|
* path="/api/v1/users",
|
|
|
|
* operationId="storeUser",
|
|
|
|
* tags={"users"},
|
|
|
|
* summary="Adds a User",
|
|
|
|
* description="Adds an User to the system",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2019-10-07 06:34:57 +02:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/include"),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Returns the saved User object",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2019-10-07 06:34:57 +02: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/User"),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=422,
|
|
|
|
* description="Validation error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
|
|
|
|
*
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
2019-12-30 22:59:12 +01:00
|
|
|
* response="default",
|
2019-10-07 06:34:57 +02:00
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
2019-04-25 13:33:03 +02:00
|
|
|
*/
|
2019-04-27 11:20:03 +02:00
|
|
|
public function store(StoreUserRequest $request)
|
2019-04-25 13:33:03 +02:00
|
|
|
{
|
2019-06-06 06:51:28 +02:00
|
|
|
$company = auth()->user()->company();
|
2019-12-08 11:28:52 +01:00
|
|
|
|
|
|
|
$user = $this->user_repo->save($request->all(), $request->fetchUser());
|
2019-06-06 06:51:28 +02:00
|
|
|
|
2019-11-22 22:10:53 +01:00
|
|
|
$user_agent = request()->input('token_name') ?: request()->server('HTTP_USER_AGENT');
|
2019-06-06 06:51:28 +02:00
|
|
|
|
2022-09-07 06:15:27 +02:00
|
|
|
$ct = (new CreateCompanyToken($company, $user, $user_agent))->handle();
|
2019-06-12 06:22:05 +02:00
|
|
|
|
2021-05-06 23:12:07 +02:00
|
|
|
event(new UserWasCreated($user, auth()->user(), $company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
|
2020-09-16 01:56:10 +02:00
|
|
|
|
2021-05-24 02:53:04 +02:00
|
|
|
$user->setCompany($company);
|
2021-05-24 03:30:30 +02:00
|
|
|
$user->company_id = $company->id;
|
|
|
|
|
2021-05-24 02:53:04 +02:00
|
|
|
return $this->itemResponse($user);
|
2019-04-25 13:33:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param ShowUserRequest $request
|
|
|
|
* @param User $user
|
|
|
|
* @return Response
|
2019-10-07 06:34:57 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* @OA\Get(
|
|
|
|
* path="/api/v1/users/{id}",
|
|
|
|
* operationId="showUser",
|
|
|
|
* tags={"users"},
|
|
|
|
* summary="Shows an User",
|
|
|
|
* description="Displays an User by id",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2019-10-07 06:34:57 +02:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/include"),
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="id",
|
|
|
|
* in="path",
|
|
|
|
* description="The User Hashed ID",
|
|
|
|
* example="D2J234DFA",
|
|
|
|
* required=true,
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="string",
|
|
|
|
* format="string",
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Returns the User object",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2019-10-07 06:34:57 +02: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/User"),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=422,
|
|
|
|
* description="Validation error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
|
|
|
|
*
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
2019-12-30 22:59:12 +01:00
|
|
|
* response="default",
|
2019-10-07 06:34:57 +02:00
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
2019-12-30 22:59:12 +01:00
|
|
|
*/
|
2019-06-05 11:50:37 +02:00
|
|
|
public function show(ShowUserRequest $request, User $user)
|
2019-04-25 13:33:03 +02:00
|
|
|
{
|
2019-06-05 11:50:37 +02:00
|
|
|
return $this->itemResponse($user);
|
2019-04-25 13:33:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for editing the specified resource.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param EditUserRequest $request
|
|
|
|
* @param User $user
|
|
|
|
* @return Response
|
2019-10-07 06:34:57 +02:00
|
|
|
*
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-10-07 06:34:57 +02:00
|
|
|
* @OA\Get(
|
|
|
|
* path="/api/v1/users/{id}/edit",
|
|
|
|
* operationId="editUser",
|
|
|
|
* tags={"users"},
|
|
|
|
* summary="Shows an User for editting",
|
|
|
|
* description="Displays an User by id",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2019-10-07 06:34:57 +02:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/include"),
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="id",
|
|
|
|
* in="path",
|
|
|
|
* description="The User Hashed ID",
|
|
|
|
* example="D2J234DFA",
|
|
|
|
* required=true,
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="string",
|
|
|
|
* format="string",
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Returns the User object",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2019-10-07 06:34:57 +02: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/User"),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=422,
|
|
|
|
* description="Validation error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
|
|
|
|
*
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
2019-12-30 22:59:12 +01:00
|
|
|
* response="default",
|
2019-10-07 06:34:57 +02:00
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
2019-12-30 22:59:12 +01:00
|
|
|
*/
|
2019-06-05 11:50:37 +02:00
|
|
|
public function edit(EditUserRequest $request, User $user)
|
2019-04-25 13:33:03 +02:00
|
|
|
{
|
2019-06-05 11:50:37 +02:00
|
|
|
return $this->itemResponse($user);
|
2019-04-25 13:33:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
2019-10-07 06:34:57 +02:00
|
|
|
* @OA\Put(
|
|
|
|
* path="/api/v1/users/{id}",
|
|
|
|
* operationId="updateUser",
|
|
|
|
* tags={"users"},
|
|
|
|
* summary="Updates an User",
|
|
|
|
* description="Handles the updating of an User by id",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2019-10-07 06:34:57 +02:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/include"),
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="id",
|
|
|
|
* in="path",
|
|
|
|
* description="The User Hashed ID",
|
|
|
|
* example="D2J234DFA",
|
|
|
|
* required=true,
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="string",
|
|
|
|
* format="string",
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
2020-04-09 12:48:04 +02:00
|
|
|
* response=200,
|
2019-10-07 06:34:57 +02:00
|
|
|
* description="Returns the User object",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2019-10-07 06:34:57 +02: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/User"),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=422,
|
|
|
|
* description="Validation error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
|
|
|
|
*
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
2019-12-30 22:59:12 +01:00
|
|
|
* response="default",
|
2019-10-07 06:34:57 +02:00
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param UpdateUserRequest $request
|
|
|
|
* @param User $user
|
|
|
|
* @return Response|mixed
|
2019-12-30 22:59:12 +01:00
|
|
|
*/
|
2019-06-05 02:43:23 +02:00
|
|
|
public function update(UpdateUserRequest $request, User $user)
|
2019-04-25 13:33:03 +02:00
|
|
|
{
|
2022-03-16 01:12:12 +01:00
|
|
|
$old_company_user = $user->company_users()->where('company_id', auth()->user()->company()->id)->first();
|
2021-02-18 11:33:54 +01:00
|
|
|
$old_user = json_encode($user);
|
|
|
|
$old_user_email = $user->getOriginal('email');
|
2021-01-29 13:29:42 +01:00
|
|
|
|
2020-07-22 05:03:33 +02:00
|
|
|
$new_email = $request->input('email');
|
2021-02-18 11:33:54 +01:00
|
|
|
$new_user = $this->user_repo->save($request->all(), $user);
|
|
|
|
$new_user = $user->fresh();
|
|
|
|
|
2021-02-19 22:42:29 +01:00
|
|
|
/* When changing email address we store the former email in case we need to rollback */
|
2022-10-27 04:11:50 +02:00
|
|
|
/* 27-10-2022 we need to wipe the oauth data at this point*/
|
2021-02-19 22:42:29 +01:00
|
|
|
if ($old_user_email != $new_email) {
|
|
|
|
$user->last_confirmed_email_address = $old_user_email;
|
2021-02-25 22:06:43 +01:00
|
|
|
$user->email_verified_at = null;
|
2022-10-27 04:11:50 +02:00
|
|
|
$user->oauth_user_id = null;
|
|
|
|
$user->oauth_provider_id = null;
|
|
|
|
$user->oauth_user_refresh_token = null;
|
|
|
|
$user->oauth_user_token = null;
|
2021-02-19 22:42:29 +01:00
|
|
|
$user->save();
|
2021-02-18 11:33:54 +01:00
|
|
|
UserEmailChanged::dispatch($new_user, json_decode($old_user), auth()->user()->company());
|
2021-02-19 22:42:29 +01:00
|
|
|
}
|
2022-03-16 01:12:12 +01:00
|
|
|
|
2021-05-06 23:12:07 +02:00
|
|
|
event(new UserWasUpdated($user, auth()->user(), auth()->user()->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
|
2021-01-14 04:44:52 +01:00
|
|
|
|
2019-06-06 06:51:28 +02:00
|
|
|
return $this->itemResponse($user);
|
2019-04-25 13:33:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param DestroyUserRequest $request
|
|
|
|
* @param User $user
|
|
|
|
* @return Response
|
2019-10-07 06:34:57 +02:00
|
|
|
*
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-10-07 06:34:57 +02:00
|
|
|
* @OA\Delete(
|
|
|
|
* path="/api/v1/users/{id}",
|
|
|
|
* operationId="deleteUser",
|
|
|
|
* tags={"users"},
|
|
|
|
* summary="Deletes a User",
|
|
|
|
* description="Handles the deletion of an User by id",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2019-10-07 06:34:57 +02:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/include"),
|
|
|
|
* @OA\Parameter(
|
2019-11-24 07:37:53 +01:00
|
|
|
* name="token_name",
|
2019-12-04 02:06:14 +01:00
|
|
|
* in="query",
|
|
|
|
* required=false,
|
2019-11-24 07:37:53 +01:00
|
|
|
* description="Customized name for the Users API Token",
|
|
|
|
* example="iOS Device 11 iPad",
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="string",
|
|
|
|
* format="string",
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* @OA\Parameter(
|
2019-10-07 06:34:57 +02:00
|
|
|
* name="id",
|
|
|
|
* in="path",
|
|
|
|
* description="The User Hashed ID",
|
|
|
|
* example="D2J234DFA",
|
|
|
|
* required=true,
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="string",
|
|
|
|
* format="string",
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Returns a HTTP status",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2019-10-07 06:34:57 +02: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\Response(
|
|
|
|
* response=422,
|
|
|
|
* description="Validation error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
|
|
|
|
*
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
2019-12-30 22:59:12 +01:00
|
|
|
* response="default",
|
2019-10-07 06:34:57 +02:00
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
2019-04-25 13:33:03 +02:00
|
|
|
*/
|
2019-06-06 06:51:28 +02:00
|
|
|
public function destroy(DestroyUserRequest $request, User $user)
|
2019-04-25 13:33:03 +02:00
|
|
|
{
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($user->isOwner()) {
|
|
|
|
return response()->json(['message', 'Cannot detach owner.'], 400);
|
|
|
|
}
|
2021-03-07 07:27:44 +01:00
|
|
|
|
2020-03-01 11:45:23 +01:00
|
|
|
/* If the user passes the company user we archive the company user */
|
2021-03-03 08:22:14 +01:00
|
|
|
$user = $this->user_repo->delete($request->all(), $user);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2021-05-06 23:12:07 +02:00
|
|
|
event(new UserWasDeleted($user, auth()->user(), auth()->user()->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
|
2021-01-14 04:44:52 +01:00
|
|
|
|
2020-03-01 11:45:23 +01:00
|
|
|
return $this->itemResponse($user->fresh());
|
2019-06-12 06:22:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Perform bulk actions on the list view.
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-06-12 06:22:05 +02:00
|
|
|
* @return Collection
|
2019-10-07 06:34:57 +02:00
|
|
|
*
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
|
|
|
*
|
2019-10-07 06:34:57 +02:00
|
|
|
* @OA\Post(
|
|
|
|
* path="/api/v1/users/bulk",
|
|
|
|
* operationId="bulkUsers",
|
|
|
|
* tags={"users"},
|
|
|
|
* summary="Performs bulk actions on an array of users",
|
|
|
|
* description="",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2019-10-07 06:34:57 +02:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/index"),
|
|
|
|
* @OA\RequestBody(
|
2019-10-07 06:57:14 +02:00
|
|
|
* description="Hashed ids",
|
2019-10-07 06:34:57 +02:00
|
|
|
* required=true,
|
|
|
|
* @OA\MediaType(
|
|
|
|
* mediaType="application/json",
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="array",
|
|
|
|
* @OA\Items(
|
|
|
|
* type="integer",
|
|
|
|
* description="Array of hashed IDs to be bulk 'actioned",
|
|
|
|
* example="[0,1,2,3]",
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
2019-10-07 06:57:14 +02:00
|
|
|
* description="The User response",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2019-10-07 06:34:57 +02: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"),
|
2019-10-07 06:57:14 +02:00
|
|
|
* @OA\JsonContent(ref="#/components/schemas/User"),
|
2019-10-07 06:34:57 +02:00
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=422,
|
|
|
|
* description="Validation error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
|
|
|
|
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
2019-12-30 22:59:12 +01:00
|
|
|
* response="default",
|
2019-10-07 06:34:57 +02:00
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
2019-06-12 06:22:05 +02:00
|
|
|
*/
|
2021-11-06 00:28:48 +01:00
|
|
|
public function bulk(BulkUserRequest $request)
|
2019-06-12 06:22:05 +02:00
|
|
|
{
|
2021-11-05 10:43:25 +01:00
|
|
|
/* Validate restore() here and check if restoring the user will exceed their user quote (hosted only)*/
|
2019-06-12 06:22:05 +02:00
|
|
|
$action = request()->input('action');
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-06-12 06:22:05 +02:00
|
|
|
$ids = request()->input('ids');
|
|
|
|
|
2019-11-21 09:38:57 +01:00
|
|
|
$users = User::withTrashed()->find($this->transformKeys($ids));
|
2019-06-12 06:22:05 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
/*
|
|
|
|
* In case a user maliciously sends keys which do not belong to them, we push
|
2020-03-18 10:40:15 +01:00
|
|
|
* each user through the Policy sieve and only return users that they
|
|
|
|
* have access to
|
|
|
|
*/
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-18 10:40:15 +01:00
|
|
|
$return_user_collection = collect();
|
|
|
|
|
|
|
|
$users->each(function ($user, $key) use ($action, $return_user_collection) {
|
2019-12-30 22:59:12 +01:00
|
|
|
if (auth()->user()->can('edit', $user)) {
|
2020-03-25 00:20:42 +01:00
|
|
|
$this->user_repo->{$action}($user);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-18 10:40:15 +01:00
|
|
|
$return_user_collection->push($user->id);
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-06-12 06:22:05 +02:00
|
|
|
});
|
|
|
|
|
2020-03-18 10:40:15 +01:00
|
|
|
return $this->listResponse(User::withTrashed()->whereIn('id', $return_user_collection));
|
2019-04-25 13:33:03 +02:00
|
|
|
}
|
|
|
|
|
2019-11-28 11:35:13 +01:00
|
|
|
/**
|
|
|
|
* Detach an existing user to a company.
|
|
|
|
*
|
|
|
|
* @OA\Delete(
|
2019-12-02 05:18:36 +01:00
|
|
|
* path="/api/v1/users/{user}/detach_from_company",
|
2019-11-28 11:35:13 +01:00
|
|
|
* operationId="detachUser",
|
|
|
|
* tags={"users"},
|
|
|
|
* summary="Detach an existing user to a company",
|
|
|
|
* description="Detach an existing user from a company",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2019-11-28 11:35:13 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/include"),
|
2019-12-04 02:06:14 +01:00
|
|
|
* @OA\Parameter(
|
|
|
|
* name="user",
|
|
|
|
* in="path",
|
|
|
|
* description="The user hashed_id",
|
|
|
|
* example="FD767dfd7",
|
|
|
|
* required=true,
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="string",
|
|
|
|
* format="string",
|
|
|
|
* ),
|
|
|
|
* ),
|
2019-11-28 11:35:13 +01:00
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Success response",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2019-11-28 11:35:13 +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\Response(
|
|
|
|
* response=422,
|
|
|
|
* description="Validation error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
|
|
|
|
*
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
2019-12-30 22:59:12 +01:00
|
|
|
* response="default",
|
2019-11-28 11:35:13 +01:00
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param DetachCompanyUserRequest $request
|
|
|
|
* @param User $user
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2019-11-28 11:35:13 +01:00
|
|
|
*/
|
|
|
|
public function detach(DetachCompanyUserRequest $request, User $user)
|
|
|
|
{
|
2023-03-15 01:05:58 +01:00
|
|
|
// if ($request->entityIsDeleted($user)) {
|
|
|
|
// return $request->disallowUpdate();
|
|
|
|
// }
|
2021-03-07 07:27:44 +01:00
|
|
|
|
2019-11-28 11:35:13 +01:00
|
|
|
$company_user = CompanyUser::whereUserId($user->id)
|
2021-03-25 11:55:59 +01:00
|
|
|
->whereCompanyId(auth()->user()->companyId())
|
|
|
|
->withTrashed()
|
|
|
|
->first();
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($company_user->is_owner) {
|
2021-03-26 09:07:43 +01:00
|
|
|
return response()->json(['message', 'Cannot detach owner.'], 401);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2021-03-26 09:07:43 +01:00
|
|
|
|
2020-02-26 05:11:17 +01:00
|
|
|
$token = $company_user->token->where('company_id', $company_user->company_id)->where('user_id', $company_user->user_id)->first();
|
|
|
|
|
2020-03-26 04:23:57 +01:00
|
|
|
if ($token) {
|
2020-03-23 18:15:56 +01:00
|
|
|
$token->delete();
|
2020-03-26 04:23:57 +01:00
|
|
|
}
|
2020-02-26 05:11:17 +01:00
|
|
|
|
2020-03-26 04:23:57 +01:00
|
|
|
if ($company_user) {
|
2020-03-23 18:15:56 +01:00
|
|
|
$company_user->delete();
|
2020-03-26 04:23:57 +01:00
|
|
|
}
|
2019-11-28 11:35:13 +01:00
|
|
|
|
2021-01-25 01:57:49 +01:00
|
|
|
return response()->json(['message' => ctrans('texts.user_detached')], 200);
|
2019-11-28 11:35:13 +01:00
|
|
|
}
|
2021-02-20 01:45:20 +01:00
|
|
|
|
|
|
|
/**
|
2021-03-10 10:15:24 +01:00
|
|
|
* Invite an existing user to a company.
|
2021-02-20 01:45:20 +01:00
|
|
|
*
|
|
|
|
* @OA\Post(
|
2021-03-03 23:39:24 +01:00
|
|
|
* path="/api/v1/users/{user}/invite",
|
|
|
|
* operationId="inviteUser",
|
2021-02-20 01:45:20 +01:00
|
|
|
* tags={"users"},
|
|
|
|
* summary="Reconfirm an existing user to a company",
|
|
|
|
* description="Reconfirm an existing user from a company",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2021-02-20 01:45:20 +01:00
|
|
|
* @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\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Success response",
|
|
|
|
* @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\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 ReconfirmUserRequest $request
|
|
|
|
* @param User $user
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
2021-03-03 23:39:24 +01:00
|
|
|
public function invite(ReconfirmUserRequest $request, User $user)
|
2021-02-20 01:45:20 +01:00
|
|
|
{
|
2021-03-03 23:39:24 +01:00
|
|
|
$user->service()->invite($user->company());
|
2021-02-20 01:45:20 +01:00
|
|
|
|
|
|
|
return response()->json(['message' => ctrans('texts.confirmation_resent')], 200);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-10 10:15:24 +01:00
|
|
|
/**
|
|
|
|
* Invite an existing user to a company.
|
|
|
|
*
|
|
|
|
* @OA\Post(
|
|
|
|
* path="/api/v1/users/{user}/reconfirm",
|
|
|
|
* operationId="inviteUserReconfirm",
|
|
|
|
* tags={"users"},
|
|
|
|
* summary="Reconfirm an existing user to a company",
|
|
|
|
* description="Reconfirm an existing user from a company",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2021-03-10 10:15:24 +01:00
|
|
|
* @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\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Success response",
|
|
|
|
* @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\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 ReconfirmUserRequest $request
|
|
|
|
* @param User $user
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
|
|
|
public function reconfirm(ReconfirmUserRequest $request, User $user)
|
|
|
|
{
|
|
|
|
$user->service()->invite($user->company());
|
|
|
|
|
|
|
|
return response()->json(['message' => ctrans('texts.confirmation_resent')], 200);
|
|
|
|
}
|
2023-04-05 03:18:10 +02:00
|
|
|
|
|
|
|
public function disconnectOauthMailer(DisconnectUserMailerRequest $request, User $user)
|
|
|
|
{
|
|
|
|
|
|
|
|
$user->oauth_user_token = null;
|
|
|
|
$user->oauth_user_refresh_token = null;
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
return $this->itemResponse($user->fresh());
|
|
|
|
|
|
|
|
}
|
2023-04-12 02:38:18 +02:00
|
|
|
|
|
|
|
public function disconnectOauth(DisconnectUserMailerRequest $request, User $user)
|
|
|
|
{
|
|
|
|
$user->oauth_user_id = null;
|
|
|
|
$user->oauth_provider_id = null;
|
|
|
|
$user->oauth_user_token_expiry = null;
|
|
|
|
$user->oauth_user_token = null;
|
|
|
|
$user->oauth_user_refresh_token = null;
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
return $this->itemResponse($user->fresh());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-10-21 00:26:21 +02:00
|
|
|
}
|