From: Vincent Vanwaelscappel Date: Thu, 5 Mar 2020 16:42:20 +0000 (+0100) Subject: wip #3466 @1 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=58abd78e78d6696b5c8219e0e2876fa6f587d967;p=cubist_cms-back.git wip #3466 @1 --- diff --git a/src/app/Magic/Controllers/CubistMagicController.php b/src/app/Magic/Controllers/CubistMagicController.php index e4fa39b..264a7e4 100644 --- a/src/app/Magic/Controllers/CubistMagicController.php +++ b/src/app/Magic/Controllers/CubistMagicController.php @@ -120,6 +120,10 @@ class CubistMagicController extends CubistCrudController if (is_array($field)) { $field = Field::getInstance($field); } + if (!$field->getAttribute('fillable', true)) { + $field->setAttribute('type','hidden'); + $field->setAttribute('type','hidden'); + } /** @var $field Field */ if ($field->isDisplayColumn()) { $this->crud->addColumn($field->getColumnData()); diff --git a/src/app/Magic/Fields/Field.php b/src/app/Magic/Fields/Field.php index cc5c373..cdcc59f 100644 --- a/src/app/Magic/Fields/Field.php +++ b/src/app/Magic/Fields/Field.php @@ -4,6 +4,7 @@ namespace Cubist\Backpack\app\Magic\Fields; use Cubist\Backpack\app\Magic\CubistMagicAttribute; +use Cubist\Backpack\CubistBackpackServiceProvider; use Doctrine\DBAL\Schema\Table; use Exception; use Illuminate\Support\Str; @@ -31,6 +32,9 @@ class Field implements \ArrayAccess protected $_translatable = false; protected $_migrateTranslatable = false; + protected $_can = null; + protected $_auth = true; + /** * @var CubistMagicAbstractModel */ @@ -104,6 +108,7 @@ class Field implements \ArrayAccess 'column_type' => $this->_columnType, 'column_move_after' => $this->_columnMoveAfter, 'default' => '', 'cast' => $this->_cast, 'column_view_namespace' => $this->_columnViewNamespace, 'searchLogic' => $this->_searchLogic, 'allow_null' => true, + 'can' => $this->_can, 'auth' => $this->_auth, 'fake' => false, 'store_in' => 'extras', 'attributes' => []]; } @@ -210,6 +215,14 @@ class Field implements \ArrayAccess $wrapperAttributes['data-when'] = json_encode($this->getAttribute('when')); $this->setAttribute('wrapperAttributes', $wrapperAttributes); } + + if (null !== $this->getAttribute('can', null)) { + if (!can($this->getAttribute('can'))) { + $this->setAttribute('auth', false); + $this->setAttribute('type', 'authhidden'); + $this->setAttribute('view_namespace', CubistBackpackServiceProvider::NAMESPACE . '::fields'); + } + } } /** diff --git a/src/app/Magic/Fields/Model.php b/src/app/Magic/Fields/Model.php index 6bf73f0..e782995 100644 --- a/src/app/Magic/Fields/Model.php +++ b/src/app/Magic/Fields/Model.php @@ -29,8 +29,8 @@ class Model extends Field /** @var \Illuminate\Database\Eloquent\Model $inst */ $inst = new $modelClass(); - $attr=$this->getAttribute('attribute'); - return $modelClass::orderBy($attr)->get()->pluck($attr, $inst->getKey())->toArray(); + $attr = $this->getAttribute('attribute'); + return $modelClass::orderBy($attr)->get()->pluck($attr, $inst->getKeyName())->toArray(); } public function getDatabaseType() diff --git a/src/app/Magic/Fields/User.php b/src/app/Magic/Fields/User.php index af342c3..360d0bc 100644 --- a/src/app/Magic/Fields/User.php +++ b/src/app/Magic/Fields/User.php @@ -5,17 +5,16 @@ namespace Cubist\Backpack\app\Magic\Fields; class User extends SelectFromModel { protected $_optionsmodel = '\App\User'; - protected $_allows_null = true; + protected $_allows_null = false; public function getDefaultAttributes() { - $default=['optionsmodel' => $this->_optionsmodel]; - - $auth=auth(); - if($auth){ - $user=$auth->user(); - if($user){ - $default['value']=$user->id; + $default = ['optionsmodel' => $this->_optionsmodel, 'attribute' => 'name']; + $auth = auth(); + if ($auth) { + $user = $auth->user(); + if ($user) { + $default['value'] = $user->id; } } return array_merge(parent::getDefaultAttributes(), $default); diff --git a/src/app/helpers.php b/src/app/helpers.php index 1b072ca..40ed2a1 100644 --- a/src/app/helpers.php +++ b/src/app/helpers.php @@ -32,3 +32,10 @@ if (!function_exists('protected_path')) { return base_path('protected/' . $path); } } + +if (!function_exists('can')) { + function can($permission, $model = null) + { + return auth()->user()->can($permission, $model = null); + } +} diff --git a/src/resources/views/fields/authhidden.blade.php b/src/resources/views/fields/authhidden.blade.php new file mode 100644 index 0000000..e69de29