/
batch_controller.rb
149 lines (141 loc) · 4.15 KB
/
batch_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
class BatchController < ApplicationController
before_action :require_user
def batch_spam
if logged_in_as(%w(moderator admin))
user_spamed = []
node_spamed = 0
params[:ids].split(',').uniq.each do |nid|
node = Node.find nid
node_spamed += 1
node.spam
user = node.author
user_spamed << user.id
user.ban
end
flash[:notice] = node_spamed.to_s + ' nodes spammed and ' + user_spamed.length.to_s + ' users banned.'
redirect_to '/spam2'
else
flash[:error] = 'Only admins and moderators can mark a batch spam.'
redirect_to '/dashboard'
end
end
def batch_publish
if logged_in_as(%w(moderator admin))
node_published = 0
user_published = []
params[:ids].split(',').uniq.each do |nid|
node = Node.find nid
node_published += 1
node.publish
user = node.author
user.unban
user_published << user.id
end
flash[:notice] = node_published.to_s + ' nodes published and ' + user_published.length.to_s + ' users unbanned.'
redirect_to '/spam2'
else
flash[:error] = 'Only admins and moderators can batch publish.'
redirect_to '/dashboard'
end
end
def batch_delete
if logged_in_as(%w(moderator admin))
node_delete = 0
params[:ids].split(',').uniq.each do |nid|
node = Node.find nid
node_delete += 1
node.delete
end
flash[:notice] = node_delete.to_s + ' nodes deleted'
redirect_back fallback_location: root_path
else
flash[:error] = 'Only admins and moderators can batch delete.'
redirect_to '/dashboard'
end
end
def batch_ban
if logged_in_as(%w(admin moderator))
user_ban = []
params[:ids].split(',').uniq.each do |nid|
node = Node.find nid
user = node.author
user_ban << user.id
user.ban
end
flash[:notice] = user_ban.length.to_s + ' users banned.'
redirect_back fallback_location: root_path
else
flash[:error] = 'Only admins and moderators can ban users.'
redirect_to '/dashboard'
end
end
def batch_unban
unbanned_users = []
if logged_in_as(%w(moderator admin))
params[:ids].split(',').uniq.each do |node_id|
node = Node.find node_id
user_unban = node.author
unbanned_users << user_unban.id
user_unban.unban
end
flash[:notice] = unbanned_users.length.to_s + ' users unbanned.'
redirect_back fallback_location: root_path
else
flash[:error] = 'Only admins and moderators can unban users.'
redirect_to '/dashboard'
end
end
def batch_ban_user
users = []
if logged_in_as(%w(admin moderator))
params[:ids].split(',').uniq.each do |user_id|
user_ban = User.find user_id
users << user_ban.id
user_ban.ban
end
flash[:notice] = users.length.to_s + ' users banned.'
redirect_back fallback_location: root_path
else
flash[:error] = 'Only moderators can moderate users.'
redirect_to '/dashboard'
end
end
def batch_unban_user
if logged_in_as(%w(moderator admin))
params[:ids].split(',').uniq.each do |id|
unban_user = User.find id
unban_user.unban
end
flash[:notice] = 'Success! users unbanned.'
redirect_back fallback_location: root_path
else
flash[:error] = 'Only admins and moderators can moderate users.'
redirect_to '/dashboard'
end
end
def batch_comment
if logged_in_as(%w(moderator admin))
comment_total = 0
params[:ids].split(',').uniq.each do |cid|
comment = Comment.find cid
comment_total += 1
case params[:type]
when 'publish'
comment.publish
when 'spam'
comment.spam
user.ban
when 'delete'
comment.delete
else
flash[:notice] = 'Invalid Url'
end
end
flash[:notice] = comment_total.to_s + ' comment moderated'
redirect_back fallback_location: root_path
else
flash[:error] = 'Only admins and moderators can moderate comments.'
redirect_to '/dashboard'
end
end
end