1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\ActiveRecord; |
4
|
|
|
|
5
|
|
|
use Ffcms\Core\Arch\ActiveModel; |
6
|
|
|
use Ffcms\Core\Interfaces\iUser; |
7
|
|
|
use Ffcms\Core\App as MainApp; |
8
|
|
|
use Ffcms\Core\Helper\Type\Obj; |
9
|
|
|
use Ffcms\Core\Helper\Type\Str; |
10
|
|
|
|
11
|
|
|
class User extends ActiveModel implements iUser |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Get user object relation. If $user_id is null - get current session user |
16
|
|
|
* @param int|null $user_id |
17
|
|
|
* @return self|null |
18
|
|
|
*/ |
19
|
|
|
public static function identity($user_id = null) |
20
|
|
|
{ |
21
|
|
|
if ($user_id === null) { |
22
|
|
|
$user_id = MainApp::$Session->get('ff_user_id'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
// convert id to real integer |
26
|
|
|
$user_id = (int)$user_id; |
27
|
|
|
|
28
|
|
|
if (!Obj::isInt($user_id) || $user_id < 1) { |
29
|
|
|
return null; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
// check in memory cache object |
33
|
|
|
if (MainApp::$Memory->get('user.object.cache.' . $user_id) !== null) { |
34
|
|
|
return MainApp::$Memory->get('user.object.cache.' . $user_id); |
35
|
|
|
} |
36
|
|
|
// not founded in memory? lets make query |
37
|
|
|
$user = self::find($user_id); |
38
|
|
|
// no rows? lets end this shit ;) |
39
|
|
|
if ($user === null || $user->id < 1) { |
40
|
|
|
return null; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// store cache and return object |
44
|
|
|
MainApp::$Memory->set('user.object.cache.' . $user->id, $user); |
45
|
|
|
return $user; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get current user id if auth |
50
|
|
|
* @return int |
51
|
|
|
*/ |
52
|
|
|
public function getId() |
53
|
|
|
{ |
54
|
|
|
return (int)$this->id; |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get user param |
59
|
|
|
* @param string $param |
60
|
|
|
* @param null|string $defaultValue |
61
|
|
|
* @return string|int|null |
62
|
|
|
*/ |
63
|
|
|
public function getParam($param, $defaultValue = null) |
64
|
|
|
{ |
65
|
|
|
return $this->{$param} === null ? $defaultValue : $this->{$param}; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Check if current user session is auth |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
|
|
public static function isAuth() |
73
|
|
|
{ |
74
|
|
|
// get data from session |
75
|
|
|
$session_token = MainApp::$Session->get('ff_user_token', null); |
76
|
|
|
$session_id = (int)MainApp::$Session->get('ff_user_id', 0); |
77
|
|
|
|
78
|
|
|
// validate session data |
79
|
|
|
if (null === $session_token || $session_id < 1 || Str::length($session_token) < 64) { |
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// find user identity |
84
|
|
|
$find = self::identity($session_id); |
85
|
|
|
if (null === $find || Str::length($find->token_data) < 64) { // check if this $id exist |
|
|
|
|
86
|
|
|
MainApp::$Session->invalidate(); // destory session data - it's not valid! |
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// check if user is approved. Default value: 0, can be null, '' or the same. |
91
|
|
|
if ($find->approve_token !== '0' && Str::length($find->approve_token) > 0) { |
|
|
|
|
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $find->token_data === $session_token; |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Check if user with $id exist |
100
|
|
|
* @param int $id |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
|
|
public static function isExist($id) |
104
|
|
|
{ |
105
|
|
|
if (!Obj::isLikeInt($id) || $id < 1) { |
106
|
|
|
return false; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// convert id to real integer |
110
|
|
|
$id = (int)$id; |
111
|
|
|
|
112
|
|
|
$find = MainApp::$Memory->get('user.counter.cache.' . $id); |
113
|
|
|
if ($find === null) { |
114
|
|
|
$find = self::where('id', '=', $id)->count(); |
115
|
|
|
MainApp::$Memory->set('user.counter.cache.' . $id, $find); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $find === 1; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Check if use with $email is exist |
123
|
|
|
* @param string $email |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
View Code Duplication |
public static function isMailExist($email) |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
if (!Obj::isString($email) || !Str::isEmail($email)) { |
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return self::where('email', '=', $email)->count() > 0; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Check if user with $login is exist |
137
|
|
|
* @param string $login |
138
|
|
|
* @return bool |
139
|
|
|
*/ |
140
|
|
View Code Duplication |
public static function isLoginExist($login) |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
if (!Obj::isString($login) || Str::length($login) < 1) { |
143
|
|
|
return false; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return self::where('login', '=', $login)->count() > 0; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get user person like a object via email |
151
|
|
|
* @param string $email |
152
|
|
|
* @return null|static |
153
|
|
|
*/ |
154
|
|
|
public static function getIdentityViaEmail($email) |
155
|
|
|
{ |
156
|
|
|
if (!self::isMailExist($email)) { |
157
|
|
|
return null; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return self::where('email', '=', $email)->first(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Get relation one-to-many for user wall posts. Ex: User::find(1)->getWall()->offset() |
165
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
166
|
|
|
*/ |
167
|
|
|
public function getWall() |
168
|
|
|
{ |
169
|
|
|
return $this->hasMany('Apps\\ActiveRecord\\WallPost', 'target_id'); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Get user role object |
174
|
|
|
* @return \Apps\ActiveRecord\Role|null |
175
|
|
|
*/ |
176
|
|
|
public function getRole() |
177
|
|
|
{ |
178
|
|
|
return Role::get($this->role_id); |
|
|
|
|
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Get user profile data as relation of user table. Ex: User::find(1)->getProfile()->nick |
183
|
|
|
* @return \Apps\ActiveRecord\Profile |
184
|
|
|
*/ |
185
|
|
|
public function getProfile() |
186
|
|
|
{ |
187
|
|
|
// lets find profile identity via current user id |
188
|
|
|
$object = Profile::identity($this->getId()); |
189
|
|
|
// is not exist? Hmmm, lets create it! |
190
|
|
|
if ($object === null && $this->getId() > 0) { |
191
|
|
|
$object = new Profile(); |
192
|
|
|
$object->user_id = $this->getId(); |
|
|
|
|
193
|
|
|
$object->save(); |
194
|
|
|
} |
195
|
|
|
// return result ;) |
|
|
|
|
196
|
|
|
return $object; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Check if target user in blacklist |
201
|
|
|
* @param int $target_id |
202
|
|
|
* @return bool |
203
|
|
|
*/ |
204
|
|
|
public function inBlacklist($target_id) |
205
|
|
|
{ |
206
|
|
|
return Blacklist::have($this->getId(), $target_id); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
} |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.