1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\ActiveRecord; |
4
|
|
|
|
5
|
|
|
use Ffcms\Core\App as MainApp; |
6
|
|
|
use Ffcms\Core\Arch\ActiveModel; |
7
|
|
|
use Ffcms\Core\Helper\Type\Any; |
8
|
|
|
use Ffcms\Core\Helper\Type\Str; |
9
|
|
|
use Ffcms\Core\Interfaces\iUser; |
10
|
|
|
use Ffcms\Core\Traits\SearchableTrait; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class User. Active record model for user auth data |
14
|
|
|
* @package Apps\ActiveRecord |
15
|
|
|
* @property int $id |
16
|
|
|
* @property string $email |
17
|
|
|
* @property string $password |
18
|
|
|
* @property int $role_id |
19
|
|
|
* @property string $approve_token |
20
|
|
|
* @property string $created_at |
21
|
|
|
* @property string $updated_at |
22
|
|
|
* @property WallPost $wall |
23
|
|
|
* @property Profile|null $profile |
24
|
|
|
* @property Role $role |
25
|
|
|
* @property UserLog $log |
26
|
|
|
* @property UserProvider $provider |
27
|
|
|
*/ |
28
|
|
|
class User extends ActiveModel implements iUser |
29
|
|
|
{ |
30
|
|
|
use SearchableTrait; |
31
|
|
|
|
32
|
|
|
protected $casts = [ |
33
|
|
|
'id' => 'integer', |
34
|
|
|
'email' => 'string', |
35
|
|
|
'role_id' => 'integer', |
36
|
|
|
'approve_token' => 'string' |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
protected $searchable = [ |
40
|
|
|
'columns' => [ |
41
|
|
|
'email' => 3, |
42
|
|
|
'nick' => 2 |
43
|
|
|
], |
44
|
|
|
'joins' => [ |
45
|
|
|
'profiles' => ['users.id', 'profiles.user_id'] |
46
|
|
|
] |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
private $openidProvider; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get user object relation. If $user_id is null - get current session user |
53
|
|
|
* @param string|int|null $id |
54
|
|
|
* @return self|null |
55
|
|
|
*/ |
56
|
|
|
public static function identity(?string $id = null): ?self |
57
|
|
|
{ |
58
|
|
|
if (!$id) { |
59
|
|
|
$id = MainApp::$Session->get('ff_user_id'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// check if id is looks like integer |
63
|
|
|
if (!Any::isInt($id) || (int)$id < 1) { |
64
|
|
|
return null; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// check in memory cache object |
68
|
|
|
if (MainApp::$Memory->get('user.object.cache.' . $id)) { |
69
|
|
|
return MainApp::$Memory->get('user.object.cache.' . $id); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// not founded in memory? lets make query |
73
|
|
|
$user = self::with(['profile', 'role']) |
74
|
|
|
->find($id); |
75
|
|
|
|
76
|
|
|
// store cache and return object |
77
|
|
|
MainApp::$Memory->set('user.object.cache.' . $user->id, $user); |
78
|
|
|
return $user; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get current user id if auth |
83
|
|
|
* @return int|null |
84
|
|
|
*/ |
85
|
|
|
public function getId(): ?int |
86
|
|
|
{ |
87
|
|
|
return (int)$this->id; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get user param |
92
|
|
|
* @param string $param |
93
|
|
|
* @param null|string $defaultValue |
94
|
|
|
* @return string|int|null |
95
|
|
|
*/ |
96
|
|
|
public function getParam(string $param, ?string $defaultValue = null): ?string |
97
|
|
|
{ |
98
|
|
|
return $this->{$param} ?? $defaultValue; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Check if current user session is auth |
103
|
|
|
* @return bool |
104
|
|
|
*/ |
105
|
|
|
public static function isAuth(): bool |
106
|
|
|
{ |
107
|
|
|
// get data from session |
108
|
|
|
$sessionUserId = (int)MainApp::$Session->get('ff_user_id', 0); |
109
|
|
|
|
110
|
|
|
// check if session contains user id data |
111
|
|
|
if ($sessionUserId < 1) { |
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// find user identity |
116
|
|
|
$identity = self::identity($sessionUserId); |
117
|
|
|
if (!$identity) { // check if this $id exist |
|
|
|
|
118
|
|
|
MainApp::$Session->invalidate(); // destory session data - it's not valid! |
119
|
|
|
return false; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// check if user is approved. Default value: 0, can be null, '' or the same. |
123
|
|
|
if ($identity->approve_token) { |
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return ($identity->id > 0 && $identity->id === $sessionUserId); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Check if user with $id exist |
132
|
|
|
* @param string|int|null $id |
133
|
|
|
* @return bool |
134
|
|
|
*/ |
135
|
|
|
public static function isExist(?string $id = null): bool |
136
|
|
|
{ |
137
|
|
|
if (!$id || !Any::isInt($id)) { |
138
|
|
|
return false; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$find = MainApp::$Memory->get('user.counter.cache.' . $id); |
142
|
|
|
if (!$find) { |
143
|
|
|
$find = self::where('id', $id)->count(); |
144
|
|
|
MainApp::$Memory->set('user.counter.cache.' . $id, $find); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return (int)$find === 1; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Check if use with $email is exist |
152
|
|
|
* @param string $email |
153
|
|
|
* @return bool |
154
|
|
|
*/ |
155
|
|
|
public static function isMailExist(?string $email = null): bool |
156
|
|
|
{ |
157
|
|
|
if (!Any::isStr($email) || !Str::isEmail($email)) { |
|
|
|
|
158
|
|
|
return false; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return self::where('email', $email)->count() > 0; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Get user person like a object via email |
166
|
|
|
* @param string|null $email |
167
|
|
|
* @return null|self |
168
|
|
|
*/ |
169
|
|
|
public static function getIdentityViaEmail(?string $email = null) |
170
|
|
|
{ |
171
|
|
|
if (!self::isMailExist($email)) { |
172
|
|
|
return null; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return self::where('email', $email)->first(); |
|
|
|
|
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get user wall post relation |
180
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
181
|
|
|
*/ |
182
|
|
|
public function wall() |
183
|
|
|
{ |
184
|
|
|
return $this->hasMany(WallPost::class, 'target_id'); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get user role relation object. |
189
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne |
190
|
|
|
*/ |
191
|
|
|
public function role() |
192
|
|
|
{ |
193
|
|
|
return $this->hasOne(Role::class, 'id', 'role_id'); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Get user profile relation object. |
198
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne |
199
|
|
|
*/ |
200
|
|
|
public function profile() |
201
|
|
|
{ |
202
|
|
|
return $this->hasOne(Profile::class, 'user_id', 'id'); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Get user logs relation object |
207
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
208
|
|
|
*/ |
209
|
|
|
public function log() |
210
|
|
|
{ |
211
|
|
|
return $this->hasMany(UserLog::class, 'user_id'); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Get user social providers data |
216
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
217
|
|
|
*/ |
218
|
|
|
public function provider() |
219
|
|
|
{ |
220
|
|
|
return $this->hasMany(UserProvider::class, 'user_id'); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Check if target user in blacklist |
225
|
|
|
* @param string|int|null $target |
226
|
|
|
* @return bool |
227
|
|
|
*/ |
228
|
|
|
public function inBlacklist(?string $target = null): bool |
229
|
|
|
{ |
230
|
|
|
if (!$target || (int)$target < 1) { |
231
|
|
|
return false; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
return Blacklist::have($this->getId(), $target); |
|
|
|
|
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Set openID library dependence object. Do not use this function, if you have no idia how it work |
239
|
|
|
* @param $provider |
240
|
|
|
*/ |
241
|
|
|
public function setOpenidInstance($provider): void |
242
|
|
|
{ |
243
|
|
|
$this->openidProvider = $provider; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Get openid provider library. Default - hybridauth |
248
|
|
|
* @return \Hybrid_Auth |
249
|
|
|
*/ |
250
|
|
|
public function getOpenidInstance() |
251
|
|
|
{ |
252
|
|
|
return $this->openidProvider; |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|