106 lines
2.5 KiB
PHP
Raw Normal View History

2022-12-05 23:01:12 +08:00
<?php
namespace Modules\User\Models;
use Catch\Base\CatchModel as Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Laravel\Sanctum\HasApiTokens;
2022-12-10 18:29:42 +08:00
use Modules\User\Models\Traits\UserRelations;
2022-12-05 23:01:12 +08:00
use Illuminate\Auth\Authenticatable;
/**
* @property int $id
* @property string $username
* @property string $email
* @property string $avatar
* @property string $password
* @property int $creator_id
* @property int $status
* @property string $login_ip
* @property int $login_at
* @property int $created_at
* @property int $updated_at
* @property string $remember_token
*/
class User extends Model implements AuthenticatableContract
2022-12-05 23:01:12 +08:00
{
use Authenticatable, UserRelations, HasApiTokens;
2022-12-05 23:01:12 +08:00
protected $fillable = [
2022-12-14 19:25:52 +08:00
'id', 'username', 'email', 'avatar', 'password', 'remember_token', 'creator_id', 'status', 'department_id', 'login_ip', 'login_at', 'created_at', 'updated_at', 'deleted_at'
2022-12-05 23:01:12 +08:00
];
/**
* @var array|string[]
*/
public array $searchable = [
'username' => 'like',
'email' => 'like',
2023-05-25 07:53:35 +08:00
'status' => '=',
2022-12-05 23:01:12 +08:00
];
/**
* @var string
*/
protected $table = 'users';
2022-12-14 19:25:52 +08:00
protected array $fields = ['id', 'username', 'email', 'avatar', 'creator_id', 'status', 'department_id', 'created_at'];
/**
* @var array|string[]
*/
protected array $form = ['username', 'email', 'password', 'department_id'];
2022-12-05 23:01:12 +08:00
/**
* @var array|string[]
*/
2022-12-14 19:25:52 +08:00
protected array $formRelations = ['roles', 'jobs'];
2022-12-05 23:01:12 +08:00
/**
* password
*
* @return Attribute
*/
protected function password(): Attribute
{
return new Attribute(
// get: fn($value) => '',
set: fn ($value) => bcrypt($value),
);
}
2023-04-06 21:33:36 +08:00
protected function DepartmentId(): Attribute
{
return new Attribute(
2023-05-10 16:51:25 +08:00
get: fn($value) => $value ? : null,
set: fn($value) => $value ? : 0
2023-04-06 21:33:36 +08:00
);
}
2022-12-06 19:27:38 +08:00
/**
* is super admin
*
* @return bool
*/
public function isSuperAdmin(): bool
{
return $this->{$this->primaryKey} == config('catch.super_admin');
}
2022-12-05 23:01:12 +08:00
/**
* update
* @param $id
* @param array $data
* @return mixed
*/
public function updateBy($id, array $data): mixed
{
if (isset($data['password']) && ! $data['password']) {
unset($data['password']);
}
2023-04-06 21:33:36 +08:00
2023-04-06 20:36:54 +08:00
return parent::updateBy($id, $data);
2022-12-05 23:01:12 +08:00
}
}