Esempio n. 1
0
 public static function installCreateUser(Request $request)
 {
     $data = $request->only(['email', 'name', 'password', 'password_confirmation', 'profile_picture', 'username']);
     try {
         $user = User::where('email', $data['email'])->firstOrFail();
         $userExists = true;
         $passwordRequired = "";
     } catch (ModelNotFoundException $e) {
         // Don't require password if user has filled out
         // information before
         $passwordRequired = "required|";
         $userExists = false;
     }
     // Validate all input
     $validator = Validator::make($data, ['name' => 'required', 'email' => 'email|sometimes', 'password' => $passwordRequired . 'confirmed|min:5', 'profile_picture' => 'sometimes|image|max:10240', 'username' => 'alpha_num|required']);
     if ($validator->fails()) {
         // If validation fails, redirect back to
         // registration form with errors
         return Response::json(['errors' => $validator->errors()->all()]);
     }
     // Image intervention ftw
     if ($request->hasFile('profile_picture')) {
         $data['profile_picture'] = UserController::uploadProfilePicture($request->file('profile_picture'), $data['email']);
     } elseif (!$userExists) {
         // If they didn't upload a picture, set it to the default picture
         $data['profile_picture'] = Setting::where('name', 'default_profile_picture')->first()->setting;
     }
     if ($userExists) {
         $allUsers = User::all();
         if (!count($allUsers) == 1) {
             $allUsers->skip(1);
             foreach ($allUsers as $indivUser) {
                 $indivUser->delete();
             }
         }
         UserController::updateUser($user->id, $data);
     } else {
         UserController::createUser($data);
         // Make this first User staff and admin.
         $user = User::first();
         $user->is_admin = 1;
         $user->is_staff = 1;
         $user->save();
     }
     return Response::json(['success']);
 }