UserController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\Hash;
  5. use Illuminate\Support\Facades\Validator;
  6. use Tymon\JWTAuth\Exceptions\JWTException;
  7. use Illuminate\Support\Facades\Storage;
  8. use Illuminate\Support\Facades\Input;
  9. use App\User;
  10. use App\Company;
  11. use JWTAuth;
  12. use DB;
  13. class UserController extends Controller
  14. {
  15. public function register_new_user (Request $request)
  16. {
  17. $validator = Validator::make($request->all(), [
  18. 'name' => 'required|string|max:255',
  19. 'email' => 'required|string|email|max:255|unique:users',
  20. 'password' => 'required|string|min:6|confirmed',
  21. 'type' => 'required|string|max:255|in:super_admin,manger_authority_person,
  22. decision_maker,supervisor,employee',
  23. ]);
  24. if($validator->fails()){
  25. $error = $validator->errors();
  26. return response()->json(compact('error'),400);
  27. }
  28. $user = User::create([
  29. 'name' => $request['name'],
  30. 'email' => $request['email'],
  31. 'type' => $request['type'],
  32. 'password' => Hash::make($request['password']),
  33. ]);
  34. $token = JWTAuth::fromUser($user);
  35. return response()->json(compact('user','token'),201);
  36. }
  37. public function get_user_by_id ($id)
  38. {
  39. $user = User::find($id);
  40. return response()->json(compact('user'));
  41. }
  42. public function edit_current_user (Request $request)
  43. {
  44. $validator = Validator::make($request->all(), [
  45. 'id' => 'required',
  46. 'name' => 'required|string|max:255',
  47. 'email' => 'required|string|max:255|unique:users,email,'.$request['id'],
  48. 'password' => 'required|string|min:6',
  49. 'type' => 'required|string|max:255|in:super_admin,manger_authority_person,
  50. decision_maker,supervisor,employee',
  51. ]);
  52. if($validator->fails()){
  53. $error = $validator->errors();
  54. return response()->json(compact('error'),400);
  55. }
  56. $user = User::find($request['id'])->update([
  57. 'name' => $request['name'],
  58. 'email' => $request['email'],
  59. 'type' => 'super_admin',
  60. 'password' => Hash::make($request['password']),
  61. ]);
  62. $user = User::find($request['id']);
  63. return response()->json(compact('user'));
  64. }
  65. public function delete_current_users (Request $request)
  66. {
  67. $validator = Validator::make($request->all(), [
  68. "users_id.*" => "required|integer"
  69. ]);
  70. if($validator->fails()){
  71. $error = $validator->errors();
  72. return response()->json(compact('error'),400);
  73. }
  74. foreach ($request['users_id'] as $key => $id) {
  75. $user = User::find($id)->delete();
  76. }
  77. return response()->json(['status' => 'done'], 204);
  78. }
  79. public function users_list ($current_page, $per_page, $type ,$key = null)
  80. {
  81. $current_page = $current_page - 1 ;
  82. $users = DB::table('users');
  83. if ($type!='all') {
  84. $users->where('type',$type);
  85. }
  86. if (isset($key) && $key != null) {
  87. $users->where(function ($query) use ($key){
  88. $query->where('name','like',"%$key%")
  89. ->orWhere('email','like',"%$key%");
  90. // ->orWhere('phone','like',"%$key%");
  91. });
  92. }
  93. $users = $users->whereNull('deleted_at');
  94. $count = $users->count();
  95. $users = $users->skip($per_page*$current_page)->limit($per_page)->orderBy('id','DESC')->get();
  96. return response()->json(compact('users','count','per_page'));
  97. }
  98. } // class