Esempio n. 1
0
 def _to_python(self, value, user):
     'Check whether the value is unique'
     if not Users.check_password(
             email=get_current_request().authenticated_userid,
             password=value):
         raise Invalid('Your old password doesn\'t match', value, user)
     # Return
     return value
Esempio n. 2
0
    def login(self):
        title = "Login"
        login_url = self.request.route_url('login')
        referrer = self.request.url
        if referrer == login_url:
            referrer = '/myaccount'  # never use the login form itself as came_from
        came_from = self.request.params.get('came_from', referrer)
        message = ''
        error_cls = ''
        form = Form(self.request, schema=LoginForm)

        if 'form_submitted' in self.request.POST and form.validate():
            '''
            try:
                resp = self.request.POST['g-recaptcha-response']
            except:
                message="Slow network, please solve the challenge"
                return dict(title=title,
                            message = message,
                            form =FormRenderer(form),
                            error_cls = error_cls,
                            url = self.request.application_url + '/login',
                            came_from = came_from)

            recaptcha_r = requests.post(recaptcha_endpoint, dict(secret=buddy_settings('recaptcha_secret'),
                                                                 response=resp))
            rdata = recaptcha_r.json()
            if rdata:
                if not rdata['success']:
                    message="Failed recaptcha, please solve the challenge"
                    error_cls = 'has-error'
                    return dict(title=title,
                                message = message,
                                form =FormRenderer(form),
                                error_cls = error_cls,
                                url = self.request.application_url + '/login',
                                came_from = came_from)
            '''
            email = form.data['email']
            password = form.data['password']
            user = Users.get_by_email(email)
            if Users.check_password(email, password):
                headers = buddy_remember(self.request, user)

                return HTTPFound(location=came_from, headers=headers)
            message = 'Failed login, incorrect email or password, Please try again'
            error_cls = 'has-error'
        return dict(title=title,
                    message=message,
                    form=FormRenderer(form),
                    error_cls=error_cls,
                    url=self.request.application_url + '/login',
                    came_from=came_from)