Beispiel #1
0
    def create_new_user(self, data, hashed_password=None):
        """Creates a new user (not active)

        Keyword arguments:
        data -- a dictonary of user data
        hashed_password --
    
        """
        # Make sure username isn't taken in Datastore
        random_passwd = User.objects.make_random_password()
        user = User.objects.create_user(data['username'], data['email'], random_passwd)
        
        if hashed_password:
            user.password = hashed_password
        else:
            from karaage.datastores import create_password_hash
            user.password = create_password_hash(data['password1'])
            
        user.is_active = False
        user.save()
    
        #Create Person
        person = Person.objects.create(
            user=user,
            first_name=data['first_name'],
            last_name=data['last_name'],
            institute=data['institute'],
            position=data.get('position', ''),
            department=data.get('department', ''),
            title=data.get('title', ''),
            address=data.get('address', ''),
            country=data.get('country', ''),
            website=data.get('website', ''),
            fax=data.get('fax', ''),
            comment=data.get('comment', ''),
            telephone=data.get('telephone', ''),
            mobile=data.get('mobile', ''),
            supervisor=data.get('supervisor', ''),
            is_systemuser=data.get('is_systemuser', ''),
            saml_id=data.get('saml_id', None),
            )
        
        try:
            current_user = get_current_user()
            if current_user.is_anonymous():
                current_user = person.user
        except:
            current_user = person.user

        log(current_user, person, 1, 'Created')
        
        return person
Beispiel #2
0
 def save(self, commit=True):
     applicant = super(UserApplicantForm, self).save(commit=commit)
     applicant.password = create_password_hash(self.cleaned_data['password1'])
     if commit:
         applicant.save()
     return applicant
Beispiel #3
0
def do_userapplication(request, token=None, saml=False,
                       application_form=UserApplicationForm):
    if request.user.is_authenticated():
        messages.warning(request, "You are already logged in")
        return HttpResponseRedirect(reverse('kg_user_profile'))

    if saml:
        response = ensure_shib_session(request)
        if response:
            return response

    if token:
        try:
            application = UserApplication.objects.get(
                                        secret_token=token,
                                        state__in=[Application.NEW, Application.OPEN],
                                        expires__gt=datetime.datetime.now())
        except UserApplication.DoesNotExist:
            return render_to_response('applications/old_userapplication.html',
                                        {'help_email': settings.ACCOUNTS_EMAIL},
                                        context_instance=RequestContext(request))
        applicant = application.applicant
        application.state = Application.OPEN
        application.save()
        captcha = False
    else:
        if not settings.ALLOW_REGISTRATIONS:
            return render_to_response('applications/registrations_disabled.html', {}, context_instance=RequestContext(request))
        application = UserApplication()
        applicant = None
        captcha = True
    if saml:
        captcha = False
        saml_user = get_saml_user(request)
    else:
        saml_user = None

    if application.content_type and application.content_type.model == 'person':
        return existing_user_application(request, token)
    init_institute = request.GET.get('institute', '')

    if request.method == 'POST':
        form = application_form(request.POST, instance=application, captcha=captcha)
        if saml:
            applicant_form = SAMLApplicantForm(request.POST, instance=applicant)
        else:
            applicant_form = UserApplicantForm(request.POST, instance=applicant)
        if form.is_valid() and applicant_form.is_valid():
            applicant = applicant_form.save(commit=False)
            if saml:
                applicant = add_saml_data(applicant, request)
                applicant.email_verified = True
            applicant.password = create_password_hash(applicant_form.cleaned_data['password1'])
            applicant.save()
            application = form.save(commit=False)
            application.applicant = applicant
            application.save()
            if not application.project:
                application.state = Application.OPEN
                application.save()
                return HttpResponseRedirect(reverse('kg_application_choose_project', args=[application.secret_token]))
            application.submitted_date = datetime.datetime.now()
            application.state = Application.WAITING_FOR_LEADER
            application.save()
            send_account_request_email(application)
            return HttpResponseRedirect(reverse('kg_application_done', args=[application.secret_token]))
    else:
        form = application_form(instance=application, captcha=captcha)
        if saml:
            applicant_form = SAMLApplicantForm(instance=applicant)
        else:
            applicant_form = UserApplicantForm(instance=applicant, initial={'institute': init_institute})
    return render_to_response('applications/userapplication_form.html',
                              {'form': form, 'applicant_form': applicant_form, 'application': application,
                               'saml': saml, 'saml_user': saml_user, },
                              context_instance=RequestContext(request))
Beispiel #4
0
def do_projectapplication(request, token=None, application_form=ProjectApplicationForm,
                          mc=MachineCategory.objects.get_default(), saml=False):

    if request.user.is_authenticated():
        messages.warning(request, "You are already logged in")
        return HttpResponseRedirect(reverse('kg_user_profile'))

    if saml:
        response = ensure_shib_session(request)
        if response:
            return response

    if token:
        application = get_object_or_404(ProjectApplication, secret_token=token)
        if application.state not in (Application.NEW, Application.OPEN):
            raise Http404
        applicant = application.applicant
        application.state = Application.OPEN
        application.save()
        captcha = False
    else:
        if not settings.ALLOW_REGISTRATIONS:
            return render_to_response('applications/registrations_disabled.html', {}, context_instance=RequestContext(request))
        application = None
        applicant = None
        captcha = True

    if saml:
        captcha = False
        saml_user = get_saml_user(request)
    else:
        saml_user = None
    init_institute = request.GET.get('institute', '')
    if request.method == 'POST':
        form = application_form(request.POST, instance=application, captcha=captcha)
        if saml:
            applicant_form = SAMLApplicantForm(request.POST, instance=applicant)
        else:
            applicant_form = UserApplicantForm(request.POST, instance=applicant)

        if form.is_valid() and applicant_form.is_valid():
            applicant = applicant_form.save(commit=False)
            if saml:
                applicant = add_saml_data(applicant, request)
                applicant.email_verified = True
            applicant.password = create_password_hash(applicant_form.cleaned_data['password1'])
            applicant.save()
            application = form.save(commit=False)
            application.applicant = applicant
            application.submitted_date = datetime.datetime.now()
            application.state = Application.WAITING_FOR_DELEGATE
            application.save()
            application.machine_categories.add(mc)
            send_project_request_email(application)
            return HttpResponseRedirect(reverse('kg_application_done', args=[application.secret_token]))
    else:
        form = application_form(instance=application, captcha=captcha, initial={'institute': init_institute})
        if saml:
            applicant_form = SAMLApplicantForm(instance=applicant)
        else:
            applicant_form = UserApplicantForm(instance=applicant, initial={'institute': init_institute})

    return render_to_response(
        'applications/projectapplication_form.html',
        {'form': form, 'applicant_form': applicant_form, 'application': application, 'saml': saml, 'saml_user': saml_user},
        context_instance=RequestContext(request))