1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-05 18:52:44 +01:00

Refactor login routes

This commit is contained in:
David Bomba 2020-11-13 20:09:20 +11:00
parent 58c6cb415e
commit 3bec5a4044
5 changed files with 53 additions and 71 deletions

View File

@ -168,11 +168,13 @@ class LoginController extends BaseController
$user = $this->guard()->user();
$user->setCompany($user->company_user->account->default_company);
$user->setCompany($user->account->default_company);
$ct = CompanyUser::whereUserId($user->id);
$cu = CompanyUser::query()
->where('user_id', auth()->user()->id);
return $this->listResponse($cu);
return $this->listResponse($ct);
} else {
LightLogs::create(new LoginFailure())
->increment()
@ -280,9 +282,10 @@ class LoginController extends BaseController
Auth::login($existing_user, true);
$existing_user->setCompany($existing_user->account->default_company);
$ct = CompanyUser::whereUserId(auth()->user()->id);
$cu = CompanyUser::query()
->where('user_id', auth()->user()->id);
return $this->listResponse($ct);
return $this->listResponse($cu);
}
}

View File

@ -21,6 +21,7 @@ use App\Utils\Ninja;
use App\Utils\Statics;
use App\Utils\Traits\AppSetup;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Request as Input;
@ -68,7 +69,7 @@ class BaseController extends Controller
'company.task_statuses',
'company.expense_categories',
'company.documents',
'company.users.company_user',
//'company.users.company_user',
'company.clients.contacts.company',
'company.clients.gateway_tokens',
'company.clients.documents',
@ -107,7 +108,7 @@ class BaseController extends Controller
'user.company_user',
'token',
'company.activities',
'company.users.company_user',
//'company.users.company_user',
'company.tax_rates',
'company.groups',
'company.payment_terms',
@ -130,7 +131,6 @@ class BaseController extends Controller
$include = implode(',', array_merge($this->forced_includes, $this->getRequestIncludes([])));
} elseif (request()->input('include') !== null) {
$include = array_merge($this->forced_includes, explode(',', request()->input('include')));
$include = implode(',', $include);
} elseif (count($this->forced_includes) >= 1) {
$include = implode(',', $this->forced_includes);
@ -271,8 +271,8 @@ class BaseController extends Controller
]
);
if (is_a($query, "Illuminate\Database\Eloquent\Builder")) {
$limit = Input::get('per_page', 20);
if ($query instanceof Builder) {
$limit = request()->input('per_page', 20);
$paginator = $query->paginate($limit);
$query = $paginator->getCollection();
@ -289,7 +289,7 @@ class BaseController extends Controller
{
$this->buildManager();
$transformer = new $this->entity_transformer(Input::get('serializer'));
$transformer = new $this->entity_transformer(request()->input('serializer'));
$includes = $transformer->getDefaultIncludes();
@ -297,40 +297,27 @@ class BaseController extends Controller
$query->with($includes);
if (auth()->user() && ! auth()->user()->hasPermission('view_'.lcfirst(class_basename($this->entity_type)))) {
if (auth()->user() && ! auth()->user()->hasPermission('view_'.lcfirst(class_basename($this->entity_type))))
$query->where('user_id', '=', auth()->user()->id);
}
if (request()->has('updated_at') && request()->input('updated_at') > 0) {
$updated_at = intval(request()->input('updated_at'));
$query->where('updated_at', '>=', date('Y-m-d H:i:s', $updated_at));
}
if (request()->has('updated_at') && request()->input('updated_at') > 0)
$query->where('updated_at', '>=', date('Y-m-d H:i:s', intval(request()->input('updated_at'))));
$data = $this->createCollection($query, $transformer, $this->entity_type);
return $this->response($data);
}
protected function createCollection($query, $transformer, $entity_type)
{
$this->buildManager();
if ($this->serializer && $this->serializer != EntityTransformer::API_SERIALIZER_JSON) {
$entity_type = null;
}
if (is_a($query, "Illuminate\Database\Eloquent\Builder")) {
$limit = Input::get('per_page', 20);
if ($this->serializer && $this->serializer != EntityTransformer::API_SERIALIZER_JSON)
$this->entity_type = null;
if ($query instanceof Builder) {
$limit = request()->input('per_page', 20);
$paginator = $query->paginate($limit);
$query = $paginator->getCollection();
$resource = new Collection($query, $transformer, $entity_type);
$resource = new Collection($query, $transformer, $this->entity_type);
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
} else {
$resource = new Collection($query, $transformer, $entity_type);
$resource = new Collection($query, $transformer, $this->entity_type);
}
return $this->manager->createData($resource)->toArray();
return $this->response($this->manager->createData($resource)->toArray());
}
protected function response($response)
@ -368,26 +355,17 @@ class BaseController extends Controller
{
$this->buildManager();
$transformer = new $this->entity_transformer(Input::get('serializer'));
$data = $this->createItem($item, $transformer, $this->entity_type);
if (auth()->user() && request()->include_static) {
$data['static'] = Statics::company(auth()->user()->getCompany()->getLocale());
}
return $this->response($data);
}
protected function createItem($data, $transformer, $entity_type)
{
if ($this->serializer && $this->serializer != EntityTransformer::API_SERIALIZER_JSON) {
$entity_type = null;
}
$transformer = new $this->entity_transformer(request()->input('serializer'));
if ($this->serializer && $this->serializer != EntityTransformer::API_SERIALIZER_JSON)
$this->entity_type = null;
$resource = new Item($data, $transformer, $entity_type);
return $this->manager->createData($resource)->toArray();
if (auth()->user() && request()->include_static)
$data['static'] = Statics::company(auth()->user()->getCompany()->getLocale());
return $this->response($this->manager->createData($resource)->toArray());
}
public static function getApiHeaders($count = 0)
@ -429,7 +407,7 @@ class BaseController extends Controller
public function flutterRoute()
{
// if ((bool) $this->checkAppSetup() !== false && Schema::hasTable('accounts') && $account = Account::first()) {
if ((bool) $this->checkAppSetup() !== false && $account = Account::first()) {
if (config('ninja.require_https') && ! request()->isSecure()) {
return redirect()->secure(request()->getRequestUri());

View File

@ -201,21 +201,21 @@ class User extends Authenticatable implements MustVerifyEmail
$this->id = auth()->user()->id;
}
return $this->hasOneThrough(CompanyUser::class, CompanyToken::class, 'user_id', 'company_id', 'id', 'company_id')
->where('company_user.user_id', $this->id)
->withTrashed();
// if(request()->header('X-API-TOKEN')){
// return $this->hasOneThrough(CompanyUser::class, CompanyToken::class, 'user_id', 'company_id', 'id', 'company_id')
// ->where('company_tokens.token', request()->header('X-API-TOKEN'))
// ->withTrashed();
// }
// else {
// return $this->hasOneThrough(CompanyUser::class, CompanyToken::class, 'user_id', 'company_id', 'id', 'company_id')
// return $this->hasOneThrough(CompanyUser::class, CompanyToken::class, 'user_id', 'company_id', 'id', 'company_id')
// ->where('company_user.user_id', $this->id)
// ->withTrashed();
// }
if(request()->header('X-API-TOKEN')){
return $this->hasOneThrough(CompanyUser::class, CompanyToken::class, 'user_id', 'company_id', 'id', 'company_id')
->where('company_tokens.token', request()->header('X-API-TOKEN'))
->withTrashed();
}
else {
return $this->hasOneThrough(CompanyUser::class, CompanyToken::class, 'user_id', 'company_id', 'id', 'company_id')
->where('company_user.user_id', $this->id)
->withTrashed();
}
}
/**

View File

@ -27,10 +27,7 @@ class CompanyUserTransformer extends EntityTransformer
* @var array
*/
protected $defaultIncludes = [
// 'account',
// 'company',
'user',
// 'token'
];
/**
@ -76,6 +73,7 @@ class CompanyUserTransformer extends EntityTransformer
public function includeUser(CompanyUser $company_user)
{
$transformer = new UserTransformer($this->serializer);
$company_user->user->company_id = $company_user->company_id;
return $this->includeItem($company_user->user, $transformer, User::class);
}

View File

@ -95,10 +95,13 @@ class UserTransformer extends EntityTransformer
}
public function includeCompanyUser(User $user)
{
{
// info("company id = ".$user->company_id);
// info($user);
$transformer = new CompanyUserTransformer($this->serializer);
$cu = $user->company_users()->whereCompanyId(config('ninja.company_id'))->first();
$cu = $user->company_users()->whereCompanyId($user->company_id)->first();
return $this->includeItem($cu, $transformer, CompanyUser::class);
}