def signup(request): """ signup page. Create a legacy account url : /signup/" templates: authopenid/signup.html, authopenid/confirm_email.txt """ logging.debug('') if settings.USE_EXTERNAL_LEGACY_LOGIN == True: logging.debug('handling external legacy login registration') return HttpResponseRedirect(reverse('user_external_legacy_login_signup')) next = get_next_url(request) logging.debug('request method was %s' % request.method) if request.method == 'POST': form = ClassicRegisterForm(request.POST) email_feeds_form = SimpleEmailSubscribeForm(request.POST) #validation outside if to remember form values form1_is_valid = form.is_valid() form2_is_valid = email_feeds_form.is_valid() if form1_is_valid and form2_is_valid: next = form.cleaned_data['next'] username = form.cleaned_data['username'] password = form.cleaned_data['password1'] email = form.cleaned_data['email'] user_ = User.objects.create_user( username,email,password ) if settings.USE_EXTERNAL_LEGACY_LOGIN == True: EXTERNAL_LOGIN_APP.api.create_user(username,email,password) user_.backend = "django.contrib.auth.backends.ModelBackend" login(request, user_) email_feeds_form.save(user_) # send email subject = _("Welcome email subject line") message_template = loader.get_template( 'authopenid/confirm_email.txt' ) message_context = Context({ 'signup_url': settings.APP_URL + reverse('user_signin'), 'username': username, 'password': password, }) message = message_template.render(message_context) send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [user_.email]) logging.debug('new user with login and password created!') return HttpResponseRedirect(next) else: logging.debug('create classic account forms were invalid') else: form = ClassicRegisterForm(initial={'next':next}) email_feeds_form = SimpleEmailSubscribeForm() logging.debug('printing legacy signup form') return render_to_response('authopenid/signup.html', { 'form': form, 'email_feeds_form': email_feeds_form }, context_instance=RequestContext(request))
def register(request, newquestion = False, newanswer = False): logging.debug('') state, context = fb.get_user_state(request) if state == fb.STATES['FIRSTTIMER']: logging.debug('FB FIRSTTIMER - try to register locally') logging.debug('request method is %s' % request.method) if request.method == 'POST' and 'bnewaccount' in request.POST: form1 = forms.FBConnectRegisterForm(request.POST) email_feeds_form = SimpleEmailSubscribeForm(request.POST) if (form1.is_valid() and email_feeds_form.is_valid()): tmp_pwd = User.objects.make_random_password() user_ = User.objects.create_user(form1.cleaned_data['username'], form1.cleaned_data['email'], tmp_pwd) user_.set_unusable_password() logging.debug('created new internal user %s' % form1.cleaned_data['username']) uassoc = FBAssociation(user=user_, fbuid=context['uid']) uassoc.save() logging.debug('created new user association') email_feeds_form.save(user_) return login_and_forward(request, user_, newquestion, newanswer) else: logging.debug('form user input is invalid') else: form1 = forms.FBConnectRegisterForm(initial={ 'next': '/', 'username': context['name'], 'email': '', }) email_feeds_form = SimpleEmailSubscribeForm() return render('authopenid/complete.html', { 'form1': form1, 'email_feeds_form': email_feeds_form, 'provider':mark_safe('facebook'), 'login_type':'facebook', 'gravatar_faq_url':reverse('faq') + '#gravatar', }, context_instance=RequestContext(request)) else: logging.debug('not a FIRSTTIMER --> redirect to index view') return HttpResponseRedirect(reverse('index'))
def signup(request): #this view works through forum and mediawiki (using apache include virtual injection) if request.is_include_virtual and request.REQUEST.get('was_posted','false')=='true': POST_INCLUDE_VIRTUAL = True POST_DATA = request.GET else: POST_INCLUDE_VIRTUAL = False if request.method == 'POST': POST_DATA = request.POST else: POST_DATA = None if POST_DATA: form = RegisterForm(POST_DATA) if form.is_valid(): data = form.cleaned_data login_name = data['login_name'] password = data['password'] first_name = data['first_name'] last_name = data['last_name'] screen_name = data['screen_name'] user_title = data['user_title'] email = data['email'] next = data['next'] #register mediawiki user user_real_name = u'%s %s' % (first_name,last_name) mwu = MWUser( user_name=login_name, user_first_name = first_name, user_last_name = last_name, user_title = user_title, user_email = email, user_real_name=user_real_name ) mwu.set_default_options() mwu.save() #password may need user id so reload it mwu = MWUser.objects.get(user_name = login_name) mwu.set_password_and_token(password) mwu.save() #create log message mwu_creation_log = Logging( log_type='newusers', log_action='create', log_timestamp=time.strftime(MW_TS), log_params=str(mwu.user_id), log_namespace=2, log_user=mwu, log_deleted=0, ) mwu_creation_log.save() mwu_creation_log.show_in_recent_changes(ip=request.META['REMOTE_ADDR']) print 'creation log saved' #register local user User.objects.create_user(screen_name, email, password) u = authenticate(username=screen_name, password=password) login(request,u) u.mediawiki_user = mwu u.save() #save email feed settings subscribe = SimpleEmailSubscribeForm(POST_DATA) if subscribe.is_valid(): subscribe.save(user=u) #save external login data eld = ExternalLoginData(external_username=login_name, user=u) eld.save() transaction.commit()#commit so that user becomes visible on the wiki side #check password through API and load MW HTTP header session data api.check_password(login_name,password) print 'wiki login worked' #create welcome message on the forum u.message_set.create(message=_('Welcome to the OSQA community!')) print 'about to send confirmation email' send_welcome_email(request, mwu, u) if POST_INCLUDE_VIRTUAL: questions = Question.objects.exclude(deleted=True, closed=True, answer_accepted=True) questions = questions.order_by('-last_activity_at')[:5] response = render_to_response('mediawiki/thanks_for_joining.html', \ { 'wiki_user':mwu, 'user':u, 'questions':questions, }, context_instance = RequestContext(request)) api.set_login_cookies(response, u) #call session middleware now to get the django login cookies from django.contrib.sessions.middleware import SessionMiddleware sm = SessionMiddleware() response = sm.process_response(request,response) cookies = response.cookies for c in cookies.values(): response.write(c.js_output()) else: response = HttpResponseRedirect(next) api.set_login_cookies(response, u) #set cookies so that user is logged in in the wiki too transaction.commit() return response else: form = RegisterForm() transaction.commit() if request.is_include_virtual: template_name = 'mediawiki/mediawiki_signup_content.html' else: template_name = 'mediawiki/mediawiki_signup.html' return render_to_response(template_name,{'form':form},\ context_instance=RequestContext(request))
def register(request): """ register an openid. If user is already a member he can associate its openid with its account. A new account could also be created and automaticaly associated to the openid. url : /complete/ template : authopenid/complete.html """ logging.debug('') openid_ = request.session.get('openid', None) next = get_next_url(request) if not openid_: logging.debug('oops, no openid in session --> go back to signin') return HttpResponseRedirect(reverse('user_signin') + '?next=%s' % next) nickname = openid_.sreg.get('nickname', '') email = openid_.sreg.get('email', '') form1 = OpenidRegisterForm(initial={ 'next': next, 'username': nickname, 'email': email, }) form2 = OpenidVerifyForm(initial={ 'next': next, 'username': nickname, }) email_feeds_form = SimpleEmailSubscribeForm() user_ = None is_redirect = False logging.debug('request method is %s' % request.method) if request.method == 'POST': if 'bnewaccount' in request.POST.keys(): logging.debug('trying to create new account associated with openid') form1 = OpenidRegisterForm(request.POST) email_feeds_form = SimpleEmailSubscribeForm(request.POST) if not form1.is_valid(): logging.debug('OpenidRegisterForm is INVALID') elif not email_feeds_form.is_valid(): logging.debug('SimpleEmailSubscribeForm is INVALID') else: logging.debug('OpenidRegisterForm and SimpleEmailSubscribeForm are valid') next = form1.cleaned_data['next'] is_redirect = True logging.debug('creatng new django user %s ...' % form1.cleaned_data['username']) tmp_pwd = User.objects.make_random_password() user_ = User.objects.create_user(form1.cleaned_data['username'], form1.cleaned_data['email'], tmp_pwd) user_.set_unusable_password() # make association with openid logging.debug('creating new openid user association %s <--> %s' \ % (user_.username, str(openid_))) uassoc = UserAssociation(openid_url=str(openid_), user_id=user_.id) uassoc.save() # login user_.backend = "django.contrib.auth.backends.ModelBackend" logging.debug('logging the user in') login(request, user_) logging.debug('saving email feed settings') email_feeds_form.save(user_) elif 'bverify' in request.POST.keys(): logging.debug('processing OpenidVerify form') form2 = OpenidVerifyForm(request.POST) if form2.is_valid(): logging.debug('form is valid') is_redirect = True next = form2.cleaned_data['next'] user_ = form2.get_user() logging.debug('creating new openid user association %s <--> %s' \ % (user_.username, str(openid_))) uassoc = UserAssociation(openid_url=str(openid_), user_id=user_.id) uassoc.save() logging.debug('logging the user in') login(request, user_) #check if we need to post a question that was added anonymously #this needs to be a function call becase this is also done #if user just logged in and did not need to create the new account if user_ != None: if settings.EMAIL_VALIDATION == 'on': logging.debug('sending email validation') send_new_email_key(user_,nomessage=True) output = validation_email_sent(request) set_email_validation_message(user_) #message set after generating view return output if user_.is_authenticated(): logging.debug('success, send user to main page') return HttpResponseRedirect(reverse('index')) else: logging.debug('have really strange error') raise Exception('openid login failed')#should not ever get here openid_str = str(openid_) bits = openid_str.split('/') base_url = bits[2] #assume this is base url url_bits = base_url.split('.') provider_name = url_bits[-2].lower() providers = {'yahoo':'<font color="purple">Yahoo!</font>', 'flickr':'<font color="#0063dc">flick</font><font color="#ff0084">r</font>™', 'google':'Google™', 'aol':'<font color="#31658e">AOL</font>', 'myopenid':'MyOpenID', } if provider_name not in providers: provider_logo = provider_name logging.error('openid provider named "%s" has no pretty customized logo' % provider_name) else: provider_logo = providers[provider_name] logging.debug('printing authopenid/complete.html output') return render_to_response('authopenid/complete.html', { 'form1': form1, 'form2': form2, 'email_feeds_form': email_feeds_form, 'provider':mark_safe(provider_logo), 'username': nickname, 'email': email, 'login_type':'openid', 'gravatar_faq_url':reverse('faq') + '#gravatar', }, context_instance=RequestContext(request))
def signin(request,newquestion=False,newanswer=False): """ signin page. It manages the legacy authentification (user/password) and openid authentification url: /signin/ template : authopenid/signin.htm """ logging.debug('in signin view') request.encoding = 'UTF-8' on_failure = signin_failure email_feeds_form = SimpleEmailSubscribeForm() next = get_next_url(request) form_signin = OpenidSigninForm(initial={'next':next}) form_auth = ClassicLoginForm(initial={'next':next}) if request.method == 'POST': #'blogin' - password login if 'blogin' in request.POST.keys(): logging.debug('processing classic login form submission') form_auth = ClassicLoginForm(request.POST) if form_auth.is_valid(): #have login and password and need to login through external website if settings.USE_EXTERNAL_LEGACY_LOGIN == True: username = form_auth.cleaned_data['username'] password = form_auth.cleaned_data['password'] next = form_auth.cleaned_data['next'] if form_auth.get_user() == None: #need to create internal user #1) save login and password temporarily in session request.session['external_username'] = username request.session['external_password'] = password #2) try to extract user email and nickname from external service email = EXTERNAL_LOGIN_APP.api.get_email(username,password) screen_name = EXTERNAL_LOGIN_APP.api.get_screen_name(username,password) #3) see if username clashes with some existing user #if so, we have to prompt the user to pick a different name username_taken = User.is_username_taken(screen_name) email_feeds_form = SimpleEmailSubscribeForm() form_data = {'username':screen_name,'email':email,'next':next} form = OpenidRegisterForm(initial=form_data) template_data = {'form1':form,'username':screen_name,\ 'email_feeds_form':email_feeds_form,\ 'provider':mark_safe(settings.EXTERNAL_LEGACY_LOGIN_PROVIDER_NAME),\ 'login_type':'legacy',\ 'gravatar_faq_url':reverse('faq') + '#gravatar',\ 'external_login_name_is_taken':username_taken} return render_to_response('authopenid/complete.html',template_data,\ context_instance=RequestContext(request)) else: #user existed, external password is ok user = form_auth.get_user() login(request,user) response = HttpResponseRedirect(get_next_url(request)) EXTERNAL_LOGIN_APP.api.set_login_cookies(response,user) return response else: #regular password authentication user = form_auth.get_user() login(request, user) return HttpResponseRedirect(get_next_url(request)) elif 'bnewaccount' in request.POST.keys(): logging.debug('processing classic (login/password) create account form submission') #register externally logged in password user with a new local account if settings.USE_EXTERNAL_LEGACY_LOGIN == True: form = OpenidRegisterForm(request.POST) email_feeds_form = SimpleEmailSubscribeForm(request.POST) form1_is_valid = form.is_valid() form2_is_valid = email_feeds_form.is_valid() if form1_is_valid and form2_is_valid: #create the user username = form.cleaned_data['username'] password = request.session.get('external_password',None) email = form.cleaned_data['email'] if password and username: User.objects.create_user(username,email,password) user = authenticate(username=username,password=password) EXTERNAL_LOGIN_APP.api.connect_local_user_to_external_user(user,username,password) external_username = request.session['external_username'] eld = ExternalLoginData.objects.get(external_username=external_username) eld.user = user eld.save() login(request,user) email_feeds_form.save(user) del request.session['external_username'] del request.session['external_password'] response = HttpResponseRedirect(reverse('index')) EXTERNAL_LOGIN_APP.api.set_login_cookies(response, user) return response else: if password: del request.session['external_username'] if username: del request.session['external_password'] return HttpResponseServerError() else: username = request.POST.get('username',None) provider = mark_safe(settings.EXTERNAL_LEGACY_LOGIN_PROVIDER_NAME) username_taken = User.is_username_taken(username) data = {'login_type':'legacy','form1':form,'username':username,\ 'email_feeds_form':email_feeds_form,'provider':provider,\ 'gravatar_faq_url':reverse('faq') + '#gravatar',\ 'external_login_name_is_taken':username_taken} return render_to_response('authopenid/complete.html',data, context_instance=RequestContext(request)) else: raise Http404 elif 'bsignin' in request.POST.keys() or 'openid_username' in request.POST.keys(): logging.debug('processing signin with openid submission') form_signin = OpenidSigninForm(request.POST) if form_signin.is_valid(): logging.debug('OpenidSigninForm is valid') next = form_signin.cleaned_data['next'] sreg_req = sreg.SRegRequest(optional=['nickname', 'email']) redirect_to = "%s%s?%s" % ( get_url_host(request), reverse('user_complete_signin'), urllib.urlencode({'next':next}) ) return ask_openid(request, form_signin.cleaned_data['openid_url'], redirect_to, on_failure=signin_failure, sreg_request=sreg_req) else: logging.debug('OpenidSigninForm is NOT valid! -> redisplay login view') #if request is GET if request.method == 'GET': logging.debug('request method was GET') question = None if newquestion == True: from forum.models import AnonymousQuestion as AQ session_key = request.session.session_key logging.debug('retrieving anonymously posted question associated with session %s' % session_key) qlist = AQ.objects.filter(session_key=session_key).order_by('-added_at') if len(qlist) > 0: question = qlist[0] answer = None if newanswer == True: from forum.models import AnonymousAnswer as AA session_key = request.session.session_key logging.debug('retrieving posted answer associated with session %s' % session_key) alist = AA.objects.filter(session_key=session_key).order_by('-added_at') if len(alist) > 0: answer = alist[0] logging.debug('showing signin view') return render_to_response('authopenid/signin.html', { 'question':question, 'answer':answer, 'form1': form_auth, 'form2': form_signin, 'msg': request.GET.get('msg',''), 'sendpw_url': reverse('user_sendpw'), 'fb_api_key': settings.FB_API_KEY, }, context_instance=RequestContext(request))