Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Wiki controller using User's new methods to check users' … #2532

Merged
merged 2 commits into from
Mar 23, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions app/controllers/wiki_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@ def show
end

if @node&.has_power_tag('redirect') && Node.where(nid: @node.power_tag('redirect')).exists?
if current_user.nil? || (current_user.role != 'admin' && current_user.role != 'moderator')
if current_user.nil? || !current_user.can_moderate?
redirect_to Node.find(@node.power_tag('redirect')).path
return
elsif current_user.role == 'admin' || current_user.role == 'moderator'
elsif current_user.can_moderate?
flash.now[:warning] = "Only moderators and admins see this page, as it is redirected to <a href='#{Node.find(@node.power_tag('redirect')).path}'>#{Node.find(@node.power_tag('redirect')).title}</a>.
To remove the redirect, delete the tag beginning with 'redirect:'"
end
end

if @node&.has_power_tag('abtest') && !Node.where(nid: @node.power_tag('abtest')).empty?
if current_user.nil? || (current_user.role != 'admin' && current_user.role != 'moderator')
if current_user.nil? || !current_user.can_moderate?
if Random.rand(2) == 0
redirect_to Node.find(@node.power_tag('abtest')).path
return
end
elsif current_user.role == 'admin' || current_user.role == 'moderator'
elsif current_user.can_moderate?
flash.now[:warning] = "Only moderators and admins see this page, as it is redirected to #{Node.find(@node.power_tag('abtest')).title} roughly around 50% of the time.
To remove this behavior, delete the tag beginning with 'abtest:'"
end
Expand Down Expand Up @@ -94,7 +94,7 @@ def edit
Node.find_wiki(params[:id])
end

if @node.has_tag('locked') && (current_user.role != 'admin' && current_user.role != 'moderator')
if @node.has_tag('locked') && !current_user.can_moderate?
flash[:warning] = "This page is <a href='/wiki/power-tags#Locking'>locked</a>, and only <a href='/wiki/moderators'>moderators</a> can edit it."
redirect_to @node.path
end
Expand Down Expand Up @@ -171,7 +171,7 @@ def update
@revision = @node.new_revision(uid: current_user.uid,
title: params[:title],
body: params[:body])
if @node.has_tag('locked') && (current_user.role != 'admin' && current_user.role != 'moderator')
if @node.has_tag('locked') && !current_user.can_moderate?
flash[:warning] = "This page is <a href='/wiki/power-tags#Locking'>locked</a>, and only <a href='/wiki/moderators'>moderators</a> can update it."
redirect_to @node.path

Expand Down Expand Up @@ -211,7 +211,7 @@ def update

def delete
@node = Node.find(params[:id])
if current_user && current_user.role == 'admin'
if current_user && current_user.admin?
@node.destroy
flash[:notice] = I18n.t('wiki_controller.wiki_page_deleted')
redirect_to '/dashboard'
Expand All @@ -224,7 +224,7 @@ def delete
def revert
revision = Revision.find params[:id]
node = revision.parent
if current_user && (current_user.role == 'moderator' || current_user.role == 'admin')
if current_user && current_user.can_moderate?
new_rev = revision.dup
new_rev.timestamp = DateTime.now.to_i
if new_rev.save!
Expand Down Expand Up @@ -259,7 +259,7 @@ def revisions
@node = Node.find_wiki(params[:id])
if @node
@revisions = @node.revisions
@revisions = @revisions.where(status: 1) unless current_user && (current_user.role == 'moderator' || current_user.role == 'admin')
@revisions = @revisions.where(status: 1) unless current_user && current_user.can_moderate?
@title = I18n.t('wiki_controller.revisions_for', title: @node.title).html_safe
@tags = @node.tags
else
Expand All @@ -278,7 +278,7 @@ def revision
if @revision.nil?
flash[:error] = I18n.t('wiki_controller.revision_not_found')
redirect_to action: 'revisions'
elsif @revision.status == 1 || current_user && (current_user.role == 'moderator' || current_user.role == 'admin')
elsif @revision.status == 1 || current_user && current_user.can_moderate?
@title = I18n.t('wiki_controller.revisions_for', title: @revision.title).html_safe
render template: 'wiki/show'
else
Expand Down