def create_admin(): if admin_exists(): flash( gettext("Cannot access admin creation form if an admin user " "already exists."), "error") return redirect(url_for('general_routes.home')) form = flaskforms.CreateAdmin() if request.method == 'POST': if form.validate(): error = False if form.password.data != form.password_repeat.data: flash(gettext("Passwords do not match. Please try again."), "error") error = True if not test_username(form.username.data): flash( gettext( "Invalid user name. Must be between 2 and 64 characters " "and only contain letters and numbers."), "error") error = True if not test_password(form.password.data): flash( gettext( "Invalid password. Must be between 6 and 64 characters " "and only contain letters, numbers, and symbols."), "error") error = True if error: return redirect(url_for('general_routes.home')) new_user = Users() new_user.user_name = form.username.data new_user.user_email = form.email.data new_user.set_password(form.password.data) new_user.user_restriction = 'admin' new_user.user_theme = 'slate' try: with session_scope( current_app.config['USER_DB_PATH']) as db_session: db_session.add(new_user) flash( gettext( "User '%(user)s' successfully created. Please " "log in below.", user=form.username.data), "success") return redirect(url_for('authentication_routes.do_login')) except Exception as except_msg: flash( gettext("Failed to create user '%(user)s': %(err)s", user=form.username.data, err=except_msg), "error") else: flash_form_errors(form) return render_template('create_admin.html', form=form)
def add_user(admin=False): new_user = Users() print('\nAdd user to database') while True: user_name = raw_input('User (a-z, A-Z, 2-64 chars): ') if test_username(user_name): new_user.user_name = user_name break while True: user_password = getpass.getpass('Password: '******'Password (again): ') if user_password != user_password_again: print("Passwords don't match") else: if test_password(user_password): new_user.set_password(user_password) break while True: user_email = raw_input('Email: ') if is_email(user_email): new_user.user_email = user_email break if admin: new_user.user_restriction = 'admin' else: new_user.user_restriction = 'guest' new_user.user_theme = 'slate' try: with session_scope(USER_DB_PATH) as db_session: db_session.add(new_user) sys.exit(0) except sqlalchemy.exc.OperationalError: print("Failed to create user. You most likely need to " "create the DB before trying to create users.") sys.exit(1) except sqlalchemy.exc.IntegrityError: print("Username already exists.") sys.exit(1)
def add_user(admin=False): new_user = Users() print('\nAdd user to database') while True: user_name = raw_input('User (a-z, A-Z, 2-64 chars): ') if test_username(user_name): new_user.user_name = user_name break while True: user_password = getpass.getpass('Password: '******'Password (again): ') if user_password != user_password_again: print("Passwords don't match") else: if test_password(user_password): new_user.set_password(user_password) break while True: user_email = raw_input('Email: ') if is_email(user_email): new_user.user_email = user_email break if admin: new_user.user_restriction = 'admin' else: new_user.user_restriction = 'guest' new_user.user_theme = 'dark' try: with session_scope(USER_DB_PATH) as db_session: db_session.add(new_user) sys.exit(0) except sqlalchemy.exc.OperationalError: print("Failed to create user. You most likely need to " "create the DB before trying to create users.") sys.exit(1) except sqlalchemy.exc.IntegrityError: print("Username already exists.") sys.exit(1)
def change_password(username): print('Changing password for {}'.format(username)) with session_scope(USER_DB_PATH) as db_session: user = db_session.query(Users).filter(Users.user_name == username).one() while True: user_password = getpass.getpass('Password: '******'Password (again): ') if user_password != user_password_again: print("Passwords don't match") else: try: if test_password(user_password): user.set_password(user_password) sys.exit(0) except sqlalchemy.orm.exc.NoResultFound: print("No user found with this name.") sys.exit(1)
def change_password(username): print('Changing password for {}'.format(username)) with session_scope(USER_DB_PATH) as db_session: user = db_session.query(Users).filter( Users.user_name == username).one() while True: user_password = getpass.getpass('Password: '******'Password (again): ') if user_password != user_password_again: print("Passwords don't match") else: try: if test_password(user_password): user.set_password(user_password) sys.exit(0) except sqlalchemy.orm.exc.NoResultFound: print("No user found with this name.") sys.exit(1)