ListController.php 12 KB

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