LoginLog.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\DB;
  5. use App\Models\DTO\SearchData;
  6. class LoginLog extends Model
  7. {
  8. protected $table = 'tb_login_log';
  9. protected $primaryKey = 'id';
  10. public $keyType = 'int';
  11. public $incrementing = true;
  12. public $timestamps = true;
  13. const CREATED_AT = 'created_at';
  14. const UPDATED_AT = null;
  15. const DELETED_AT = null;
  16. protected $guarded = [];
  17. /**
  18. * 로그인 이력 조회
  19. */
  20. public function data(SearchData $params): object
  21. {
  22. $query = $this->query();
  23. $query->select('tb_login_log.*', 'users.sid');
  24. if($params->keyword) {
  25. switch ($params->field) {
  26. case 'users.name' :
  27. case 'users.nickname' :
  28. case 'users.email' :
  29. case 'tb_login_log.ip_address' :
  30. case 'tb_login_log.created_at' :
  31. $query->where($params->field, 'LIKE', "%{$params->keyword}%");
  32. break;
  33. case 'users.id' :
  34. case 'users.sid' :
  35. $query->where($params->field, '=', $params->keyword);
  36. break;
  37. }
  38. }
  39. $query->leftJoin('users', 'users.id', '=', 'tb_login_log.user_id');
  40. $query->orderByDesc('tb_login_log.id');
  41. $list = $query->paginate($params->perPage, ['*'], 'page', $params->page);
  42. $total = $this->count();
  43. $rows = $list->count();
  44. return (object)[
  45. 'total' => $total,
  46. 'list' => $list,
  47. 'rows' => $rows
  48. ];
  49. }
  50. /**
  51. * 로그인 통계
  52. */
  53. public function getLoginSuccessCount(string $dateType, string $startDate, string $endDate, int $success): object
  54. {
  55. $left = ($dateType === 'y') ? 4 : ($dateType === 'm' ? 7 : 10);
  56. $sql = sprintf("SELECT COUNT(*) AS cnt, LEFT(created_at, %s) AS day FROM tb_login_log
  57. WHERE success = %d AND LEFT(created_at, 10) >= '%s' AND LEFT(created_at, 10) <= '%s'
  58. GROUP BY created_at", $left, $success, $startDate, $endDate);
  59. $list = DB::select($sql);
  60. $total = count($list);
  61. return (object)[
  62. 'total' => $total,
  63. 'list' => $list
  64. ];
  65. }
  66. /**
  67. * 로그인 성공 기록
  68. */
  69. public function setSucceedLog(string $email, string $reason): bool
  70. {
  71. return $this->insert([
  72. 'user_id' => UID,
  73. 'success' => 1,
  74. 'account' => $email,
  75. 'ip_address' => IP_ADDRESS,
  76. 'reason' => $reason,
  77. 'user_agent' => USER_AGENT,
  78. 'url' => FULL_URL,
  79. 'referer' => REFERER
  80. ]);
  81. }
  82. /**
  83. * 로그인 실패 기록
  84. */
  85. public function setFailedLog(string $email, string $reason): bool
  86. {
  87. return $this->insert([
  88. 'user_id' => null,
  89. 'success' => 0,
  90. 'account' => $email,
  91. 'reason' => $reason,
  92. 'ip_address' => IP_ADDRESS,
  93. 'user_agent' => USER_AGENT,
  94. 'url' => FULL_URL,
  95. 'referer' => REFERER
  96. ]);
  97. }
  98. /**
  99. * 로그인 기록 조회
  100. */
  101. public function logs(int $userID, SearchData $params): object
  102. {
  103. $query = $this->query();
  104. $query->where('user_id', $userID);
  105. if ($params->startDate || $params->endDate) {
  106. if ($params->startDate) {
  107. $query->where('created_at', '>=', $params->startDate . ' 00:00:00');
  108. }
  109. if ($params->endDate) {
  110. $query->where('created_at', '<=', $params->endDate . ' 23:59:59');
  111. }
  112. }
  113. $query->orderByDesc('id');
  114. $list = $query->paginate($params->perPage, ['*'], 'page', $params->page);
  115. $total = $list->total();
  116. $rows = $list->count();
  117. return (object)[
  118. 'total' => $total,
  119. 'rows' => $rows,
  120. 'list' => $list
  121. ];
  122. }
  123. }