akaunting/app/Models/Auth/Permission.php

82 lines
2.0 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
namespace App\Models\Auth;
2022-06-01 10:15:55 +03:00
use Akaunting\Sortable\Traits\Sortable;
2020-05-02 14:53:06 +03:00
use App\Traits\Tenants;
2019-11-16 10:21:14 +03:00
use Laratrust\Models\LaratrustPermission;
2017-09-14 22:21:00 +03:00
use Laratrust\Traits\LaratrustPermissionTrait;
2019-11-16 10:21:14 +03:00
use Lorisleiva\LaravelSearchString\Concerns\SearchString;
2017-09-14 22:21:00 +03:00
class Permission extends LaratrustPermission
{
2022-06-01 10:15:55 +03:00
use LaratrustPermissionTrait, SearchString, Sortable, Tenants;
2017-09-14 22:21:00 +03:00
protected $table = 'permissions';
2020-01-07 23:36:18 +03:00
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = ['title'];
2017-09-14 22:21:00 +03:00
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'display_name', 'description'];
/**
* Scope to get all rows filtered, sorted and paginated.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param $sort
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeCollect($query, $sort = 'display_name')
{
$request = request();
2019-11-16 10:21:14 +03:00
$search = $request->get('search');
$limit = (int) $request->get('limit', setting('default.list_limit', '25'));
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
return $query->usingSearchString($search)->sortable($sort)->paginate($limit);
2017-09-14 22:21:00 +03:00
}
2020-01-07 23:36:18 +03:00
/**
2020-02-14 15:08:16 +03:00
* Scope to only include by action.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $action
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeAction($query, $action = 'read')
{
return $query->where('name', 'like', $action . '-%');
}
/**
* Transform display name.
2020-01-07 23:36:18 +03:00
*
* @return string
*/
public function getTitleAttribute()
{
$replaces = [
'Create ' => '',
'Read ' => '',
'Update ' => '',
'Delete ' => '',
'Modules' => 'Apps',
];
$title = str_replace(array_keys($replaces), array_values($replaces), $this->display_name);
return $title;
}
2017-09-14 22:21:00 +03:00
}