Esempio n. 1
0
def create_request(request):
    a = auth.getAuth(request)
    if (a.cn == None or a.provider == None or a.token == None):
        return HttpResponse(status=401)
    q = NeSIUser.objects.filter(username = a.username, provider= a.provider)
    if (q.count() < 1):
        return HttpResponse(status=401)
    else:
        u = q[0]
    r = Request(user = q[0].id, message = request.POST["message"])
    r.save()
    return render_to_response("request.html")
Esempio n. 2
0
def registration(request, resubmit=False):
    try:
        a = auth.getAuth(request)
    except KeyError:
        return HttpResponse(status=403)
    form = RequestForm()
    if (a.username ==None or a.cn == None or a.provider == None or a.token == None):
        return HttpResponse(status=403)
    
    v = VomsConnector()
    groups = v.listGroups()
    
    try:
        userDN = shib.shib2dn(shib.getACL(), a.cn, a.token, a.provider)
        # create user if does not exist
        q = NeSIUser.objects.filter(username = a.username, provider= a.provider)
        if (q.count() < 1):
            u = NeSIUser(username=a.username, provider = a.provider, email = a.email, token = a.token)
            u.save()
        else:
            u = q[0]
    except shib.SlcsUserNotFoundException:
        return HttpResponse(status=403)
    
    userGroups = v.listGroups(userDN, shib.SLCS_CA)
    nonUserGroups = []
    defaultChoices = []
    for g in groups:
        try:
            userGroups.index(g)
        except ValueError:
            pq = Project.objects.filter(vo=g)
            inst = institution_mapping(a.provider)
            if ( in_ignore(g) or not ( g.startswith(inst) or (in_default(g)) or in_collaboration(g) ) ): 
                continue
            if (pq.count() > 0):
                nonUserGroups.append((g,pq[0].label))
                if (g == inst or in_default(g)):
                    defaultChoices.append(g)
            else:
                nonUserGroups.append((g,g))
    #nonUserGroups.sort(lambda a,b: cmp(a[0],b[0]))
    nonUserGroups.sort(key=lambda tuple: tuple[1])
    msgstr = ""
    if request.method == 'POST':
        form = RequestForm(request.POST)
    else:
        form = RequestForm(initial={"email": u.email, "message": msgstr})
    
    form.fields['groups'].choices = nonUserGroups
    form.fields['groups'].initial = defaultChoices

    requestSubmitted = False
    qr = Request.objects.filter(user=q[0])
    if (qr.count() > 1 and not resubmit ):
        r = qr[0]
        requestSubmitted = True
    elif ((qr.count() > 1) and resubmit):
        r = qr[0]
        r.delete()
        requestSubmitted = False
    elif request.method == 'POST':
        if (not form.is_valid()):
            pass
        else:
            m = form.cleaned_data["message"]
            groupsToApply = form.cleaned_data["groups"]
            email = form.cleaned_data["email"]
            phone = form.cleaned_data["phone"]
            r = Request(user = q[0], message = m)
#            message = "#assignee [email protected]\n"
            message = "DN is " + userDN + "\n"
            message += "email is " + email + "\n"
            message += "phone is " + phone + "\n"
            message += "I would like to apply for the following groups: " + ",".join(groupsToApply) + "\n"
            message += request.POST["message"]
            regemail.MailSender().send(message,email)
            r.save()
            requestSubmitted = True
        
    return render_to_response("reg.html", 
                              {"dn": a.cn, 
                               "groups": nonUserGroups, 
                               "userGroups": userGroups,
                               "requestSubmitted": requestSubmitted,
                               "form": form})