⚝
One Hat Cyber Team
⚝
Your IP:
216.73.216.19
Server IP:
178.33.27.10
Server:
Linux cpanel.dev-unit.com 3.10.0-1160.108.1.el7.x86_64 #1 SMP Thu Jan 25 16:17:31 UTC 2024 x86_64
Server Software:
Apache/2.4.57 (Unix) OpenSSL/1.0.2k-fips
PHP Version:
8.2.11
Buat File
|
Buat Folder
Eksekusi
Dir :
~
/
home
/
id
/
crm.dev-unit.com
/
app
/
Models
/
View File Name :
Role.php
<?php namespace App\Models; use Barryvdh\LaravelIdeHelper\Eloquent; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\MorphToMany; use Illuminate\Support\Carbon; use Spatie\Permission\Contracts\Role as RoleContract; use Spatie\Permission\Exceptions\RoleDoesNotExist; use Spatie\Permission\Guard; use Spatie\Permission\Traits\HasPermissions; /** * App\Models\Role * * @property int $id * @property string $name * @property string|null $display_name * @property string|null $description * @property string $guard_name * @property bool $is_default * @property Carbon|null $created_at * @property Carbon|null $updated_at * @property-read Collection|\Spatie\Permission\Models\Permission[] $permissions * @property-read int|null $permissions_count * * @method static Builder|Role newModelQuery() * @method static Builder|Role newQuery() * @method static Builder|Role permission($permissions) * @method static Builder|Role query() * @method static Builder|Role whereCreatedAt($value) * @method static Builder|Role whereDescription($value) * @method static Builder|Role whereDisplayName($value) * @method static Builder|Role whereGuardName($value) * @method static Builder|Role whereId($value) * @method static Builder|Role whereName($value) * @method static Builder|Role whereUpdatedAt($value) * @method static Builder|Role whereIsDefault($value) * * @property-read Collection|User[] $users * @mixin Eloquent */ class Role extends Model implements RoleContract { use HasPermissions; /** * Validation rules * * @var array */ public static $rules = [ 'name' => 'required|unique:roles,name', 'permissions' => 'required|array', ]; public static $messages = [ 'permissions.required' => 'Role must have at least one permission.', ]; /** * @var string */ protected $table = 'roles'; /** * @var string[] */ protected $fillable = [ 'name', 'display_name', 'description', 'is_default', ]; /** * The attributes that should be casted to native types. * * @var array */ protected $casts = [ 'id' => 'integer', 'name' => 'string', 'display_name' => 'string', 'description' => 'string', 'is_default' => 'boolean', ]; public static function boot() { parent::boot(); self::creating(function (self $role) { if (empty($role->guard_name)) { $role->guard_name = Guard::getDefaultName(static::class); } }); } /** * A role may be given various permissions. */ public function permissions(): BelongsToMany { return $this->belongsToMany( config('permission.models.permission'), config('permission.table_names.role_has_permissions'), 'role_id', 'permission_id' ); } /** * A role belongs to some users of the model associated with its guard. */ public function users(): MorphToMany { return $this->morphedByMany( getModelForGuard($this->attributes['guard_name']), 'model', config('permission.table_names.model_has_roles'), 'role_id', config('permission.column_names.model_morph_key') ); } /** * @param string $name * @param null|string $guardName * @return RoleContract|Builder */ public static function findByName(string $name, $guardName = null): RoleContract { $guardName = $guardName ?? Guard::getDefaultName(static::class); $role = static::where('name', $name)->where('guard_name', $guardName)->first(); if (! $role) { throw RoleDoesNotExist::named($name); } return $role; } /** * @param int $id * @param null|string $guardName * @return RoleContract|Builder */ public static function findById(int $id, $guardName = null): RoleContract { $guardName = $guardName ?? Guard::getDefaultName(static::class); $role = static::where('id', $id)->where('guard_name', $guardName)->first(); if (! $role) { throw RoleDoesNotExist::withId($id); } return $role; } /** * Find or create role by its name (and optionally guardName). * * @param string $name * @param string|null $guardName * @return RoleContract|Builder */ public static function findOrCreate(string $name, $guardName = null): RoleContract { $guardName = $guardName ?? Guard::getDefaultName(static::class); $role = static::where('name', $name)->where('guard_name', $guardName)->first(); if (! $role) { return static::query()->create(['name' => $name, 'guard_name' => $guardName]); } return $role; } }