Exemplo n.º 1
0
 /**
  * Create a new user
  * @param  array   $input
  * @param  integer $role  Default role is 2
  * @return BFACP\Account\User
  */
 public function signup($input = [], $role = 2, $confirmed = false)
 {
     $user = new User();
     $user->username = array_get($input, 'username');
     $user->email = array_get($input, 'email');
     $user->password = array_get($input, 'password');
     // The password confirmation will be removed from model
     // before saving. This field will be used in Ardent's
     // auto validation.
     $user->password_confirmation = array_get($input, 'password_confirmation');
     // Generate a random confirmation code
     $user->confirmation_code = md5(uniqid(mt_rand(), true));
     // Update last seen timestamp
     $user->lastseen_at = Carbon::now();
     $user->confirmed = $confirmed;
     // Save if valid. Password field will be hashed before save
     $user->save();
     if (!is_null($user->id)) {
         $user->roles()->attach($role);
         $user->setting()->save(new Setting(['lang' => array_get($input, 'lang', 'en')]));
         if (!empty(array_get($input, 'ign'))) {
             $players = Player::where('SoldierName', array_get($input, 'ign'))->lists('PlayerID');
             foreach ($players as $player) {
                 // Check if an existing user already has claimed the player
                 // and if so do not associate with the new account.
                 if (Soldier::where('player_id', $player)->count() == 0) {
                     $soldier = new Soldier(['player_id' => $player]);
                     $soldier->user()->associate($user)->save();
                 }
             }
         }
     }
     return $user;
 }
Exemplo n.º 2
0
 /**
  * Update user
  *
  * @param  integer $id User ID
  */
 public function update($id)
 {
     try {
         $user = User::findOrFail($id);
         $username = trim(Input::get('username', null));
         $email = trim(Input::get('email', null));
         $roleId = trim(Input::get('role', null));
         $lang = trim(Input::get('language', null));
         $status = trim(Input::get('confirmed', null));
         $soldiers = explode(',', Input::get('soldiers', ''));
         $v = Validator::make(Input::all(), ['username' => 'required|alpha_dash|min:4|unique:bfacp_users,username,' . $id, 'email' => 'required|email|unique:bfacp_users,email,' . $id, 'language' => 'required|in:' . implode(',', array_keys(Config::get('bfacp.site.languages'))), 'generate_pass' => 'boolean', 'confirmed' => 'boolean']);
         if ($v->fails()) {
             return Redirect::route('admin.site.users.edit', [$id])->withErrors($v)->withInput();
         }
         // Update the user role if it's been changed
         if ($roleId != $user->roles[0]->id) {
             $user->roles()->detach($user->roles[0]->id);
             $user->roles()->attach($roleId);
         }
         // Update the user language if it's been changed
         if ($lang != $user->setting->lang) {
             $user->setting()->update(['lang' => $lang]);
         }
         // Update account stats
         if ($status != $user->confirmed) {
             $user->confirmed = $status;
         }
         // Update username
         if ($username != $user->username) {
             $user->username = $username;
         }
         // Update email
         if ($email != $user->email) {
             $user->email = $email;
         }
         if (Input::has('generate_pass')) {
             $repo = app('BFACP\\Repositories\\UserRepository');
             // Generate a new password
             $newPassword = $repo->generatePassword();
             $repo->sendPasswordChangeEmail($user->username, $user->email, $newPassword);
             // Change the user password
             $user->password = $newPassword;
             $user->password_confirmation = $newPassword;
             $this->messages[] = Lang::get('site.admin.users.updates.password.generated', ['username' => $user->username, 'email' => $user->email]);
         }
         $soldier_ids = [];
         $user->soldiers()->delete();
         if (Input::has('soldiers')) {
             foreach ($soldiers as $soldier) {
                 $soldier_ids[] = new Soldier(['player_id' => $soldier]);
             }
         }
         if (Input::has('soldier')) {
             $players = Player::where('SoldierName', Input::get('soldier'))->lists('PlayerID');
             foreach ($players as $player) {
                 if (!in_array($player, $soldiers)) {
                     $soldier_ids[] = new Soldier(['player_id' => $player]);
                 }
             }
         }
         if (!empty($soldier_ids)) {
             foreach ($soldier_ids as $key => $soldier) {
                 // Check if an existing user already has claimed the player
                 // and if so do not associate with the account.
                 if (Soldier::where('player_id', $soldier->player_id)->count() == 1) {
                     $this->messages[] = Lang::get('alerts.user.soldier_taken', ['playerid' => $soldier->player_id]);
                     unset($soldier_ids[$key]);
                 }
             }
             $user->soldiers()->saveMany($soldier_ids);
         }
         $user->save();
         return Redirect::route('admin.site.users.edit', [$id])->withMessages($this->messages);
     } catch (ModelNotFoundException $e) {
         $this->messages[] = Lang::get('alerts.user.invlid', ['userid' => $id]);
         return Redirect::route('admin.site.users.edit', [$id])->withErrors($this->messages);
     }
 }