/app/Http/Controllers/UserController.php

https://github.com/kutaloweb/spala · PHP · 379 lines · 184 code · 63 blank · 132 comment · 4 complexity · 8b4c0e3702843145823749ecea14f47f MD5 · raw file

  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\User;
  4. use Illuminate\Support\Facades\Auth;
  5. use Illuminate\Support\Facades\File;
  6. use Illuminate\Auth\Access\AuthorizationException;
  7. use Illuminate\Contracts\Pagination\LengthAwarePaginator;
  8. use Illuminate\Http\JsonResponse;
  9. use Illuminate\Http\Request;
  10. use App\Repositories\RoleRepository;
  11. use App\Repositories\UserRepository;
  12. use App\Http\Requests\RegisterRequest;
  13. use App\Http\Requests\UserProfileRequest;
  14. use App\Repositories\ActivityLogRepository;
  15. use App\Http\Requests\ChangePasswordRequest;
  16. use Illuminate\Validation\ValidationException;
  17. class UserController extends Controller
  18. {
  19. /**
  20. * @var Request
  21. */
  22. protected $request;
  23. /**
  24. * @var UserRepository
  25. */
  26. protected $repo;
  27. /**
  28. * @var ActivityLogRepository
  29. */
  30. protected $activity;
  31. /**
  32. * @var RoleRepository
  33. */
  34. protected $role;
  35. /**
  36. * @var string
  37. */
  38. protected $module = 'user';
  39. /**
  40. * Instantiate a new controller instance
  41. *
  42. * @param Request $request
  43. * @param UserRepository $repo
  44. * @param ActivityLogRepository $activity
  45. * @param RoleRepository $role
  46. */
  47. public function __construct(Request $request, UserRepository $repo, ActivityLogRepository $activity, RoleRepository $role)
  48. {
  49. $this->request = $request;
  50. $this->repo = $repo;
  51. $this->activity = $activity;
  52. $this->role = $role;
  53. }
  54. /**
  55. * Get pre-requisites for user module
  56. *
  57. * @return JsonResponse
  58. * @throws AuthorizationException
  59. */
  60. public function preRequisite()
  61. {
  62. $this->authorize('create', User::class);
  63. $countries = generateSelect(config('country'));
  64. $genders = generateSelectVueTranslated(config('list.gender'));
  65. $roles = generateSelectVue($this->role->listExceptName([config('system.default_role.admin')]));
  66. return $this->success(compact('countries', 'roles', 'genders'));
  67. }
  68. /**
  69. * Get all users
  70. *
  71. * @return LengthAwarePaginator
  72. * @throws AuthorizationException
  73. */
  74. public function index()
  75. {
  76. $this->authorize('view', User::class);
  77. return $this->repo->paginate($this->request->all());
  78. }
  79. /**
  80. * Store user
  81. *
  82. * @param RegisterRequest $request
  83. *
  84. * @return JsonResponse
  85. * @throws AuthorizationException
  86. */
  87. public function store(RegisterRequest $request)
  88. {
  89. $this->authorize('create', User::class);
  90. $user = $this->repo->create($this->request->all());
  91. $this->activity->record([
  92. 'module' => $this->module,
  93. 'module_id' => $user->id,
  94. 'activity' => 'created'
  95. ]);
  96. return $this->success(['message' => trans('user.added')]);
  97. }
  98. /**
  99. * Get current user.
  100. *
  101. * @return User
  102. * @throws ValidationException
  103. */
  104. public function detail()
  105. {
  106. return $this->repo->findOrFail(Auth::user()->id);
  107. }
  108. /**
  109. * Get user details
  110. *
  111. * @param int $id
  112. *
  113. * @return JsonResponse
  114. * @throws AuthorizationException
  115. * @throws ValidationException
  116. */
  117. public function show($id)
  118. {
  119. $this->authorize('view', User::class);
  120. $user = $this->repo->findOrFail($id);
  121. $selected_roles = generateSelectVue($user->roles()->pluck('name', 'id')->all());
  122. $roles = $user->roles()->pluck('id')->all();
  123. return $this->success(compact('user', 'selected_roles', 'roles'));
  124. }
  125. /**
  126. * Update user
  127. *
  128. * @param UserProfileRequest $request
  129. * @param int $id
  130. *
  131. * @return JsonResponse
  132. * @throws AuthorizationException
  133. * @throws ValidationException
  134. */
  135. public function update(UserProfileRequest $request, $id)
  136. {
  137. $user = $this->repo->findOrFail($id);
  138. $this->authorize('update', $user);
  139. $this->repo->update($user, $this->request->all());
  140. return $this->success(['message' => trans('user.profile_updated')]);
  141. }
  142. /**
  143. * Update user status
  144. *
  145. * @param int $id
  146. *
  147. * @return JsonResponse
  148. * @throws AuthorizationException
  149. * @throws ValidationException
  150. */
  151. public function updateStatus($id)
  152. {
  153. $user = $this->repo->findOrFail($id);
  154. $this->authorize('update', $user);
  155. $this->repo->status($user, request('status'));
  156. $this->activity->record([
  157. 'module' => $this->module,
  158. 'module_id' => $user->id,
  159. 'activity' => 'updated'
  160. ]);
  161. return $this->success(['message' => trans('user.profile_updated')]);
  162. }
  163. /**
  164. * Update user contact info
  165. *
  166. * @param UserProfileRequest $request
  167. * @param int $id
  168. *
  169. * @return JsonResponse
  170. * @throws AuthorizationException
  171. * @throws ValidationException
  172. */
  173. public function updateContact(UserProfileRequest $request, $id)
  174. {
  175. $user = $this->repo->findOrFail($id);
  176. $this->authorize('update', $user);
  177. $user = $this->repo->update($user, $this->request->all());
  178. $this->activity->record([
  179. 'module' => $this->module,
  180. 'module_id' => $user->id,
  181. 'activity' => 'updated'
  182. ]);
  183. return $this->success(['message' => trans('user.profile_updated')]);
  184. }
  185. /**
  186. * Change user password
  187. *
  188. * @param ChangePasswordRequest $request
  189. * @param int $id
  190. *
  191. * @return JsonResponse
  192. * @throws AuthorizationException
  193. * @throws ValidationException
  194. */
  195. public function forceResetPassword(ChangePasswordRequest $request, $id)
  196. {
  197. $user = $this->repo->findOrFail($id);
  198. $this->authorize('forceResetUserPassword', $user);
  199. $user = $this->repo->forceResetPassword($user, request('new_password'));
  200. $this->activity->record([
  201. 'module' => $this->module,
  202. 'module_id' => $user->id,
  203. 'activity' => 'updated'
  204. ]);
  205. return $this->success(['message' => trans('passwords.change')]);
  206. }
  207. /**
  208. * Update user profile
  209. *
  210. * @param UserProfileRequest $request
  211. *
  212. * @return JsonResponse
  213. */
  214. public function updateProfile(UserProfileRequest $request)
  215. {
  216. $auth_user = Auth::user();
  217. $this->repo->update($auth_user, $this->request->all());
  218. $this->activity->record([
  219. 'module' => $this->module,
  220. 'module_id' => $auth_user->id,
  221. 'sub_module' => 'profile',
  222. 'activity' => 'updated'
  223. ]);
  224. return $this->success(['message' => trans('user.profile_updated')]);
  225. }
  226. /**
  227. * Update user avatar
  228. *
  229. * @param int $id
  230. *
  231. * @return JsonResponse
  232. * @throws AuthorizationException
  233. * @throws ValidationException
  234. */
  235. public function uploadAvatar($id)
  236. {
  237. $user = $this->repo->findOrFail($id);
  238. $this->authorize('avatar', $user);
  239. $image_path = config('system.upload_path.avatar') . '/';
  240. $profile = $user->profile;
  241. $image = $profile->avatar;
  242. if ($image && File::exists($image)) {
  243. File::delete($image);
  244. }
  245. $extension = request()->file('image')->getClientOriginalExtension();
  246. $filename = uniqid();
  247. request()->file('image')->move($image_path, $filename . "." . $extension);
  248. $img = \Image::make($image_path . $filename . "." . $extension);
  249. $img->resize(200, null, function ($constraint) {
  250. $constraint->aspectRatio();
  251. });
  252. $img->save($image_path . $filename . "." . $extension);
  253. $profile->avatar = $image_path . $filename . "." . $extension;
  254. $profile->save();
  255. $this->activity->record([
  256. 'module' => $this->module,
  257. 'module_id' => $user->id,
  258. 'sub_module' => 'avatar',
  259. 'activity' => 'uploaded'
  260. ]);
  261. return $this->success(['message' => trans('user.avatar_uploaded'), 'image' => $image_path . $filename . "." . $extension]);
  262. }
  263. /**
  264. * Remove user avatar
  265. *
  266. * @param int $id
  267. *
  268. * @return JsonResponse
  269. * @throws AuthorizationException
  270. * @throws ValidationException
  271. */
  272. public function removeAvatar($id)
  273. {
  274. $user = $this->repo->findOrFail($id);
  275. $this->authorize('avatar', $user);
  276. $profile = $user->profile;
  277. $image = $profile->avatar;
  278. if (!$image) {
  279. return $this->error(['message' => trans('user.no_avatar_uploaded')]);
  280. }
  281. if (File::exists($image)) {
  282. File::delete($image);
  283. }
  284. $profile->avatar = null;
  285. $profile->save();
  286. $this->activity->record([
  287. 'module' => $this->module,
  288. 'module_id' => $user->id,
  289. 'sub_module' => 'avatar',
  290. 'activity' => 'removed'
  291. ]);
  292. return $this->success(['message' => trans('user.avatar_removed')]);
  293. }
  294. /**
  295. * Delete user
  296. *
  297. * @param int $id
  298. *
  299. * @return JsonResponse
  300. * @throws AuthorizationException
  301. * @throws ValidationException
  302. * @throws \Exception
  303. */
  304. public function destroy($id)
  305. {
  306. $user = $this->repo->findOrFail($id);
  307. $this->authorize('delete', $user);
  308. $this->activity->record([
  309. 'module' => $this->module,
  310. 'module_id' => $user->id,
  311. 'activity' => 'deleted'
  312. ]);
  313. $this->repo->delete($user);
  314. return $this->success(['message' => trans('user.deleted')]);
  315. }
  316. }