123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?php
- namespace App\Models\System;
- use App\Exceptions\ApiException;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\DB;
- use Spatie\Permission\Contracts\Permission as PermissionContract;
- use Spatie\Permission\Exceptions\PermissionAlreadyExists;
- use Spatie\Permission\Guard;
- use Spatie\Permission\Models\Permission;
- use Symfony\Component\HttpFoundation\Response;
- class Menu extends Permission
- {
- protected $attributes = [
- 'title' => 'name'
- // 'name' => 'title',
- // 'permission' => 'name'
- ];
- public static $snakeAttributes = false;
- protected $appends = ['title'];
- protected $casts = [
- 'visible' => 'bool',
- 'keepAlive' => 'bool',
- 'alwaysShow' => 'bool',
- // 'parent_id' => 'camel'
- ];
- /**
- * @throws ApiException
- */
- public static function create(array $attributes = [])
- {
- $attributes['guard_name'] = $attributes['guard_name'] ?? Guard::getDefaultName(static::class);
- if (isset($attributes['permission']) && $attributes['permission']) {
- $permission = static::getPermission(['permission' => $attributes['permission'], 'guard_name' => $attributes['guard_name']]);
- if ($permission) {
- throw new ApiException(['code' => Response::HTTP_UNPROCESSABLE_ENTITY, 'message' => 'PERMISSION_ALREADY_EXISTS']);
- }
- }
- return static::query()->create($attributes);
- }
- /**
- * @param array $attributes
- * @param array $options
- * @throws ApiException
- */
- public function update(array $attributes = [], array $options = []): void
- {
- $attributes['guard_name'] = $attributes['guard_name'] ?? Guard::getDefaultName(static::class);
- if (isset($attributes['permission']) && $attributes['permission']) {
- $permission = static::getPermission(['permission' => $attributes['permission'], 'guard_name' => $attributes['guard_name']]);
- if ($permission && $permission->id !== $attributes['id']) {
- throw new ApiException(['code' => Response::HTTP_UNPROCESSABLE_ENTITY, 'message' => 'PERMISSION_ALREADY_EXISTS']);
- }
- }
- static::query()->where('id', $attributes['id'])->update($attributes, $options);
- Cache::forget(config('permission.cache.key'));
- }
- public function setTitleAttribute($value)
- {
- $this->attributes['name'] = $value;
- }
- public function getTitleAttribute()
- {
- return $this->attributes['name'];
- }
- public function setNameAttribute($value)
- {
- $this->attributes['permission'] = $value;
- }
- public function getNameAttribute()
- {
- return $this->attributes['permission'];
- }
- public function setComponentNameAttribute($value)
- {
- $this->attributes['component_name'] = $value;
- }
- public function setVisibleAttribute($value)
- {
- $this->attributes['visible'] = $value ? 1 : 0;
- }
- public function setAlwaysShowAttribute($value)
- {
- $this->attributes['always_show'] = $value ? 1 : 0;
- }
- public function setKeepAliveAttribute($value)
- {
- $this->attributes['keep_alive'] = $value ? 1 : 0;
- }
- }
|