]> _ Git - cubist_cms-back.git/commitdiff
wip #3753 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 2 Dec 2020 17:05:35 +0000 (18:05 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 2 Dec 2020 17:05:35 +0000 (18:05 +0100)
src/app/CubistCrudPanel.php
src/app/Http/Controllers/Operations/ShowOperation.php [new file with mode: 0644]
src/app/Magic/Fields/Field.php
src/app/Magic/Models/CubistMagicAbstractModel.php

index c9c2b95d7317f92efa5dca551ed5629fc7e69137..893be9916b5d3762f47e882e206841d21b96c14a 100644 (file)
@@ -60,4 +60,69 @@ class CubistCrudPanel extends CrudPanel
         $this->_seenFieldTypes[$type] = true;
         return true;
     }
+
+    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])) {
+                return $model->$func(backpack_user());
+            }
+            return true;
+        }
+        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;
+    }
 }
diff --git a/src/app/Http/Controllers/Operations/ShowOperation.php b/src/app/Http/Controllers/Operations/ShowOperation.php
new file mode 100644 (file)
index 0000000..e844cf3
--- /dev/null
@@ -0,0 +1,8 @@
+<?php
+
+namespace Cubist\Backpack\app\Http\Controllers\Operations;
+
+trait ShowOperation
+{
+    use \Backpack\CRUD\app\Http\Controllers\Operations\ShowOperation;
+}
index cc84ec07f28b4d89c177191bdf3d2384671114cf..7106915a28342dc74ca36f506869d8632f73e323 100644 (file)
@@ -237,6 +237,7 @@ class Field implements \ArrayAccess
 
         if (null !== $this->getAttribute('can', null)) {
             if (!can($this->getAttribute('can'))) {
+                $this->setAttribute('column',false);
                 $this->setAttribute('auth', false);
                 $this->setAttribute('type', 'authhidden');
                 $this->setAttribute('view_namespace', CubistBackpackServiceProvider::NAMESPACE . '::fields');
index bf1d800e06c735f1e4b39dd96460cec7f1611b83..dd674ba77765fdb4ed7c4a5fad00117b20137716 100644 (file)
@@ -679,19 +679,6 @@ class CubistMagicAbstractModel extends Model implements HasMedia
         Cache::tags($tags)->flush();
     }
 
-    /**
-     * @param $user CubistMagicAuthenticatable
-     * @return bool
-     */
-    public function isOwner($user)
-    {
-        if (null === $this->id) {
-            return true;
-        }
-
-        return null !== $user && ($this->canAdmin($user) || $this->getAttribute($this->_ownerAttribute) === $user->id);
-    }
-
     /**
      * @param $user CubistMagicAuthenticatable
      * @return bool
@@ -739,7 +726,7 @@ class CubistMagicAbstractModel extends Model implements HasMedia
             return false;
         }
         $permission = $this->getOption('name') . ':' . $operation;
-        return $user->can($permission);
+        return $user->hasPermissionTo($permission);
     }
 
     /**
@@ -748,7 +735,7 @@ class CubistMagicAbstractModel extends Model implements HasMedia
      */
     public function canView($user)
     {
-        return $this->isOwner($user);
+        return $this->canList($user);
     }
 
     /**
@@ -757,7 +744,7 @@ class CubistMagicAbstractModel extends Model implements HasMedia
      */
     public function canUpdate($user)
     {
-        return $this->isOwner($user);
+        return $this->canCreate($user);
     }
 
     /**
@@ -766,7 +753,7 @@ class CubistMagicAbstractModel extends Model implements HasMedia
      */
     public function canDelete($user)
     {
-        return $this->isOwner($user);
+        return $this->canUpdate($user);
     }
 
     /**