123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Hash;
- use Illuminate\Support\Facades\Validator;
- use Tymon\JWTAuth\Exceptions\JWTException;
- use Illuminate\Support\Facades\Storage;
- use Illuminate\Support\Facades\Input;
- use App\User;
- use App\Company;
- use JWTAuth;
- use DB;
- class UserController extends Controller
- {
- public function register_new_user (Request $request)
- {
- $validator = Validator::make($request->all(), [
- 'name' => 'required|string|max:255',
- 'email' => 'required|string|email|max:255|unique:users',
- 'password' => 'required|string|min:6|confirmed',
- 'type' => 'required|string|max:255|in:super_admin,manger_authority_person,
- decision_maker,supervisor,employee',
- ]);
- if($validator->fails()){
- $error = $validator->errors();
- return response()->json(compact('error'),400);
- }
- $user = User::create([
- 'name' => $request['name'],
- 'email' => $request['email'],
- 'type' => $request['type'],
- 'password' => Hash::make($request['password']),
- ]);
- $token = JWTAuth::fromUser($user);
- return response()->json(compact('user','token'),201);
- }
- public function get_user_by_id ($id)
- {
- $user = User::find($id);
- return response()->json(compact('user'));
- }
- public function edit_current_user (Request $request)
- {
- $validator = Validator::make($request->all(), [
- 'id' => 'required',
- 'name' => 'required|string|max:255',
- 'email' => 'required|string|max:255|unique:users,email,'.$request['id'],
- 'password' => 'required|string|min:6',
- 'type' => 'required|string|max:255|in:super_admin,manger_authority_person,
- decision_maker,supervisor,employee',
- ]);
- if($validator->fails()){
- $error = $validator->errors();
- return response()->json(compact('error'),400);
- }
- $user = User::find($request['id'])->update([
- 'name' => $request['name'],
- 'email' => $request['email'],
- 'type' => 'super_admin',
- 'password' => Hash::make($request['password']),
- ]);
- $user = User::find($request['id']);
- return response()->json(compact('user'));
- }
- public function delete_current_users (Request $request)
- {
- $validator = Validator::make($request->all(), [
- "users_id.*" => "required|integer"
- ]);
- if($validator->fails()){
- $error = $validator->errors();
- return response()->json(compact('error'),400);
- }
- foreach ($request['users_id'] as $key => $id) {
- $user = User::find($id)->delete();
- }
- return response()->json(['status' => 'done'], 204);
- }
- public function users_list ($current_page, $per_page, $type ,$key = null)
- {
- $current_page = $current_page - 1 ;
- $users = DB::table('users');
- if ($type!='all') {
- $users->where('type',$type);
- }
- if (isset($key) && $key != null) {
- $users->where(function ($query) use ($key){
- $query->where('name','like',"%$key%")
- ->orWhere('email','like',"%$key%");
- // ->orWhere('phone','like',"%$key%");
- });
- }
- $users = $users->whereNull('deleted_at');
- $count = $users->count();
- $users = $users->skip($per_page*$current_page)->limit($per_page)->orderBy('id','DESC')->get();
- return response()->json(compact('users','count','per_page'));
- }
- } // class
|