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