Skip to content

Commit

Permalink
Fix massban admin tool and display IPs on admin users list
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalp committed Jun 10, 2018
1 parent 0e81193 commit 5e5f660
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 14 deletions.
8 changes: 8 additions & 0 deletions misago/templates/misago/admin/users/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<th style="width: 1%;">&nbsp;</th>
<th style="width: 1%;">&nbsp;</th>
<th style="width: 200px;">{% trans "E-mail" %}</th>
<th style="width: 128px;">{% trans "IP Address" %}</th>
<th style="width: 150px;">{% trans "Rank" %}</th>
<th style="width: 200px;">{% trans "Joined" %}</th>
<th style="width: 128px;">{% trans "Posts" %}</th>
Expand Down Expand Up @@ -70,6 +71,13 @@
<td>
<a href="mailto:{{ item.email }}">{{ item.email }}</a>
</td>
<td>
{% if item.joined_from_ip %}
{{ item.joined_from_ip }}
{% else %}
<i>{% trans "IP removed" %}</i>
{% endif %}
</td>
<td>
{{ item.rank }}
</td>
Expand Down
28 changes: 20 additions & 8 deletions misago/users/forms/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,14 +460,7 @@ class BanUsersForm(forms.Form):
ban_type = forms.MultipleChoiceField(
label=_("Values to ban"),
widget=forms.CheckboxSelectMultiple,
choices=[
('usernames', _('Usernames')),
('emails', _('E-mails')),
('domains', _('E-mail domains')),
('ip', _('IP addresses')),
('ip_first', _('First segment of IP addresses')),
('ip_two', _('First two segments of IP addresses')),
]
choices=[]
)
user_message = forms.CharField(
label=_("User message"),
Expand Down Expand Up @@ -495,6 +488,25 @@ class BanUsersForm(forms.Form):
help_text=_("Leave this field empty for set bans to never expire.")
)

def __init__(self, *args, **kwargs):
users = kwargs.pop('users')

super(BanUsersForm, self).__init__(*args, **kwargs)

self.fields['ban_type'].choices = [
('usernames', _('Usernames')),
('emails', _('E-mails')),
('domains', _('E-mail domains')),
]

enable_ip_bans = list(filter(None, [u.joined_from_ip for u in users]))
if enable_ip_bans:
self.fields['ban_type'].choices += [
('ip', _('IP addresses')),
('ip_first', _('First segment of IP addresses')),
('ip_two', _('First two segments of IP addresses')),
]


class BanForm(forms.ModelForm):
check_type = forms.TypedChoiceField(
Expand Down
40 changes: 39 additions & 1 deletion misago/users/tests/test_useradmin_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,45 @@ def test_mass_ban(self):
'selected_items': user_pks,
}
)
self.assertEqual(response.status_code, 200)
self.assertNotContains(response, 'value="ip"')
self.assertNotContains(response, 'value="ip_first"')
self.assertNotContains(response, 'value="ip_two"')

response = self.client.post(
reverse('misago:admin:users:accounts:index'),
data={
'action': 'ban',
'selected_items': user_pks,
'ban_type': ['usernames', 'emails', 'domains'],
'finalize': '',
},
)
self.assertEqual(response.status_code, 302)
self.assertEqual(Ban.objects.count(), 21)

def test_mass_ban_with_ips(self):
"""users list bans multiple users that also have ips"""
user_pks = []
for i in range(10):
test_user = UserModel.objects.create_user(
'Bob%s' % i,
'bob%s@test.com' % i,
'pass123',
joined_from_ip='73.95.67.27',
requires_activation=1,
)
user_pks.append(test_user.pk)

response = self.client.post(
reverse('misago:admin:users:accounts:index'),
data={
'action': 'ban',
'selected_items': user_pks,
}
)
self.assertContains(response, 'value="ip"')
self.assertContains(response, 'value="ip_first"')
self.assertContains(response, 'value="ip_two"')

response = self.client.post(
reverse('misago:admin:users:accounts:index'),
Expand Down
12 changes: 7 additions & 5 deletions misago/users/views/admin/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ def action_ban(self, request, users):
mesage = message % {'user': user.username}
raise generic.MassActionError(mesage)

form = BanUsersForm()
form = BanUsersForm(users=users)
if 'finalize' in request.POST:
form = BanUsersForm(request.POST)
form = BanUsersForm(request.POST, users=users)
if form.is_valid():
cleaned_data = form.cleaned_data
banned_values = []
Expand All @@ -141,6 +141,8 @@ def action_ban(self, request, users):

for user in users:
for ban in cleaned_data['ban_type']:
banned_value = None

if ban == 'usernames':
check_type = Ban.USERNAME
banned_value = user.username.lower()
Expand All @@ -155,11 +157,11 @@ def action_ban(self, request, users):
at_pos = banned_value.find('@')
banned_value = '*%s' % banned_value[at_pos:]

if ban == 'ip':
if ban == 'ip' and user.joined_from_ip:
check_type = Ban.IP
banned_value = user.joined_from_ip

if ban in ('ip_first', 'ip_two'):
if ban in ('ip_first', 'ip_two') and user.joined_from_ip:
check_type = Ban.IP

if ':' in user.joined_from_ip:
Expand All @@ -174,7 +176,7 @@ def action_ban(self, request, users):
formats = (bits[0], ip_separator, bits[1], ip_separator)
banned_value = '%s*' % (''.join(formats))

if banned_value not in banned_values:
if banned_value and banned_value not in banned_values:
ban_kwargs.update({
'check_type': check_type,
'banned_value': banned_value,
Expand Down

0 comments on commit 5e5f660

Please sign in to comment.