def create_user(request): if request.POST: form = CreateUserForm(request.POST) if form.is_valid(): user_ = User(username=form.cleaned_data['username'], email=form.cleaned_data['email']) user_.set_password(form.cleaned_data['password1']) if not form.cleaned_data.get('validate_email', False): user_.email_isvalid = True user_.save() UserJoinsAction(user=user_).save() request.user.message_set.create( message=_("New user created sucessfully. %s.") % html.hyperlink(user_.get_profile_url(), _("See %s profile") % user_.username, target="_blank")) return HttpResponseRedirect( reverse("admin_tools", kwargs={'name': 'createuser'})) else: form = CreateUserForm() return ('osqaadmin/createuser.html', { 'form': form, })
def register(request): if request.method == 'POST': form = ClassicRegisterForm(request.POST) if form.is_valid(): username = form.cleaned_data['username'] password = form.cleaned_data['password1'] email = form.cleaned_data['email'] user_ = User(username=username, email=email) user_.set_password(password) if User.objects.all().count() == 0: user_.is_superuser = True user_.is_staff = True user_.save() UserJoinsAction(user=user_, ip=request.META['REMOTE_ADDR']).save() return login_and_forward(request, user_, None, _("A welcome email has been sent to your email address. ")) else: form = ClassicRegisterForm(initial={'next':reverse('index')}) return render_to_response('auth/complete.html', { 'form1': form }, context_instance=RequestContext(request))
def register(request): if request.method == 'POST': form = ClassicRegisterForm(request.POST) email_feeds_form = SimpleEmailSubscribeForm(request.POST) if form.is_valid() and email_feeds_form.is_valid(): username = form.cleaned_data['username'] password = form.cleaned_data['password1'] email = form.cleaned_data['email'] user_ = User(username=username, email=email) user_.set_password(password) if User.objects.all().count() == 0: user_.is_superuser = True user_.is_staff = True user_.save() UserJoinsAction(user=user_, ip=request.META['REMOTE_ADDR']).save() if email_feeds_form.cleaned_data['subscribe'] == 'n': user_.subscription_settings.enable_notifications = False user_.subscription_settings.save() return login_and_forward(request, user_, None, _("A welcome email has been sent to your email address. ")) else: form = ClassicRegisterForm(initial={'next':'/'}) email_feeds_form = SimpleEmailSubscribeForm() return render_to_response('auth/complete.html', { 'form1': form, 'email_feeds_form': email_feeds_form }, context_instance=RequestContext(request))
def process_authentication_request(self, request): logout_request = request.POST.get('logoutRequest', None) if logout_request: if settings.CAS_LOGOUT_REQUESTS_ENABLED: handle_logout_request(request, logout_request) raise InvalidAuthentication('CAS logout request processed') else: raise InvalidAuthentication('CAS logout request ignored, disabled in configuration') ticket = request.GET.get('ticket', None) if not ticket: raise InvalidAuthentication(_('Login failed. CAS ticket is missing.')) service = service_url(request) username, attributes = validate(ticket, service) if not username: raise InvalidAuthentication(_('Login failed. CAS ticket is invalid.')) try: _user = User.objects.get(username=username) self._sync_user_attributes(_user, attributes) except User.DoesNotExist: _user = User(username=username) _user.set_unusable_password() self._sync_user_attributes(_user, attributes) _user.save() UserJoinsAction(user=_user, ip=request.META['REMOTE_ADDR']).save() request.session[CAS_TICKET_SESSION_ATTR] = ticket return _user
def external_register(request): if request.method == 'POST' and 'bnewaccount' in request.POST: form1 = SimpleRegistrationForm(request.POST) if form1.is_valid(): user_ = User(username=form1.cleaned_data['username'], email=form1.cleaned_data['email']) user_.email_isvalid = request.session.get('auth_validated_email', '') == form1.cleaned_data['email'] user_.set_unusable_password() if User.objects.all().count() == 0: user_.is_superuser = True user_.is_staff = True user_.save() UserJoinsAction(user=user_, ip=request.META['REMOTE_ADDR']).save() try: assoc_key = request.session['assoc_key'] auth_provider = request.session['auth_provider'] except: request.session['auth_error'] = _( "Oops, something went wrong in the middle of this process. Please try again. Note that you need to have cookies enabled for the authentication to work." ) logging.error("Missing session data when trying to complete user registration: %s" % ", ".join( ["%s: %s" % (k, v) for k, v in request.META.items()])) return HttpResponseRedirect(reverse('auth_signin')) uassoc = AuthKeyUserAssociation(user=user_, key=assoc_key, provider=auth_provider) uassoc.save() del request.session['assoc_key'] del request.session['auth_provider'] return login_and_forward(request, user_, message=_("A welcome email has been sent to your email address. ")) else: auth_provider = request.session.get('auth_provider', None) if not auth_provider: request.session['auth_error'] = _( "Oops, something went wrong in the middle of this process. Please try again.") logging.error("Missing session data when trying to complete user registration: %s" % ", ".join( ["%s: %s" % (k, v) for k, v in request.META.items()])) return HttpResponseRedirect(reverse('auth_signin')) provider_class = AUTH_PROVIDERS[auth_provider].consumer user_data = provider_class.get_user_data(request.session['assoc_key']) if not user_data: user_data = request.session.get('auth_consumer_data', {}) username = user_data.get('username', '') email = user_data.get('email', '') if email: request.session['auth_validated_email'] = email form1 = SimpleRegistrationForm(initial={ 'next': '/', 'username': username, 'email': email, }) provider_context = AUTH_PROVIDERS[request.session['auth_provider']].context return render_to_response('auth/complete.html', { 'form1': form1, 'provider':provider_context and mark_safe(provider_context.human_name) or _('unknown'), 'login_type':provider_context.id, 'gravatar_faq_url':reverse('faq') + '#gravatar', }, context_instance=RequestContext(request))
AcceptAnswerAction.hook(answer_accepted) def member_joined(action, new): subscribers = User.objects.filter( subscription_settings__enable_notifications=True, subscription_settings__member_joins='i').exclude( id=action.user.id).distinct() subscribers = filter_subscribers(subscribers) send_template_email(subscribers, "notifications/newmember.html", {'newmember': action.user}) UserJoinsAction.hook(member_joined) def question_viewed(action, new): if not action.viewuser.is_authenticated(): return try: subscription = QuestionSubscription.objects.get(question=action.node, user=action.viewuser) subscription.last_view = datetime.datetime.now() subscription.save() except: if action.viewuser.subscription_settings.questions_viewed: subscription = QuestionSubscription(question=action.node, user=action.viewuser)
def process_authentication_request(self, request): username = request.POST['username'].strip() password = request.POST['password'] uid = str(settings.LDAP_USER_MASK) % username #an empty password will cause ldap to try an anonymous bind. This is picked up here if not password: raise InvalidAuthentication( _('Login failed. Please enter valid username and password (both are case-sensitive)' )) ldapo = ldap.initialize(str(settings.LDAP_SERVER)) if (settings.LDAP_USE_TLS): ldapo.start_tls_s() ldapo.set_option(ldap.OPT_PROTOCOL_VERSION, 3) try: ldapo.simple_bind_s(str(settings.LDAP_BIND_DN), str(settings.LDAP_BIND_SECRET)) search = ldapo.search_s(str(settings.LDAP_BASE_DN), ldap.SCOPE_SUBTREE, uid) except ldap.LDAPError: #could not bind using credentials specified in ldap config raise InvalidAuthentication( _('Login failed - LDAP bind error. Please contact your system administrator' )) ldapo.unbind_s() if not search: #could not find user raise InvalidAuthentication( _('Login failed. Please enter valid username and password (both are case-sensitive)' )) #now try to bind as selected user; should raise exception if bind fails ldapo = ldap.initialize(str(settings.LDAP_SERVER)) if (settings.LDAP_USE_TLS): ldapo.start_tls_s() ldapo.set_option(ldap.OPT_PROTOCOL_VERSION, 3) try: # F.CRESPEL: use search[0][0] as the DN, so that it works consistently across OpenLDAP/ActiveDirectory ###ldapo.simple_bind_s(search[0][1][str(settings.LDAP_DN)][0],password) ldapo.simple_bind_s(search[0][0], password) except ldap.LDAPError: #could not bind as user - password is incorrect raise InvalidAuthentication( _('Login failed. Please enter valid username and password (both are case-sensitive)' )) ldapo.unbind_s() try: return User.objects.get(username=username) except User.DoesNotExist: userinfo = search[0][1] _user = User(username=userinfo[str(settings.LDAP_UID)][0], email=userinfo[str(settings.LDAP_MAIL)][0], real_name=userinfo[str(settings.LDAP_NAME)][0]) _user.email_isvalid = True _user.set_unusable_password() _user.save() UserJoinsAction(user=_user, ip=request.META['REMOTE_ADDR']).save() return _user
send_template_email(subscribers, "notifications/answeraccepted.html", {'answer': action.node}) AcceptAnswerAction.hook(answer_accepted) def member_joined(action, new): subscribers = User.objects.filter( subscription_settings__enable_notifications=True, subscription_settings__member_joins='i' ).exclude(id=action.user.id).distinct() subscribers = filter_subscribers(subscribers) send_template_email(subscribers, "notifications/newmember.html", {'newmember': action.user}) UserJoinsAction.hook(member_joined) def question_viewed(action, new): if not action.viewuser.is_authenticated(): return try: subscription = QuestionSubscription.objects.get(question=action.node, user=action.viewuser) subscription.last_view = datetime.datetime.now() subscription.save() except: if action.viewuser.subscription_settings.questions_viewed: subscription = QuestionSubscription(question=action.node, user=action.viewuser) subscription.save() QuestionViewAction.hook(question_viewed)