]> _ Git - cubedesigners_userdatabase.git/commitdiff
wip #3753 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 2 Dec 2020 12:36:48 +0000 (13:36 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 2 Dec 2020 12:36:48 +0000 (13:36 +0100)
src/Company.php
src/Permissions.php
src/User.php

index 9b9849b1a092496d2d3cfcf028e7930354c024eb..be7ff30652cf30a1cad2762868b185aac1f78948 100644 (file)
@@ -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());
+    }
+
 }
index e8f5afe4f879c48c475acf2f2d8290b7ab227eb9..50abc718a4f467b02c8b6e9334e0edd9cbcd66b1 100644 (file)
@@ -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;
                 }
index 96a28d3f46ba53c692116bb3eb317aea478b5115..7bd482aca325468ea1e0c79c1a314b1b2fc4b4e8 100644 (file)
@@ -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));
+    }
+
+
 }