ListController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. namespace App\Http\Controllers\Admin\User;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Validation\Rule;
  5. use App\Http\Controllers\Controller;
  6. use App\Models\User;
  7. use App\Models\Config;
  8. use App\Models\UserRegister;
  9. use App\Models\DTO\SearchData;
  10. use App\Rules\NumberLength;
  11. use App\Rules\SpecialCharLength;
  12. use App\Rules\UppercaseLength;
  13. use App\Rules\DeniedEmail;
  14. class ListController extends Controller
  15. {
  16. private User $userModel;
  17. private UserRegister $userRegisterModel;
  18. public function __construct(
  19. User $user,
  20. UserRegister $userRegister
  21. ) {
  22. $this->userModel = $user;
  23. $this->userRegisterModel = $userRegister;
  24. }
  25. /**
  26. * 회원 관리
  27. * @method GET
  28. * @see /admin/user/list
  29. */
  30. public function index(Request $request)
  31. {
  32. $params = SearchData::fromRequest($request);
  33. $params->activated = $request->get('activated');
  34. $params->isAdmin = $request->get('is_admin');
  35. $params->isDenied = $request->get('is_denied');
  36. $params->isWithdraw = $request->get('is_withdraw');
  37. $userData = $this->userModel->data($params);
  38. if ($userData->rows > 0) {
  39. $num = listNum($userData->total, $params->page, $params->perPage);
  40. foreach ($userData->list as $i => $row) {
  41. $row->num = $num--;
  42. $row->lastLoginAt = dateBr($row->last_login_at, '-');
  43. $row->deletedAt = dateBr($row->deleted_at, '-');
  44. $row->createdAt = dateBr($row->created_at);
  45. $row->editURL = route('admin.user.list.edit', $row->id);
  46. $userData->list[$i] = $row;
  47. }
  48. }
  49. return view('admin.user.list.index', [
  50. 'userData' => $userData,
  51. 'params' => $params
  52. ]);
  53. }
  54. /**
  55. * 회원 등록
  56. * @method GET
  57. * @see /admin/user/create
  58. */
  59. public function create()
  60. {
  61. return view('admin.user.list.write', [
  62. 'actionURL' => route('admin.user.list.store'),
  63. 'userData' => [],
  64. 'uid' => null
  65. ]);
  66. }
  67. /**
  68. * 회원 수정
  69. * @method GET
  70. * @see /admin/user/{pk}/edit
  71. */
  72. public function edit(int $uid)
  73. {
  74. return view('admin.user.list.write', [
  75. 'actionURL' => route('admin.user.list.update', $uid),
  76. 'userData' => $this->userModel->find($uid),
  77. 'uid' => $uid
  78. ]);
  79. }
  80. /**
  81. * 회원 등록 저장
  82. * @method POST
  83. * @see /admin/user/list
  84. */
  85. public function store(Request $request, Config $config)
  86. {
  87. // 비밀번호 유효성 검사 규칙 지정
  88. $passwordRule = ['required', 'confirmed'];
  89. $passwordMinLength = $config->item('password_min_length');
  90. // 비밀번호 최소 길이
  91. if($passwordMinLength > 0) {
  92. $passwordRule[] = 'min:' . $passwordMinLength;
  93. $passwordRule[] = new UppercaseLength;
  94. $passwordRule[] = new NumberLength;
  95. $passwordRule[] = new SpecialCharLength;
  96. }
  97. $rules = [
  98. 'email' => ['required', 'email', 'unique:users,email', new DeniedEmail],
  99. 'name' => 'required|string|min:2|max:20',
  100. 'nickname' => 'required|string|min:2|max:20|unique:users,nickname',
  101. 'password' => $passwordRule,
  102. 'thumb_img' => 'nullable|mimes:jpg,jpeg,gif,png|max:3192',
  103. 'about_me' => 'string|nullable|max:500',
  104. 'receive_email' => 'nullable|numeric|in:0,1',
  105. 'is_denied' => 'nullable|numeric|in:0,1',
  106. 'is_withdraw' => 'nullable|numeric|in:0,1',
  107. 'is_admin' => 'nullable|numeric|in:0,1',
  108. 'is_open_profile' => 'nullable|numeric|in:0,1'
  109. ];
  110. $attributes = [
  111. 'email' => '이메일',
  112. 'name' => '이름',
  113. 'nickname' => '닉네임',
  114. 'password' => '비밀번호',
  115. 'thumb_img' => '프로필 이미지',
  116. 'about_me' => '자기소개',
  117. 'receive_email' => '이메일 수신 여부',
  118. 'is_denied' => '차단 여부',
  119. 'is_withdraw' => '탈퇴 여부',
  120. 'is_admin' => '관리자 여부',
  121. 'is_open_profile' => '정보 공개 여부'
  122. ];
  123. $posts = $this->validate($request, $rules, [], $attributes);
  124. [$sid] = explode('@', $posts['email']);
  125. $saveData = [
  126. 'sid' => $sid,
  127. 'name' => $posts['name'],
  128. 'nickname' => $posts['nickname'],
  129. 'email' => $posts['email'],
  130. 'email_verified_at' => now(),
  131. 'password' => bcrypt($posts['password']),
  132. 'thumb' => ($posts['thumb_img'] ?? 0),
  133. 'about_me' => $posts['about_me'],
  134. 'remember_token' => null,
  135. 'is_email_cert' => 1,
  136. 'is_denied' => ($posts['is_denied'] ?? 0),
  137. 'is_withdraw' => ($posts['is_withdraw'] ?? 0),
  138. 'is_admin' => ($posts['is_admin'] ?? 0),
  139. 'is_open_profile' => ($posts['is_open_profile'] ?? 0),
  140. 'receive_email' => ($posts['receive_email'] ?? 0),
  141. 'register_ip' => IP_ADDRESS,
  142. 'last_login_ip' => null,
  143. 'last_login_at' => null,
  144. 'password_updated_at' => now(),
  145. 'deleted_at' => null,
  146. 'created_at' => now(),
  147. 'updated_at' => null,
  148. ];
  149. // 파일 저장
  150. if($request->hasFile('thumb')) {
  151. $thumb = $request->file('thumb');
  152. $thumb->store(UPLOAD_PATH_PUBLIC . DIRECTORY_SEPARATOR . UPLOAD_PATH_USER_THUMB);
  153. $saveData['thumb'] = (UPLOAD_PATH_STORAGE . DIRECTORY_SEPARATOR . UPLOAD_PATH_USER_THUMB . DIRECTORY_SEPARATOR . $thumb->hashName());
  154. }
  155. // 파일 삭제
  156. if($request->get('thumb_del')) {
  157. $thumbPath = $request->get('thumb_url');
  158. if(file_exists($thumbPath)) {
  159. unlink($thumbPath);
  160. }
  161. $saveData['thumb'] = null;
  162. }
  163. $uid = $this->userModel->insertGetId($saveData);
  164. $this->userRegisterModel->insert([
  165. 'user_id' => $uid,
  166. 'device' => DEVICE_TYPE,
  167. 'language' => null,
  168. 'browser' => BROWSER,
  169. 'platform' => PLATFORM,
  170. 'robot' => null,
  171. 'ip_address' => IP_ADDRESS,
  172. 'user_agent' => USER_AGENT,
  173. 'referer' => REFERER
  174. ]);
  175. $message = '회원이 등록되었습니다.';
  176. return redirect()->route('admin.user.list.index')->with('message', $message);
  177. }
  178. /**
  179. * 회원 수정 저장
  180. * @method PUT
  181. * @see /admin/user/list/{pk}
  182. */
  183. public function update(int $uid, Request $request, Config $config)
  184. {
  185. // 비밀번호 유효성 검사 규칙 지정
  186. $passwordRule = ['nullable', 'confirmed'];
  187. $passwordMinLength = $config->item('password_min_length');
  188. // 비밀번호 최소 길이
  189. if($passwordMinLength > 0) {
  190. $passwordRule[] = 'min:' . $passwordMinLength;
  191. $passwordRule[] = new UppercaseLength;
  192. $passwordRule[] = new NumberLength;
  193. $passwordRule[] = new SpecialCharLength;
  194. }
  195. $rules = [
  196. 'uid' => 'required|numeric|exists:users,id',
  197. 'email' => [
  198. 'required',
  199. 'email',
  200. Rule::unique('users', 'email')->ignore($uid, 'id'),
  201. new DeniedEmail
  202. ],
  203. 'name' => 'required|string|min:2|max:20',
  204. 'nickname' => 'required|string|min:2|max:20|' . Rule::unique('users', 'nickname')->ignore($uid, 'id'),
  205. 'password' => $passwordRule,
  206. 'thumb_img' => 'nullable|mimes:jpg,jpeg,gif,png|max:3192',
  207. 'about_me' => 'string|nullable|max:500',
  208. 'receive_email' => 'nullable|numeric|in:0,1',
  209. 'is_denied' => 'nullable|numeric|in:0,1',
  210. 'is_withdraw' => 'nullable|numeric|in:0,1',
  211. 'is_admin' => 'nullable|numeric|in:0,1',
  212. 'is_open_profile' => 'nullable|numeric|in:0,1'
  213. ];
  214. $attributes = [
  215. 'uid' => '회원 ID',
  216. 'email' => '이메일',
  217. 'name' => '이름',
  218. 'nickname' => '닉네임',
  219. 'password' => '비밀번호',
  220. 'thumb_img' => '프로필 이미지',
  221. 'about_me' => '자기소개',
  222. 'receive_email' => '이메일 수신 여부',
  223. 'is_denied' => '차단 여부',
  224. 'is_withdraw' => '탈퇴 여부',
  225. 'is_admin' => '관리자 여부',
  226. 'is_open_profile' => '정보 공개 여부'
  227. ];
  228. $posts = $this->validate($request, $rules, [], $attributes);
  229. $user = $this->userModel->find($uid);
  230. $saveData = [
  231. 'name' => $posts['name'],
  232. 'nickname' => $posts['nickname'],
  233. 'email' => $posts['email'],
  234. 'password' => bcrypt($posts['password']),
  235. 'thumb' => ($posts['thumb_img'] ?? 0),
  236. 'about_me' => $posts['about_me'],
  237. 'receive_email' => ($posts['receive_email'] ?? 0),
  238. 'is_denied' => ($posts['is_denied'] ?? 0),
  239. 'is_withdraw' => ($posts['is_withdraw'] ?? 0),
  240. 'is_admin' => ($posts['is_admin'] ?? 0),
  241. 'is_open_profile' => ($posts['is_open_profile'] ?? 0),
  242. 'deleted_at' => null,
  243. 'updated_at' => now()
  244. ];
  245. if($user->password != $saveData['password']) {
  246. $saveData['password_updated_at'] = now();
  247. }
  248. // 파일 저장
  249. if($request->hasFile('thumb')) {
  250. $thumb = $request->file('thumb');
  251. $thumb->store(UPLOAD_PATH_PUBLIC . DIRECTORY_SEPARATOR . UPLOAD_PATH_USER_THUMB);
  252. $saveData['thumb'] = (UPLOAD_PATH_STORAGE . DIRECTORY_SEPARATOR . UPLOAD_PATH_USER_THUMB . DIRECTORY_SEPARATOR . $thumb->hashName());
  253. }
  254. // 파일 삭제
  255. if($request->get('thumb_del')) {
  256. $thumbPath = $request->get('thumb_url');
  257. if(file_exists($thumbPath)) {
  258. unlink($thumbPath);
  259. }
  260. $saveData['thumb'] = null;
  261. }
  262. $this->userModel->updater($uid, $saveData);
  263. $message = '회원 정보가 수정되었습니다.';
  264. return redirect()->route('admin.user.list.edit', $uid)->with('message', $message);
  265. }
  266. /**
  267. * 회원 삭제
  268. * @method DELETE
  269. * @see /admin/user/list/destroy
  270. */
  271. public function destroy(Request $request)
  272. {
  273. $chk = $request->post('chk');
  274. if ($chk) {
  275. foreach ($chk as $uid) {
  276. $user = $this->userModel->findOrNew($uid);
  277. if($user->exists) {
  278. // 프로필 이미지 삭제
  279. if(file_exists($user->thumb)) {
  280. unlink($user->thumb);
  281. }
  282. $user->delete();
  283. }
  284. }
  285. }
  286. $message = '회원 정보가 삭제되었습니다.';
  287. return redirect()->route('admin.user.list.index')->with('message', $message);
  288. }
  289. /**
  290. * 회원 탈퇴
  291. * @method POST
  292. * @see /admin/user/list/withdraw
  293. */
  294. public function withdraw(Request $request)
  295. {
  296. $chk = $request->post('chk');
  297. if ($chk) {
  298. foreach ($chk as $uid) {
  299. $this->userModel->find($uid)->update([
  300. 'is_withdraw' => 1,
  301. 'deleted_at' => now()
  302. ]);
  303. }
  304. }
  305. $message = '선택 회원이 탈퇴 처리되었습니다.';
  306. return redirect()->route('admin.user.list.index')->with('message', $message);
  307. }
  308. }