def register(request): try: # check to see if a user is already logged in. if so, redirect them to profile. user = interface.get_logged_in_user(request) except DoesNotExistError: pass else: return HttpResponseRedirect(reverse("profile")) page_top_errors = [] if request.method == 'POST': #TODO: what if the form data isn't in the POST request? we need to check for this. form = forms.GeniUserCreationForm(request.POST, request.FILES) # Calling the form's is_valid() function causes all form "clean_..." methods to be checked. # If this succeeds, then the form input data is validated per field-specific cleaning checks. (see forms.py) # However, we still need to do some checks which aren't doable from inside the form class. if form.is_valid(): username = form.cleaned_data['username'] password = form.cleaned_data['password1'] affiliation = form.cleaned_data['affiliation'] email = form.cleaned_data['email'] pubkey = form.cleaned_data['pubkey'] try: validations.validate_username_and_password_different( username, password) except ValidationError, err: page_top_errors.append(str(err)) # NOTE: gen_upload_choice turns out to be a *string* when retrieved, hence '2' if form.cleaned_data['gen_upload_choice'] == '2' and pubkey == None: page_top_errors.append("Please select a public key to upload.") # only proceed with registration if there are no validation errors if page_top_errors == []: try: # we should never error here, since we've already finished validation at this point. # but, just to be safe... user = interface.register_user(username, password, email, affiliation, pubkey) except ValidationError, err: page_top_errors.append(str(err)) else: return _show_login( request, 'accounts/login.html', { 'msg': "Username %s has been successfully registered." % (user.username) })
def register(request): try: # check to see if a user is already logged in. if so, redirect them to profile. user = interface.get_logged_in_user(request) except DoesNotExistError: pass else: return HttpResponseRedirect(reverse("profile")) page_top_errors = [] if request.method == 'POST': #TODO: what if the form data isn't in the POST request? we need to check for this. form = forms.GeniUserCreationForm(request.POST, request.FILES) # Calling the form's is_valid() function causes all form "clean_..." methods to be checked. # If this succeeds, then the form input data is validated per field-specific cleaning checks. (see forms.py) # However, we still need to do some checks which aren't doable from inside the form class. if form.is_valid(): username = form.cleaned_data['username'] password = form.cleaned_data['password1'] affiliation = form.cleaned_data['affiliation'] email = form.cleaned_data['email'] pubkey = form.cleaned_data['pubkey'] try: validations.validate_username_and_password_different(username, password) except ValidationError, err: page_top_errors.append(str(err)) # NOTE: gen_upload_choice turns out to be a *string* when retrieved, hence '2' if form.cleaned_data['gen_upload_choice'] == '2' and pubkey == None: page_top_errors.append("Please select a public key to upload.") # only proceed with registration if there are no validation errors if page_top_errors == []: try: # we should never error here, since we've already finished validation at this point. # but, just to be safe... user = interface.register_user(username, password, email, affiliation, pubkey) except ValidationError, err: page_top_errors.append(str(err)) else: return _show_login(request, 'accounts/login.html', {'msg' : "Username %s has been successfully registered." % (user.username)})
def register_user(username, password, email, affiliation, pubkey=None): """ <Purpose> Creates a user record with the specified information and sets any additional information necessary for the user record to be complete. <Arguments> username password email affiliation pubkey Optional. A string. If not provided, a key pair will be generated for this user. <Exceptions> UsernameAlreadyExistsError If there is already a user with the specified username. ValidationError If any of the arguments contains invalid values or if the username is the same as the password. <Side Effects> The user record in the django db is created as well as a user record in the corresponding user profile table that stores our custom information. A port will be assigned to the user and the user's donation keys will be set. <Returns> GeniUser instance (our GeniUser model, not the django User) corresponding to the newly registered user. """ # If the frontend code that called this function wants to know which field # is invalid, it must call the validation functions itself before making the # call to register_user(). # These will raise a ValidationError if any of the fields are invalid. # These ensure that the data is of the correct type (e.g. a string) as well as # that we like the content of the variable. validations.validate_username(username) validations.validate_password(password) validations.validate_username_and_password_different(username, password) validations.validate_email(email) validations.validate_affiliation(affiliation) if pubkey is not None: validations.validate_pubkey_string(pubkey) # Lock the user. lockserver_handle = lockserver.create_lockserver_handle() lockserver.lock_user(lockserver_handle, username) try: # Ensure there is not already a user with this username. try: # Raises a DoesNotExistError if the user doesn't exist. maindb.get_user(username) raise UsernameAlreadyExistsError except DoesNotExistError: # This is what we wanted: the username isn't already taken. pass # Get a key pair from the keygen api if the user didn't supply their own pubkey. if pubkey is None: (pubkey, privkey) = keygen.generate_keypair() else: privkey = None # Generate a donor key for this user. This is done through the backend # as the private key must be stored in the keydb, which the website cannot # directly access. keydescription = "donor:" + username donor_pubkey = backend.generate_key(keydescription) # Create the user record. geniuser = maindb.create_user(username, password, email, affiliation, pubkey, privkey, donor_pubkey) finally: # Unlock the user. lockserver.unlock_user(lockserver_handle, username) lockserver.destroy_lockserver_handle(lockserver_handle) return geniuser