akaunting/app/Traits/Users.php

110 lines
2.1 KiB
PHP
Raw Normal View History

2019-11-16 10:21:14 +03:00
<?php
namespace App\Traits;
trait Users
{
/**
* Get user logged in
*
* @return object
*/
public function getCurrentUser()
{
return user();
}
/**
* Check user company assignment
*
* @param $id
*
* @return boolean
*/
public function isUserCompany($id)
{
$user = user();
if (empty($user)) {
return false;
}
2021-04-16 00:59:43 +03:00
$company = $user->withoutEvents(function () use ($user, $id) {
return $user->companies()->where('id', $id)->first();
});
2019-11-16 10:21:14 +03:00
2020-02-12 17:35:49 +03:00
return !empty($company);
2019-11-16 10:21:14 +03:00
}
2020-01-07 17:15:00 +03:00
2021-04-16 00:59:43 +03:00
public function isNotUserCompany($id)
{
return !$this->isUserCompany($id);
}
2020-01-07 17:15:00 +03:00
/**
* Check user dashboard assignment
*
* @param $id
*
* @return boolean
*/
public function isUserDashboard($id)
{
$user = user();
if (empty($user)) {
return app()->runningInConsole() ? true : false;
2020-01-07 17:15:00 +03:00
}
2021-04-16 00:59:43 +03:00
$dashboard = $user->withoutEvents(function () use ($user, $id) {
return $user->dashboards()->where('id', $id)->first();
});
2020-01-07 17:15:00 +03:00
2020-02-12 17:35:49 +03:00
return !empty($dashboard);
2020-01-07 17:15:00 +03:00
}
2021-04-16 00:59:43 +03:00
public function isNotUserDashboard($id)
{
return !$this->isUserDashboard($id);
}
/**
* Get the fist company of active user
*
* @return null|\App\Models\Common\Company
*/
public function getFirstCompanyOfUser()
{
$user = user();
if (empty($user)) {
return null;
}
$company = $user->withoutEvents(function () use ($user) {
return $user->companies()->enabled()->first();
});
if (empty($company)) {
return null;
}
return $company;
}
public function getLandingPageOfUser()
{
$user = user();
if (empty($user)) {
return route('login');
}
$route_name = $user->contact ? 'portal.dashboard' : $user->landing_page;
$company_id = company_id() ?: optional($this->getFirstCompanyOfUser())->id;
return route($route_name, ['company_id' => $company_id]);
}
2019-11-16 10:21:14 +03:00
}