def edit_user(): """Render page for editing user information. This page can only be accessed by an admin """ if not current_user.admin: return abort(403) username = request.args.get('username', '') if username == '': flash('No username specified') abort(404) user = database.get_user(username) if user is None: flash('Unknown username') abort(404) if request.method == 'POST': successful = form_parsers.update_user(username) if successful: database.save_changes() flash('Settings successfully saved', 'info') return redirect(url_for('user_managment')) else: database.revert_changes() flash(messages.ERROR_MESSAGE['invalid_settings'], 'error') user_data = database.get_user_data(username) return render_template('edit_user.html', user=user_data)
def probe_setup(): """Render page for adding/editing probe data. Also parse data on POST. More specifically, the data that can be changed are: - Basic probe info (name, MAC and location) - Script configurations (interval and enabled/disabled for each script) - Network configration (SSID, anon id, username, password, cert) """ probe_id = request.args.get('id', '') if probe_id == '': flash('No probe ID specified') abort(404) probe_id = util.convert_mac(probe_id, mode='storage') probe = database.get_probe(probe_id) if probe is None or probe.user.username != current_user.username: flash('Unknown probe ID') abort(404) if request.method == 'POST': successful_script_update = form_parsers.update_scripts() successful_network_update = form_parsers.update_network_configs() successful_certificate_upload = form_parsers.upload_certificate( probe_id, current_user.username) successful_probe_update = form_parsers.update_probe(probe_id) if (successful_script_update and successful_probe_update and successful_network_update and successful_certificate_upload): database.save_changes() action = request.form.get('action', '') if action == 'save_as_default': ansible.export_group_config( current_user.username, {'group_script_configs': database.get_script_data(probe)}, 'script_configs') ansible.export_group_config( current_user.username, {'networks': database.get_network_config_data(probe)}, 'network_configs') ansible.make_certificate_default(probe_id, current_user.username) return redirect(url_for('probes')) else: database.revert_changes() return generate_probe_setup_template(probe_id, current_user.username)
def user_managment(): """Render page for adding, editing or removing users on GET. Also parse input on POST. This page can only be accessed by an admin. """ if not current_user.admin: return abort(403) if request.method == 'POST': action = request.form.get('action', '') if action == 'new_user': username = request.form.get('username', '') password = request.form.get('password', '') contact_person = request.form.get('contact_person', '') contact_email = request.form.get('contact_email', '') success = database.add_user(username, password, contact_person, contact_email) if not success: database.revert_changes() flash( 'Could not add user. The username is probably already taken', 'error') else: database.save_changes() flash('Successfully added user {}'.format(username), 'info') elif action == 'remove_user': username = request.form.get('username', '') if username == current_user.username: flash('You cannot remove yourself!', 'error') else: success = database.remove_user(username) if success: database.save_changes() flash('User {} successfully removed'.format(username), 'info') else: database.revert_changes() elif action == 'edit_user': username = request.form.get('username', '') return render_template('user_managment.html', users=database.get_all_user_data())
def databases(): """Render database page for GET. Also parse args and export Ansible config for POST.""" if request.method == 'POST': successful = form_parsers.update_databases(current_user.username) if successful: database.save_changes() ansible.export_group_config(current_user.username, { 'databases': database.get_database_info(current_user.username) }, 'database_configs') flash('Settings successfully saved', 'info') else: database.revert_changes() flash(messages.ERROR_MESSAGE['invalid_settings'], 'error') db_info = database.get_database_info(current_user.username) # Make sure the databases occur in the same order each time db_info = OrderedDict(sorted(db_info.items(), key=lambda x: x[0])) return render_template('databases.html', dbs=db_info)