2019-03-28 03:36:36 +01: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
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2019-03-28 03:36:36 +01:00
|
|
|
|
|
|
|
namespace App\Transformers;
|
|
|
|
|
2019-04-18 08:11:37 +02:00
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\CompanyToken;
|
2019-05-22 02:56:47 +02:00
|
|
|
use App\Models\CompanyUser;
|
2019-03-28 03:36:36 +01:00
|
|
|
use App\Models\User;
|
2019-04-03 02:09:22 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2019-10-04 13:54:03 +02:00
|
|
|
use Illuminate\Support\Carbon;
|
2019-03-28 03:36:36 +01:00
|
|
|
|
|
|
|
class UserTransformer extends EntityTransformer
|
|
|
|
{
|
2019-04-03 02:09:22 +02:00
|
|
|
use MakesHash;
|
2019-08-26 10:25:05 +02:00
|
|
|
|
2019-03-28 03:36:36 +01:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $defaultIncludes = [
|
2020-12-08 21:12:10 +01:00
|
|
|
//'company_user'
|
2019-03-28 03:36:36 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $availableIncludes = [
|
2019-04-18 13:57:22 +02:00
|
|
|
'companies',
|
2019-07-14 11:34:49 +02:00
|
|
|
'company_users',
|
2020-09-06 11:38:10 +02:00
|
|
|
'company_user',
|
2019-03-28 03:36:36 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
public function transform(User $user)
|
|
|
|
{
|
|
|
|
return [
|
2019-04-03 02:09:22 +02:00
|
|
|
'id' => $this->encodePrimaryKey($user->id),
|
2019-09-27 00:14:02 +02:00
|
|
|
'first_name' => $user->first_name ?: '',
|
|
|
|
'last_name' => $user->last_name ?: '',
|
|
|
|
'email' => $user->email ?: '',
|
2019-10-04 13:54:03 +02:00
|
|
|
'last_login' => Carbon::parse($user->last_login)->timestamp,
|
2020-09-06 11:38:10 +02:00
|
|
|
'created_at' => (int) $user->created_at,
|
|
|
|
'updated_at' => (int) $user->updated_at,
|
|
|
|
'archived_at' => (int) $user->deleted_at,
|
|
|
|
'is_deleted' => (bool) $user->is_deleted,
|
2019-09-27 00:14:02 +02:00
|
|
|
'phone' => $user->phone ?: '',
|
2019-10-04 13:54:03 +02:00
|
|
|
'email_verified_at' => $user->getEmailVerifiedAt(),
|
2019-09-27 00:14:02 +02:00
|
|
|
'signature' => $user->signature ?: '',
|
2019-12-17 11:57:15 +01:00
|
|
|
'custom_value1' => $user->custom_value1 ?: '',
|
|
|
|
'custom_value2' => $user->custom_value2 ?: '',
|
|
|
|
'custom_value3' => $user->custom_value3 ?: '',
|
|
|
|
'custom_value4' => $user->custom_value4 ?: '',
|
2020-09-06 11:38:10 +02:00
|
|
|
'oauth_provider_id' => (string) $user->oauth_provider_id,
|
2019-03-28 03:36:36 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-04-18 08:11:37 +02:00
|
|
|
public function includeCompanies(User $user)
|
|
|
|
{
|
|
|
|
$transformer = new CompanyTransformer($this->serializer);
|
|
|
|
|
2019-05-22 02:56:47 +02:00
|
|
|
return $this->includeCollection($user->companies, $transformer, Company::class);
|
2019-04-18 08:11:37 +02:00
|
|
|
}
|
|
|
|
|
2019-06-11 05:20:23 +02:00
|
|
|
public function includeToken(User $user)
|
2019-04-18 08:11:37 +02:00
|
|
|
{
|
|
|
|
$transformer = new CompanyTokenTransformer($this->serializer);
|
|
|
|
|
2019-06-25 05:55:02 +02:00
|
|
|
return $this->includeItem($user->token, $transformer, CompanyToken::class);
|
2019-05-22 02:56:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function includeCompanyTokens(User $user)
|
|
|
|
{
|
|
|
|
$transformer = new CompanyTokenTransformer($this->serializer);
|
|
|
|
|
|
|
|
return $this->includeCollection($user->tokens, $transformer, CompanyToken::class);
|
2019-04-18 08:11:37 +02:00
|
|
|
}
|
2019-07-14 11:34:49 +02:00
|
|
|
|
|
|
|
public function includeCompanyUsers(User $user)
|
|
|
|
{
|
|
|
|
$transformer = new CompanyUserTransformer($this->serializer);
|
|
|
|
|
2019-11-22 22:10:53 +01:00
|
|
|
return $this->includeCollection($user->company_users, $transformer, CompanyUser::class);
|
2019-07-14 11:34:49 +02:00
|
|
|
}
|
2019-11-13 12:32:53 +01:00
|
|
|
|
|
|
|
public function includeCompanyUser(User $user)
|
2020-11-25 15:19:52 +01:00
|
|
|
{
|
|
|
|
if (!$user->company_id && request()->header('X-API-TOKEN')) {
|
2020-11-13 11:42:06 +01:00
|
|
|
$company_token = CompanyToken::whereRaw('BINARY `token`= ?', [request()->header('X-API-TOKEN')])->first();
|
|
|
|
$user->company_id = $company_token->company_id;
|
|
|
|
}
|
|
|
|
|
2019-11-13 12:32:53 +01:00
|
|
|
$transformer = new CompanyUserTransformer($this->serializer);
|
|
|
|
|
2020-11-13 10:09:20 +01:00
|
|
|
$cu = $user->company_users()->whereCompanyId($user->company_id)->first();
|
2020-03-11 12:05:05 +01:00
|
|
|
|
|
|
|
return $this->includeItem($cu, $transformer, CompanyUser::class);
|
2019-11-13 12:32:53 +01:00
|
|
|
}
|
2019-03-28 03:36:36 +01:00
|
|
|
}
|