65 lines
1.7 KiB
PHP
65 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Notifications\InviteToTeam;
|
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
|
use Illuminate\Database\Eloquent\Attributes\Scope;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Illuminate\Notifications\Notification;
|
|
use Str;
|
|
|
|
#[Fillable(['team_id', 'invited_by_user_id', 'invited_email'])]
|
|
class TeamInvite extends Model
|
|
{
|
|
use Notifiable;
|
|
|
|
protected ?string $plainInviteToken = null;
|
|
|
|
protected static function booted(): void
|
|
{
|
|
self::creating(function (self $model) {
|
|
$model->plainInviteToken = Str::random(128);
|
|
$hashedToken = hash('sha256', $model->plainInviteToken);
|
|
$model->invite_token = $hashedToken;
|
|
});
|
|
self::created(function (self $model) {
|
|
$model->notify(new InviteToTeam);
|
|
});
|
|
}
|
|
|
|
public function team(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Team::class);
|
|
}
|
|
|
|
public function invitedBy(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'invited_by_user_id');
|
|
}
|
|
|
|
public function plainInviteToken(): ?string
|
|
{
|
|
return $this->plainInviteToken;
|
|
}
|
|
|
|
/**
|
|
* Route notifications for the mail channel.
|
|
*
|
|
* @return array<string, string>|string
|
|
*/
|
|
public function routeNotificationForMail(Notification $notification): array|string
|
|
{
|
|
return $this->invited_email;
|
|
}
|
|
|
|
#[Scope]
|
|
protected function findByPlainToken(Builder $query, string $token): Builder
|
|
{
|
|
return $query->where('invite_token', hash('sha256', $token));
|
|
}
|
|
}
|