Skip to content

Commit

Permalink
Merge pull request #392 from NobodyNada/patch-2
Browse files Browse the repository at this point in the history
Require review privileges to use regex search
  • Loading branch information
Undo1 committed Apr 2, 2018
2 parents 1fa5623 + 2ff24a5 commit 74a97a6
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/controllers/search_controller.rb
Expand Up @@ -10,7 +10,7 @@ def index
body, body_operation,
why, why_operation,
username, username_operation = [:title, :body, :why, :username].map do |s|
SearchHelper.parse_search_params(params, s, user_signed_in?)
SearchHelper.parse_search_params(params, s, current_user)
end.flatten

if [title_operation, body_operation, why_operation, username_operation].any?(&:!)
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/search_helper.rb
@@ -1,11 +1,11 @@
# frozen_string_literal: true

module SearchHelper
def self.parse_search_params(params, symbol, user_signed_in)
def self.parse_search_params(params, symbol, user)
input = params[symbol] || ''

if params[is_regex?(symbol)]
operation = if user_signed_in
operation = if !user.nil? && user.can_use_regex_search?
params[is_inverse_regex?(symbol)] ? 'NOT REGEXP' : 'REGEXP'
else
false
Expand Down
4 changes: 4 additions & 0 deletions app/models/user.rb
Expand Up @@ -251,4 +251,8 @@ def add_pinned_role(name)
def has_pinned_role?(role)
UsersRole.where(user: self, role: Role.find_by(name: role), pinned: true).exists?
end

def can_use_regex_search?
(has_role? :reviewer) || moderator_sites.any?
end
end
8 changes: 4 additions & 4 deletions app/views/search/search.html.erb
Expand Up @@ -3,31 +3,31 @@
<%= form_tag (params[:option] == 'graphs' ? search_path(anchor: "graphs") : search_path), method: "get" do |f| %>
<div class="form-group">
<%= label_tag :title %>
<% if user_signed_in? %>
<% if current_user&.can_use_regex_search? %>
<%= check_box_tag :title_is_regex, 1, params[:title_is_regex] %> <span class="text-muted">regex</span>
<%= check_box_tag :title_is_inverse_regex, 1, params[:title_is_inverse_regex] %> <span class="text-muted">invert</span>
<% end %>
<%= text_field_tag :title, params[:title], placeholder: "*", class: "form-control" %>
</div>
<div class="form-group">
<%= label_tag :body %>
<% if user_signed_in? %>
<% if current_user&.can_use_regex_search? %>
<%= check_box_tag :body_is_regex, 1, params[:body_is_regex] %> <span class="text-muted">regex</span>
<%= check_box_tag :body_is_inverse_regex, 1, params[:body_is_inverse_regex] %> <span class="text-muted">invert</span>
<% end %>
<%= text_field_tag :body, params[:body], placeholder: "*", class: "form-control" %>
</div>
<div class="form-group">
<%= label_tag :username %>
<% if user_signed_in? %>
<% if current_user&.can_use_regex_search? %>
<%= check_box_tag :username_is_regex, 1, params[:username_is_regex] %> <span class="text-muted">regex</span>
<%= check_box_tag :username_is_inverse_regex, 1, params[:username_is_inverse_regex] %> <span class="text-muted">invert</span>
<% end %>
<%= text_field_tag :username, params[:username], placeholder: "*", class: "form-control" %>
</div>
<div class="form-group">
<%= label_tag :why %>
<% if user_signed_in? %>
<% if current_user&.can_use_regex_search? %>
<%= check_box_tag :why_is_regex, 1, params[:why_is_regex] %> <span class="text-muted">regex</span>
<%= check_box_tag :why_is_inverse_regex, 1, params[:why_is_inverse_regex] %> <span class="text-muted">invert</span>
<% end %>
Expand Down

0 comments on commit 74a97a6

Please sign in to comment.