user.py 21.5 KB
Newer Older
Őry Máté committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# Copyright 2014 Budapest University of Technology and Economics (BME IK)
#
# This file is part of CIRCLE Cloud.
#
# CIRCLE is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# CIRCLE is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with CIRCLE.  If not, see <http://www.gnu.org/licenses/>.
from __future__ import unicode_literals, absolute_import

import json
import logging

22 23
import pyotp

Őry Máté committed
24 25 26 27 28 29 30
from django.conf import settings
from django.contrib import messages
from django.contrib.auth import login
from django.contrib.auth.models import User, Group
from django.contrib.auth.views import login as login_view
from django.contrib.messages.views import SuccessMessageMixin
from django.core import signing
31
from django.core.exceptions import PermissionDenied, SuspiciousOperation
Őry Máté committed
32
from django.core.urlresolvers import reverse, reverse_lazy
33
from django.core.paginator import Paginator, InvalidPage
34
from django.db.models import Q
Őry Máté committed
35 36
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import redirect, get_object_or_404
37
from django.templatetags.static import static
Őry Máté committed
38 39 40
from django.utils.translation import ugettext as _
from django.views.decorators.http import require_POST
from django.views.generic import (
41
    TemplateView, View, UpdateView, CreateView,
Őry Máté committed
42 43 44 45 46
)
from django_sshkey.models import UserKey

from braces.views import LoginRequiredMixin, PermissionRequiredMixin

47 48
from django_tables2 import SingleTableView

Őry Máté committed
49 50 51 52 53
from vm.models import Instance, InstanceTemplate

from ..forms import (
    CircleAuthenticationForm, MyProfileForm, UserCreationForm, UnsubscribeForm,
    UserKeyForm, CirclePasswordChangeForm, ConnectCommandForm,
54
    UserListSearchForm, UserEditForm, TwoFactorForm, DisableTwoFactorForm
Őry Máté committed
55
)
56
from ..models import Profile, GroupProfile, ConnectCommand
57 58 59
from ..tables import (
    UserKeyListTable, ConnectCommandListTable, UserListTable,
)
Őry Máté committed
60

61
from .util import saml_available, DeleteViewBase
Őry Máté committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77


logger = logging.getLogger(__name__)


class NotificationView(LoginRequiredMixin, TemplateView):

    def get_template_names(self):
        if self.request.is_ajax():
            return ['dashboard/_notifications-timeline.html']
        else:
            return ['dashboard/notifications.html']

    def get_context_data(self, *args, **kwargs):
        context = super(NotificationView, self).get_context_data(
            *args, **kwargs)
78 79 80 81 82 83 84 85 86 87 88 89
        paginate_by = 10 if self.request.is_ajax() else 25
        page = self.request.GET.get("page", 1)

        notifications = self.request.user.notification_set.all()
        paginator = Paginator(notifications, paginate_by)
        try:
            current_page = paginator.page(page)
        except InvalidPage:
            current_page = paginator.page(1)

        context['page'] = current_page
        context['paginator'] = paginator
Őry Máté committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
        return context

    def get(self, *args, **kwargs):
        response = super(NotificationView, self).get(*args, **kwargs)
        un = self.request.user.notification_set.filter(status="new")
        for u in un:
            u.status = "read"
            u.save()
        return response


def circle_login(request):
    authentication_form = CircleAuthenticationForm
    extra_context = {
        'saml2': saml_available,
105 106
        'og_image': (settings.DJANGO_URL.rstrip("/") +
                     static("dashboard/img/og.png"))
Őry Máté committed
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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    }
    response = login_view(request, authentication_form=authentication_form,
                          extra_context=extra_context)
    set_language_cookie(request, response)
    return response


