def resendSms(request, user_id): url = reverse_lazy('horizon_jiocloud:user_sms_activation', kwargs={'user_id':user_id}) # This method serves re-sending SMS multiple times # to cater to SMS sending failures. # If the OTP is expired, a new one will be generated try: res = keystoneapi.get_user(user_id) if not (res.get("success") or res.get("result")): raise Exception('Unable to get user details') except Exception as e: LOG.exception(e) return http.HttpResponseRedirect(url + "?resentSms=True&resentSuccess=False") user = res.get("result") phone = user.get("phone") sms_activation_code = user.get("sms_activation_code") sms_activation_code_time = user.get("sms_activation_code_time") try: if is_sms_expired(sms_activation_code_time): sms_activation_code = generate_sms_activation_code() data = {"sms_activation_code": sms_activation_code, "sms_activation_code_time": timeutils.strtime()} # push changes to keystone database response = keystoneapi.update_user(user_id, data) generate_and_send_sms(phone, sms_activation_code) except Exception as e: LOG.exception(e) return http.HttpResponseRedirect(url + "?resentSms=True&resentSuccess=False") return http.HttpResponseRedirect(url + "?resentSms=True&resentSuccess=True")
def activate(request, activation_code): new_email = request.GET.get('email', '') try: res = keystoneapi.get_user(request.user.id) if not (res.get("success") or res.get("result")): raise Exception() except Exception as ex: LOG.exception(ex) return render_to_response('change_email/email_change_failure.html') user = res.get("result") email_activation_code = user.get("email_activation_code") email_activation_code_time = user.get("email_activation_code_time") if not new_email or email_activation_code != activation_code or is_sms_expired(email_activation_code_time): return render_to_response('change_email/email_change_failure.html') # codes matched. so we will change the email in database try: # push changes to keystone database if user.get("name") == user.get("email"): response = keystoneapi.update_user(request.user.id, {"name" : new_email, "email" : new_email}) else: response = keystoneapi.update_user(request.user.id, {"email" : new_email}) except Exception as ex: LOG.exception(ex) return render_to_response('change_email/email_change_failure.html') return render_to_response('change_email/email_change_success.html')
def __init__(self, request, *args, **kwargs): super(PhoneForm, self).__init__(request, *args, **kwargs) res = {} self.user = None self.sms_expired = True request.session["show_sms_button"] = None try: # erro handling: when the user details could not be fetched, # show the default error message try: res = keystoneapi.get_user(self.request.user.id) except Exception as ex: LOG.exception(ex) self.set_non_field_errors([self.get_default_error_message()]) del self.fields["sms_activation_code"] return None if not (res.get("success") or res.get("result")): del self.fields["sms_activation_code"] return None self.user = res.get("result") if not kwargs["initial"]["phone"]: # we populate the initial value from request.session in views.py # No intial value implies that the user hasn't yet started the # phone number change process and hasn't keyed-in the new phone # number. So, pre-fill the field with the current phone number # from database kwargs["initial"]["phone"] = self.user.get("phone") sms_activation_code = self.user.get("sms_activation_code") sms_activation_code_time = \ self.user.get("sms_activation_code_time") self.sms_expired = is_sms_expired(sms_activation_code_time) if (not (sms_activation_code and sms_activation_code_time)) or \ self.sms_expired: # empty sms_activation_code => the user hasn't yet started the # phone number change process. # sms_expired => the user has to generate a new OTP. # In either case, remove the sms_activation_code field from form del self.fields["sms_activation_code"] return None else: # the user is in the middle of phone number change. # In some cases, SMS may not have been delivered. # So, set this flag to true. Based on this flag, 'Resend SMS' # button will be displayed in UI. Logic to this effect is # implemented in the template request.session["show_sms_button"] = True except Exception as ex: import traceback traceback.print_exc() LOG.exception(ex) self.set_non_field_errors([self.get_default_error_message()]) return None if not res.get("success"): self.set_non_field_errors([ res.get("error", self.get_default_error_message() ) ]) return None
def handle(self, request, data): res = {} user = None try: code = data.get("code") user_id = data.get("user_id") try: res = keystoneapi.get_user(user_id) except Exception as ex: LOG.exception(ex) self.set_non_field_errors([self.get_default_error_message()]) return None if not res.get("success"): self.set_non_field_errors(["Invalid Account"]) return None user = res.get("result") if not user: self.set_non_field_errors(["Invalid Account"]) return None else: if user.get("enabled"): self.set_non_field_errors(["Account already activated"]) return None else: sms_activation_code = user.get("sms_activation_code") sms_activation_code_time = \ user.get("sms_activation_code_time") if not (sms_activation_code and sms_activation_code_time): self.set_non_field_errors(["Invalid Activation Code"]) return None if sms_activation_code not in [code]: self.set_non_field_errors(["Invalid Activation Code"]) return None if is_sms_expired(sms_activation_code_time): self.set_non_field_errors(["Activation Code Expired. Click 'Resend OTP'"]) return None user_id = user.get("id") if not user_id: self.set_non_field_errors(["Invalid Account"]) return None _data = {"user_id": user_id} try: res = keystoneapi.activate_user(_data) keystoneapi.update_user(user_id, {"sms_activation_code" : ""}) except Exception as ex: LOG.exception(ex) self.set_non_field_errors([self.get_default_error_message()]) return None try: send_registration_email(user, "signup/registration_success_email.html") except Exception as ex: LOG.exception(ex) except Exception as ex: import traceback traceback.print_exc() LOG.exception(ex) self.set_non_field_errors([self.get_default_error_message()]) return None if not res.get("success"): self.set_non_field_errors([ res.get("error", self.get_default_error_message() ) ]) return None return res