1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Models\Eloquent; |
4
|
|
|
|
5
|
|
|
use Illuminate\Notifications\Notifiable; |
6
|
|
|
use Illuminate\Contracts\Auth\MustVerifyEmail; |
7
|
|
|
use Illuminate\Foundation\Auth\User as Authenticatable; |
8
|
|
|
use Laravel\Passport\HasApiTokens; |
9
|
|
|
use App\Models\Eloquent\UserExtra; |
10
|
|
|
use PDO; |
11
|
|
|
|
12
|
|
|
class User extends Authenticatable |
13
|
|
|
{ |
14
|
|
|
use HasApiTokens, Notifiable; |
|
|
|
|
15
|
|
|
|
16
|
|
|
protected $table='users'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The attributes that are mass assignable. |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $fillable=[ |
24
|
|
|
'name', 'email', 'password', 'avatar', 'contest_account' |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The attributes that should be hidden for arrays. |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $hidden=[ |
33
|
|
|
'password', 'remember_token', 'tokens' |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
public function banneds() { |
37
|
|
|
return $this->hasMany('App\Models\Eloquent\UserBanned'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function announcements() { |
41
|
|
|
return $this->hasMany('App\Models\Eloquent\Announcement'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function permissions() { |
45
|
|
|
return $this->hasMany('App\Models\Eloquent\UserPermission'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function imagehostings() { |
49
|
|
|
return $this->hasMany('App\Models\Eloquent\Tool\ImageHosting'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function extras() { |
53
|
|
|
return $this->hasMany('App\Models\Eloquent\UserExtra', 'uid'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function hasPermission($permissionID) { |
57
|
|
|
return ($this->permissions()->where(['permission_id'=>$permissionID])->count())>0; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function hasIndependentPassword() { |
61
|
|
|
return filled($this->password); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function hasIndependentEmail() { |
65
|
|
|
return !in_array(explode('@', $this->email)[1], ['temporary.email']) && !$this->contest_account; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function isIndependent() { |
69
|
|
|
return $this->hasIndependentPassword() && $this->hasIndependentEmail(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* To get some extra info of a user. |
74
|
|
|
* |
75
|
|
|
* @param string|array $need An array is returned when an array is passed in, Only one value is returned when a string is passed in. |
76
|
|
|
* @param int|null $secretLevel the secret level this query currently running on |
77
|
|
|
* @return string|array $result |
78
|
|
|
*/ |
79
|
|
|
public function getExtra($need, $secretLevel=0) { |
80
|
|
|
$ret=$this->extras()->orderBy('key')->get()->toArray(); |
81
|
|
|
$result=[]; |
82
|
|
|
if (!empty($ret)) { |
83
|
|
|
if (is_string($need)) { |
84
|
|
|
foreach ($ret as $value) { |
85
|
|
|
if (empty($value['secret_level']) || $value['secret_level']<=$secretLevel) { |
86
|
|
|
$keyName=UserExtra::$extraMapping[$value['key']] ?? 'unknown'; |
87
|
|
|
if ($keyName==$need) { |
88
|
|
|
return $value['value']; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
return null; |
93
|
|
|
} else { |
94
|
|
|
foreach ($ret as $value) { |
95
|
|
|
if (empty($value['secret_level']) || $value['secret_level']<=$secretLevel) { |
96
|
|
|
$keyName=UserExtra::$extraMapping[$value['key']] ?? 'unknown'; |
97
|
|
|
if (in_array($keyName, $need)) { |
98
|
|
|
$result[$keyName]=$value['value']; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
return $result; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* To set some extra info of a user. |
109
|
|
|
* |
110
|
|
|
* @param string $keyName insert when key not found or update when key exists. Only values declared in UserExtra Model are accepted |
111
|
|
|
* @param string|null $value the extra info will be delete when value is null |
112
|
|
|
* @param int|null $secretLevel the secret level this query currently running on |
113
|
|
|
* @return mixed $result |
114
|
|
|
*/ |
115
|
|
|
public function setExtra($keyName, $value=null, $secretLevel=-1) { |
116
|
|
|
$key=array_search($keyName, UserExtra::$extraMapping); |
117
|
|
|
if ($key===false) { |
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
$ret=$this->extras()->where('key', $key)->limit(1)->get()->toArray(); |
121
|
|
|
if (!empty($ret)) { |
122
|
|
|
$ret=$ret[0]; |
123
|
|
|
unset($ret['id']); |
124
|
|
|
if (!is_null($value)) { |
125
|
|
|
$ret['value']=$value; |
126
|
|
|
} else { |
127
|
|
|
$this->extras()->where('key', $key)->delete(); |
128
|
|
|
return true; |
129
|
|
|
} |
130
|
|
|
if ($secretLevel!=-1) { |
131
|
|
|
$ret['secret_level']=$secretLevel; |
132
|
|
|
} |
133
|
|
|
return $this->extras()->where('key', $key)->update($ret); |
134
|
|
|
} else { |
135
|
|
|
if ($value===null) { |
136
|
|
|
return true; |
137
|
|
|
} |
138
|
|
|
return $this->extras()->create([ |
139
|
|
|
'key' => $key, |
140
|
|
|
'value' => $value, |
141
|
|
|
'secret_level' => $secretLevel==-1 ? 0 : $secretLevel, |
142
|
|
|
])->id; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function getSocialiteInfo($secretLevel=-1) |
|
|
|
|
147
|
|
|
{ |
148
|
|
|
$socialites=[]; |
149
|
|
|
foreach (UserExtra::$socialite_support as $key => $value) { |
150
|
|
|
$id_keyname=$key.'_id'; |
151
|
|
|
$id=$this->getExtra($id_keyname); |
152
|
|
|
if (!empty($id)) { |
153
|
|
|
$info=[ |
154
|
|
|
'id' => $id, |
155
|
|
|
]; |
156
|
|
|
foreach ($value as $info_name) { |
157
|
|
|
$info_temp=$this->getExtra($key.'_'.$info_name); |
158
|
|
|
if ($info_temp!==null) { |
159
|
|
|
$info[$info_name]=$info_temp; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
$socialites[$key]=$info; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $socialites; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|