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