95 lines
3.0 KiB
PHP
95 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Notifications\InviteToTeam;
|
|
use Carbon\CarbonImmutable;
|
|
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\DatabaseNotification;
|
|
use Illuminate\Notifications\DatabaseNotificationCollection;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Illuminate\Notifications\Notification;
|
|
use Str;
|
|
|
|
/**
|
|
* @property int $id
|
|
* @property int $team_id
|
|
* @property int $invited_by_user_id
|
|
* @property string $invited_email
|
|
* @property string $invite_token
|
|
* @property CarbonImmutable|null $created_at
|
|
* @property CarbonImmutable|null $updated_at
|
|
* @property-read User $invitedBy
|
|
* @property-read DatabaseNotificationCollection<int, DatabaseNotification> $notifications
|
|
* @property-read int|null $notifications_count
|
|
* @property-read Team $team
|
|
*
|
|
* @method static Builder<static>|TeamInvite findByPlainToken(string $token)
|
|
* @method static Builder<static>|TeamInvite newModelQuery()
|
|
* @method static Builder<static>|TeamInvite newQuery()
|
|
* @method static Builder<static>|TeamInvite query()
|
|
* @method static Builder<static>|TeamInvite whereCreatedAt($value)
|
|
* @method static Builder<static>|TeamInvite whereId($value)
|
|
* @method static Builder<static>|TeamInvite whereInviteToken($value)
|
|
* @method static Builder<static>|TeamInvite whereInvitedByUserId($value)
|
|
* @method static Builder<static>|TeamInvite whereInvitedEmail($value)
|
|
* @method static Builder<static>|TeamInvite whereTeamId($value)
|
|
* @method static Builder<static>|TeamInvite whereUpdatedAt($value)
|
|
*
|
|
* @mixin \Eloquent
|
|
*/
|
|
#[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));
|
|
}
|
|
}
|