def login(): """Log in an existing user.""" if current_user.is_authenticated: # if user is logged in we get out of here return redirect(url_for('dashboard.index')) form = LoginForm() if form.validate_on_submit(): user = User.query.filter_by(username=form.username.data).first() if user is not None and user.password_hash is not None and \ user.verify_password(form.password.data) \ and user.verify_totp(form.token.data): login_user(user, form.remember_me.data) flash('You are now logged in. Welcome back!', 'success') save_action(current_user, request, " Successfully authenticated") return redirect( request.args.get('next') or url_for('dashboard.index')) else: save_action(current_user, request, "Inserted wrong username or password") flash('Invalid username,password or token.', 'error') flash('Invalid username,password or token.', 'form-error') return render_template('account/login.html', form=form, header_nav_info=HeaderNavbarInfo(), sidebar_info=SidebarInfo())
def delete_user(user_id): """Delete a user's account.""" if current_user.id == user_id: flash( 'You cannot delete your own account. Please ask another ' 'administrator to do this.', 'error') save_action(current_user, request, "Tried to delete own account.") else: user = User.query.filter_by(id=user_id).first() db.session.delete(user) db.session.commit() flash('Successfully deleted user %s.' % user.full_name(), 'success') save_action(current_user, request, 'Successfully deleted user %s.' % user.full_name()) return redirect(url_for('admin.registered_users'))
def new_user(): """Create a new user.""" form = NewUserForm() if form.validate_on_submit(): user = User(role=form.role.data, username=form.username.data, password=form.password.data) db.session.add(user) db.session.commit() flash('User {} successfully created'.format(user.full_name()), 'form-success') save_action( current_user, request, "Added new user" + form.username.data + "with " + form.role.data.name + " role.") return render_template('admin/new_user.html', form=form, sidebar_info=SidebarInfo(), header_nav_info=HeaderNavbarInfo())
def change_password(): """Change an existing user's password.""" form = ChangePasswordForm() if form.validate_on_submit(): if current_user.verify_password(form.old_password.data): current_user.password = form.new_password.data db.session.add(current_user) db.session.commit() flash('Your password has been updated.', 'success') flash('Your password has been updated.', 'form-success') save_action(current_user, request, "Changed Password") return redirect(url_for('main.index')) else: save_action( current_user, request, "Inserted wrong password while trying to change password") flash('Original password is invalid.', 'error') flash('Original password is invalid.', 'form-error') return render_template('account/change_password.html', form=form, user=current_user, header_nav_info=HeaderNavbarInfo(), sidebar_info=SidebarInfo())
def change_account_type(user_id): """Change a user's account type.""" if current_user.id == user_id: flash( 'You cannot change the type of your own account. Please ask ' 'another administrator to do this.', 'error') save_action(current_user, request, "Tried to change Account type of Own Account.") return redirect( url_for('admin.user_info', user_id=user_id, sidebar_info=SidebarInfo(), header_nav_info=HeaderNavbarInfo())) user = User.query.get(user_id) if user is None: abort(404) form = ChangeAccountTypeForm() if form.validate_on_submit(): user.role = form.role.data db.session.add(user) db.session.commit() flash( 'Role for user {} successfully changed to {}.'.format( user.full_name(), user.role.name), 'form-success') flash( 'Role for user {} successfully changed to {}.'.format( user.full_name(), user.role.name), 'success') save_action( current_user, request, 'Role for user {} successfully changed to {}.'.format( user.full_name(), user.role.name)) return render_template('admin/manage_user_change_account_type.html', user=user, form=form, sidebar_info=SidebarInfo(), header_nav_info=HeaderNavbarInfo())
def change_location(): """Save location""" try: hostname = str(request.form.get('hostname')) location = str(request.form.get('location')) loc = LocationFieldPanel(hostname=hostname, number=location) if LocationFieldPanel.query.filter_by(hostname=hostname).first() is None \ and LocationFieldPanel.query.filter_by(number=location).first() is None: # session.query(User).filter_by(id=123).update({"name": u"Bob Marley"}) db.session.add(loc) db.session.commit() save_action( current_user, request, "Added location: {} for rig: {} ".format( loc.number, loc.hostname)) else: loc = LocationFieldPanel.query.filter_by(hostname=hostname).first() if LocationFieldPanel.query.filter_by( number=location).first() is None: loc.hostname = hostname loc.number = location db.session.add(loc) db.session.commit() save_action( current_user, request, "Added location: {} for rig: {} ".format( loc.number, loc.hostname)) else: loc = LocationFieldPanel.query.filter_by( number=location).first() loc.hostname = hostname loc.number = location db.session.add(loc) db.session.commit() save_action( current_user, request, "Changed location: {} for rig: {} ".format( loc.number, loc.hostname)) except Exception as e: print(e) db.session.rollback() abort(500) save_action(current_user, request, "Failed to add/change location") raise return "200"
def logout(): logout_user() save_action(current_user, request, "Logged out") flash('You have been logged out.', 'info') return redirect(url_for('main.index'))
def panel_dash_rig(panel_name, rig_name): form = ManagementRigsForm() if not is_panel_exist(panel_name=panel_name): abort(404) else: if form.validate_on_submit(): user = User.query.filter_by(username=form.username.data).first() if user is not None and user.password_hash is not None and \ user.verify_password(form.password.data): hosts = request.form.get('selectedHosts[]') hosts = ast.literal_eval(hosts) hosts = [n.strip() for n in hosts] commands = { 'clear_thermals': form.clear_thermals.data, 'put_conf': form.put_conf.data, 'reboot': form.reboot_task.data, "update_miners": form.update_miners.data, "allow_command": form.allow_command.data, "update_miner_with_name": form.update_miner_with_name.data, "change_password": form.change_password.data, "execute_custom_command": form.execute_custom_command.data } # hostnames = request.form.get('selectedHostnames[]') if hosts and form.ssh_username.data and form.ssh_password.data \ and ( form.clear_thermals.data or form.put_conf.data or form.reboot_task.data or form.update_miners.data or form.allow_command.data or form.update_miner_with_name.data or form.change_password.data or form.execute_custom_command.data): results = execute_commands_on_multiple_rigs.apply_async( args=[], kwargs={ "panel_name": panel_name, "hosts": hosts, "commands": commands, "username": form.ssh_username.data, "password": form.ssh_password.data, "miner_name": form.miner_name.data, "new_password": form.new_password.data, "custom_command": form.custom_command.data }, expires=60) # task = execute_rig_reboot.apply_async(args=[hosts[0], form.ssh_username.data, # form.ssh_password.data]) flash('Commands have been sent to selected rigs.', 'success') act_list = ([ str(k) for k, v in commands.items() if v == True ]) act_str = "" if act_list: for s in act_list: act_str += " " + s action = "Sent " + act_str + " to " + str(hosts) save_action(current_user, request, action) else: flash('No hosts selected,or ssh fields are empty.', 'error') return render_template('dashboard/dashboard_panel.html', panel_dash=PanelDashboard( panel_name, rig_name=rig_name), sidebar_info=SidebarInfo(), header_nav_info=HeaderNavbarInfo(), form=form) # return redirect(request.args.get('next') or url_for('dashboard.index')) else: flash('Invalid username or password.', 'form-error') flash('Invalid username or password.', 'error') return render_template('dashboard/dashboard_panel.html', panel_dash=PanelDashboard( panel_name, rig_name=rig_name), sidebar_info=SidebarInfo(), header_nav_info=HeaderNavbarInfo(), form=form) else: return render_template('dashboard/dashboard_panel.html', panel_dash=PanelDashboard( panel_name, rig_name=rig_name), sidebar_info=SidebarInfo(), header_nav_info=HeaderNavbarInfo(), form=form)