protected $_databaseAttributes = [];
+
+ protected static $_cachedCan = [];
+ protected static $_cachedCanUser = null;
+
/**
* @param $attributes
* @return Field
public function setPermissions()
{
if (null !== $this->getAttribute('can', null)) {
- if (!can($this->getAttribute('can'))) {
+ if (!self::can($this->getAttribute('can'))) {
$this->setAttribute('preview', false);
$this->setAttribute('filter', false);
$this->setAttribute('column', false);
}
if (null !== $this->getAttribute('can_write', null)) {
- if (!can($this->getAttribute('can_write'))) {
+ if (!self::can($this->getAttribute('can_write'))) {
$this->setAttribute('type', 'column_value');
$this->setAttribute('view_namespace', CubistBackpackServiceProvider::NAMESPACE . '::fields');
}
return $this->_isRelationship;
}
+ public static function can($permission)
+ {
+ $uid = backpack_user() === null ? null : backpack_user()->id;
+ if ($uid !== self::$_cachedCanUser) {
+ self::$_cachedCanUser = $uid;
+ self::$_cachedCan = [];
+ }
+ if (!isset(self::$_cachedCan[$permission])) {
+ self::$_cachedCan[$permission] = can($permission);
+ }
+ return self::$_cachedCan[$permission];
+ }
}
public function getDefaultAttributes()
{
- return array_merge(parent::getDefaultAttributes(), ['attribute' => 'name', 'column_attribute' => null, 'allows_null' => false, 'allows_multiple' => $this->_multiple]);
+ return array_merge(parent::getDefaultAttributes(), ['attribute' => 'name', 'column_attribute' => null, 'allows_null' => false, 'allows_multiple' => $this->_multiple, 'optionsmodel_global_scopes' => true]);
}
- protected function _getOptions()
+ public function _getOptions()
{
return $this->pluck($this->getAttribute('attribute'));
}
public function pluck($attr)
{
- $bui = '';
- if (backpack_user() !== null) {
- $bui = backpack_user()->id;
- }
+ $bui = backpack_user() === null ? '' : backpack_user()->id;
$modelClass = $this->getAttribute('optionsmodel');
+ $globalCacheKey = '_getOption_Model_' . $modelClass . '-' . $attr;
$cacheKey = '_getOption_Model_' . $modelClass . '-' . $attr . '-' . $bui;
+ $cacheKeysKey = '_getOption_Model_' . $modelClass . '-Keys-' . $bui;
if (isset(static::$_options[$cacheKey])) {
return static::$_options[$cacheKey];
}
- start_measure($cacheKey, 'Get options for model ' . $modelClass . ' / ' . $attr);
$tag = 'model_' . $modelClass;
- static::$_options[$cacheKey] = cache()->tags([$tag])->remember($cacheKey, 86400, function () use ($modelClass, $attr) {
- /** @var \Illuminate\Database\Eloquent\Model $inst */
- $inst = new $modelClass();
- $options = $modelClass::all();
- return $options->pluck($attr, $inst->getKeyName())->toArray();
+
+ if (!isset(static::$_options[$globalCacheKey])) {
+ start_measure($globalCacheKey, 'Get options for model ' . $modelClass . ' / ' . $attr);
+ static::$_options[$globalCacheKey] = cache()->tags([$tag])->remember($globalCacheKey, 86400, function () use ($modelClass, $attr) {
+ set_time_limit(0);
+ /** @var \Illuminate\Database\Eloquent\Model $inst */
+ $inst = new $modelClass();
+ return $modelClass::withoutGlobalScopes()->get()->pluck($attr, $inst->getKeyName())->toArray();
+ });
+ stop_measure($globalCacheKey);
+ }
+
+ $globalScopes = $this->getAttribute('optionsmodel_global_scopes', true);
+ if (!$globalScopes) {
+ static::$_options[$cacheKey] = static::$_options[$globalCacheKey];
+ return static::$_options[$cacheKey];
+ }
+
+ if (!isset(static::$_options[$cacheKeysKey])) {
+ static::$_options[$cacheKeysKey] = cache()->tags([$tag])->remember($cacheKeysKey, 86400, function () use ($modelClass) {
+ set_time_limit(0);
+ return $modelClass::all()->modelKeys();
+ });
+ }
+
+ static::$_options[$cacheKey] = cache()->tags([$tag])->remember($cacheKey, 86400, function () use ($globalCacheKey, $cacheKeysKey) {
+ $res = [];
+ foreach (static::$_options[$cacheKeysKey] as $k) {
+ if (isset(static::$_options[$globalCacheKey][$k])) {
+ $res[$k] = static::$_options[$globalCacheKey][$k];
+ }
+ }
+ return $res;
});
- stop_measure($cacheKey);
return static::$_options[$cacheKey];
}
$this->addField(['name' => 'enabled',
'label' => 'Enabled',
'type' => 'checkbox',
- 'tab' => 'Login']);
+ 'tab' => 'Login',
+ 'default' => true]);
$this->addField(['name' => 'remember_token',
'type' => 'Text',
'label' => 'E-mail',
'type' => 'Email',
'column' => true,
- 'database_unique'=>true,
+ 'database_unique' => true,
'tab' => 'Login']);
$this->addField(['name' => 'password',
'label' => 'Password',
'type' => 'Password',
'tab' => 'Login',
- 'can'=>'users:admin'
+ 'can' => 'users:admin'
]);
$this->addField(['name' => 'api_token',
'label' => 'API Token',
'type' => 'APIToken',
'tab' => 'Login',
- 'can'=>'users:admin'
+ 'can' => 'users:admin'
]);
$this->addField(['name' => 'rolesandperms',
'label' => '',
'type' => 'RolesPermissions',
'tab' => 'Login',
- 'can'=>'users:admin'
+ 'can' => 'users:admin'
]);
}
}