| 1 |
# Copyright (c) 2016 Catalyst IT Ltd. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
|
| 16 |
from django.conf import settings |
| 17 |
from django import http |
| 18 |
from django.utils.translation import gettext_lazy as _ |
| 19 |
|
| 20 |
from horizon import exceptions |
| 21 |
from horizon import forms |
| 22 |
from horizon.utils import functions as utils |
| 23 |
|
| 24 |
from adjutant_ui.api import adjutant |
| 25 |
from adjutant_ui.content.token import forms as token_forms |
| 26 |
|
| 27 |
|
| 28 |
def _logout_msg_response(request, msg): |
| 29 |
response = http.HttpResponseRedirect(settings.LOGOUT_URL) |
| 30 |
utils.add_logout_reason(request, response, msg) |
| 31 |
return response |
| 32 |
|
| 33 |
|
| 34 |
def _logout_msg_response_success(request, msg): |
| 35 |
response = _logout_msg_response(request, msg) |
| 36 |
response.set_cookie('logout_status', 'success', max_age=10) |
| 37 |
return response |
| 38 |
|
| 39 |
|
| 40 |
def submit_token_router(request, *args, **kwargs): |
| 41 |
"""Routes requests to the correct view, based on submission parameters.""" |
| 42 |
|
| 43 |
# Get details of the token |
| 44 |
token_uuid = kwargs['token'] |
| 45 |
token = adjutant.token_get(request, token_uuid, {}) |
| 46 |
if not token or token.status_code != 200: |
| 47 |
msg = _("Invalid token. Please request another.") |
| 48 |
return _logout_msg_response(request, msg) |
| 49 |
|
| 50 |
json = token.json() |
| 51 |
if 'password' in json['required_fields']: |
| 52 |
return SubmitTokenPasswordView.as_view()(request, *args, **kwargs) |
| 53 |
elif 'confirm' in json['required_fields']: |
| 54 |
|
| 55 |
if 'UpdateUserEmailAction' in json['actions']: |
| 56 |
return UpdateEmailTokenSubmitView.as_view()( |
| 57 |
request, *args, **kwargs) |
| 58 |
return SubmitTokenConfirmView.as_view()(request, *args, **kwargs) |
| 59 |
|
| 60 |
return _logout_msg_response(request, _("Unsupported token type.")) |
| 61 |
|
| 62 |
|
| 63 |
class SubmitTokenPasswordView(forms.ModalFormView): |
| 64 |
form_class = token_forms.PasswordForm |
| 65 |
template_name = 'token/setpassword.html' |
| 66 |
|
| 67 |
def get(self, request, *args, **kwargs): |
| 68 |
sc = super(SubmitTokenPasswordView, self) |
| 69 |
return sc.get(request, *args, **kwargs) |
| 70 |
|
| 71 |
def post(self, request, *args, **kwargs): |
| 72 |
sc = super(SubmitTokenPasswordView, self) |
| 73 |
return sc.post(request, *args, **kwargs) |
| 74 |
|
| 75 |
def form_valid(self, form): |
| 76 |
# All form data has been validated and POSTed. |
| 77 |
# We can now submit the token for processing along with parameters. |
| 78 |
# All codepaths here return a Redirect response. |
| 79 |
parameters = { |
| 80 |
'password': form.cleaned_data['new_password'] |
| 81 |
} |
| 82 |
token_uuid = self.kwargs['token'] |
| 83 |
token_response = adjutant.token_submit(form.request, |
| 84 |
token_uuid, |
| 85 |
parameters) |
| 86 |
|
| 87 |
if token_response.ok: |
| 88 |
msg = _("Password successfully set. Please log in to continue.") |
| 89 |
return _logout_msg_response_success(form.request, msg) |
| 90 |
|
| 91 |
msg = (_("Token form submission failed. Response code %(code)s.") % |
| 92 |
{'code': token_response.status_code}) |
| 93 |
return _logout_msg_response(form.request, msg) |
| 94 |
|
| 95 |
def get_context_data(self, **kwargs): |
| 96 |
sc = super(SubmitTokenPasswordView, self) |
| 97 |
context = sc.get_context_data(**kwargs) |
| 98 |
try: |
| 99 |
context['token'] = self.kwargs['token'] |
| 100 |
except Exception: |
| 101 |
exceptions.handle(self.request) |
| 102 |
return context |
| 103 |
|
| 104 |
|
| 105 |
class SubmitTokenConfirmView(forms.ModalFormView): |
| 106 |
form_class = token_forms.ConfirmForm |
| 107 |
template_name = 'token/tokenconfirm.html' |
| 108 |
success_msg = _("Welcome to the project! Please log in to continue.") |
| 109 |
|
| 110 |
def get(self, request, *args, **kwargs): |
| 111 |
sc = super(SubmitTokenConfirmView, self) |
| 112 |
return sc.get(request, *args, **kwargs) |
| 113 |
|
| 114 |
def post(self, request, *args, **kwargs): |
| 115 |
sc = super(SubmitTokenConfirmView, self) |
| 116 |
return sc.post(request, *args, **kwargs) |
| 117 |
|
| 118 |
def form_valid(self, form): |
| 119 |
token_uuid = self.kwargs['token'] |
| 120 |
parameters = { |
| 121 |
'confirm': True |
| 122 |
} |
| 123 |
token_response = adjutant.token_submit(form.request, |
| 124 |
token_uuid, |
| 125 |
parameters) |
| 126 |
|
| 127 |
if token_response.ok: |
| 128 |
return _logout_msg_response_success(form.request, self.success_msg) |
| 129 |
|
| 130 |
msg = (_("Token form submission failed. " |
| 131 |
"Response code %(code)s.") % {'code': |
| 132 |
token_response.status_code}) |
| 133 |
return _logout_msg_response(form.request, msg) |
| 134 |
|
| 135 |
def get_context_data(self, **kwargs): |
| 136 |
sc = super(SubmitTokenConfirmView, self) |
| 137 |
context = sc.get_context_data(**kwargs) |
| 138 |
try: |
| 139 |
context['token'] = self.kwargs['token'] |
| 140 |
except Exception: |
| 141 |
exceptions.handle(self.request) |
| 142 |
return context |
| 143 |
|
| 144 |
|
| 145 |
class UpdateEmailTokenSubmitView(SubmitTokenConfirmView): |
| 146 |
template_name = 'token/emailtokenconfirm.html' |
| 147 |
success_msg = _("Your email has been updated! Please log in to continue.") |