Beispiel #1
0
 /**
  * @api            {put} /users/{username} Update User Info
  * @apiGroup       Users
  * @apiDescription Update a user's account information.
  * @apiParam {string} sessionKey A session key belonging to this user.
  * @apiParam {string} [username] A new username for the user.
  * @apiParam {string} [email] A new email address for the user.
  * @apiParam {string} [password] A new password for the user. Minimum 3 characters.
  * @apiParam {boolean=0,1} [defaultAnonymous=0] Display the username on images uploaded by this user?
  * @apiParam {string} [defaultPassword] A password that will be required to view newly uploaded images.
  *     (Can be changed per image after uploading, see "Update Image Info").
  * @apiUse         UserSuccessResponse
  *
  * @param User           $user
  * @param PasswordHasher $passwordHasher
  *
  * @return Response
  */
 public function update(User $user, PasswordHasher $passwordHasher)
 {
     $this->requireAuthentication($user->userId);
     $validationRules = ['username' => 'unique:users,username,' . $user->userId . ',userId', 'email' => 'unique:users,email,' . $user->userId . ',userId', 'password' => 'min:3', 'defaultAnonymous' => 'boolean', 'defaultPassword' => 'string'];
     $this->validate($this->request, $validationRules);
     if ($this->request->has('username')) {
         $user->username = $this->request->input('username');
     }
     if ($this->request->has('email')) {
         $user->email = $this->request->input('email');
     }
     if ($this->request->has('password')) {
         $user->password = $passwordHasher->generateHash($this->request->input('password'));
     }
     if ($this->request->has('defaultAnonymous')) {
         $user->defaultAnonymous = (bool) $this->request->input('defaultAnonymous');
     }
     if ($this->request->exists('defaultPassword')) {
         if ($password = $this->request->input('defaultPassword')) {
             $user->defaultPassword = $passwordHasher->generateHash($password);
         } else {
             $user->defaultPassword = null;
         }
     }
     $success = $user->isDirty() ? $user->save() : false;
     return $this->response(['success' => $success, 'user' => $user->fresh()]);
 }