示例#1
0
def _log(request, email, password, nextview):
    """ Authenticate or create a new account """
    seaf_root = settings.SEAFILE_ROOT
    user = authenticate(username=email, password=password)
    # if known user:
    if user is not None and user.is_active:
        login(request, user)
        # login, -> nexturl
        return HttpResponseRedirect(reverse(nextview))
    elif user is not None: # not active
        raise AuthError
    else:
        # try to connect to seafile using credentials
        seaf = Seafile(seaf_root, verifycerts=settings.VERIFYCERTS)
        seaf.authenticate(email, password) # may raise AuthError
        token = seaf.token
        # create new user, save the token
        user = User.objects.create_user(email, email, password)
        user.save()
        seafuser = SeafileUser(user=user, seafroot=seaf_root,
                                seaftoken=token)
        seafuser.save()
        # login
        user2 = authenticate(username=email, password=password)
        login(request, user2)
        # -> nextview
        return HttpResponseRedirect(reverse(nextview))
示例#2
0
def index(request):
    """Main login view"""
    #TODO: whatif the seafile password change?
        # the user should with it's old password, and should be able to
        # enter its new password and resync
    justlogout = False
    autherror = False
    seaf_root = settings.SEAFILE_ROOT
    
    # if authenticated, redirect to /private
    if request.user.is_authenticated():
        return HttpResponseRedirect(reverse('private'))
    
    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = LoginForm(request.POST)
        # check whether it's valid:
        if form.is_valid():
            
            email = form.cleaned_data['email']
            password = form.cleaned_data['password']
            
            user = authenticate(username=email, password=password)
            # if known user:
            if user is not None and user.is_active:
                login(request, user)
                # login, -> /private/
                return HttpResponseRedirect(reverse('private'))
            elif user is not None: # not active
                autherror = True
            else:
                # try to connect to seafile using credentials
                seaf = Seafile(seaf_root)
                try:
                    seaf.authenticate(email, password)
                except AuthError:
                    autherror = True
                else:
                    token = seaf.token
                    # create new user, save the token
                    user = User.objects.create_user(email, email, password)
                    user.save()
                    seafuser = SeafileUser(user=user, seafroot=seaf_root,
                                           seaftoken=token)
                    seafuser.save()
                    # login
                    user2 = authenticate(username=email, password=password)
                    login(request, user2)
                    # -> /private/
                    return HttpResponseRedirect(reverse('private'))

    # if a GET (or any other method) we'll create a blank form
    else:
        form = LoginForm()
    
    if 'action' in request.GET:
        justlogout = (request.GET['action'] == 'logout')

    return render(request, 'seafform/index.html', {
        'loginform': form,
        'autherror': autherror,
        'justlogout': justlogout,
        'seaf_root': seaf_root,
    })