示例#1
0
文件: views.py 项目: JioCloud/horizon
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')
示例#2
0
文件: forms.py 项目: JioCloud/horizon
 def handle(self, request, data):
     data.pop('confirm_password')
     user_id = data.pop('user_id')
     code = data.pop('code')
     try:
         res = keystoneapi.get_user(user_id)
         if not (res.get("success") or res.get("result")):
             return None                
     except Exception as e:
         LOG.exception(e)
         return None                
     user = res.get("result")
     # check if the code submitted in the form and that in the 
     # database match
     email_activation_code = user.get("email_activation_code")
     if email_activation_code not in [code]:
         self.set_non_field_errors(["Invalid Activation Code"])
         return None
     # codes matched. so we will change the password
     # in database.
     # email_activation_code no more required so unset it
     # in database.
     data["email_activation_code"] = ""
     try:
         res = keystoneapi.update_user(user_id, data)
     except Exception as e:
         LOG.exception(e)
     return render_to_response('signup/reset_password_success.html') 
示例#3
0
文件: forms.py 项目: JioCloud/horizon
 def handle(self, request, data):
     email = data.get("email")
     try:
         res = keystoneapi.get_user(email)
         if not (res.get("success") or res.get("result")):
             return None                
         user = res.get("result")
         user_id = user.get('id')
         email_activation_code = generate_sms_activation_code()
         data = {"email_activation_code": email_activation_code,
                 "email_activation_code_time": timeutils.strtime()}
         # email confirmation about the change
         send_templated_email("JioCloud account password change confirmation",
             [email],
             html_template="signup/password_change_verification.html",
             template_context={
             "name": user.get("first_name") if user.get("first_name") else user.get("name"),
             "reset_password_path": reverse_lazy('horizon_jiocloud:resetPassword', kwargs={'user_id':user_id, 'code':email_activation_code}),
             "SITE_URL": getattr(settings, "SITE_URL", "http://127.0.0.1"),
             })
         # push changes to keystone database
         response = keystoneapi.update_user(user_id, data)
     except Exception as e:
         LOG.exception(e)
     return render_to_response('signup/forgot_password_success.html', 
             {'email':settings.DEFAULT_FROM_EMAIL, 'user_id': user_id})
示例#4
0
文件: views.py 项目: JioCloud/horizon
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")
示例#5
0
文件: forms.py 项目: JioCloud/horizon
    def handle(self, request, data):
        phone = data.get("phone")
        request.session["phone"] = phone
        if not self.fields.has_key("sms_activation_code"):
            # No "sms_activation_code" field in form
            # So generate OTP to start phone number change process;
            # send SMS containing the OTP and store OTP details
            # in database for later verification
            sms_activation_code = generate_sms_activation_code()
            data = {"sms_activation_code": sms_activation_code,
                    "sms_activation_code_time": timeutils.strtime()}
            try:
                generate_and_send_sms(phone, sms_activation_code)
                messages.success(request,
                                 'Please enter the SMS code to change phone number')
            except Exception as ex:
                LOG.exception(ex)
                self.set_non_field_errors([
                        self.get_default_error_message()
                    ])
                return None
        else:
            # check if the code submitted in the form and that in the 
            # database match
            code = data.get("sms_activation_code")
            sms_activation_code = self.user.get("sms_activation_code")
            if sms_activation_code not in [code]:
                self.set_non_field_errors(["Invalid Activation Code"])
                return None
            # codes matched. so we will change the phone number
            # in database.
            # sms_activation_code no more required so unset it
            # in database.
            data["sms_activation_code"] = ""
            request.session["phone"] = ""
            try:
                # email confirmation about the change
                send_templated_email("JioCloud account phone number changed",
                    [self.user.get("email")],
                    html_template="change_phone/phone_change_cofirmation_email.html",
                    template_context={
                    "name": self.user.get("first_name") if self.user.get("first_name") else self.user.get("name"),
                    "phone": phone,
                    })
                messages.success(request,
                                 'Phone number has been updated successfully.')
            except Exception as ex:
                LOG.exception(ex)

        try:
            # push changes to keystone database
            response = keystoneapi.update_user(self.request.user.id, data)
        except Exception:
            response = exceptions.handle(request, ignore=True)
            messages.error(request, 'Unable to change phone number.')

        return shortcuts.redirect(request.build_absolute_uri())
示例#6
0
文件: forms.py 项目: JioCloud/horizon
    def handle(self, request, data):
        new_email = data.get("email")
        email_activation_code = generate_sms_activation_code()
        data = {"email_activation_code": email_activation_code,
                "email_activation_code_time": timeutils.strtime()}
        try:
            # email confirmation about the change
            send_templated_email("JioCloud account email change confirmation",
                [new_email],
                html_template="change_email/email_change_verification.html",
                template_context={
                "name": self.user.get("first_name") if self.user.get("first_name") else self.user.get("name"),
                "email_activation_path": reverse('horizon:settings:email:index') + 'activate/' + email_activation_code + '?email=' + new_email,
                })
            # push changes to keystone database
            response = keystoneapi.update_user(self.request.user.id, data)
            messages.success(request,
                             'Please click the link sent to your new email to activate it')
        except Exception as ex:
            LOG.exception(ex)
            response = exceptions.handle(request, ignore=True)
            messages.error(request, 'Unable to change email.')

        return shortcuts.redirect(request.build_absolute_uri())
示例#7
0
文件: forms.py 项目: JioCloud/horizon
    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