From 9951869a498dad030756127f18bb857610ae49dc Mon Sep 17 00:00:00 2001 From: Thomas Steen Rasmussen Date: Wed, 11 Apr 2018 10:22:28 +0200 Subject: [PATCH] add a view to re-add IRC ACL if something went wrong the first time, add button on team_detail page to activate it --- src/teams/templates/fix_irc_acl.html | 18 ++++++++ src/teams/templates/team_detail.html | 3 ++ src/teams/urls.py | 5 +++ src/teams/views.py | 63 ++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 src/teams/templates/fix_irc_acl.html diff --git a/src/teams/templates/fix_irc_acl.html b/src/teams/templates/fix_irc_acl.html new file mode 100644 index 00000000..658a432b --- /dev/null +++ b/src/teams/templates/fix_irc_acl.html @@ -0,0 +1,18 @@ +{% extends 'base.html' %} +{% load commonmark %} + +{% block title %} +Fix IRC permissions for NickServ user {{ request.user.profile.nickserv_username }} for IRC channel {{ team.irc_channel_name }} +{% endblock %} + +{% block content %} + +

Fix IRC permissions

+

This will make the bot re-add IRC ACL for your NickServ user {{ request.user.profile.nickserv_username }} for IRC channel {{ team.irc_channel_name }}. Use this in cases where you are unable to join the team IRC channel after entering your NickServ username in your profile.

+
+{% csrf_token %} +{{ form }} + + Cancel +
+{% endblock %} diff --git a/src/teams/templates/team_detail.html b/src/teams/templates/team_detail.html index 690dca3b..6f98a05d 100644 --- a/src/teams/templates/team_detail.html +++ b/src/teams/templates/team_detail.html @@ -70,6 +70,9 @@ Team: {{ team.name }} | {{ block.super }}

Your membership status: {% membershipstatus user team %}

{% if request.user in team.members.all %} + {% if team.irc_channel and team.irc_channel_managed and request.user.profile.nickserv_username %} + Fix IRC ACL  + {% endif %} Leave Team {% else %} {% if team.needs_members %} diff --git a/src/teams/urls.py b/src/teams/urls.py index 28072dbe..42de9a28 100644 --- a/src/teams/urls.py +++ b/src/teams/urls.py @@ -43,6 +43,11 @@ urlpatterns = [ TeamManageView.as_view(), name='manage' ), + url( + r'^fix_irc_acl/$', + FixIrcAclView.as_view(), + name='fix_irc_acl', + ), url( r'^tasks/', include([ url( diff --git a/src/teams/views.py b/src/teams/views.py index b16ed10b..98d29488 100644 --- a/src/teams/views.py +++ b/src/teams/views.py @@ -215,3 +215,66 @@ class TaskUpdateView(LoginRequiredMixin, CampViewMixin, EnsureTeamResponsibleMix def get_success_url(self): return self.get_object().get_absolute_url() + +class FixIrcAclView(LoginRequiredMixin, CampViewMixin, UpdateView): + template_name = "fix_irc_acl.html" + model = Team + fields = [] + slug_url_kwarg = 'team_slug' + + def dispatch(self, request, *args, **kwargs): + # we need to call the super().dispatch() method early so self.camp gets populated by CampViewMixin, + # because the lookups below depend on self.camp being set :) + response = super().dispatch( + request, *args, **kwargs + ) + + # check if the logged in user has an approved membership of this team + if request.user not in self.get_object().approved_members.all(): + messages.error(request, 'No thanks') + return redirect('teams:detail', camp_slug=self.get_object().camp.slug, team_slug=self.get_object().slug) + + # check if we manage the channel for this team + if not self.get_object().irc_channel or not self.get_object().irc_channel_managed: + messages.error(request, 'IRC functionality is disabled for this team, or the team channel is not managed by the bot') + return redirect('teams:detail', camp_slug=self.get_object().camp.slug, team_slug=self.get_object().slug) + + # check if user has a nickserv username + if not request.user.profile.nickserv_username: + messages.error(request, 'Please go to your profile and set your NickServ username first. Make sure the account is registered with NickServ first!') + return redirect('teams:detail', camp_slug=self.get_object().camp.slug, team_slug=self.get_object().slug) + + return response + + def get(self, request, *args, **kwargs): + # get membership + try: + TeamMember.objects.get( + user=request.user, + team=self.get_object(), + approved=True, + irc_channel_acl_ok=True + ) + except TeamMember.DoesNotExist: + # this membership is already marked as membership.irc_channel_acl_ok=False, no need to do anything + messages.error(request, 'No need, this membership is already marked as irc_channel_acl_ok=False, so the bot will fix the ACL soon') + return redirect('teams:detail', camp_slug=self.get_object().camp.slug, team_slug=self.get_object().slug) + + return super().get( + request, *args, **kwargs + ) + + + def form_valid(self, form): + membership = TeamMember.objects.get( + user=self.request.user, + team=self.get_object(), + approved=True, + irc_channel_acl_ok=True + ) + + membership.irc_channel_acl_ok = False + membership.save() + messages.success(self.request, "OK, hang on while we fix the permissions for your NickServ user '%s' for IRC channel '%s'" % (self.request.user.profile.nickserv_username, form.instance.irc_channel_name)) + return redirect('teams:detail', camp_slug=form.instance.camp.slug, team_slug=form.instance.slug) +