1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 08:51:34 +02:00
invoiceninja/app/Http/Controllers/UserController.php

631 lines
22 KiB
PHP
Raw Normal View History

<?php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Http\Controllers;
use App\DataMapper\DefaultSettings;
2019-04-27 11:20:03 +02:00
use App\Factory\UserFactory;
2019-04-25 13:33:03 +02:00
use App\Filters\UserFilters;
use App\Http\Controllers\Traits\VerifiesUserEmail;
use App\Http\Requests\User\AttachCompanyUserRequest;
2019-04-27 11:20:03 +02:00
use App\Http\Requests\User\CreateUserRequest;
use App\Http\Requests\User\DestroyUserRequest;
use App\Http\Requests\User\DetachCompanyUserRequest;
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;
use App\Http\Requests\User\UpdateUserRequest;
2019-06-12 06:22:05 +02:00
use App\Jobs\Company\CreateCompanyToken;
use App\Models\CompanyUser;
2019-04-25 13:33:03 +02:00
use App\Models\User;
2019-06-06 06:51:28 +02:00
use App\Repositories\UserRepository;
2019-04-25 13:33:03 +02:00
use App\Transformers\UserTransformer;
use App\Utils\Traits\MakesHash;
use Illuminate\Http\Request;
2019-06-12 06:22:05 +02:00
use Illuminate\Support\Facades\Log;
2019-01-27 00:22:57 +01:00
/**
* Class UserController
* @package App\Http\Controllers
*/
2019-03-28 22:34:58 +01:00
class UserController extends BaseController
{
use VerifiesUserEmail;
2019-04-25 13:33:03 +02:00
use MakesHash;
protected $entity_type = User::class;
protected $entity_transformer = UserTransformer::class;
2019-06-06 06:51:28 +02:00
protected $user_repo;
2019-06-11 07:28:24 +02:00
/**
* Constructor
*
* @param \App\Repositories\UserRepository $user_repo The user repo
*/
public function __construct(UserRepository $user_repo)
2019-03-28 22:34:58 +01:00
{
parent::__construct();
2019-06-06 06:51:28 +02:00
$this->user_repo = $user_repo;
2019-06-11 07:28:24 +02:00
2019-03-28 22:34:58 +01:00
}
2019-04-25 13:33:03 +02:00
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
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.
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",
* @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\Response(
* response=200,
* description="A list of users",
2019-10-07 08:31:26 +02:00
* @OA\Header(header="X-API-Version", ref="#/components/headers/X-API-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(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*
2019-04-25 13:33:03 +02:00
*/
2019-10-07 06:34:57 +02:00
public function index(UserFilters $filters)
2019-04-25 13:33:03 +02:00
{
2019-06-12 06:22:05 +02:00
2019-04-25 13:33:03 +02:00
$users = User::filter($filters);
return $this->listResponse($users);
2019-06-12 06:22:05 +02:00
2019-04-25 13:33:03 +02:00
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
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",
* @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\Response(
* response=200,
* description="A blank User object",
2019-10-07 08:31:26 +02:00
* @OA\Header(header="X-API-Version", ref="#/components/headers/X-API-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(
* response="default",
* 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
{
2019-06-12 06:22:05 +02:00
2019-04-27 11:20:03 +02:00
$user = UserFactory::create();
return $this->itemResponse($user);
2019-06-12 06:22:05 +02:00
2019-04-25 13:33:03 +02:00
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\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",
* @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\Response(
* response=200,
* description="Returns the saved User object",
2019-10-07 08:31:26 +02:00
* @OA\Header(header="X-API-Version", ref="#/components/headers/X-API-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(
* response="default",
* 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-12 06:22:05 +02:00
2019-06-06 06:51:28 +02:00
$company = auth()->user()->company();
2019-06-12 06:22:05 +02:00
$user = $this->user_repo->save($request->all(), UserFactory::create());
2019-06-06 06:51:28 +02:00
$user_agent = request()->input('token_name') ?: request()->server('HTTP_USER_AGENT');
2019-06-06 06:51:28 +02:00
$ct = CreateCompanyToken::dispatchNow($company, $user, $user_agent);
2019-06-12 06:22:05 +02:00
return $this->itemResponse($user->fresh());
2019-04-28 12:25:18 +02:00
2019-04-25 13:33:03 +02:00
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\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",
* @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="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",
2019-10-07 08:31:26 +02:00
* @OA\Header(header="X-API-Version", ref="#/components/headers/X-API-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(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*
*/
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.
*
* @param int $id
* @return \Illuminate\Http\Response
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",
* @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="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",
2019-10-07 08:31:26 +02:00
* @OA\Header(header="X-API-Version", ref="#/components/headers/X-API-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(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*
*/
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.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
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",
* @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="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",
2019-10-07 08:31:26 +02:00
* @OA\Header(header="X-API-Version", ref="#/components/headers/X-API-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(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*
*/
2019-06-05 02:43:23 +02:00
public function update(UpdateUserRequest $request, User $user)
2019-04-25 13:33:03 +02:00
{
2019-06-12 06:22:05 +02:00
2019-06-06 06:51:28 +02:00
$user = $this->user_repo->save($request->all(), $user);
return $this->itemResponse($user);
2019-04-25 13:33:03 +02:00
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
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",
* @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="token_name",
* in="path",
* 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",
2019-10-07 08:31:26 +02:00
* @OA\Header(header="X-API-Version", ref="#/components/headers/X-API-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(
* response="default",
* 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
{
2019-06-12 06:22:05 +02:00
2019-06-06 06:51:28 +02:00
$user->delete();
2019-06-06 06:51:28 +02:00
return response()->json([], 200);
2019-06-12 06:22:05 +02:00
}
/**
* Perform bulk actions on the list view
*
* @return Collection
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="",
* @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/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",
2019-10-07 08:31:26 +02:00
* @OA\Header(header="X-API-Version", ref="#/components/headers/X-API-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(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*
2019-06-12 06:22:05 +02:00
*/
public function bulk()
{
$action = request()->input('action');
$ids = request()->input('ids');
$users = User::withTrashed()->find($this->transformKeys($ids));
2019-06-12 06:22:05 +02:00
$users->each(function ($user, $key) use($action){
if(auth()->user()->can('edit', $user))
$this->user_repo->{$action}($user);
});
return $this->listResponse(User::withTrashed()->whereIn('id', $this->transformKeys($ids)));
2019-06-12 06:22:05 +02:00
2019-04-25 13:33:03 +02:00
}
/**
* Attach an existing user to a company.
*
* @OA\Post(
* path="/api/v1/users/{user}/attachToCompany",
* 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\RequestBody(ref="#/components/schemas/CompanyUser"),
* @OA\Response(
* response=200,
* description="Returns the saved User object",
* @OA\Header(header="X-API-Version", ref="#/components/headers/X-API-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"),
* ),
* )
*
*/
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]));
$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.
*
* @OA\Delete(
* path="/api/v1/users/{user}/detachFromCompany",
* operationId="detachUser",
* tags={"users"},
* summary="Detach an existing user to a company",
* description="Detach an existing user from 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\Response(
* response=200,
* description="Success response",
* @OA\Header(header="X-API-Version", ref="#/components/headers/X-API-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"),
* ),
* )
*
*/
public function detach(DetachCompanyUserRequest $request, User $user)
{
$company_user = CompanyUser::whereUserId($user->id)
->whereCompanyId(auth()->user()->companyId())->first();
$company_user->token->delete();
$company_user->delete();
return response()->json(['message' => 'User detached from company'], 200);
}
}