Esempio n. 1
0
    def login(self):
        from uliweb.contrib.auth import login

        form = functions.get_form('auth.LoginForm')()

        if request.user:
            next = request.GET.get('next','/admin')
            if next:
                return redirect(next)

        if request.method == 'GET':
            form.next.data = request.GET.get('next', request.referrer or '/')
            return {'form':form, 'msg':''}
        if request.method == 'POST':
            flag = form.validate(request.params)
            if flag:
                f, d = functions.authenticate(username=form.username.data, password=form.password.data)
                if f:
                    request.session.remember = form.rememberme.data
                    login(form.username.data)
                    next = urllib.unquote(request.POST.get('next', '/admin'))
                    return redirect(next)
                else:
                    form.errors.update(d)
            msg = form.errors.get('_', '') or _('Login failed!')
            return {'msg':str(msg)}
Esempio n. 2
0
def register():
    from uliweb.contrib.auth import create_user, login

    form = functions.get_form('auth.RegisterForm')()

    if request.method == 'GET':
        form.next.data = request.GET.get('next', add_prefix('/'))
        return {'form':form, 'msg':''}
    if request.method == 'POST':
        flag = form.validate(request.params)
        if flag:
            from uliweb import settings
            f, d = create_user(username=form.username.data,
                password=form.password.data,
                auth_type=settings.AUTH.AUTH_TYPE_DEFAULT)
            if f:
                #add auto login support 2012/03/23
                login(d)
                next = urllib.unquote(request.POST.get('next', add_prefix('/')))
                return redirect(next)
            else:
                form.errors.update(d)

        msg = form.errors.get('_', '') or _('Register failed!')
        return {'form':form, 'msg':str(msg)}
Esempio n. 3
0
def login():
    from uliweb.contrib.auth import login

    form = functions.get_form('auth.LoginForm')()

    if request.user:
        next = request.values.get('next')
        if next:
            return redirect(next)

    next = request.values.get('next')
    if not next:
        next = request.referrer
        if not next or (next and next.endswith('/login')):
            next = add_prefix('/')
    if request.method == 'GET':
        form.next.data = next
        return {}
    if request.method == 'POST':
        flag = form.validate(request.params)
        if flag:
            f, d = functions.authenticate(username=form.username.data, password=form.password.data)
            if f:
                request.session.remember = form.rememberme.data
                login(form.username.data)
                next = urllib.unquote(next)
                return redirect(next)
            else:
                form.errors.update(d)
        if request.is_xhr:
            return json({'success':False, 'errors':form.errors})
        else:
            msg = form.errors.get('_', '') or _('Login failed!')
            return {'form':form, 'msg':str(msg)}
Esempio n. 4
0
def register():
    from uliweb.contrib.auth import create_user, login

    form = functions.get_form('auth.RegisterForm')()

    if request.method == 'GET':
        form.next.data = request.GET.get('next', add_prefix('/'))
        return {'form':form, 'msg':''}
    if request.method == 'POST':
        flag = form.validate(request.params)
        if flag:
            from uliweb import settings
            f, d = create_user(username=form.username.data,
                password=form.password.data,
                auth_type=settings.AUTH.AUTH_TYPE_DEFAULT)
            if f:
                #add auto login support 2012/03/23
                login(d)
                next = urllib.unquote(request.POST.get('next', add_prefix('/')))
                return redirect(next)
            else:
                form.errors.update(d)

        msg = form.errors.get('_', '') or _('Register failed!')
        return {'form':form, 'msg':str(msg)}
Esempio n. 5
0
def login():
    from uliweb.contrib.auth import login

    form = functions.get_form('auth.LoginForm')()

    if request.user:
        next = request.values.get('next')
        if next:
            return redirect(next)

    next = request.values.get('next')
    if not next:
        next = add_prefix('/')
    if request.method == 'GET':
        form.next.data = next
        return {'next': next}
    if request.method == 'POST':
        flag = form.validate(request.values)
        if flag:
            f, d = functions.authenticate(username=form.username.data, password=form.password.data)
            if f:
                request.session.remember = form.rememberme.data
                login(form.username.data)
                next = unquote(next)
                return redirect(next)
            else:
                form.errors.update(d)
        if request.is_xhr:
            return json({'success': False, '_': 'Login Failed', 'errors': form.errors})
        else:
            msg = form.errors.get('_', '') or _('Login failed!')
            return {'form': form, 'msg': str(msg)}
