| Total Complexity | 85 |
| Total Lines | 570 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like User often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use User, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 146 | class User extends Authenticatable |
||
| 147 | { |
||
| 148 | use HasRoles, Notifiable, SoftDeletes, UserVerification; |
||
| 149 | |||
| 150 | public const ERR_SIGNUP_BADUNAME = -1; |
||
| 151 | |||
| 152 | public const ERR_SIGNUP_BADPASS = -2; |
||
| 153 | |||
| 154 | public const ERR_SIGNUP_BADEMAIL = -3; |
||
| 155 | |||
| 156 | public const ERR_SIGNUP_UNAMEINUSE = -4; |
||
| 157 | |||
| 158 | public const ERR_SIGNUP_EMAILINUSE = -5; |
||
| 159 | |||
| 160 | public const ERR_SIGNUP_BADINVITECODE = -6; |
||
| 161 | |||
| 162 | public const SUCCESS = 1; |
||
| 163 | |||
| 164 | public const ROLE_USER = 1; |
||
| 165 | |||
| 166 | public const ROLE_ADMIN = 2; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Users SELECT queue type. |
||
| 170 | */ |
||
| 171 | public const QUEUE_NONE = 0; |
||
| 172 | |||
| 173 | public const QUEUE_SABNZBD = 1; |
||
| 174 | |||
| 175 | public const QUEUE_NZBGET = 2; |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @var string |
||
| 179 | */ |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @var bool |
||
| 183 | */ |
||
| 184 | protected $dateFormat = false; |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @var array |
||
| 188 | */ |
||
| 189 | protected $hidden = ['remember_token', 'password']; |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @var array |
||
| 193 | */ |
||
| 194 | protected $guarded = []; |
||
| 195 | |||
| 196 | protected function getDefaultGuardName(): string |
||
| 197 | { |
||
| 198 | return 'web'; |
||
| 199 | } |
||
| 200 | |||
| 201 | public function role(): BelongsTo |
||
| 202 | { |
||
| 203 | return $this->belongsTo(Role::class, 'roles_id'); |
||
| 204 | } |
||
| 205 | |||
| 206 | public function request(): HasMany |
||
| 207 | { |
||
| 208 | return $this->hasMany(UserRequest::class, 'users_id'); |
||
| 209 | } |
||
| 210 | |||
| 211 | public function download(): HasMany |
||
| 212 | { |
||
| 213 | return $this->hasMany(UserDownload::class, 'users_id'); |
||
| 214 | } |
||
| 215 | |||
| 216 | public function release(): HasMany |
||
| 217 | { |
||
| 218 | return $this->hasMany(UsersRelease::class, 'users_id'); |
||
| 219 | } |
||
| 220 | |||
| 221 | public function series(): HasMany |
||
| 222 | { |
||
| 223 | return $this->hasMany(UserSerie::class, 'users_id'); |
||
| 224 | } |
||
| 225 | |||
| 226 | public function invitation(): HasMany |
||
| 227 | { |
||
| 228 | return $this->hasMany(Invitation::class, 'invited_by'); |
||
| 229 | } |
||
| 230 | |||
| 231 | public function failedRelease(): HasMany |
||
| 232 | { |
||
| 233 | return $this->hasMany(DnzbFailure::class, 'users_id'); |
||
| 234 | } |
||
| 235 | |||
| 236 | public function comment(): HasMany |
||
| 237 | { |
||
| 238 | return $this->hasMany(ReleaseComment::class, 'users_id'); |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Get the user's timezone or default to UTC |
||
| 243 | */ |
||
| 244 | public function getTimezone(): string |
||
| 245 | { |
||
| 246 | return $this->timezone ?? 'UTC'; |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @throws \Exception |
||
| 251 | */ |
||
| 252 | public static function deleteUser($id): void |
||
| 253 | { |
||
| 254 | self::find($id)->delete(); |
||
| 255 | } |
||
| 256 | |||
| 257 | public static function getCount(?string $role = null, ?string $username = '', ?string $host = '', ?string $email = '', ?string $createdFrom = '', ?string $createdTo = ''): int |
||
| 258 | { |
||
| 259 | $res = self::query()->withTrashed()->where('email', '<>', '[email protected]'); |
||
| 260 | |||
| 261 | if (! empty($role)) { |
||
| 262 | $res->where('roles_id', $role); |
||
| 263 | } |
||
| 264 | |||
| 265 | if ($username !== '') { |
||
| 266 | $res->where('username', 'like', '%'.$username.'%'); |
||
| 267 | } |
||
| 268 | |||
| 269 | if ($host !== '') { |
||
| 270 | $res->where('host', 'like', '%'.$host.'%'); |
||
| 271 | } |
||
| 272 | |||
| 273 | if ($email !== '') { |
||
| 274 | $res->where('email', 'like', '%'.$email.'%'); |
||
| 275 | } |
||
| 276 | |||
| 277 | if ($createdFrom !== '') { |
||
| 278 | $res->where('created_at', '>=', $createdFrom.' 00:00:00'); |
||
| 279 | } |
||
| 280 | |||
| 281 | if ($createdTo !== '') { |
||
| 282 | $res->where('created_at', '<=', $createdTo.' 23:59:59'); |
||
| 283 | } |
||
| 284 | |||
| 285 | return $res->count(['id']); |
||
| 286 | } |
||
| 287 | |||
| 288 | public static function updateUser(int $id, string $userName, ?string $email, int $grabs, int $role, ?string $notes, int $invites, int $movieview, int $musicview, int $gameview, int $xxxview, int $consoleview, int $bookview, string $style = 'None'): int |
||
| 289 | { |
||
| 290 | $userName = trim($userName); |
||
| 291 | |||
| 292 | $rateLimit = Role::query()->where('id', $role)->first(); |
||
| 293 | |||
| 294 | $sql = [ |
||
| 295 | 'username' => $userName, |
||
| 296 | 'grabs' => $grabs, |
||
| 297 | 'roles_id' => $role, |
||
| 298 | 'notes' => substr($notes, 0, 255), |
||
| 299 | 'invites' => $invites, |
||
| 300 | 'movieview' => $movieview, |
||
| 301 | 'musicview' => $musicview, |
||
| 302 | 'gameview' => $gameview, |
||
| 303 | 'xxxview' => $xxxview, |
||
| 304 | 'consoleview' => $consoleview, |
||
| 305 | 'bookview' => $bookview, |
||
| 306 | 'style' => $style, |
||
| 307 | 'rate_limit' => $rateLimit ? $rateLimit['rate_limit'] : 60, |
||
| 308 | ]; |
||
| 309 | |||
| 310 | if (! empty($email)) { |
||
| 311 | $email = trim($email); |
||
| 312 | $sql += ['email' => $email]; |
||
| 313 | } |
||
| 314 | |||
| 315 | $user = self::find($id); |
||
| 316 | $user->update($sql); |
||
| 317 | $user->syncRoles([$rateLimit['name']]); |
||
| 318 | |||
| 319 | return self::SUCCESS; |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @return User|Builder|Model|object|null |
||
| 324 | */ |
||
| 325 | public static function getByUsername(string $userName) |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @return Model|static |
||
| 332 | * |
||
| 333 | * @throws ModelNotFoundException |
||
| 334 | */ |
||
| 335 | public static function getByEmail(string $email) |
||
| 336 | { |
||
| 337 | return self::whereEmail($email)->first(); |
||
| 338 | } |
||
| 339 | |||
| 340 | public static function updateUserRole(int $uid, int|string $role): bool |
||
| 341 | { |
||
| 342 | if (is_int($role)) { |
||
| 343 | $roleQuery = Role::query()->where('id', $role)->first(); |
||
| 344 | } else { |
||
| 345 | $roleQuery = Role::query()->where('name', $role)->first(); |
||
| 346 | } |
||
| 347 | $roleName = $roleQuery->name; |
||
| 348 | |||
| 349 | $user = self::find($uid); |
||
| 350 | $user->syncRoles([$roleName]); |
||
| 351 | |||
| 352 | return self::find($uid)->update(['roles_id' => $roleQuery->id]); |
||
| 353 | } |
||
| 354 | |||
| 355 | public static function updateUserRoleChangeDate(int $uid, $date = '', int $addYear = 0): void |
||
| 356 | { |
||
| 357 | $user = self::find($uid); |
||
| 358 | $currRoleExp = $user->rolechangedate ?? now()->toDateTimeString(); |
||
| 359 | if (! empty($date)) { |
||
| 360 | $user->update(['rolechangedate' => $date]); |
||
| 361 | } |
||
| 362 | if (empty($date) && ! empty($addYear)) { |
||
| 363 | $user->update(['rolechangedate' => Carbon::createFromDate($currRoleExp)->addYears($addYear)]); |
||
| 364 | } |
||
| 365 | } |
||
| 366 | |||
| 367 | public static function updateExpiredRoles(): void |
||
| 368 | { |
||
| 369 | $now = CarbonImmutable::now(); |
||
| 370 | $period = [ |
||
| 371 | 'day' => $now->addDay(), |
||
| 372 | 'week' => $now->addWeek(), |
||
| 373 | 'month' => $now->addMonth(), |
||
| 374 | ]; |
||
| 375 | |||
| 376 | foreach ($period as $value) { |
||
| 377 | $users = self::query()->whereDate('rolechangedate', '=', $value)->get(); |
||
| 378 | $days = $now->diffInDays($value, true); |
||
| 379 | foreach ($users as $user) { |
||
| 380 | SendAccountWillExpireEmail::dispatch($user, $days)->onQueue('emails'); |
||
| 381 | } |
||
| 382 | } |
||
| 383 | foreach (self::query()->whereDate('rolechangedate', '<', $now)->get() as $expired) { |
||
| 384 | $expired->update(['roles_id' => self::ROLE_USER, 'rolechangedate' => null]); |
||
| 385 | $expired->syncRoles(['User']); |
||
| 386 | SendAccountExpiredEmail::dispatch($expired)->onQueue('emails'); |
||
| 387 | } |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @throws \Throwable |
||
| 392 | */ |
||
| 393 | public static function getRange($start, $offset, $orderBy, ?string $userName = '', ?string $email = '', ?string $host = '', ?string $role = '', bool $apiRequests = false, ?string $createdFrom = '', ?string $createdTo = ''): Collection |
||
| 394 | { |
||
| 395 | if ($apiRequests) { |
||
| 396 | UserRequest::clearApiRequests(false); |
||
| 397 | $query = " |
||
| 398 | SELECT users.*, roles.name AS rolename, COUNT(user_requests.id) AS apirequests |
||
| 399 | FROM users |
||
| 400 | INNER JOIN roles ON roles.id = users.roles_id |
||
| 401 | LEFT JOIN user_requests ON user_requests.users_id = users.id |
||
| 402 | WHERE users.id != 0 %s %s %s %s %s %s |
||
| 403 | AND email != '[email protected]' |
||
| 404 | GROUP BY users.id |
||
| 405 | ORDER BY %s %s %s "; |
||
| 406 | } else { |
||
| 407 | $query = ' |
||
| 408 | SELECT users.*, roles.name AS rolename |
||
| 409 | FROM users |
||
| 410 | INNER JOIN roles ON roles.id = users.roles_id |
||
| 411 | WHERE 1=1 %s %s %s %s %s %s |
||
| 412 | ORDER BY %s %s %s'; |
||
| 413 | } |
||
| 414 | $order = self::getBrowseOrder($orderBy); |
||
| 415 | |||
| 416 | return self::fromQuery( |
||
| 417 | sprintf( |
||
| 418 | $query, |
||
| 419 | ! empty($userName) ? 'AND users.username '.'LIKE '.escapeString('%'.$userName.'%') : '', |
||
| 420 | ! empty($email) ? 'AND users.email '.'LIKE '.escapeString('%'.$email.'%') : '', |
||
| 421 | ! empty($host) ? 'AND users.host '.'LIKE '.escapeString('%'.$host.'%') : '', |
||
| 422 | (! empty($role) ? ('AND users.roles_id = '.$role) : ''), |
||
| 423 | ! empty($createdFrom) ? 'AND users.created_at >= '.escapeString($createdFrom.' 00:00:00') : '', |
||
| 424 | ! empty($createdTo) ? 'AND users.created_at <= '.escapeString($createdTo.' 23:59:59') : '', |
||
| 425 | $order[0], |
||
| 426 | $order[1], |
||
| 427 | ($start === false ? '' : ('LIMIT '.$offset.' OFFSET '.$start)) |
||
| 428 | ) |
||
| 429 | ); |
||
| 430 | } |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Get sort types for sorting users on the web page user list. |
||
| 434 | * |
||
| 435 | * @return string[] |
||
| 436 | */ |
||
| 437 | public static function getBrowseOrder($orderBy): array |
||
| 438 | { |
||
| 439 | $order = (empty($orderBy) ? 'username_desc' : $orderBy); |
||
| 440 | $orderArr = explode('_', $order); |
||
| 441 | $orderField = match ($orderArr[0]) { |
||
| 442 | 'email' => 'email', |
||
| 443 | 'host' => 'host', |
||
| 444 | 'createdat' => 'created_at', |
||
| 445 | 'lastlogin' => 'lastlogin', |
||
| 446 | 'apiaccess' => 'apiaccess', |
||
| 447 | 'grabs' => 'grabs', |
||
| 448 | 'role' => 'rolename', |
||
| 449 | 'rolechangedate' => 'rolechangedate', |
||
| 450 | 'verification' => 'verified', |
||
| 451 | default => 'username', |
||
| 452 | }; |
||
| 453 | $orderSort = (isset($orderArr[1]) && preg_match('/^asc|desc$/i', $orderArr[1])) ? $orderArr[1] : 'desc'; |
||
| 454 | |||
| 455 | return [$orderField, $orderSort]; |
||
| 456 | } |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Verify a password against a hash. |
||
| 460 | * |
||
| 461 | * Automatically update the hash if it needs to be. |
||
| 462 | * |
||
| 463 | * @param string $password Password to check against hash. |
||
| 464 | * @param bool|string $hash Hash to check against password. |
||
| 465 | * @param int $userID ID of the user. |
||
| 466 | */ |
||
| 467 | public static function checkPassword(string $password, bool|string $hash, int $userID = -1): bool |
||
| 468 | { |
||
| 469 | if (Hash::check($password, $hash) === false) { |
||
| 470 | return false; |
||
| 471 | } |
||
| 472 | |||
| 473 | // Update the hash if it needs to be. |
||
| 474 | if (is_numeric($userID) && $userID > 0 && Hash::needsRehash($hash)) { |
||
| 475 | $hash = self::hashPassword($password); |
||
| 476 | |||
| 477 | if ($hash !== false) { |
||
| 478 | self::find($userID)->update(['password' => $hash]); |
||
| 479 | } |
||
| 480 | } |
||
| 481 | |||
| 482 | return true; |
||
| 483 | } |
||
| 484 | |||
| 485 | public static function updateRssKey($uid): int |
||
| 486 | { |
||
| 487 | self::find($uid)->update(['api_token' => md5(Password::getRepository()->createNewToken())]); |
||
| 488 | |||
| 489 | return self::SUCCESS; |
||
| 490 | } |
||
| 491 | |||
| 492 | public static function updatePassResetGuid($id, $guid): int |
||
| 493 | { |
||
| 494 | self::find($id)->update(['resetguid' => $guid]); |
||
| 495 | |||
| 496 | return self::SUCCESS; |
||
| 497 | } |
||
| 498 | |||
| 499 | public static function updatePassword(int $id, string $password): int |
||
| 500 | { |
||
| 501 | self::find($id)->update(['password' => self::hashPassword($password)]); |
||
| 502 | |||
| 503 | return self::SUCCESS; |
||
| 504 | } |
||
| 505 | |||
| 506 | public static function hashPassword($password): string |
||
| 507 | { |
||
| 508 | return Hash::make($password); |
||
| 509 | } |
||
| 510 | |||
| 511 | /** |
||
| 512 | * @return Model|static |
||
| 513 | * |
||
| 514 | * @throws ModelNotFoundException |
||
| 515 | */ |
||
| 516 | public static function getByPassResetGuid(string $guid) |
||
| 517 | { |
||
| 518 | return self::whereResetguid($guid)->first(); |
||
| 519 | } |
||
| 520 | |||
| 521 | public static function incrementGrabs(int $id, int $num = 1): void |
||
| 522 | { |
||
| 523 | self::find($id)->increment('grabs', $num); |
||
| 524 | } |
||
| 525 | |||
| 526 | /** |
||
| 527 | * @return Model|null|static |
||
| 528 | */ |
||
| 529 | public static function getByRssToken(string $rssToken) |
||
| 530 | { |
||
| 531 | return self::whereApiToken($rssToken)->first(); |
||
| 532 | } |
||
| 533 | |||
| 534 | public static function isValidUrl($url): bool |
||
| 535 | { |
||
| 536 | return (! preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $url)) ? false : true; |
||
| 537 | } |
||
| 538 | |||
| 539 | /** |
||
| 540 | * @throws \Exception |
||
| 541 | */ |
||
| 542 | public static function generatePassword(int $length = 15): string |
||
| 543 | { |
||
| 544 | return Str::password($length); |
||
| 545 | } |
||
| 546 | |||
| 547 | /** |
||
| 548 | * @throws \Exception |
||
| 549 | */ |
||
| 550 | public static function signUp($userName, $password, $email, $host, $notes, int $invites = Invitation::DEFAULT_INVITES, string $inviteCode = '', bool $forceInviteMode = false, int $role = self::ROLE_USER, bool $validate = true): bool|int|string |
||
| 551 | { |
||
| 552 | $user = [ |
||
| 553 | 'username' => trim($userName), |
||
| 554 | 'password' => trim($password), |
||
| 555 | 'email' => trim($email), |
||
| 556 | ]; |
||
| 557 | |||
| 558 | if ($validate) { |
||
| 559 | $validator = Validator::make($user, [ |
||
| 560 | 'username' => ['required', 'string', 'min:5', 'max:255', 'unique:users'], |
||
| 561 | 'email' => ['required', 'string', 'email', 'max:255', 'unique:users', 'indisposable'], |
||
| 562 | 'password' => ['required', 'string', 'min:8', 'confirmed', 'regex:/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$/'], |
||
| 563 | ]); |
||
| 564 | |||
| 565 | if ($validator->fails()) { |
||
| 566 | return implode('', Arr::collapse($validator->errors()->toArray())); |
||
| 567 | } |
||
| 568 | } |
||
| 569 | |||
| 570 | // Make sure this is the last check, as if a further validation check failed, the invite would still have been used up. |
||
| 571 | $invitedBy = 0; |
||
| 572 | if (! $forceInviteMode && (int) Settings::settingValue('registerstatus') === Settings::REGISTER_STATUS_INVITE) { |
||
| 573 | if ($inviteCode === '') { |
||
| 574 | return self::ERR_SIGNUP_BADINVITECODE; |
||
| 575 | } |
||
| 576 | |||
| 577 | $invitedBy = self::checkAndUseInvite($inviteCode); |
||
| 578 | if ($invitedBy < 0) { |
||
| 579 | return self::ERR_SIGNUP_BADINVITECODE; |
||
| 580 | } |
||
| 581 | } |
||
| 582 | |||
| 583 | return self::add($user['username'], $user['password'], $user['email'], $role, $notes, $host, $invites, $invitedBy); |
||
| 584 | } |
||
| 585 | |||
| 586 | /** |
||
| 587 | * If a invite is used, decrement the person who invited's invite count. |
||
| 588 | */ |
||
| 589 | public static function checkAndUseInvite(string $inviteCode): int |
||
| 590 | { |
||
| 591 | $invite = Invitation::findValidByToken($inviteCode); |
||
| 592 | if (! $invite) { |
||
| 593 | return -1; |
||
| 594 | } |
||
| 595 | |||
| 596 | self::query()->where('id', $invite->invited_by)->decrement('invites'); |
||
| 597 | $invite->markAsUsed(0); // Will be updated with actual user ID later |
||
| 598 | |||
| 599 | return $invite->invited_by; |
||
| 600 | } |
||
| 601 | |||
| 602 | /** |
||
| 603 | * @return false|int|mixed |
||
| 604 | */ |
||
| 605 | public static function add(string $userName, string $password, string $email, int $role, ?string $notes = '', string $host = '', int $invites = Invitation::DEFAULT_INVITES, int $invitedBy = 0) |
||
| 606 | { |
||
| 607 | $password = self::hashPassword($password); |
||
| 608 | if (! $password) { |
||
| 609 | return false; |
||
| 610 | } |
||
| 611 | |||
| 612 | $storeips = config('nntmux:settings.store_user_ip') === true ? $host : ''; |
||
| 613 | |||
| 614 | $user = self::create( |
||
| 615 | [ |
||
| 616 | 'username' => $userName, |
||
| 617 | 'password' => $password, |
||
| 618 | 'email' => $email, |
||
| 619 | 'host' => $storeips, |
||
| 620 | 'roles_id' => $role, |
||
| 621 | 'invites' => $invites, |
||
| 622 | 'invitedby' => (int) $invitedBy === 0 ? null : $invitedBy, |
||
| 623 | 'notes' => $notes, |
||
| 624 | ] |
||
| 625 | ); |
||
| 626 | |||
| 627 | return $user->id; |
||
| 628 | } |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Get the list of categories the user has excluded. |
||
| 632 | * |
||
| 633 | * @param int $userID ID of the user. |
||
| 634 | * |
||
| 635 | * @throws \Exception |
||
| 636 | */ |
||
| 637 | public static function getCategoryExclusionById(int $userID): array |
||
| 638 | { |
||
| 639 | $ret = []; |
||
| 640 | |||
| 641 | $user = self::find($userID); |
||
| 642 | |||
| 643 | $userAllowed = $user->getDirectPermissions()->pluck('name')->toArray(); |
||
| 644 | $roleAllowed = $user->getAllPermissions()->pluck('name')->toArray(); |
||
| 645 | |||
| 646 | $allowed = array_intersect($roleAllowed, $userAllowed); |
||
| 647 | |||
| 648 | $cats = ['view console', 'view movies', 'view audio', 'view tv', 'view pc', 'view adult', 'view books', 'view other']; |
||
| 649 | |||
| 650 | if (! empty($allowed)) { |
||
| 651 | foreach ($cats as $cat) { |
||
| 652 | if (! \in_array($cat, $allowed, false)) { |
||
| 653 | $ret[] = match ($cat) { |
||
| 654 | 'view console' => 1000, |
||
| 655 | 'view movies' => 2000, |
||
| 656 | 'view audio' => 3000, |
||
| 657 | 'view pc' => 4000, |
||
| 658 | 'view tv' => 5000, |
||
| 659 | 'view adult' => 6000, |
||
| 660 | 'view books' => 7000, |
||
| 661 | 'view other' => 1, |
||
| 662 | }; |
||
| 663 | } |
||
| 664 | } |
||
| 665 | } |
||
| 666 | |||
| 667 | return Category::query()->whereIn('root_categories_id', $ret)->pluck('id')->toArray(); |
||
| 668 | } |
||
| 669 | |||
| 670 | /** |
||
| 671 | * @throws \Exception |
||
| 672 | */ |
||
| 673 | public static function getCategoryExclusionForApi(Request $request): array |
||
| 679 | } |
||
| 680 | |||
| 681 | /** |
||
| 682 | * @throws \Exception |
||
| 683 | */ |
||
| 684 | public static function sendInvite($serverUrl, $uid, $emailTo): string |
||
| 685 | { |
||
| 686 | $user = self::find($uid); |
||
| 687 | |||
| 688 | // Create invitation using our custom system |
||
| 689 | $invitation = Invitation::createInvitation($emailTo, $user->id); |
||
| 690 | $url = $serverUrl.'/register?token='.$invitation->token; |
||
| 691 | |||
| 692 | // Send invitation email |
||
| 693 | $invitationService = app(InvitationService::class); |
||
| 694 | $invitationService->sendInvitationEmail($invitation); |
||
| 695 | |||
| 696 | return $url; |
||
| 697 | } |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Deletes users that have not verified their accounts for 3 or more days. |
||
| 701 | */ |
||
| 702 | public static function deleteUnVerified(): void |
||
| 703 | { |
||
| 704 | static::whereVerified(0)->where('created_at', '<', now()->subDays(3))->delete(); |
||
| 705 | } |
||
| 706 | |||
| 707 | public function passwordSecurity(): HasOne |
||
| 710 | } |
||
| 711 | |||
| 712 | public static function canPost($user_id): bool |
||
| 713 | { |
||
| 714 | // return true if can_post column is true and false if can_post column is false |
||
| 715 | return self::where('id', $user_id)->value('can_post'); |
||
| 716 | } |
||
| 717 | } |
||
| 718 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: