summaryrefslogtreecommitdiff
path: root/src/Models/AuthLog.php
blob: 597888ad468c8eb4c268185bd3bf9cf0d9126f09 (plain)
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
<?php

namespace Lightscale\LaralightAuthLog\Models;

use Lightscale\LaralightAuthLog\Enums\Status;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Prunable;
use Illuminate\Database\Eloquent\Builder;

class AuthLog extends Model
{
    use Prunable;

    const UPDATED_AT = null;

    protected $guarded = [
        'user_id'
    ];

    public function __construct(array $attributes = [])
    {
        $this->setTable(config('auth_log.table_name'));

        parent::__construct($attributes);
    }

    protected function casts(): array
    {
        return [
            'status' => Status::class,
            'properties' => 'collection',
        ];
    }

    public function user(): BelongsTo
    {
        return $this->belongsTo(config('auth_log.user_model'));
    }

    public function prunable(): Builder
    {
        $days = config('auth_log.max_age_days');
        if ($days === null) {
            return static::whereRaw('1 = 0');
        }
        else return static::where(static::CREATED_AT, '<=', now()->subDays($days));
    }
}