You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
5.4 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Support\Facades\Mail;
use Yadahan\AuthenticationLog\AuthenticationLogable;
use Carbon\Carbon;
use App\Libs\EncryptUser;
use App\Libs\EncryptQuery;
use App\Casts\EncryptFixed;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
use SoftDeletes;
use AuthenticationLogable;
use EncryptUser, EncryptQuery {
EncryptUser::setAttribute as setEncryptAttribute;
EncryptUser::getAttribute as getEncryptAttribute;
}
protected $table = 'user';
public function getAttribute($key)
{
if (in_array($key, $this->encryptable)) {
$value = $this->getEncryptAttribute($key);
} else {
$value = parent::getAttribute($key);
}
return $value;
}
public function setAttribute($key, $value)
{
if (in_array($key, $this->encryptable)) {
$this->setEncryptAttribute($key, $value);
} else {
parent::setAttribute($key, $value);
}
}
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'uid',
'type',
'email',
'email_verify',
'phone',
'phone_verify',
'name',
'memo',
'password',
'agree_service',
'agree_private',
'agree_marketing',
'status',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
protected $visible = [
'uid',
'type',
'email',
'email_verify',
'phone',
'phone_verify',
'name',
'memo',
'status',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email' => EncryptFixed::class,
'name' => EncryptFixed::class,
'phone' => EncryptFixed::class
];
/**
* Get the entity's last logout at.
*/
public function lastLogoutAt()
{
return optional($this->authentications()->first())->logout_at;
}
/**
* Get age from birthday.
*/
public function getBirthDayAgeAttribute()
{
$birthday = $this->getAttribute('birthday');
if (empty($birthday)) {
return '';
}
$birthday = Carbon::parse($birthday);
if ($birthday) {
return $birthday->age;
}
return '';
}
/**
* 회원 탈퇴 처리
* @param string $id
* @return array
*/
public static function userSignout($id) {
$time = Carbon::now()->format("YmdHis");
$data = self::query()->find($id);
// 탈퇴시 중복체크 피하기 위함
$data->email = $data->email .'#'. $time;
$data->phone = $data->phone .'#'. $time;
$data->status = 'SignOut';
$data->save();
// 삭제 처리
$data->delete();
return [
'success' => true,
'message' => '회원 탈퇴 완료',
'data' => $data->toArray()
];
}
/**
* 회원 복구 처리
* @param string $id
* @return array
*/
public static function userRestore($id) {
$data = self::query()->withTrashed()->find($id);
// 복구
$data->email = trimSignoutText($data->email);
$data->phone = trimSignoutText($data->phone);
$data->status = 'Registered';
// 중복검사
$count = self::query()->whereCrypt('email', $data->email)->count();
if ($count) {
return ['success' => false, 'message' => '이미 존재하는 이메일입니다.\n복구할 수 없습니다.'];
}
$count = self::query()->whereCrypt('phone', $data->phone)->count();
if ($count) {
return ['success' => false, 'message' => '이미 가입된 전화번호입니다.\n복구할 수 없습니다.'];
}
$data->save();
// 복구 처리
$data = self::query()->withTrashed()->find($id);
$data->restore();
return ['success' => true, 'message' => '복구되었습니다.'];
}
/**
* 비밀번호 재설정 이메일 전송 오버라이드
* @param $token
* @return void
*/
public function sendPasswordResetNotification($token) {
$data = [
$this->email
];
Mail::send('emails.reset-password-email', [
'expire' => config('auth.passwords.users.expire'),
'reset_url' => route('password.reset', ['token' => $token, 'email' => $this->email]),
], function($message) use($data){
$message->subject('요청하신 비밀번호 재설정 안내 메일입니다');
$message->to($data[0]);
});
}
public function getAppends()
{
if (!count($this->appends)) {
return [];
}
return $this->getArrayableItems(
array_combine($this->appends, $this->appends)
);
}
}