| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424 |
- <?php
- namespace App\Models;
- use Illuminate\Contracts\Auth\MustVerifyEmail;
- 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\Auth;
- use Illuminate\Support\Facades\Hash;
- use Illuminate\Support\Facades\DB;
- use App\Http\Traits\AgentTrait;
- use App\Models\DTO\SearchData;
- use App\Notifications\VerifyNotify;
- class User extends Authenticatable implements MustVerifyEmail
- {
- use HasApiTokens, HasFactory, Notifiable, AgentTrait;
- protected $table = 'users';
- protected $primaryKey = 'id';
- const CREATED_AT = 'created_at';
- const UPDATED_AT = 'updated_at';
- const DELETED_AT = 'deleted_at';
- /**
- * The attributes that are mass assignable.
- *
- * @var array<int, string>
- */
- protected $fillable = [
- 'user_group_id',
- 'user_grade_id',
- 'sid',
- 'email',
- 'name',
- 'nickname',
- 'password',
- 'today_message',
- 'group',
- 'grade',
- 'phone',
- 'birthday',
- 'gender',
- 'icon',
- 'thumb',
- 'receive_email',
- 'receive_sms',
- 'receive_note',
- 'register_ip',
- 'last_login_at',
- 'last_login_ip',
- 'is_open_profile',
- 'is_email_cert',
- 'is_auth_cert',
- 'is_adult_cert',
- 'is_denied',
- 'is_admin',
- 'is_withdraw',
- 'about_me',
- 'email_verified_at'
- ];
- /**
- * The attributes that should be hidden for serialization.
- *
- * @var array<int, string>
- */
- protected $hidden = [
- 'password',
- 'remember_token',
- ];
- /**
- * The attributes that should be cast.
- *
- * @var array<string, string>
- */
- protected $casts = [
- 'last_login_at' => 'datetime', // 마지막 로그인 일시
- 'email_verified_at' => 'datetime', // 이메일 인증일시
- 'auth_certified_at' => 'datetime', // 본인 인증일시
- 'password_updated_at' => 'datetime' // 비밀번호 변경일시
- ];
- /**
- * The attributes that should be mutated to dates.
- *
- * @var array
- */
- protected $dates = [
- 'last_login_at',
- 'created_at',
- 'updated_at',
- 'deleted_at',
- 'email_verified_at',
- 'auth_certified_at',
- 'password_updated_at'
- ];
- /**
- * 회원 비밀번호 조회
- */
- public function getAuthPassword()
- {
- return $this->password;
- }
- /**
- * 회원 정보 모두 조회
- */
- public function info()
- {
- return Auth::user();
- }
- /**
- * 회원 ID로 조회
- */
- public function findByUserID(string $sid): User
- {
- $query = $this->query();
- $query->where('sid', $sid);
- return $query->firstOrNew();
- }
- /**
- * 회원 Email로 조회
- */
- public function findByEmail(string $sid): User
- {
- $query = $this->query();
- $query->where('email', $sid);
- return $query->firstOrNew();
- }
- /**
- * 최고 관리자 조회
- */
- public function getMaster(): User
- {
- return $this->where('is_admin', 1)->findOrNew(config('master_key'));
- }
- /**
- * 운영자 조회
- */
- public function getAdmins()
- {
- return $this->where('is_admin', 1)->get();
- }
- /**
- * 회원 ID Unique 생성
- */
- public function getHashKey()
- {
- return md5(bin2hex($this->getKey()));
- }
- /**
- * 인증 이메일 반환
- */
- public function getEmailForPasswordReset(): string
- {
- return $this->email;
- }
- /**
- * 자동 로그인 토큰 이름
- */
- public function getRememberTokenName(): string
- {
- return 'remember_token';
- }
- /**
- * 자동 로그인 토큰 조회
- */
- public function getRememberToken(): ?string
- {
- return $this->remember_token;
- }
- /**
- * 인증받은 이메일 조회
- */
- public function getEmailForVerification(): string
- {
- return $this->email;
- }
- // /**
- // * 이메일 인증 알림 메시지 발신자 정보 지정
- // */
- // public function routeNotificationForMail($notification)
- // {
- // return [$this->email => $this->username];
- // }
- /**
- * 이메일 인증을 받았는지 확인
- */
- public function hasVerifiedEmail(): bool
- {
- return (config('use_register_email_auth') ? !is_null($this->email_verified_at) : true);
- }
- /**
- * 관리자 여부 확인
- */
- public function isAdministrator(): bool
- {
- return boolval($this->is_admin);
- }
- /**
- * 이메일 인증 완료 후 값 변경
- */
- public function markEmailAsVerified(): bool
- {
- return $this->forceFill([
- 'is_email_cert' => 1,
- 'email_verified_at' => $this->freshTimestamp()
- ])->save();
- }
- /**
- * 회원 조회
- */
- public function data(SearchData $params): object
- {
- $query = $this->query();
- $query->select(
- 'users.*'
- );
- if ($params->keyword) {
- switch ($params->field) {
- case 'users.id':
- case 'users.sid':
- $query->where($params->field, '=', $params->keyword);
- break;
- case 'users.name':
- case 'users.email':
- $query->where($params->field, 'LIKE', "%{$params->keyword}%");
- break;
- case 'users.created_at':
- case 'users.last_login_at':
- $query->whereDate($params->field, $params->keyword);
- break;
- }
- }
- if($params->receiveEmail) {
- $query->where('users.receive_email', 1);
- }
- if($params->receiveSms) {
- $query->where('users.receive_sms', 1);
- }
- if($params->receiveNote) {
- $query->where('users.receive_note',1);
- }
- if ($params->isAdmin) {
- $query->where('users.is_admin', '=', $params->isAdmin);
- }
- if ($params->isDenied) {
- $query->where('users.is_denied', '=', $params->isDenied);
- }
- if ($params->isWithdraw) {
- $query->where('users.is_withdraw', '=', $params->isWithdraw);
- }
- $query->orderByDesc('users.id');
- $list = $query->paginate($params->perPage, ['*'], 'page', $params->page);
- $total = $this->count();
- $rows = $list->count();
- return (object)[
- 'total' => $total,
- 'rows' => $rows,
- 'list' => $list
- ];
- }
- /**
- * 휴면회원 조회
- */
- public function dormantUsers(SearchData $params): object
- {
- $query = $this->query();
- $query->select(
- 'users.*'
- );
- if ($params->keyword) {
- switch ($params->field) {
- case 'users.id':
- case 'users.sid':
- $query->where($params->field, '=', $params->keyword);
- break;
- case 'users.name':
- case 'users.email':
- $query->where($params->field, 'LIKE', "%{$params->keyword}%");
- break;
- case 'users.created_at':
- case 'users.last_login_at':
- $query->whereDate($params->field, $params->keyword);
- break;
- }
- }
- $query->whereNotBetween('last_login_at', [
- now()->subYears(1)->format('Y-m-d 00:00:00'),
- now()->format('Y-m-d 23:59:59')
- ]);
- $query->orderByDesc('users.created_at');
- $list = $query->paginate($params->perPage, ['*'], 'page', $params->page);
- $total = $this->count();
- $rows = $list->count();
- return (object)[
- 'total' => $total,
- 'rows' => $rows,
- 'list' => $list
- ];
- }
- /**
- * 회원가입
- */
- public function register(array $data): mixed
- {
- return DB::transaction(function () use ($data)
- {
- $sid = current(explode('@', $data['email']));
- $ipAddress = request()->getClientIp();
- $userAgent = request()->headers->get('user-agent');
- $referer = request()->headers->get('referer');
- $device = $this->device($userAgent);
- $browser = $this->browser($userAgent);
- $platform = $this->platform($userAgent);
- $robot = $this->robot($userAgent);
- $language = $this->languages();
- $user = User::create([
- 'sid' => $sid,
- 'email' => $data['email'],
- 'name' => $data['nickname'],
- 'nickname' => $data['nickname'],
- 'password' => Hash::make($data['password']),
- 'register_ip' => $ipAddress,
- 'updated_at' => null
- ]);
- UserRegister::create([
- 'user_id' => $user->id,
- 'device' => $device,
- 'language' => $language,
- 'browser' => $browser,
- 'platform' => $platform,
- 'robot' => $robot,
- 'ip_address' => $ipAddress,
- 'user_agent' => $userAgent,
- 'referer' => $referer
- ]);
- return $user;
- });
- }
- /**
- * 회원 정보 수정
- */
- public function updater(int $uid, array $data): bool
- {
- return $this->find($uid)->update($data);
- }
- /**
- * 중복 이메일 확인
- */
- public function sendVerifyCodeToMail(): void
- {
- $this->notify(new VerifyNotify);
- }
- /**
- * 이메일 인증 정보 변경
- */
- public function generateEmailVerified(string $email): void
- {
- [$sid] = explode('@', $email);
- $this->sid = $sid;
- $this->email = $email;
- $this->email_verified_at = now();
- $this->is_email_cert = 1;
- $this->save();
- $this->sendVerifyCodeToMail();
- }
- /**
- * 회원 포인트(P)를 최신 상태로 변경
- */
- public function updateTotalPoint(int $userID): bool
- {
- $sql = "
- UPDATE users USR
- SET USR.point = (SELECT COALESCE(SUM(P.point), 0) FROM tb_point P WHERE P.user_id = USR.id)
- WHERE USR.id = ?;
- ";
- return DB::statement($sql, [$userID]);
- }
- }
|