1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Modules\Users; |
4
|
|
|
|
5
|
|
|
use App\User; |
6
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
7
|
|
|
use Laravel\Passport\HasApiTokens; |
8
|
|
|
use App\Modules\Notifications\Notification; |
9
|
|
|
use App\Modules\Roles\Role; |
10
|
|
|
use App\Modules\OauthClients\OauthClient; |
11
|
|
|
use App\Modules\Users\ModelObservers\AclUserObserver; |
12
|
|
|
|
13
|
|
|
class AclUser extends User |
14
|
|
|
{ |
15
|
|
|
use SoftDeletes, HasApiTokens; |
16
|
|
|
protected $table = 'users'; |
17
|
|
|
protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
18
|
|
|
protected $hidden = ['password', 'remember_token', 'deleted_at']; |
19
|
|
|
protected $guarded = ['id']; |
20
|
|
|
public $fillable = ['profile_picture', 'name', 'email', 'password', 'locale', 'timezone']; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Encrypt the password attribute before |
24
|
|
|
* saving it in the storage. |
25
|
|
|
* |
26
|
|
|
* @param string $value |
27
|
|
|
*/ |
28
|
|
|
public function setPasswordAttribute($value) |
29
|
|
|
{ |
30
|
|
|
$this->attributes['password'] = \Hash::make($value); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get the entity's notifications. |
35
|
|
|
*/ |
36
|
|
|
public function notifications() |
37
|
|
|
{ |
38
|
|
|
return $this->morphMany(Notification::class, 'notifiable')->orderBy('created_at', 'desc'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get the entity's read notifications. |
43
|
|
|
*/ |
44
|
|
|
public function readNotifications() |
45
|
|
|
{ |
46
|
|
|
return $this->notifications()->whereNotNull('read_at'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get the entity's unread notifications. |
51
|
|
|
*/ |
52
|
|
|
public function unreadNotifications() |
53
|
|
|
{ |
54
|
|
|
return $this->notifications()->whereNull('read_at'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function roles() |
58
|
|
|
{ |
59
|
|
|
return $this->belongsToMany(Role::class, 'role_user', 'user_id', 'role_id')->whereNull('role_user.deleted_at')->withTimestamps(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function oauthClients() |
63
|
|
|
{ |
64
|
|
|
return $this->hasMany(OauthClient::class, 'user_id'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function setProfilePictureAttribute($value) { |
68
|
|
|
$this->attributes['profile_picture'] = \Media::uploadImageBas64($value, 'users/profile_pictures'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Return fcm device tokens that will be used in sending fcm notifications. |
73
|
|
|
* |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
public function routeNotificationForFCM() |
77
|
|
|
{ |
78
|
|
|
$devices = \Core::pushNotificationDevices()->findBy(['user_id' => $this->id]); |
79
|
|
|
$tokens = []; |
80
|
|
|
|
81
|
|
|
foreach ($devices as $device) { |
82
|
|
|
if (\Core::oauthClients()->accessTokenExpiredOrRevoked($device->access_token)) { |
83
|
|
|
$device->forceDelete(); |
84
|
|
|
continue; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$tokens[] = $device->device_token; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $tokens; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* The channels the user receives notification broadcasts on. |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function receivesBroadcastNotificationsOn() |
99
|
|
|
{ |
100
|
|
|
return 'users.'.$this->id; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Custom password validation. |
105
|
|
|
* |
106
|
|
|
* @param string $password |
107
|
|
|
* @return boolean |
108
|
|
|
*/ |
109
|
|
|
public function validateForPassportPasswordGrant($password) |
110
|
|
|
{ |
111
|
|
|
if ($password == config('user.social_pass')) { |
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return \Hash::check($password, $this->password); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public static function boot() |
119
|
|
|
{ |
120
|
|
|
parent::boot(); |
121
|
|
|
AclUser::observe(AclUserObserver::class); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|