Esempio n. 6
0
def login():
    from uliweb.contrib.auth import login

    form = functions.get_form('auth.LoginForm')()

    if request.user:
        next = request.GET.get('next')
        if next:
            return redirect(next)

    if request.method == 'GET':
        form.next.data = request.GET.get('next', request.referrer
                                         or add_prefix('/'))
        return {'form': form, 'msg': ''}

    if request.method == 'POST':
        flag = form.validate(request.params)
        if flag:
            f, d = functions.authenticate(username=form.username.data,
                                          password=form.password.data)
            if f:
                request.session.remember = form.rememberme.data
                login(form.username.data)
                next = unquote(request.POST.get('next', add_prefix('/')))
                return redirect(next)
            else:
                form.errors.update(d)
        msg = form.errors.get('_', '') or _('Login failed!')
        return {'form': form, 'msg': str(msg)}
Esempio n. 7
0
def login():
    from uliweb.contrib.auth import authenticate, login
    from forms import LoginForm
    from uliweb.form import Submit, Tag
    
    LoginForm.form_buttons = [Submit(value=_('Login'), _class="button")]
    
    form = LoginForm()
    
    if request.method == 'GET':
        form.next.data = request.GET.get('next', '/')
        return {'form':form, 'msg':''}
    if request.method == 'POST':
        flag = form.validate(request.params)
        if flag:
            f, d = authenticate(username=form.username.data, password=form.password.data)
            if f:
                request.session.remember = form.rememberme.data
                login(form.username.data)
                next = request.POST.get('next', '/')
                return redirect(next)
            else:
                data = d
        msg = form.errors.get('_', '') or _('Login failed!')
        return {'form':form, 'msg':str(msg)}
Esempio n. 8
0
def login():
    from uliweb.contrib.auth import login

    text = ""
    username = request.params.get("username", "")

    form = LoginForm()

    if request.user:
        next = request.GET.get('next')
        if next:
            return redirect(next)

    if request.method == 'GET':
        form.next.data = request.GET.get('next', request.referrer or add_prefix('/'))
        return {'form':form, 'msg':'', 'username': username}
    if request.method == 'POST':
        login_failed_history = functions.get_model("login_failed_history")
        login_failed_user = login_failed_history.get(login_failed_history.c.username == username)

        if login_failed_user and login_failed_user.failed_times >= settings.VERIFICATIONCODE.FAILED_TIMES:
            form = CheckLoginForm()

        flag = form.validate(request.params)
        try:
            if flag:
                if not login_failed_user:
                    login_failed_user = login_failed_history(username = username, failed_times = 0, verification_code = text)
                    login_failed_user.save()

                f, d = functions.authenticate(username = form.username.data, password = form.password.data)
                if f:
                    request.session.remember = form.rememberme.data
                    login(form.username.data)
                    login_failed_user.update(failed_times = 0).save()
                    next = urllib.unquote(request.POST.get('next', add_prefix('/')))
                    return redirect(next)
                form.errors.update(d)
                current_failed_times = login_failed_user.failed_times
                current_failed_times = current_failed_times + 1
                if current_failed_times >= settings.VERIFICATIONCODE.FAILED_TIMES:
                    text = gene_code(username)
                    if not isinstance(form, CheckLoginForm):
                        form = CheckLoginForm()
                login_failed_user.update(failed_times = current_failed_times, verification_code = text).save()
            else:
                if login_failed_user and login_failed_user.failed_times >= settings.VERIFICATIONCODE.FAILED_TIMES:
                    text = gene_code(username)
                    login_failed_user.update(verification_code = text).save()
        except Exception, e:
            logger.error("count user: [%s] login failed times error" % (username))
            logger.error("-- error message: %s" % (e))
        msg = form.errors.get('_', '') or _('Login failed!')
        return {'form':form, 'msg':str(msg), 'username': username}
Esempio n. 9
0
def register():
    from uliweb import settings
    from uliweb.contrib.auth import create_user, login

    if not settings.LOGIN.register:
        error('不允许用户自行注册')

    next = request.values.get('next')
    if not next:
        next = request.referrer
        if not next or (next and next.endswith('/register')):
            next = add_prefix('/')

    form = functions.get_form('auth.RegisterForm')()

    if request.method == 'GET':
        form.next.data = next
        return {'form': form, 'msg': ''}
    if request.method == 'POST':
        flag = form.validate(request.values)
        if flag:
            from uliweb import settings
            f, d = create_user(username=form.username.data,
                               password=form.password.data,
                               auth_type=settings.AUTH.AUTH_TYPE_DEFAULT)
            if f:
                # add auto login support 2012/03/23
                login(d)
                next = unquote(next)
                return redirect(next)
            else:
                form.errors.update(d)

        if request.is_xhr:
            return json({
                'success': False,
                '_': 'Register Failed',
                'errors': form.errors
            })
        else:
            msg = form.errors.get('_', '') or _('Register failed!')
            return {'form': form, 'msg': str(msg)}