class TokenLogin(View):

    token_max_age = 120  # seconds

    @classmethod
    def get_salt(cls):
        return unicode(cls)

    @classmethod
    def get_token(cls, user, sudoer):
        return signing.dumps((sudoer.pk, user.pk),
                             salt=cls.get_salt(), compress=True)

    @classmethod
    def get_token_url(cls, user, sudoer):
        key = cls.get_token(user, sudoer)
        return reverse("dashboard.views.token-login", args=(key, ))

    def get(self, request, token, *args, **kwargs):
        try:
            data = signing.loads(token, salt=self.get_salt(),
                                 max_age=self.token_max_age)
            logger.debug('TokenLogin token data: %s', unicode(data))
            sudoer, user = data
            logger.debug('Extracted TokenLogin data: sudoer: %s, user: %s',
                         unicode(sudoer), unicode(user))
        except (signing.BadSignature, ValueError, TypeError) as e:
            logger.warning('Tried invalid TokenLogin token. '
                           'Token: %s, user: %s. %s',
                           token, unicode(self.request.user), unicode(e))
            raise SuspiciousOperation()
        sudoer = User.objects.get(pk=sudoer)
        if not sudoer.is_superuser:
            raise PermissionDenied()
        user = User.objects.get(pk=user)
        user.backend = 'django.contrib.auth.backends.ModelBackend'
        logger.warning('%s %d logged in as user %s %d',
                       unicode(sudoer), sudoer.pk, unicode(user), user.pk)
        login(request, user)
        messages.info(request, _("Logged in as user %s.") % unicode(user))
        return redirect("/")


class MyPreferencesView(UpdateView):
    model = Profile

    def get_context_data(self, *args, **kwargs):
        context = super(MyPreferencesView, self).get_context_data(*args,
                                                                  **kwargs)
        context['forms'] = {
            'change_password': CirclePasswordChangeForm(
                user=self.request.user),
            'change_language': MyProfileForm(instance=self.get_object()),
        }
        key_table = UserKeyListTable(
            UserKey.objects.filter(user=self.request.user),
            request=self.request)
        key_table.page = None
        context['userkey_table'] = key_table
        cmd_table = ConnectCommandListTable(
            self.request.user.command_set.all(),
            request=self.request)
        cmd_table.page = None
        context['connectcommand_table'] = cmd_table
        return context

    def get_object(self, queryset=None):
        if self.request.user.is_anonymous():
            raise PermissionDenied()
        try:
            return self.request.user.profile
        except Profile.DoesNotExist:
            raise Http404(_("You don't have a profile."))

    def post(self, request, *args, **kwargs):
        self.ojbect = self.get_object()
        redirect_response = HttpResponseRedirect(
            reverse("dashboard.views.profile-preferences"))
        if "preferred_language" in request.POST:
            form = MyProfileForm(request.POST, instance=self.get_object())
            if form.is_valid():
                lang = form.cleaned_data.get("preferred_language")
                set_language_cookie(self.request, redirect_response, lang)
                form.save()
        else:
            form = CirclePasswordChangeForm(user=request.user,
                                            data=request.POST)
            if form.is_valid():
                form.save()
203 204
                messages.success(self.request,
                                 _("Password successfully changed."))
Őry Máté committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280

        if form.is_valid():
            return redirect_response
        else:
            return self.get(request, form=form, *args, **kwargs)

    def get(self, request, form=None, *args, **kwargs):
        # if this is not here, it won't work
        self.object = self.get_object()
        context = self.get_context_data(*args, **kwargs)
        if form is not None:
            # a little cheating, users can't post invalid
            # language selection forms (without modifying the HTML)
            context['forms']['change_password'] = form
        return self.render_to_response(context)


class UnsubscribeFormView(SuccessMessageMixin, UpdateView):
    model = Profile
    form_class = UnsubscribeForm
    template_name = "dashboard/unsubscribe.html"
    success_message = _("Successfully modified subscription.")

    def get_success_url(self):
        if self.request.user.is_authenticated():
            return super(UnsubscribeFormView, self).get_success_url()
        else:
            return self.request.path

    @classmethod
    def get_salt(cls):
        return unicode(cls)

    @classmethod
    def get_token(cls, user):
        return signing.dumps(user.pk, salt=cls.get_salt(), compress=True)

    def get_object(self, queryset=None):
        key = self.kwargs['token']
        try:
            pk = signing.loads(key, salt=self.get_salt(), max_age=48 * 3600)
        except signing.SignatureExpired:
            raise
        except (signing.BadSignature, ValueError, TypeError) as e:
            logger.warning('Tried invalid token. Token: %s, user: %s. %s',
                           key, unicode(self.request.user), unicode(e))
            raise Http404
        else:
            return (queryset or self.get_queryset()).get(user_id=pk)

    def dispatch(self, request, *args, **kwargs):
        try:
            return super(UnsubscribeFormView, self).dispatch(
                request, *args, **kwargs)
        except signing.SignatureExpired:
            return redirect('dashboard.views.profile-preferences')


def set_language_cookie(request, response, lang=None):
    if lang is None:
        try:
            lang = request.user.profile.preferred_language
        except:
            return

    cname = getattr(settings, 'LANGUAGE_COOKIE_NAME', 'django_language')
    response.set_cookie(cname, lang, 365 * 86400)


class UserCreationView(LoginRequiredMixin, PermissionRequiredMixin,
                       CreateView):
    form_class = UserCreationForm
    model = User
    template_name = 'dashboard/user-create.html'
    permission_required = "auth.add_user"

281 282 283 284 285 286 287 288 289 290 291 292
    def get_template_names(self):
        return ['dashboard/nojs-wrapper.html']

    def get_context_data(self, *args, **kwargs):
        context = super(UserCreationView, self).get_context_data(*args,
                                                                 **kwargs)
        context.update({
            'template': self.template_name,
            'box_title': _('Create a User'),
        })
        return context

Bach Dániel committed
293
    def get_success_url(self):
294 295 296 297 298 299 300 301 302 303 304 305
        return reverse('dashboard.views.profile', args=[self.object.username])

    def get_form_kwargs(self):
        profiles = GroupProfile.get_objects_with_level(
            'owner', self.request.user)
        choices = Group.objects.filter(groupprofile__in=profiles)
        group_pk = self.request.GET.get('group_pk')
        if group_pk:
            try:
                default = choices.get(pk=group_pk)
            except (ValueError, Group.DoesNotExist):
                raise Http404()
Őry Máté committed
306
        else:
307 308 309 310 311
            default = None

        val = super(UserCreationView, self).get_form_kwargs()
        val.update({'choices': choices, 'default': default})
        return val
Őry Máté committed
312 313


314
class ProfileView(LoginRequiredMixin, SuccessMessageMixin, UpdateView):
Őry Máté committed
315 316 317 318
    template_name = "dashboard/profile.html"
    model = User
    slug_field = "username"
    slug_url_kwarg = "username"
319 320
    form_class = UserEditForm
    success_message = _("Successfully modified user.")
Őry Máté committed
321

322 323 324 325 326 327 328 329 330 331 332 333 334 335
    def get(self, *args, **kwargs):
        user = self.request.user
        target = self.get_object()

        # get the list of groups where the user is operator
        user_g_w_op = GroupProfile.get_objects_with_level("operator", user)
        # get the list of groups the "target" (the profile) is member of
        target_groups = GroupProfile.objects.filter(
            group__in=target.groups.all())
        intersection = set(user_g_w_op).intersection(target_groups)

        # if the intersection of the 2 lists is empty the logged in user
        # has no permission to check the target's profile
        # (except if the user want to see his own profile)
336
        if not intersection and target != user and not user.is_superuser:
337 338 339 340
            raise PermissionDenied

        return super(ProfileView, self).get(*args, **kwargs)

