-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Administrator.php
92 lines (73 loc) · 2.38 KB
/
Administrator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
namespace Encore\Admin\Auth\Database;
use Encore\Admin\Traits\DefaultDatetimeFormat;
use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Support\Facades\Storage;
/**
* Class Administrator.
*
* @property Role[] $roles
*/
class Administrator extends Model implements AuthenticatableContract
{
use Authenticatable;
use HasPermissions;
use DefaultDatetimeFormat;
protected $fillable = ['username', 'password', 'name', 'avatar'];
/**
* Create a new Eloquent model instance.
*
* @param array $attributes
*/
public function __construct(array $attributes = [])
{
$connection = config('admin.database.connection') ?: config('database.default');
$this->setConnection($connection);
$this->setTable(config('admin.database.users_table'));
parent::__construct($attributes);
}
/**
* Get avatar attribute.
*
* @param string $avatar
*
* @return string
*/
public function getAvatarAttribute($avatar)
{
if ($avatar && url()->isValidUrl($avatar)) {
return $avatar;
}
$disk = config('admin.upload.disk');
if ($avatar && array_key_exists($disk, config('filesystems.disks'))) {
return Storage::disk(config('admin.upload.disk'))->url($avatar);
}
$default = config('admin.default_avatar') ?: '/vendor/laravel-admin/AdminLTE/dist/img/user2-160x160.jpg';
return admin_asset($default);
}
/**
* A user has and belongs to many roles.
*
* @return BelongsToMany
*/
public function roles(): BelongsToMany
{
$pivotTable = config('admin.database.role_users_table');
$relatedModel = config('admin.database.roles_model');
return $this->belongsToMany($relatedModel, $pivotTable, 'user_id', 'role_id');
}
/**
* A User has and belongs to many permissions.
*
* @return BelongsToMany
*/
public function permissions(): BelongsToMany
{
$pivotTable = config('admin.database.user_permissions_table');
$relatedModel = config('admin.database.permissions_model');
return $this->belongsToMany($relatedModel, $pivotTable, 'user_id', 'permission_id');
}
}