From f2565104965b2b8a43bf338f46e05503d923b32f Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 2 Dec 2020 13:36:48 +0100 Subject: [PATCH] wip #3753 @1 --- src/Company.php | 8 +++++++- src/Permissions.php | 4 ++-- src/User.php | 12 +++++++++++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/Company.php b/src/Company.php index 9b9849b..be7ff30 100644 --- a/src/Company.php +++ b/src/Company.php @@ -3,10 +3,10 @@ namespace Cubedesigners\UserDatabase; use Cubist\Backpack\app\Magic\Models\CubistMagicAbstractModel; +use Illuminate\Database\Eloquent\Builder; class Company extends CubistMagicAbstractModel { - protected $connection = 'extranet_users'; protected $table = 'company'; protected $_options = ['name' => 'company', @@ -83,4 +83,10 @@ class Company extends CubistMagicAbstractModel /** @var $user User */ return in_array($this->id, $user->getManagedCompanies()); } + + public static function addOwnerClause(Builder $builder) + { + $builder->whereIn( 'id', backpack_user()->getManagedCompanies()); + } + } diff --git a/src/Permissions.php b/src/Permissions.php index e8f5afe..50abc71 100644 --- a/src/Permissions.php +++ b/src/Permissions.php @@ -27,7 +27,7 @@ class Permissions 'managedUsers' => [], ]; - foreach (User::all() as $user) { + foreach (User::withoutGlobalScopes()->get() as $user) { if (!isset($data['companyUsers'][$user->company])) { $data['companyUsers'][$user->company] = []; } @@ -36,7 +36,7 @@ class Permissions $data['usersCompany'][$user->id] = $user->company; } - foreach (Company::all() as $company) { + foreach (Company::withoutGlobalScopes()->get() as $company) { if (!$company->admin || !isset($data['usersCompany'][$company->admin])) { continue; } diff --git a/src/User.php b/src/User.php index 96a28d3..7bd482a 100644 --- a/src/User.php +++ b/src/User.php @@ -3,6 +3,7 @@ namespace Cubedesigners\UserDatabase; use Cubist\Backpack\app\Magic\Models\CubistMagicAuthenticatable; +use Illuminate\Database\Eloquent\Builder; class User extends CubistMagicAuthenticatable { @@ -103,7 +104,7 @@ class User extends CubistMagicAuthenticatable $cacheKey = '_get_compagny_names'; start_measure($cacheKey, 'Get compagny names'); self::$_companyNames = cache()->tags(['model_' . Company::class])->remember($cacheKey, 86400, function () { - return Company::all()->pluck('name', 'id')->toArray(); + return Company::withoutGlobalScopes()->pluck('name', 'id')->toArray(); }); stop_measure($cacheKey); if (null === $id) { @@ -131,6 +132,9 @@ class User extends CubistMagicAuthenticatable public function isOwner($user) { + if (null === $this->id) { + return true; + } if (null === $user) { return false; } @@ -139,4 +143,10 @@ class User extends CubistMagicAuthenticatable } + public static function addOwnerClause(Builder $builder) + { + $builder->whereIn('id', Permissions::getManagedUsers(backpack_user()->id)); + } + + } -- 2.39.5