def reset_password(token): """View function that handles a reset password request.""" expired, invalid, user = reset_password_token_status(token) if invalid: do_flash(*get_message('INVALID_RESET_PASSWORD_TOKEN')) if expired: do_flash(*get_message('PASSWORD_RESET_EXPIRED', email=user.email, within=_security.reset_password_within)) if invalid or expired: return redirect(url_for('browser.forgot_password')) has_error = False form = _security.reset_password_form() if form.validate_on_submit(): try: update_password(user, form.password.data) except SOCKETErrorException as e: # Handle socket errors which are not covered by SMTPExceptions. logging.exception(str(e), exc_info=True) flash( gettext( u'SMTP Socket error: {}\nYour password has not been changed.' ).format(e), 'danger') has_error = True except (SMTPConnectError, SMTPResponseException, SMTPServerDisconnected, SMTPDataError, SMTPHeloError, SMTPException, SMTPAuthenticationError, SMTPSenderRefused, SMTPRecipientsRefused) as e: # Handle smtp specific exceptions. logging.exception(str(e), exc_info=True) flash( gettext( u'SMTP error: {}\nYour password has not been changed.' ).format(e), 'danger') has_error = True except Exception as e: # Handle other exceptions. logging.exception(str(e), exc_info=True) flash( gettext(u'Error: {}\nYour password has not been changed.'). format(e), 'danger') has_error = True if not has_error: after_this_request(_commit) do_flash(*get_message('PASSWORD_RESET')) login_user(user) return redirect( get_url(_security.post_reset_view) or get_url(_security.post_login_view)) return _security.render_template( config_value('RESET_PASSWORD_TEMPLATE'), reset_password_form=form, reset_password_token=token, **_ctx('reset_password'))
def login(): if request.is_json: form = _security.login_form(MultiDict(request.get_json())) else: form = _security.login_form(request.form) if form.validate_on_submit(): login_user(form.user, remember=form.remember.data) after_this_request(_commit) if not request.is_json: return redirect(get_post_login_redirect(form.next.data)) if not request.is_json: return _security.render_template(config_value('LOGIN_USER_TEMPLATE'), login_user_form=form, **_ctx('login')) # override error messages if necessary confirmation_required = get_message('CONFIRMATION_REQUIRED')[0] if confirmation_required in form.errors.get('email', []): return jsonify({ 'error': confirmation_required, }), HTTPStatus.UNAUTHORIZED elif form.errors: username_fields = config_value('USER_IDENTITY_ATTRIBUTES') return jsonify({ 'error': f"Invalid {', '.join(username_fields)} and/or password." }), HTTPStatus.UNAUTHORIZED return jsonify({ 'user': form.user, 'token': form.user.get_auth_token(), })
def forgot_password(): """View function that handles a forgotten password request.""" has_error = False form_class = _security.forgot_password_form if request.json: form = form_class(MultiDict(request.json)) else: form = form_class() if form.validate_on_submit(): # Check the Authentication source of the User user = User.query.filter_by(email=form.data['email'], auth_source=INTERNAL).first() if user is None: # If the user is not an internal user, raise the exception flash( gettext( 'Your account is authenticated using an ' 'external {} source. ' 'Please contact the administrators of this ' 'service if you need to reset your password.').format( form.user.auth_source), 'danger') has_error = True if not has_error: try: send_reset_password_instructions(form.user) except SOCKETErrorException as e: # Handle socket errors which are not # covered by SMTPExceptions. logging.exception(str(e), exc_info=True) flash(gettext(SMTP_SOCKET_ERROR).format(e), 'danger') has_error = True except (SMTPConnectError, SMTPResponseException, SMTPServerDisconnected, SMTPDataError, SMTPHeloError, SMTPException, SMTPAuthenticationError, SMTPSenderRefused, SMTPRecipientsRefused) as e: # Handle smtp specific exceptions. logging.exception(str(e), exc_info=True) flash(gettext(SMTP_ERROR).format(e), 'danger') has_error = True except Exception as e: # Handle other exceptions. logging.exception(str(e), exc_info=True) flash(gettext(PASS_ERROR).format(e), 'danger') has_error = True if request.json is None and not has_error: do_flash(*get_message('PASSWORD_RESET_REQUEST', email=form.user.email)) if request.json and not has_error: return default_render_json(form, include_user=False) return _security.render_template( config_value('FORGOT_PASSWORD_TEMPLATE'), forgot_password_form=form, **_ctx('forgot_password'))
def change_password(): """View function which handles a change password request.""" has_error = False form_class = _security.change_password_form if request.json: form = form_class(MultiDict(request.json)) else: form = form_class() if form.validate_on_submit(): try: change_user_password(current_user, form.new_password.data) except SOCKETErrorException as e: # Handle socket errors which are not covered by SMTPExceptions. logging.exception(str(e), exc_info=True) flash(gettext(u'SMTP Socket error: {}\n' u'Your password has not been changed.' ).format(e), 'danger') has_error = True except (SMTPConnectError, SMTPResponseException, SMTPServerDisconnected, SMTPDataError, SMTPHeloError, SMTPException, SMTPAuthenticationError, SMTPSenderRefused, SMTPRecipientsRefused) as e: # Handle smtp specific exceptions. logging.exception(str(e), exc_info=True) flash(gettext(u'SMTP error: {}\n' u'Your password has not been changed.' ).format(e), 'danger') has_error = True except Exception as e: # Handle other exceptions. logging.exception(str(e), exc_info=True) flash( gettext( u'Error: {}\n' u'Your password has not been changed.' ).format(e), 'danger' ) has_error = True if request.json is None and not has_error: after_this_request(_commit) do_flash(*get_message('PASSWORD_CHANGE')) return redirect(get_url(_security.post_change_view) or get_url(_security.post_login_view)) if request.json and not has_error: form.user = current_user return _render_json(form) return _security.render_template( config_value('CHANGE_PASSWORD_TEMPLATE'), change_password_form=form, **_ctx('change_password'))
def reset_password(token): """View function that handles a reset password request.""" expired, invalid, user = reset_password_token_status(token) if invalid: do_flash(*get_message('INVALID_RESET_PASSWORD_TOKEN')) if expired: do_flash(*get_message('PASSWORD_RESET_EXPIRED', email=user.email, within=_security.reset_password_within)) if invalid or expired: return redirect(url_for('browser.forgot_password')) has_error = False form = _security.reset_password_form() if form.validate_on_submit(): try: update_password(user, form.password.data) except SOCKETErrorException as e: # Handle socket errors which are not covered by SMTPExceptions. logging.exception(str(e), exc_info=True) flash(gettext(u'SMTP Socket error: {}\n' u'Your password has not been changed.' ).format(e), 'danger') has_error = True except (SMTPConnectError, SMTPResponseException, SMTPServerDisconnected, SMTPDataError, SMTPHeloError, SMTPException, SMTPAuthenticationError, SMTPSenderRefused, SMTPRecipientsRefused) as e: # Handle smtp specific exceptions. logging.exception(str(e), exc_info=True) flash(gettext(u'SMTP error: {}\n' u'Your password has not been changed.' ).format(e), 'danger') has_error = True except Exception as e: # Handle other exceptions. logging.exception(str(e), exc_info=True) flash(gettext(u'Error: {}\n' u'Your password has not been changed.' ).format(e), 'danger') has_error = True if not has_error: after_this_request(_commit) do_flash(*get_message('PASSWORD_RESET')) login_user(user) return redirect(get_url(_security.post_reset_view) or get_url(_security.post_login_view)) return _security.render_template( config_value('RESET_PASSWORD_TEMPLATE'), reset_password_form=form, reset_password_token=token, **_ctx('reset_password'))
def change_password(): """View function which handles a change password request.""" has_error = False form_class = _security.change_password_form if request.json: form = form_class(MultiDict(request.json)) else: form = form_class() if form.validate_on_submit(): try: change_user_password(current_user._get_current_object(), form.new_password.data) except SOCKETErrorException as e: # Handle socket errors which are not covered by SMTPExceptions. logging.exception(str(e), exc_info=True) flash(gettext(SMTP_SOCKET_ERROR).format(e), 'danger') has_error = True except (SMTPConnectError, SMTPResponseException, SMTPServerDisconnected, SMTPDataError, SMTPHeloError, SMTPException, SMTPAuthenticationError, SMTPSenderRefused, SMTPRecipientsRefused) as e: # Handle smtp specific exceptions. logging.exception(str(e), exc_info=True) flash(gettext(SMTP_ERROR).format(e), 'danger') has_error = True except Exception as e: # Handle other exceptions. logging.exception(str(e), exc_info=True) flash(gettext(PASS_ERROR).format(e), 'danger') has_error = True if request.json is None and not has_error: after_this_request(view_commit) do_flash(*get_message('PASSWORD_CHANGE')) old_key = get_crypt_key()[1] set_crypt_key(form.new_password.data, False) from pgadmin.browser.server_groups.servers.utils \ import reencrpyt_server_passwords reencrpyt_server_passwords(current_user.id, old_key, form.new_password.data) return redirect( get_url(_security.post_change_view) or get_url(_security.post_login_view)) if request.json and not has_error: form.user = current_user return default_render_json(form) return _security.render_template( config_value('CHANGE_PASSWORD_TEMPLATE'), change_password_form=form, **_ctx('change_password'))
def forgot_password(): """View function that handles a forgotten password request.""" has_error = False form_class = _security.forgot_password_form if request.json: form = form_class(MultiDict(request.json)) else: form = form_class() if form.validate_on_submit(): try: send_reset_password_instructions(form.user) except SOCKETErrorException as e: # Handle socket errors which are not covered by SMTPExceptions. logging.exception(str(e), exc_info=True) flash( gettext(u'SMTP Socket error: {}\n' u'Your password has not been changed.').format(e), 'danger') has_error = True except (SMTPConnectError, SMTPResponseException, SMTPServerDisconnected, SMTPDataError, SMTPHeloError, SMTPException, SMTPAuthenticationError, SMTPSenderRefused, SMTPRecipientsRefused) as e: # Handle smtp specific exceptions. logging.exception(str(e), exc_info=True) flash( gettext(u'SMTP error: {}\n' u'Your password has not been changed.').format(e), 'danger') has_error = True except Exception as e: # Handle other exceptions. logging.exception(str(e), exc_info=True) flash( gettext(u'Error: {}\n' u'Your password has not been changed.').format(e), 'danger') has_error = True if request.json is None and not has_error: do_flash(*get_message('PASSWORD_RESET_REQUEST', email=form.user.email)) if request.json and not has_error: return _render_json(form, include_user=False) return _security.render_template( config_value('FORGOT_PASSWORD_TEMPLATE'), forgot_password_form=form, **_ctx('forgot_password'))
def forgot_password(): """View function that handles a forgotten password request.""" has_error = False form_class = _security.forgot_password_form if request.json: form = form_class(MultiDict(request.json)) else: form = form_class() if form.validate_on_submit(): try: send_reset_password_instructions(form.user) except SOCKETErrorException as e: # Handle socket errors which are not covered by SMTPExceptions. logging.exception(str(e), exc_info=True) flash(gettext(u'SMTP Socket error: {}\n' u'Your password has not been changed.' ).format(e), 'danger') has_error = True except (SMTPConnectError, SMTPResponseException, SMTPServerDisconnected, SMTPDataError, SMTPHeloError, SMTPException, SMTPAuthenticationError, SMTPSenderRefused, SMTPRecipientsRefused) as e: # Handle smtp specific exceptions. logging.exception(str(e), exc_info=True) flash(gettext(u'SMTP error: {}\n' u'Your password has not been changed.' ).format(e), 'danger') has_error = True except Exception as e: # Handle other exceptions. logging.exception(str(e), exc_info=True) flash(gettext(u'Error: {}\n' u'Your password has not been changed.' ).format(e), 'danger') has_error = True if request.json is None and not has_error: do_flash(*get_message('PASSWORD_RESET_REQUEST', email=form.user.email)) if request.json and not has_error: return _render_json(form, include_user=False) return _security.render_template( config_value('FORGOT_PASSWORD_TEMPLATE'), forgot_password_form=form, **_ctx('forgot_password'))
def register_users(): register_user_form = RegisterForm() if request.method == 'POST': if register_user_form.validate_on_submit(): user = register_user(**register_user_form.to_dict()) user.sex = request.form['mf'] user.grade = request.form['grade'] if request.form.get('volun1', None): user.volun1 = 1 else: user.volun1 = 0 if request.form.get('volun2', None): user.volun2 = 1 else: user.volun2 = 0 if request.form.get('volun3', None): user.volun3 = 1 else: user.volun3 = 0 if request.form.get('volun4', None): user.volun4 = 1 else: user.volun4 = 0 if request.form.get('volun5', None): user.volun5 = 1 else: user.volun5 = 0 if request.form.get('volun6', None): user.volun6 = 1 else: user.volun6 = 0 if request.form.get('volun7', None): user.volun7 = 1 else: user.volun7 = 0 if request.form.get('volun8', None): user.volun8 = 1 else: user.volun8 = 0 if request.form.get('volun9', None): user.volun9 = 1 else: user.volun9 = 0 if request.form.get('volun10', None): user.volun10 = 1 else: user.volun10 = 0 if request.form.get('volun11', None): user.volun11 = 1 else: user.volun11 = 0 if request.form.get('volun12', None): user.volun12 = 1 else: user.volun12 = 0 if request.form.get('volun13', None): user.volun13 = 1 else: user.volun13 = 0 if request.form.get('volun14', None): user.volun14 = 1 else: user.volun14 = 0 if request.form.get('volun15', None): user.volun15 = 1 else: user.volun15 = 0 if request.form.get('volun16', None): user.volun16 = 1 else: user.volun16 = 0 if request.form.get('volun17', None): user.volun17 = 1 else: user.volun17 = 0 if request.form.get('volun18', None): user.volun18 = 1 else: user.volun18 = 0 if request.form.get('volun19', None): user.volun19 = 1 else: user.volun19 = 0 if request.form.get('volun20', None): user.volun20 = 1 else: user.volun20 = 0 user.prefer_bloc1 = request.form['category'] user.prefer_sloc1 = request.form['choices'] user.prefer_bloc2 = request.form['category1'] user.prefer_sloc2 = request.form['choices1'] user.prefer_bloc3 = request.form['category2'] user.prefer_sloc3 = request.form['choices2'] register_user_form.user = user db.session.commit() return redirect(url_for('index')) # register가 완성되면 초기페이지로 else: flash(register_user_form.errors, 'info') return render_template("register.html", register_user_form=register_user_form, **_ctx('register')) else: return render_template("register.html", register_user_form=register_user_form)
def register_users(): register_user_form = RegisterForm() if request.method == 'POST': if register_user_form.validate_on_submit(): user = register_user(**register_user_form.to_dict()) user.sex = request.form['mf'] user.grade = request.form['grade'] # if request.form.get('volun1', None): # user.volun1 = 1 # else: # user.volun1 = 0 # # if request.form.get('volun2', None): # user.volun2 = 1 # else: # user.volun2 = 0 # # if request.form.get('volun3', None): # user.volun3 = 1 # else: # user.volun3 = 0 # # if request.form.get('volun4', None): # user.volun4 = 1 # else: # user.volun4 = 0 # # if request.form.get('volun5', None): # user.volun5 = 1 # else: # user.volun5 = 0 # # if request.form.get('volun6', None): # user.volun6 = 1 # else: # user.volun6 = 0 # # if request.form.get('volun7', None): # user.volun7 = 1 # else: # user.volun7 = 0 # # if request.form.get('volun8', None): # user.volun8 = 1 # else: # user.volun8 = 0 # # if request.form.get('volun9', None): # user.volun9 = 1 # else: # user.volun9 = 0 # # if request.form.get('volun10', None): # user.volun10 = 1 # else: # user.volun10 = 0 # if request.form.get('volun11', None): # user.volun11 = 1 # else: # user.volun11 = 0 # if request.form.get('volun12', None): # user.volun12 = 1 # else: # user.volun12 = 0 # if request.form.get('volun13', None): # user.volun13 = 1 # else: # user.volun13 = 0 # if request.form.get('volun14', None): # user.volun14 = 1 # else: # user.volun14 = 0 # if request.form.get('volun15', None): # user.volun15 = 1 # else: # user.volun15= 0 # if request.form.get('volun16', None): # user.volun16 = 1 # else: # user.volun16 = 0 # if request.form.get('volun17', None): # user.volun17 = 1 # else: # user.volun17 = 0 # if request.form.get('volun18', None): # user.volun18 = 1 # else: # user.volun18 = 0 # if request.form.get('volun19', None): # user.volun19 = 1 # else: # user.volun19 = 0 # if request.form.get('volun20', None): # user.volun20 = 1 # else: # user.volun20 = 0 volun1 = 1 if request.form.get('volun1', None) else 0 volun2 = 1 if request.form.get('volun2', None) else 0 volun3 = 1 if request.form.get('volun3', None) else 0 volun4 = 1 if request.form.get('volun4', None) else 0 volun5 = 1 if request.form.get('volun5', None) else 0 volun6 = 1 if request.form.get('volun6', None) else 0 volun7 = 1 if request.form.get('volun7', None) else 0 volun8 = 1 if request.form.get('volun8', None) else 0 volun9 = 1 if request.form.get('volun9', None) else 0 volun10 = 1 if request.form.get('volun10', None) else 0 volun11 = 1 if request.form.get('volun11', None) else 0 volun12 = 1 if request.form.get('volun12', None) else 0 volun13 = 1 if request.form.get('volun13', None) else 0 volun14 = 1 if request.form.get('volun14', None) else 0 volun15 = 1 if request.form.get('volun15', None) else 0 volun16 = 1 if request.form.get('volun16', None) else 0 volun17 = 1 if request.form.get('volun17', None) else 0 volun18 = 1 if request.form.get('volun18', None) else 0 volun19 = 1 if request.form.get('volun19', None) else 0 volun20 = 1 if request.form.get('volun20', None) else 0 volun21 = 1 if request.form.get('volun21', None) else 0 volun22 = 1 if request.form.get('volun22', None) else 0 recruit1 = 1 if request.form.get('recruit1', None) else 0 recruit2 = 1 if request.form.get('recruit2', None) else 0 recruit3 = 1 if request.form.get('recruit3', None) else 0 recruit4 = 1 if request.form.get('recruit4', None) else 0 recruit5 = 1 if request.form.get('recruit5', None) else 0 recruit6 = 1 if request.form.get('recruit6', None) else 0 recruit7 = 1 if request.form.get('recruit7', None) else 0 recruit8 = 1 if request.form.get('recruit8', None) else 0 recruit9 = 1 if request.form.get('recruit9', None) else 0 recruit10 = 1 if request.form.get('recruit10', None) else 0 recruit11 = 1 if request.form.get('recruit11', None) else 0 temp_category = User_category() temp_category.id = user.id temp_category.category_life = volun1 temp_category.category_world = volun2 temp_category.category_environment = volun3 temp_category.category_human = volun4 temp_category.category_disaster = volun5 temp_category.category_country = volun6 temp_category.category_home = volun7 temp_category.category_medic = volun8 temp_category.category_education = volun9 temp_category.category_administration = volun10 temp_category.category_consulting = volun11 temp_category.category_culture = volun12 temp_category.category_physical = volun13 temp_category.category_history = volun14 temp_category.category_circles = volun15 temp_category.category_travel = volun16 temp_category.category_marketing = volun17 temp_category.category_social = volun18 temp_category.category_plan = volun19 temp_category.category_language = volun20 temp_category.category_scene = volun21 temp_category.category_etc = volun22 temp_category.recruit_disabled = recruit1 temp_category.recruit_oldman = recruit2 temp_category.recruit_foreigner = recruit3 temp_category.recruit_homeless = recruit4 temp_category.recruit_multiculture = recruit5 temp_category.recruit_worker = recruit6 temp_category.recruit_baby = recruit7 temp_category.recruit_women = recruit8 temp_category.recruit_zzokbang = recruit9 temp_category.recruit_teenager = recruit10 temp_category.recruit_nation = recruit11 user.prefer_bloc1 = request.form['category'] user.prefer_sloc1 = request.form['choices'] user.prefer_bloc2 = request.form['category1'] user.prefer_sloc2 = request.form['choices1'] user.prefer_bloc3 = request.form['category2'] user.prefer_sloc3 = request.form['choices2'] user.university = request.form['university'] user.majoring = request.form['majoring'] register_user_form.user = user db.session.add(temp_category) db.session.commit() global univer, major major = "" univer = "" return user_detail() # return redirect(url_for('user_detail')) # register가 완성되면 초기페이지로 else: flash(register_user_form.errors, 'info') return render_template("register.html", register_user_form=register_user_form, **_ctx('register')) else: return render_template("register.html", register_user_form=register_user_form, univer=univer, major=major)
def reset_password(token): """View function that handles a reset password request.""" expired, invalid, user = reset_password_token_status(token) if invalid: do_flash(*get_message('INVALID_RESET_PASSWORD_TOKEN')) if expired: do_flash(*get_message('PASSWORD_RESET_EXPIRED', email=user.email, within=_security.reset_password_within)) if invalid or expired: return redirect(url_for('browser.forgot_password')) has_error = False form = _security.reset_password_form() if form.validate_on_submit(): try: update_password(user, form.password.data) except SOCKETErrorException as e: # Handle socket errors which are not covered by SMTPExceptions. logging.exception(str(e), exc_info=True) flash(gettext(SMTP_SOCKET_ERROR).format(e), 'danger') has_error = True except (SMTPConnectError, SMTPResponseException, SMTPServerDisconnected, SMTPDataError, SMTPHeloError, SMTPException, SMTPAuthenticationError, SMTPSenderRefused, SMTPRecipientsRefused) as e: # Handle smtp specific exceptions. logging.exception(str(e), exc_info=True) flash(gettext(SMTP_ERROR).format(e), 'danger') has_error = True except Exception as e: # Handle other exceptions. logging.exception(str(e), exc_info=True) flash(gettext(PASS_ERROR).format(e), 'danger') has_error = True if not has_error: after_this_request(view_commit) auth_obj = AuthSourceManager(form, [INTERNAL]) session['_auth_source_manager_obj'] = auth_obj.as_dict() if user.login_attempts >= config.MAX_LOGIN_ATTEMPTS > 0: flash( gettext('You successfully reset your password but' ' your account is locked. Please contact ' 'the Administrator.'), 'warning') return redirect(get_post_logout_redirect()) do_flash(*get_message('PASSWORD_RESET')) login_user(user) auth_obj = AuthSourceManager(form, [INTERNAL]) session['auth_source_manager'] = auth_obj.as_dict() return redirect( get_url(_security.post_reset_view) or get_url(_security.post_login_view)) return _security.render_template( config_value('RESET_PASSWORD_TEMPLATE'), reset_password_form=form, reset_password_token=token, **_ctx('reset_password'))