Őry Máté committed
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
    def get_context_data(self, **kwargs):
        context = super(ProfileView, self).get_context_data(**kwargs)
        user = self.get_object()
        context['profile'] = user
        context['avatar_url'] = user.profile.get_avatar_url()
        context['instances_owned'] = Instance.get_objects_with_level(
            "owner", user, disregard_superuser=True).filter(destroyed_at=None)
        context['instances_with_access'] = Instance.get_objects_with_level(
            "user", user, disregard_superuser=True
        ).filter(destroyed_at=None).exclude(pk__in=context['instances_owned'])

        group_profiles = GroupProfile.get_objects_with_level(
            "operator", self.request.user)
        groups = Group.objects.filter(groupprofile__in=group_profiles)
        context['groups'] = user.groups.filter(pk__in=groups)

        # permissions
        # show groups only if the user is superuser, or have access
        # to any of the groups the user belongs to
        context['perm_group_list'] = (
            self.request.user.is_superuser or len(context['groups']) > 0)
        context['perm_email'] = (
            context['perm_group_list'] or self.request.user == user)

        # filter the virtual machine list
        # if the logged in user is not superuser or not the user itself
        # filter the list so only those virtual machines are shown that are
        # originated from templates the logged in user is operator or higher
        if not (self.request.user.is_superuser or self.request.user == user):
            it = InstanceTemplate.get_objects_with_level("operator",
                                                         self.request.user)
            context['instances_owned'] = context['instances_owned'].filter(
                template__in=it)
            context['instances_with_access'] = context[
                'instances_with_access'].filter(template__in=it)
        if self.request.user.is_superuser:
            context['login_token'] = TokenLogin.get_token_url(
                user, self.request.user)
        return context

381 382 383 384 385 386 387 388 389
    def post(self, request, *args, **kwargs):
        if not request.user.has_perm('auth.change_user'):
            raise PermissionDenied()
        return super(ProfileView, self).post(self, request, *args, **kwargs)

    def get_success_url(self):
        return reverse('dashboard.views.profile',
                       kwargs=self.kwargs)

Őry Máté committed
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430

@require_POST
def toggle_use_gravatar(request, **kwargs):
    user = get_object_or_404(User, username=kwargs['username'])
    if not request.user == user:
        raise PermissionDenied()

    profile = user.profile
    profile.use_gravatar = not profile.use_gravatar
    profile.save()

    new_avatar_url = user.profile.get_avatar_url()
    return HttpResponse(
        json.dumps({'new_avatar_url': new_avatar_url}),
        content_type="application/json",
    )


class UserKeyDetail(LoginRequiredMixin, SuccessMessageMixin, UpdateView):
    model = UserKey
    template_name = "dashboard/userkey-edit.html"
    form_class = UserKeyForm
    success_message = _("Successfully modified SSH key.")

    def get(self, request, *args, **kwargs):
        object = self.get_object()
        if object.user != request.user:
            raise PermissionDenied()
        return super(UserKeyDetail, self).get(request, *args, **kwargs)

    def get_success_url(self):
        return reverse_lazy("dashboard.views.userkey-detail",
                            kwargs=self.kwargs)

    def post(self, request, *args, **kwargs):
        object = self.get_object()
        if object.user != request.user:
            raise PermissionDenied()
        return super(UserKeyDetail, self).post(self, request, args, kwargs)


431
class UserKeyDelete(DeleteViewBase):
Őry Máté committed
432
    model = UserKey
433
    success_message = _("SSH key successfully deleted.")
Őry Máté committed
434 435 436 437

    def get_success_url(self):
        return reverse("dashboard.views.profile-preferences")

438 439
    def check_auth(self):
        if self.get_object().user != self.request.user:
Őry Máté committed
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
            raise PermissionDenied()


class UserKeyCreate(LoginRequiredMixin, SuccessMessageMixin, CreateView):
    model = UserKey
    form_class = UserKeyForm
    template_name = "dashboard/userkey-create.html"
    success_message = _("Successfully created a new SSH key.")

    def get_success_url(self):
        return reverse_lazy("dashboard.views.profile-preferences")

    def get_form_kwargs(self):
        kwargs = super(UserKeyCreate, self).get_form_kwargs()
        kwargs['user'] = self.request.user
        return kwargs


class ConnectCommandDetail(LoginRequiredMixin, SuccessMessageMixin,
                           UpdateView):
    model = ConnectCommand
    template_name = "dashboard/connect-command-edit.html"
    form_class = ConnectCommandForm
    success_message = _("Successfully modified command template.")

    def get(self, request, *args, **kwargs):
        object = self.get_object()
        if object.user != request.user:
            raise PermissionDenied()
        return super(ConnectCommandDetail, self).get(request, *args, **kwargs)

    def get_success_url(self):
        return reverse_lazy("dashboard.views.connect-command-detail",
                            kwargs=self.kwargs)

    def post(self, request, *args, **kwargs):
        object = self.get_object()
        if object.user != request.user:
            raise PermissionDenied()
        return super(ConnectCommandDetail, self).post(request, args, kwargs)

    def get_form_kwargs(self):
        kwargs = super(ConnectCommandDetail, self).get_form_kwargs()
        kwargs['user'] = self.request.user
        return kwargs


