]> _ Git - cubist_cms-back.git/commitdiff
wip #3753 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 1 Dec 2020 20:07:16 +0000 (21:07 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 1 Dec 2020 20:07:16 +0000 (21:07 +0100)
src/app/CubistCrudPanel.php
src/app/Http/Controllers/Operations/ListOperation.php
src/app/Magic/Models/CubistMagicAbstractModel.php

index 0a2375f978be5f061bcc54a2d1fdfe15a7345c68..c9c2b95d7317f92efa5dca551ed5629fc7e69137 100644 (file)
@@ -60,84 +60,4 @@ class CubistCrudPanel extends CrudPanel
         $this->_seenFieldTypes[$type] = true;
         return true;
     }
-
-    public function addOwnerClause($user)
-    {
-        if (!$this->model instanceof CubistMagicAbstractModel) {
-            return;
-        }
-        if ($this->model->canAdmin($user)) {
-            return;
-        }
-        $this->model->addOwnerClause($this,$user);
-    }
-
-    public function hasAccess($operation)
-    {
-        // First filter with standard backpack gate
-        if (!parent::hasAccess($operation)) {
-            return false;
-        }
-
-        // Then, apply model rules
-        if ($this->model instanceof CubistMagicAbstractModel) {
-            $funcMap = ['list' => 'canList',
-                'create' => 'canCreate',
-                'update' => 'canUpdate',
-                'delete' => 'canDelete',
-                'revisions' => 'canUpdate',
-                'revise' => 'canUpdate',
-                'bulkClone' => 'canCreate',
-                'clone' => 'canCreate',
-                'bulkDelete' => 'canDelete',
-            ];
-
-            if (isset($funcMap[$operation])) {
-                $func = $funcMap[$operation];
-            } else {
-                $func = 'can' . ucfirst($operation);
-            }
-            $model = $this->entry ?? $this->model;
-            if (is_callable([$model, $func])) {
-                $res = $model->$func(backpack_user());
-            } else {
-                $res = true;
-            }
-            return $res;
-        }
-
-        return true;
-    }
-
-    public function hasAccessOrFail($operation)
-    {
-        if (!$this->hasAccess($operation)) {
-            throw new AccessDeniedException(trans('backpack::crud.unauthorized_access', ['access' => $operation]));
-        }
-
-
-        return true;
-    }
-
-    public function hasAccessToAll($operation_array)
-    {
-        foreach ((array)$operation_array as $key => $operation) {
-            if (!$this->hasAccess($operation)) {
-                return false;
-            }
-        }
-
-        return true;
-    }
-
-    public function hasAccessToAny($operation_array)
-    {
-        foreach ((array)$operation_array as $key => $operation) {
-            if ($this->hasAccess($operation) == true) {
-                return true;
-            }
-        }
-
-        return false;
-    }
 }
index 58ef58439b634843b1650c49d5a62b260cb18afe..caf06866cc3cfedb80a4f35a1f548254c565c5ce 100644 (file)
@@ -6,8 +6,5 @@ namespace Cubist\Backpack\app\Http\Controllers\Operations;
 
 trait ListOperation
 {
-    public function setupListOperation()
-    {
-        $this->crud->addOwnerClause(backpack_user());
-    }
+    use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
 }
index a45b550052e2ca4d1f36c7ac4c786b2093d5099c..0f85f6a5d5a17f608175563e40b57650d5d77218 100644 (file)
@@ -56,7 +56,7 @@ class CubistMagicAbstractModel extends Model implements HasMedia
     protected $_enableCreation = true;
     protected $_enableRevisions = true;
     protected $_enableBulk = true;
-    protected $_ownerAttribute = 'owner';
+    protected static $_ownerAttribute = 'owner';
 
     protected $_syncDbSchema = true;
 
@@ -91,6 +91,15 @@ class CubistMagicAbstractModel extends Model implements HasMedia
     public static function boot()
     {
         parent::boot();
+
+        static::addGlobalScope('userfilter', function (Builder $builder) {
+            static::addOwnerClause($builder);
+        });
+    }
+
+    public static function addOwnerClause(Builder $builder)
+    {
+        $builder->where(static::$_ownerAttribute, backpack_user()->id);
     }
 
     public function __construct(array $attributes = [])
@@ -678,25 +687,29 @@ class CubistMagicAbstractModel extends Model implements HasMedia
      */
     public function isOwner($user)
     {
+        if (null === $this->id) {
+            return true;
+        }
+
         return null !== $user && ($this->canAdmin($user) || $this->getAttribute($this->_ownerAttribute) === $user->id);
     }
 
     /**
-     * @param $crud CubistCrudPanel
      * @param $user CubistMagicAuthenticatable
+     * @return bool
      */
-    public function addOwnerClause($crud, $user)
+    public function canList($user)
     {
-        $crud->addClause('where', $this->_ownerAttribute, $user->id);
+        return null !== $user && ($this->canAdmin($user) || $this->_can('read', $user));
     }
 
     /**
      * @param $user CubistMagicAuthenticatable
      * @return bool
      */
-    public function canList($user)
+    public function canShow($user)
     {
-        return null !== $user && ($this->canAdmin($user) || $this->_can('read', $user));
+        return $this->canList($user);
     }
 
     /**