pundit: convert ip bans to pundit.
This commit is contained in:
@@ -1,35 +1,32 @@
|
|||||||
class IpBansController < ApplicationController
|
class IpBansController < ApplicationController
|
||||||
respond_to :html, :xml, :json, :js
|
respond_to :html, :xml, :json, :js
|
||||||
before_action :moderator_only
|
|
||||||
|
|
||||||
def new
|
def new
|
||||||
@ip_ban = IpBan.new
|
@ip_ban = authorize IpBan.new(permitted_attributes(IpBan))
|
||||||
|
respond_with(@ip_ban)
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
@ip_ban = CurrentUser.ip_bans.create(ip_ban_params)
|
@ip_ban = authorize IpBan.new(creator: CurrentUser.user, **permitted_attributes(IpBan))
|
||||||
|
@ip_ban.save
|
||||||
respond_with(@ip_ban, :location => ip_bans_path)
|
respond_with(@ip_ban, :location => ip_bans_path)
|
||||||
end
|
end
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@ip_bans = IpBan.paginated_search(params, count_pages: true)
|
@ip_bans = authorize IpBan.paginated_search(params, count_pages: true)
|
||||||
@ip_bans = @ip_bans.includes(:creator) if request.format.html?
|
@ip_bans = @ip_bans.includes(:creator) if request.format.html?
|
||||||
|
|
||||||
respond_with(@ip_bans)
|
respond_with(@ip_bans)
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
@ip_ban = IpBan.find(params[:id])
|
@ip_ban = authorize IpBan.find(params[:id])
|
||||||
@ip_ban.destroy
|
@ip_ban.destroy
|
||||||
respond_with(@ip_ban)
|
respond_with(@ip_ban)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def ip_ban_params
|
|
||||||
params.fetch(:ip_ban, {}).permit(%i[ip_addr reason])
|
|
||||||
end
|
|
||||||
|
|
||||||
def search_params
|
def search_params
|
||||||
params.fetch(:search, {}).permit(%i[ip_addr order])
|
params.fetch(:search, {}).permit(%i[ip_addr order])
|
||||||
end
|
end
|
||||||
|
|||||||
17
app/policies/ip_ban_policy.rb
Normal file
17
app/policies/ip_ban_policy.rb
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
class IpBanPolicy < ApplicationPolicy
|
||||||
|
def create?
|
||||||
|
user.is_moderator?
|
||||||
|
end
|
||||||
|
|
||||||
|
def index?
|
||||||
|
user.is_moderator?
|
||||||
|
end
|
||||||
|
|
||||||
|
def destroy?
|
||||||
|
user.is_moderator?
|
||||||
|
end
|
||||||
|
|
||||||
|
def permitted_attributes
|
||||||
|
[:ip_addr, :reason]
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -157,6 +157,9 @@
|
|||||||
<% if CurrentUser.is_moderator? %>
|
<% if CurrentUser.is_moderator? %>
|
||||||
<li><%= link_to("Moderation Reports", moderation_reports_path) %></li>
|
<li><%= link_to("Moderation Reports", moderation_reports_path) %></li>
|
||||||
<li><%= link_to("IP Addresses", ip_addresses_path) %></li>
|
<li><%= link_to("IP Addresses", ip_addresses_path) %></li>
|
||||||
|
<% end %>
|
||||||
|
|
||||||
|
<% if policy(IpBan).index? %>
|
||||||
<li><%= link_to("IP Bans", ip_bans_path) %></li>
|
<li><%= link_to("IP Bans", ip_bans_path) %></li>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ class IpBansControllerTest < ActionDispatch::IntegrationTest
|
|||||||
context "The ip bans controller" do
|
context "The ip bans controller" do
|
||||||
setup do
|
setup do
|
||||||
@admin = create(:admin_user)
|
@admin = create(:admin_user)
|
||||||
|
@ip_ban = create(:ip_ban)
|
||||||
end
|
end
|
||||||
|
|
||||||
context "new action" do
|
context "new action" do
|
||||||
@@ -17,17 +18,12 @@ class IpBansControllerTest < ActionDispatch::IntegrationTest
|
|||||||
should "create a new ip ban" do
|
should "create a new ip ban" do
|
||||||
assert_difference("IpBan.count", 1) do
|
assert_difference("IpBan.count", 1) do
|
||||||
post_auth ip_bans_path, @admin, params: {:ip_ban => {:ip_addr => "1.2.3.4", :reason => "xyz"}}
|
post_auth ip_bans_path, @admin, params: {:ip_ban => {:ip_addr => "1.2.3.4", :reason => "xyz"}}
|
||||||
|
assert_response :redirect
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "index action" do
|
context "index action" do
|
||||||
setup do
|
|
||||||
as(@admin) do
|
|
||||||
create(:ip_ban)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
should "render" do
|
should "render" do
|
||||||
get_auth ip_bans_path, @admin
|
get_auth ip_bans_path, @admin
|
||||||
assert_response :success
|
assert_response :success
|
||||||
@@ -42,15 +38,10 @@ class IpBansControllerTest < ActionDispatch::IntegrationTest
|
|||||||
end
|
end
|
||||||
|
|
||||||
context "destroy action" do
|
context "destroy action" do
|
||||||
setup do
|
|
||||||
as(@admin) do
|
|
||||||
@ip_ban = create(:ip_ban)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
should "destroy an ip ban" do
|
should "destroy an ip ban" do
|
||||||
assert_difference("IpBan.count", -1) do
|
assert_difference("IpBan.count", -1) do
|
||||||
delete_auth ip_ban_path(@ip_ban), @admin, params: {:format => "js"}
|
delete_auth ip_ban_path(@ip_ban), @admin, params: {:format => "js"}
|
||||||
|
assert_response :success
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user