487
class ConnectCommandDelete(DeleteViewBase):
Őry Máté committed
488
    model = ConnectCommand
489
    success_message = _("Command template successfully deleted.")
Őry Máté committed
490 491 492 493

    def get_success_url(self):
        return reverse("dashboard.views.profile-preferences")

494 495
    def check_auth(self):
        if self.get_object().user != self.request.user:
Őry Máté committed
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
            raise PermissionDenied()


class ConnectCommandCreate(LoginRequiredMixin, SuccessMessageMixin,
                           CreateView):
    model = ConnectCommand
    form_class = ConnectCommandForm
    template_name = "dashboard/connect-command-create.html"
    success_message = _("Successfully created a new command template.")

    def get_success_url(self):
        return reverse_lazy("dashboard.views.profile-preferences")

    def get_form_kwargs(self):
        kwargs = super(ConnectCommandCreate, self).get_form_kwargs()
        kwargs['user'] = self.request.user
        return kwargs
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533


class UserList(LoginRequiredMixin, PermissionRequiredMixin, SingleTableView):
    template_name = "dashboard/user-list.html"
    permission_required = "auth.change_user"
    model = User
    table_class = UserListTable
    table_pagination = True

    def get_context_data(self, *args, **kwargs):
        context = super(UserList, self).get_context_data(*args, **kwargs)
        context['search_form'] = self.search_form
        return context

    def get(self, *args, **kwargs):
        self.search_form = UserListSearchForm(self.request.GET)
        self.search_form.full_clean()

        if self.request.is_ajax():
            users = [
                {'url': reverse("dashboard.views.profile", args=[i.username]),
534 535 536
                 'name': i.get_full_name() or i.username,
                 'org_id': i.profile.org_id,
                 }
537 538 539 540 541 542 543 544 545
                for i in self.get_queryset()]
            return HttpResponse(
                json.dumps(users), content_type="application/json")
        else:
            return super(UserList, self).get(*args, **kwargs)

    def get_queryset(self):
        logger.debug('UserList.get_queryset() called. User: %s',
                     unicode(self.request.user))
546
        qs = User.objects.all().order_by("-pk")
547 548 549

        q = self.search_form.cleaned_data.get('s')
        if q:
550 551
            filters = (Q(username__icontains=q) | Q(email__icontains=q) |
                       Q(profile__org_id__icontains=q))
552 553 554 555 556 557
            for w in q.split()[:3]:
                filters |= (
                    Q(first_name__icontains=w) | Q(last_name__icontains=w))
            qs = qs.filter(filters)

        return qs.select_related("profile")
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591


class EnableTwoFactorView(LoginRequiredMixin, UpdateView):
    model = Profile
    form_class = TwoFactorForm
    template_name = "dashboard/enable-two-factor.html"
    success_url = reverse_lazy("dashboard.views.profile-preferences")

    def get_object(self, queryset=None):
        if self.request.user.is_anonymous():
            raise PermissionDenied

        return self.request.user.profile

    def get_context_data(self, **kwargs):
        ctx = super(EnableTwoFactorView, self).get_context_data(**kwargs)
        random_base32 = pyotp.random_base32()
        ctx['uri'] = pyotp.TOTP(random_base32).provisioning_uri(
            self.request.user.username, issuer_name=settings.TWO_FACTOR_ISSUER)
        ctx['secret'] = random_base32
        return ctx


class DisableTwoFactorView(LoginRequiredMixin, UpdateView):
    model = Profile
    form_class = DisableTwoFactorForm
    template_name = "dashboard/disable-two-factor.html"
    success_url = reverse_lazy("dashboard.views.profile-preferences")

    def get_object(self, queryset=None):
        if self.request.user.is_anonymous():
            raise PermissionDenied

        return self.request.user.profile