mirror of
https://github.com/discourse/discourse.git
synced 2024-11-26 04:23:37 +08:00
REFACTOR: Use common path for RESTful DELETE
action from upload image
component
This commit is contained in:
parent
3cbb32cc20
commit
9000c358d1
|
@ -13,10 +13,7 @@ export default Em.Component.extend(UploadMixin, {
|
|||
this.set('imageUrl', data.result.url);
|
||||
},
|
||||
|
||||
actions: {
|
||||
trash: function() {
|
||||
deleteDone: function() {
|
||||
this.set('imageUrl', null);
|
||||
this.sendAction('clear');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
@ -70,10 +70,6 @@ export default Discourse.ObjectController.extend({
|
|||
|
||||
actions: {
|
||||
|
||||
clearProfileBackground: function() {
|
||||
this.get('model').clearProfileBackground();
|
||||
},
|
||||
|
||||
save: function() {
|
||||
var self = this;
|
||||
this.setProperties({ saving: true, saved: false });
|
||||
|
|
|
@ -12,17 +12,15 @@ export default Discourse.Controller.extend(Discourse.ModalFunctionality, {
|
|||
local: false,
|
||||
showMore: false,
|
||||
|
||||
init: function() {
|
||||
this._super();
|
||||
_initialize: function() {
|
||||
this.setProperties({
|
||||
local: this.get("allowLocal"),
|
||||
showMore: false
|
||||
});
|
||||
},
|
||||
}.on('init'),
|
||||
|
||||
allowLocal: function() {
|
||||
return Discourse.SiteSettings.max_attachment_size_kb > 0;
|
||||
}.property(),
|
||||
maxSize: Discourse.computed.setting('max_attachment_size_kb'),
|
||||
allowLocal: Em.computed.gt('maxSize', 0),
|
||||
|
||||
actions: {
|
||||
useLocal: function() { this.setProperties({ local: true, showMore: false}); },
|
||||
|
|
|
@ -6,6 +6,10 @@ export default Em.Mixin.create({
|
|||
Em.warn("You should implement `uploadDone`");
|
||||
},
|
||||
|
||||
deleteDone: function() {
|
||||
Em.warn("You should implement `deleteDone`");
|
||||
},
|
||||
|
||||
_initializeUploader: function() {
|
||||
var $upload = this.$('input[type=file]'), // note: we can't cache this as fileupload replaces the input after upload
|
||||
self = this;
|
||||
|
@ -49,6 +53,18 @@ export default Em.Mixin.create({
|
|||
actions: {
|
||||
selectFile: function() {
|
||||
this.$('input[type=file]').click();
|
||||
},
|
||||
|
||||
trash: function() {
|
||||
var self = this;
|
||||
Discourse.ajax(this.get('uploadUrl'), {
|
||||
type: 'DELETE',
|
||||
data: { image_type: this.get('type') }
|
||||
}).then(function() {
|
||||
self.deleteDone();
|
||||
}).catch(function() {
|
||||
bootbox.alert(I18n.t('generic_error'));
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
@ -343,19 +343,6 @@ Discourse.User = Discourse.Model.extend({
|
|||
});
|
||||
},
|
||||
|
||||
/*
|
||||
Clear profile background
|
||||
|
||||
@method clearProfileBackground
|
||||
@returns {Promise} the result of the clear profile background request
|
||||
*/
|
||||
clearProfileBackground: function() {
|
||||
return Discourse.ajax("/users/" + this.get("username_lower") + "/preferences/profile_background/clear", {
|
||||
type: 'PUT',
|
||||
data: { }
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
Determines whether the current user is allowed to upload a file.
|
||||
|
||||
|
|
|
@ -94,8 +94,7 @@
|
|||
<div class="controls">
|
||||
{{image-uploader uploadUrl=imageUploadUrl
|
||||
imageUrl=profile_background
|
||||
type="profile_background"
|
||||
clear="clearProfileBackground"}}
|
||||
type="profile_background"}}
|
||||
</div>
|
||||
</div>
|
||||
{{/if}}
|
||||
|
|
|
@ -7,7 +7,7 @@ class UsersController < ApplicationController
|
|||
skip_before_filter :authorize_mini_profiler, only: [:avatar]
|
||||
skip_before_filter :check_xhr, only: [:show, :password_reset, :update, :activate_account, :authorize_email, :user_preferences_redirect, :avatar, :my_redirect]
|
||||
|
||||
before_filter :ensure_logged_in, only: [:username, :update, :change_email, :user_preferences_redirect, :upload_user_image, :pick_avatar, :clear_profile_background, :destroy]
|
||||
before_filter :ensure_logged_in, only: [:username, :update, :change_email, :user_preferences_redirect, :upload_user_image, :pick_avatar, :destroy_user_image, :destroy]
|
||||
before_filter :respond_to_suspicious_request, only: [:create]
|
||||
|
||||
# we need to allow account creation with bad CSRF tokens, if people are caching, the CSRF token on the
|
||||
|
@ -380,11 +380,16 @@ class UsersController < ApplicationController
|
|||
render nothing: true
|
||||
end
|
||||
|
||||
def clear_profile_background
|
||||
def destroy_user_image
|
||||
user = fetch_user_from_params
|
||||
guardian.ensure_can_edit!(user)
|
||||
|
||||
image_type = params.require(:image_type)
|
||||
if image_type == 'profile_background'
|
||||
user.user_profile.clear_profile_background
|
||||
else
|
||||
raise Discourse::InvalidParameters.new(:image_type)
|
||||
end
|
||||
|
||||
render nothing: true
|
||||
end
|
||||
|
|
|
@ -203,8 +203,8 @@ Discourse::Application.routes.draw do
|
|||
get "users/:username/avatar(/:size)" => "users#avatar", constraints: {username: USERNAME_ROUTE_FORMAT} # LEGACY ROUTE
|
||||
post "users/:username/preferences/avatar" => "users#upload_avatar", constraints: {username: USERNAME_ROUTE_FORMAT} # LEGACY ROUTE
|
||||
post "users/:username/preferences/user_image" => "users#upload_user_image", constraints: {username: USERNAME_ROUTE_FORMAT}
|
||||
delete "users/:username/preferences/user_image" => "users#destroy_user_image", constraints: {username: USERNAME_ROUTE_FORMAT}
|
||||
put "users/:username/preferences/avatar/pick" => "users#pick_avatar", constraints: {username: USERNAME_ROUTE_FORMAT}
|
||||
put "users/:username/preferences/profile_background/clear" => "users#clear_profile_background", constraints: {username: USERNAME_ROUTE_FORMAT}
|
||||
get "users/:username/invited" => "users#invited", constraints: {username: USERNAME_ROUTE_FORMAT}
|
||||
post "users/:username/send_activation_email" => "users#send_activation_email", constraints: {username: USERNAME_ROUTE_FORMAT}
|
||||
get "users/:username/activity" => "users#show", constraints: {username: USERNAME_ROUTE_FORMAT}
|
||||
|
|
|
@ -1269,10 +1269,10 @@ describe UsersController do
|
|||
|
||||
end
|
||||
|
||||
describe '.clear_profile_background' do
|
||||
describe '.destroy_user_image' do
|
||||
|
||||
it 'raises an error when not logged in' do
|
||||
lambda { xhr :put, :clear_profile_background, username: 'asdf' }.should raise_error(Discourse::NotLoggedIn)
|
||||
lambda { xhr :put, :destroy_user_image, type: 'profile_background', username: 'asdf' }.should raise_error(Discourse::NotLoggedIn)
|
||||
end
|
||||
|
||||
context 'while logged in' do
|
||||
|
@ -1281,12 +1281,20 @@ describe UsersController do
|
|||
|
||||
it 'raises an error when you don\'t have permission to clear the profile background' do
|
||||
Guardian.any_instance.expects(:can_edit?).with(user).returns(false)
|
||||
xhr :put, :clear_profile_background, username: user.username
|
||||
xhr :put, :destroy_user_image, username: user.username, image_type: 'profile_background'
|
||||
response.should be_forbidden
|
||||
end
|
||||
|
||||
it 'it successful' do
|
||||
xhr :put, :clear_profile_background, username: user.username
|
||||
it "requires the `image_type` param" do
|
||||
-> { xhr :put, :destroy_user_image, username: user.username }.should raise_error(ActionController::ParameterMissing)
|
||||
end
|
||||
|
||||
it "only allows certain `image_types`" do
|
||||
-> { xhr :put, :destroy_user_image, username: user.username, image_type: 'wat' }.should raise_error(Discourse::InvalidParameters)
|
||||
end
|
||||
|
||||
it 'can clear the profile background' do
|
||||
xhr :put, :destroy_user_image, image_type: 'profile_background', username: user.username
|
||||
user.reload.user_profile.profile_background.should == ""
|
||||
response.should be_success
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue
Block a user