FIX: Don't show the link button in the composer if linking is disabled

This commit is contained in:
Robin Ward
2018-02-08 12:56:10 -05:00
parent 8c89f5704d
commit dedeb2deb8
6 changed files with 48 additions and 6 deletions

View File

@ -45,7 +45,8 @@ const isInside = (text, regex) => {
class Toolbar { class Toolbar {
constructor(site) { constructor(opts) {
const { site, siteSettings } = opts;
this.shortcuts = {}; this.shortcuts = {};
this.groups = [ this.groups = [
@ -74,7 +75,14 @@ class Toolbar {
perform: e => e.applySurround('_', '_', 'italic_text') perform: e => e.applySurround('_', '_', 'italic_text')
}); });
this.addButton({id: 'link', group: 'insertions', shortcut: 'K', action: 'showLinkModal'}); if (opts.showLink) {
this.addButton({
id: 'link',
group: 'insertions',
shortcut: 'K',
action: 'showLinkModal'
});
}
this.addButton({ this.addButton({
id: 'quote', id: 'quote',
@ -108,7 +116,7 @@ class Toolbar {
perform: e => e.applyList(i => !i ? "1. " : `${parseInt(i) + 1}. `, 'list_item') perform: e => e.applyList(i => !i ? "1. " : `${parseInt(i) + 1}. `, 'list_item')
}); });
if (Discourse.SiteSettings.support_mixed_text_direction) { if (siteSettings.support_mixed_text_direction) {
this.addButton({ this.addButton({
id: 'toggle-direction', id: 'toggle-direction',
group: 'extras', group: 'extras',
@ -200,6 +208,7 @@ export default Ember.Component.extend({
lastSel: null, lastSel: null,
_mouseTrap: null, _mouseTrap: null,
emojiPickerIsActive: false, emojiPickerIsActive: false,
showLink: true,
@computed('placeholder') @computed('placeholder')
placeholderTranslated(placeholder) { placeholderTranslated(placeholder) {
@ -279,7 +288,9 @@ export default Ember.Component.extend({
@computed @computed
toolbar() { toolbar() {
const toolbar = new Toolbar(this.site); const toolbar = new Toolbar(
this.getProperties('site', 'siteSettings', 'showLink')
);
_createCallbacks.forEach(cb => cb(toolbar)); _createCallbacks.forEach(cb => cb(toolbar));
this.sendAction('extraButtons', toolbar); this.sendAction('extraButtons', toolbar);
return toolbar; return toolbar;

View File

@ -11,6 +11,7 @@
validation=validation validation=validation
loading=composer.loading loading=composer.loading
forcePreview=forcePreview forcePreview=forcePreview
showLink=currentUser.can_post_link
composerEvents=true composerEvents=true
onExpandPopupMenuOptions="onExpandPopupMenuOptions" onExpandPopupMenuOptions="onExpandPopupMenuOptions"
onPopupMenuAction=onPopupMenuAction onPopupMenuAction=onPopupMenuAction

View File

@ -39,7 +39,12 @@ class CurrentUserSerializer < BasicUserSerializer
:seen_notification_id, :seen_notification_id,
:primary_group_id, :primary_group_id,
:primary_group_name, :primary_group_name,
:can_create_topic :can_create_topic,
:can_post_link
def can_post_link
scope.can_post_link?
end
def can_create_topic def can_create_topic
scope.can_create_topic?(nil) scope.can_create_topic?(nil)

View File

@ -1,6 +1,11 @@
#mixin for all guardian methods dealing with post permissions #mixin for all guardian methods dealing with post permissions
module PostGuardian module PostGuardian
def can_post_link?
authenticated? &&
@user.has_trust_level?(TrustLevel[SiteSetting.min_trust_to_post_links])
end
# Can the user act on the post in a particular way. # Can the user act on the post in a particular way.
# taken_actions = the list of actions the user has already taken # taken_actions = the list of actions the user has already taken
def post_can_act?(post, action_key, opts: {}, can_see_post: nil) def post_can_act?(post, action_key, opts: {}, can_see_post: nil)

View File

@ -93,7 +93,9 @@ class Validators::PostValidator < ActiveModel::Validator
end end
def can_post_links_validator(post) def can_post_links_validator(post)
return if post.link_count == 0 || acting_user_is_trusted?(post, SiteSetting.min_trust_to_post_links) || private_message?(post) return if post.link_count == 0 ||
Guardian.new(post.acting_user).can_post_link? ||
private_message?(post)
post.errors.add(:base, I18n.t(:links_require_trust)) post.errors.add(:base, I18n.t(:links_require_trust))
end end

View File

@ -26,6 +26,24 @@ describe Guardian do
expect { Guardian.new(user) }.not_to raise_error expect { Guardian.new(user) }.not_to raise_error
end end
describe "can_post_link?" do
it "returns false for anonymous users" do
expect(Guardian.new.can_post_link?).to eq(false)
end
it "returns true for a regular user" do
expect(Guardian.new(user).can_post_link?).to eq(true)
end
it "supports customization by site setting" do
user.trust_level = 0
SiteSetting.min_trust_to_post_links = 0
expect(Guardian.new(user).can_post_link?).to eq(true)
SiteSetting.min_trust_to_post_links = 1
expect(Guardian.new(user).can_post_link?).to eq(false)
end
end
describe 'post_can_act?' do describe 'post_can_act?' do
let(:post) { build(:post) } let(:post) { build(:post) }
let(:user) { build(:user) } let(:user) { build(:user) }