Esempio n. 10
0
def callback(site):
    from socialoauth import socialsites
    from socialoauth.utils import import_oauth_class
    from socialoauth.exception import SocialAPIError
    from uliweb.contrib.auth import login
    from uliweb.utils import date

    code = request.GET.get('code')
    if not code:
        # error occurred
        error("Can't found the code from oauth return data")
    
    socialsites._sites_id_name_table = {}
    socialsites.config(settings.SOCIALOAUTH)

    s = import_oauth_class(socialsites[site])()
    try:
        s.get_access_token(code)
    except SocialAPIError as e:
        print e.site_name      
        print e.url            
        print e.error_msg     
        raise
    
    # we can get uid, name, avatar from s
    # we should store the user info to database

    User = functions.get_model('user')
    user = User.get((User.c.username==s.uid) & (User.c.login_type=='1') & (User.c.login_site==site))
    if not user:
        user = User(username=s.uid, nickname=s.name, image=s.avatar,
            login_type='1', login_site=site)
        user.save()
        login(user)
        #引导用户填写邮箱地址
        return redirect('/')
    else:
        user.last_login = date.now()
        user.save()
        login(user)
        return redirect('/')
Esempio n. 11
0
 def call_back(self):
     lpsust = request.values.get(settings.AUTH_LENOVOID.LENOVOID_WUST_NAME)
     lenovoid_action = request.values.get("lenovoid.action")
     if lenovoid_action == 'uilogout':
         Redirect("%s/login"%(settings.DOMAINS.default.get('url_prefix')))
     rememberme = request.values.get("rememberme")
     error_message = ""
     if rememberme:
         rememberme = (rememberme.lower()=="true") or (rememberme=='1')
     if lpsust:
         f,d = authenticate(lpsust=lpsust)
         if f:
             from uliweb.contrib.auth import login
             user = d
             request.session.remember = rememberme
             login(user.username)
             next = urllib.unquote(request.GET.get('next', '%s/'%(settings.DOMAINS.default.get('url_prefix'))))
             return redirect(next)
         else:
             error_message = d.get("error_message")
     error_message = error_message if error_message else "Login failed."
     Redirect("%s/login?msg=%s"%(settings.DOMAINS.default.get('url_prefix'), error_message))
Esempio n. 12
0
def register():
    from uliweb import settings
    from uliweb.contrib.auth import create_user, login

    if not settings.LOGIN.register:
        error('不允许用户自行注册')

    next = request.values.get('next')
    if not next:
        next = request.referrer
        if not next or (next and next.endswith('/register')):
            next = add_prefix('/')

    form = functions.get_form('auth.RegisterForm')()

    if request.method == 'GET':
        form.next.data = next
        return {'form': form, 'msg': ''}
    if request.method == 'POST':
        flag = form.validate(request.values)
        if flag:
            from uliweb import settings
            f, d = create_user(username=form.username.data,
                               password=form.password.data,
                               auth_type=settings.AUTH.AUTH_TYPE_DEFAULT)
            if f:
                # add auto login support 2012/03/23
                login(d)
                next = unquote(next)
                return redirect(next)
            else:
                form.errors.update(d)

        if request.is_xhr:
            return json({'success': False, '_': 'Register Failed', 'errors': form.errors})
        else:
            msg = form.errors.get('_', '') or _('Register failed!')
            return {'form': form, 'msg': str(msg)}
Esempio n. 13
0
    def register(self):
        from uliweb.contrib.auth import create_user, login
        from uliweb.i18n import ugettext_lazy as _

        form = functions.get_form('RegisterForm')()

        if request.method == 'GET':
            form.next.data = request.GET.get('next', '/')
            return {'form':form, 'msg':''}
        if request.method == 'POST':
            flag = form.validate(request.params)
            if flag:
                f, d = create_user(username=form.username.data, password=form.password.data,email=form.email.data)
                if f:
                    #add auto login support 2012/03/23
                    login(d)
                    next = urllib.unquote(request.POST.get('next', '/'))
                    return redirect(next)
                else:
                    form.errors.update(d)

            msg = form.errors.get('_', '') or _('Register failed!')
            return {'form':form, 'msg':str(msg)}