2020-10-25 23:50:13 +01:00
|
|
|
<?php
|
|
|
|
|
2021-12-02 13:15:53 +01:00
|
|
|
namespace App\Models;
|
2020-10-25 23:50:13 +01:00
|
|
|
|
2023-03-02 14:32:53 +01:00
|
|
|
use App\Events\GroupDeleted;
|
2021-11-30 17:34:35 +01:00
|
|
|
use App\Events\GroupDeleting;
|
2022-11-22 15:15:52 +01:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2020-10-25 23:50:13 +01:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2021-10-15 23:46:21 +02:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2020-10-25 23:50:13 +01:00
|
|
|
|
2022-09-07 17:54:27 +02:00
|
|
|
/**
|
2023-03-02 15:24:57 +01:00
|
|
|
* App\Models\Group
|
|
|
|
*
|
2022-09-07 17:54:27 +02:00
|
|
|
* @property int $twofaccounts_count
|
2023-03-02 15:24:57 +01:00
|
|
|
* @property int $id
|
|
|
|
* @property string $name
|
|
|
|
* @property \Illuminate\Support\Carbon|null $created_at
|
|
|
|
* @property \Illuminate\Support\Carbon|null $updated_at
|
|
|
|
* @property int|null $user_id
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\TwoFAccount[] $twofaccounts
|
|
|
|
* @property-read \App\Models\User|null $user
|
2022-09-07 17:54:27 +02:00
|
|
|
*/
|
2020-10-25 23:50:13 +01:00
|
|
|
class Group extends Model
|
|
|
|
{
|
2021-12-02 13:15:53 +01:00
|
|
|
use HasFactory;
|
|
|
|
|
2020-10-25 23:50:13 +01:00
|
|
|
/**
|
|
|
|
* model's array form.
|
|
|
|
*
|
2022-08-26 15:57:18 +02:00
|
|
|
* @var string[]
|
2020-10-25 23:50:13 +01:00
|
|
|
*/
|
|
|
|
protected $fillable = ['name'];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The accessors to append to the model's array form.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2021-09-21 22:43:39 +02:00
|
|
|
protected $appends = [];
|
2020-10-25 23:50:13 +01:00
|
|
|
|
2020-11-21 21:50:33 +01:00
|
|
|
/**
|
|
|
|
* The attributes that should be hidden for arrays.
|
|
|
|
*
|
2022-11-17 16:46:29 +01:00
|
|
|
* @var array<int, string>
|
2020-11-21 21:50:33 +01:00
|
|
|
*/
|
|
|
|
protected $hidden = ['created_at', 'updated_at'];
|
|
|
|
|
2020-11-23 12:54:06 +01:00
|
|
|
/**
|
|
|
|
* The attributes that should be cast.
|
|
|
|
*
|
2022-11-17 16:46:29 +01:00
|
|
|
* @var array<string, string>
|
2020-11-23 12:54:06 +01:00
|
|
|
*/
|
|
|
|
protected $casts = [
|
|
|
|
'twofaccounts_count' => 'integer',
|
|
|
|
];
|
|
|
|
|
2021-11-30 17:34:35 +01:00
|
|
|
/**
|
|
|
|
* The event map for the model.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $dispatchesEvents = [
|
|
|
|
'deleting' => GroupDeleting::class,
|
2023-03-02 14:32:53 +01:00
|
|
|
'deleted' => GroupDeleted::class,
|
2021-11-30 17:34:35 +01:00
|
|
|
];
|
|
|
|
|
2021-10-03 21:49:48 +02:00
|
|
|
/**
|
|
|
|
* Override The "booting" method of the model
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected static function boot()
|
|
|
|
{
|
|
|
|
parent::boot();
|
2021-10-15 23:46:21 +02:00
|
|
|
|
2023-03-02 14:32:53 +01:00
|
|
|
static::created(function (object $model) {
|
2023-03-07 15:17:07 +01:00
|
|
|
Log::info(sprintf('Group %s (id #%d) created for user ID #%s', var_export($model->name, true), $model->id, $model->user_id));
|
2023-03-02 14:32:53 +01:00
|
|
|
});
|
|
|
|
static::updated(function (object $model) {
|
2023-03-07 15:17:07 +01:00
|
|
|
Log::info(sprintf('Group %s (id #%d) updated by user ID #%s', var_export($model->name, true), $model->id, $model->user_id));
|
2023-03-02 14:32:53 +01:00
|
|
|
});
|
2022-08-26 15:57:18 +02:00
|
|
|
static::deleted(function (object $model) {
|
2023-03-02 14:32:53 +01:00
|
|
|
Log::info(sprintf('Group %s (id #%d) deleted ', var_export($model->name, true), $model->id));
|
2021-10-15 23:46:21 +02:00
|
|
|
});
|
2021-10-03 21:49:48 +02:00
|
|
|
}
|
|
|
|
|
2020-10-25 23:50:13 +01:00
|
|
|
/**
|
|
|
|
* Get the TwoFAccounts of the group.
|
2022-11-22 15:15:52 +01:00
|
|
|
*
|
2022-11-21 11:16:43 +01:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany<TwoFAccount>
|
2020-10-25 23:50:13 +01:00
|
|
|
*/
|
|
|
|
public function twofaccounts()
|
|
|
|
{
|
2022-08-26 15:57:18 +02:00
|
|
|
return $this->hasMany(\App\Models\TwoFAccount::class);
|
2020-10-25 23:50:13 +01:00
|
|
|
}
|
2023-02-27 00:32:49 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the user that owns the group.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\App\Models\User, \App\Models\Group>
|
|
|
|
*/
|
|
|
|
public function user()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(\App\Models\User::class);
|
|
|
|
}
|
2023-04-19 08:38:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Scope a query to only include orphan (userless) groups.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder<User> $query
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder<User>
|
|
|
|
*/
|
|
|
|
public function scopeOrphans($query)
|
|
|
|
{
|
|
|
|
return $query->where('user_id', null);
|
|
|
|
}
|
2020-10-25 23:50:13 +01:00
|
|
|
}
|