diff --git a/app/Commands/SaveAccountSettingsCommand.php b/app/Commands/SaveAccountSettingsCommand.php index df3a303b..8207900b 100644 --- a/app/Commands/SaveAccountSettingsCommand.php +++ b/app/Commands/SaveAccountSettingsCommand.php @@ -29,18 +29,14 @@ use Validator; class SaveAccountSettingsCommand extends CommandBase { private $_input; - private $_slug; - private $_user; - private $_current; - public function __construct($input, $slug) + /** @var User */ + private $_user; + + public function __construct($input, User $user) { $this->_input = $input; - $this->_slug = $slug; - - /** @var User _user */ - $this->_user = null; - $this->_current = null; + $this->_user = $user; } /** @@ -48,25 +44,7 @@ class SaveAccountSettingsCommand extends CommandBase */ public function authorize() { - if (Auth::user() != null) { - $this->_current = Auth::user(); - - if ($this->_slug == $this->_current->slug) { - $this->_user = $this->_current; - } else { - $this->_user = User::where('slug', $this->_slug)->whereNull('disabled_at')->first(); - } - - if ($this->_user == null) { - return false; - } - - if (Gate::allows('edit', $this->_user)) { - return true; - } - } - - return false; + return Gate::allows('edit', $this->_user); } /** @@ -75,14 +53,6 @@ class SaveAccountSettingsCommand extends CommandBase */ public function execute() { - if ($this->_user == null) { - if ($_current->hasRole('admin')) { - return CommandResponse::fail(['Not found']); - } else { - return CommandResponse::fail(['Permission denied']); - } - } - $rules = [ 'display_name' => 'required|min:3|max:26', 'bio' => 'textarea_length:250', @@ -110,31 +80,26 @@ class SaveAccountSettingsCommand extends CommandBase return CommandResponse::fail($validator); } - if ($this->_input['uses_gravatar'] != 'true') { - if ($this->_user->avatar_id == null && !isset($this->_input['avatar']) && !isset($this->_input['avatar_id'])) { - $validator->messages()->add('avatar', - 'You must upload or select an avatar if you are not using gravatar!'); - - return CommandResponse::fail($validator); - } - } - $this->_user->bio = $this->_input['bio']; $this->_user->display_name = $this->_input['display_name']; $this->_user->slug = $this->_input['slug']; $this->_user->can_see_explicit_content = $this->_input['can_see_explicit_content'] == 'true'; $this->_user->uses_gravatar = $this->_input['uses_gravatar'] == 'true'; - if ($this->_user->uses_gravatar) { + if ($this->_user->uses_gravatar && !empty($this->_input['gravatar'])) { $this->_user->avatar_id = null; $this->_user->gravatar = $this->_input['gravatar']; } else { + $this->_user->uses_gravatar = false; + if (isset($this->_input['avatar_id'])) { $this->_user->avatar_id = $this->_input['avatar_id']; + + } elseif (isset($this->_input['avatar'])) { + $this->_user->avatar_id = Image::upload($this->_input['avatar'], $this->_user)->id; + } else { - if (isset($this->_input['avatar'])) { - $this->_user->avatar_id = Image::upload($this->_input['avatar'], $this->_user)->id; - } + $this->_user->avatar_id = null; } } diff --git a/app/Http/Controllers/Api/Web/AccountController.php b/app/Http/Controllers/Api/Web/AccountController.php index ff1f39c6..cb952eee 100644 --- a/app/Http/Controllers/Api/Web/AccountController.php +++ b/app/Http/Controllers/Api/Web/AccountController.php @@ -75,8 +75,8 @@ class AccountController extends ApiControllerBase ], 200); } - public function postSave($slug) + public function postSave(User $user) { - return $this->execute(new SaveAccountSettingsCommand(Input::all(), $slug)); + return $this->execute(new SaveAccountSettingsCommand(Input::all(), $user)); } } diff --git a/app/Http/routes.php b/app/Http/routes.php index a7e09d50..88fa844e 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -126,7 +126,7 @@ Route::group(['prefix' => 'api/web'], function() { Route::post('/comments/{type}/{id}', 'Api\Web\CommentsController@postCreate')->where('id', '\d+'); - Route::post('/account/settings/save/{slug}', 'Api\Web\AccountController@postSave'); + Route::post('/account/settings/save/{userSlug}', 'Api\Web\AccountController@postSave'); Route::post('/favourites/toggle', 'Api\Web\FavouritesController@postToggle'); diff --git a/app/Models/User.php b/app/Models/User.php index dc82ccb6..f715608c 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -257,7 +257,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon public function getAvatarUrl($type = Image::NORMAL) { - if (!$this->uses_gravatar) { + if (!$this->uses_gravatar && $this->avatar !== null) { return $this->avatar->getUrl($type); } diff --git a/resources/assets/scripts/app/directives/image-upload.coffee b/resources/assets/scripts/app/directives/image-upload.coffee index 6de4da4e..3cf42c52 100644 --- a/resources/assets/scripts/app/directives/image-upload.coffee +++ b/resources/assets/scripts/app/directives/image-upload.coffee @@ -67,6 +67,10 @@ module.exports = angular.module('ponyfm').directive 'pfmImageUpload', () -> else if $scope.imageUrl lightbox.openImageUrl $scope.imageUrl + # An explicit return value is needed here to avoid this issue: + # https://docs.angularjs.org/error/$parse/isecdom + return null + $scope.uploadImage = () -> $uploader.trigger 'click' return