/
User.php
267 lines (229 loc) · 5.31 KB
/
User.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php namespace App;
use App\Board;
use App\Post;
use App\Report;
use App\Contracts\PermissionUser as PermissionUserContract;
use App\Traits\PermissionUser;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
//use Laravel\Cashier\Billable;
//use Laravel\Cashier\Contracts\Billable as BillableContract;
use InfinityNext\Braintree\Billable;
use InfinityNext\Braintree\Contracts\Billable as BillableContract;
class User extends Model implements AuthenticatableContract, BillableContract, CanResetPasswordContract, PermissionUserContract {
use Authenticatable, Billable, CanResetPassword, PermissionUser;
/**
* Distinguishes this model from an Anonymous user.
*
* @var boolean
*/
protected $anonymous = false;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The primary key that is used by ::get()
*
* @var string
*/
protected $primaryKey = 'user_id';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'username',
'email',
'password',
'password_legacy',
];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = [
'username',
'email',
'email_verified',
'password',
'password_legacy',
'remember_token',
'stripe_active',
'stripe_id',
'stripe_subscription',
'stripe_plan',
'last_four',
'trial_ends_at',
'subscription_ends_at',
'subscription_kill_token',
'braintree_active',
'braintree_id',
];
/**
* Ties database triggers to the model.
*
* @return void
*/
public static function boot()
{
parent::boot();
// Setup event bindings...
// When creating a user, make empty email fields into NULL.
static::creating(function($user)
{
if ($user->email == "")
{
$user->email = NULL;
}
return true;
});
}
public function boards()
{
return $this->hasMany('\App\Board', 'operated_by', 'user_id');
}
public function createdBoards()
{
return $this->hasMany('\App\Board', 'created_by', 'user_id');
}
public function payments()
{
return $this->hasMany('\App\Payment', 'customer_id', 'user_id');
}
public function roles()
{
return $this->belongsToMany('\App\Role', 'user_roles', 'user_id', 'role_id');
}
/**
* Supplies the hashed password for this user.
*
* @return string
*/
public function getAuthPassword()
{
$hash = $this->password;
if (is_null($hash))
{
$legacyData = json_decode($this->password_legacy);
return $legacyData->hash;
}
return $hash;
}
/**
* Returns a hasher based on password data, allowing support for old encryption types.
*
* @return \Illuminate\Contracts\Hashing\Hasher|false
*/
public function getAuthObject()
{
$hasher = false;
if (!is_null($this->password_legacy))
{
$legacyData = json_decode($this->password_legacy);
$legacyHasher = "App\\Services\\Hashing\\{$legacyData->hasher}Hasher";
$hasher = new $legacyHasher;
foreach ($legacyData as $option => $value)
{
$hasher->{$option} = $value;
}
}
return $hasher;
}
/**
* Fetches all reports that this user can view (not submitted reports).
*
* @return Collection
*/
public function getReportedPostsViewable()
{
$posts = Post::whereHasReportsFor($this)
->withEverything()
->get();
foreach ($posts as $post)
{
foreach ($post->reports as $report)
{
$report->setRelation('post', $post);
}
}
return $posts;
}
/**
* Returns the full URL for a user profile.
*
* @return string
*/
public function getURL()
{
return url("/cp/user/{$this->getURLSlug()}");
}
/**
* Returns the fill URL for a staff profile regarding a single board.
*
* @param \App\Board $board
* @param string $action
* @return string
*/
public function getURLForBoardStaff(Board $board, $action = "")
{
return url("/cp/board/{$board->board_uri}/staff/{$this->getURLSlug()}/{$action}");
}
/**
* Returns part of the URL that would be used to identify this user.
*
* @return string
*/
public function getURLSlug()
{
return "{$this->username}.{$this->user_id}";
}
/**
* Query where has admin role.
*
* @return Query
*/
public function scopeWhereAdmin($query)
{
return $query->whereHas('roles', function($query)
{
$tempInstance = with(new Role);
$directSelect = $tempInstance->getTable();
$directKey = $tempInstance->getKeyName();
//$query->where(\DB::raw("`{$directSelect}`.`{$directKey}`"), '=', Role::ID_ADMIN);
$query->where("{$directSelect}.{$directKey}", '=', Role::ID_ADMIN);
});
}
/**
* Query where has admin role.
*
* @return Query
*/
public function scopeWhereOwner($query)
{
return $query->whereHas('roles', function($query)
{
$tempInstance = with(new Role);
$directSelect = $tempInstance->getTable();
$directKey = $tempInstance->getKeyName();
$query->where('role', "owner");
});
}
/**
* Query by username.
*
* @param string $username
* @return Query
*/
public function scopeWhereUsername($query, $username)
{
return $query->where('username', '=', $username)->limit(1);
}
}