public function create(Request $request)
 {
     if (!$request->ajax()) {
         return redirect('/login');
     }
     $createUserRequest = new CreateUserRequest();
     $validator = Validator::make($request->all(), $createUserRequest->rules(), $createUserRequest->messages());
     // Validate Form
     if ($validator->fails()) {
         return response()->json(['success' => false, 'errors' => $validator->errors()->toArray()], 400);
     }
     // Check Captcha still Valid or Used!
     if ($this->captchaCheck() == false) {
         $errors = ['captchaError' => trans('auth.captchaError')];
         return response()->json(['success' => false, 'errors' => $errors], 400);
     }
     // Check Sponsor Cookie , Provide One if None
     if (\Cookie::get('sponsor') == false) {
         try {
             $link = Link::with('user', 'user.profile')->where('link', $request->sponsor_link)->first();
             $cookie = $link->toArray();
             $errors = ['CookieError' => trans('auth.cookieError'), 'cookieNew' => trans('auth.cookieNew'), 'resubmitForm' => trans('auth.resubmitForm')];
             return response()->json(['success' => false, 'errors' => $errors], 400)->withCookie(\Cookie::forever('sponsor', $cookie));
         } catch (\Exception $e) {
             $errors = ['Warning' => 'Warning :Forbiden Link Forgery!'];
             return response()->json(['success' => false, 'errors' => $errors], 400);
         }
     }
     // This Will Prevent Unnecessary Creation of Account if Something Failed!
     DB::beginTransaction();
     $user = User::create($request->all());
     $profile = $user->profile()->create($request->all());
     $link = new Link();
     $link->link = Input::get('username');
     $user->links()->save($link);
     // IF Error Occured Throw an Exception then Rollback!
     $role = $user->assign('customer');
     $ability1 = \Bouncer::allow($user)->to('add-order', Order::class);
     $ability2 = \Bouncer::allow($user)->to('edit-order', Order::class);
     $ability3 = \Bouncer::allow($user)->to('delete-order', Order::class);
     $ability4 = \Bouncer::allow($user)->to('view-itemOrder', ItemOrder::class);
     try {
         if (!$user && !$profile && !$link && !$role && $ability1 && $ability2 && $ability3 && $ability4) {
             throw new \Exception('Account Creation Failed ,Account is Rollback');
         }
     } catch (\Exception $e) {
         DB::rollback();
         $errors = ['ExceptionError' => $e->getMessage()];
         return response()->json(['success' => false, 'errors' => $errors], 400);
         // Failed Creation
     }
     // Account Successfully Created
     DB::commit();
     // Send Email To The New User
     $this->mail->registered($user);
     $this->mail->sendToSponsor($user);
     $cookie = \Cookie::forget('sponsor');
     // Return With a Response to Delete Cookie
     Auth::LoginUsingId($user->id);
     // return redirect()->route('profile');
     return response()->json(['success' => true, 'url' => 'shipping-address'], 201)->withCookie($cookie);
 }
Esempio n. 2
0
 public function create(Request $request)
 {
     $createUserRequest = new CreateUserRequest();
     $validator = Validator::make($request->all(), $createUserRequest->rules(), $createUserRequest->messages());
     // Validate Form
     if ($validator->fails()) {
         return response()->json(['success' => false, 'errors' => $validator->errors()->toArray()], 400);
     }
     // Check Captcha still Valid or Used!
     if ($this->captchaCheck() == false) {
         $errors = ['captchaError' => trans('auth.captchaError')];
         return response()->json(['success' => false, 'errors' => $errors], 400);
     }
     // Check Sponsor Cookie , Provide One if None
     if (\Cookie::get('sponsor') == false) {
         $link = Link::with('user', 'user.profile')->where('link', $request->sponsor_link)->first();
         $cookie = $link->toArray();
         $errors = ['CookieError' => trans('auth.cookieError'), 'cookieNew' => trans('auth.cookieAttached')];
         return response()->json(['success' => false, 'errors' => $errors], 400)->withCookie(\Cookie::forever('sponsor', $cookie));
     }
     // This Will Prevent Unnecessary Creation of Account if Something Failed!
     DB::beginTransaction();
     $user = User::create($request->all());
     $profile = $user->profile()->create($request->all());
     $link = new Link();
     $link->link = Input::get('username');
     $user->links()->save($link);
     // IF Error Occured Throw an Exception then Rollback!
     try {
         if (!$user && !$profile && !$link) {
             throw new \Exception('Account Creation Failed ,Account is Rollback');
         }
     } catch (\Exception $e) {
         DB::rollback();
         $errors = ['ExceptionError' => $e->getMessage(), 'RefreshPage' => trans('auth.refreshPage')];
         return response()->json(['success' => false, 'errors' => $errors], 400);
         // Failed Creation
     }
     // Account Successfully Created
     DB::commit();
     // Send Email To The New User
     $this->mail->registered($user);
     $data = ['event' => 'UserSignedUp', 'data' => ['display_name' => $profile->display_name, 'created_at' => $user->created_at]];
     // BroadCast Realtime in NewsBar
     \PHPRedis::publish('rfn-chanel', json_encode($data));
     // Forget the Set Cookie
     $cookie = \Cookie::forget('sponsor');
     // Return With a Response to Delete Cookie
     return response()->json(['success' => true], 201)->withCookie($cookie);
 }