def home(): try: d = DomainQuery() domain_db = d.get_data() except Exception as e: return render_template('error.html', errorinfo=traceback.print_exc()) lastUpdated = datetime.strftime(datetime.now(), 'atualizado em %d-%m-%Y %H:%M:%S %p') return render_template('home.html', domain_data=domain_db, timestamp=lastUpdated)
def try_login(username, password): if app.config['auth_mode'] == 'ldap': conn = ldap.initialize(app.config['LDAP_PROVIDER_URL']) if conn is None: flash('error connecting to LDAP server') else: DomainQuery.log( 'authentication options: certfile: %s, user=%s, password=%s, search base=%s' % (app.config['LDAP_CACERT'], username, password, app.config['LDAP_SEARCH_BASE'])) conn.set_option(ldap.OPT_X_TLS, True) if app.config['LDAP_CACERT'] != '': conn.set_option(ldap.OPT_X_TLS_CACERTFILE, app.config['LDAP_CACERT']) conn.simple_bind_s( '%s,%s' % (app.config['LDAP_BIND_USER'], app.config['LDAP_BIND_DN']), app.config['LDAP_AUTHTOK']) #conn.simple_bind_s('uid=%s,%s' % (username, app.config['LDAP_BIND_DN']), password) # do a search query = 'uid=%s' % username result = conn.search_s(app.config['LDAP_SEARCH_BASE'], ldap.SCOPE_SUBTREE, query) DomainQuery.log('result = [%s]' % result) if result: userdata = result[0][1] displayname = userdata['cn'][0].decode('utf-8') return (True, displayname, userdata) else: flash('authentication error') return (False, '', {}) elif app.config['auth_mode'] == 'plain': if password == app.config[ 'default_password'] and username == app.config[ 'default_user']: displayname = app.config['default_user'] return (True, displayname, {}) return (False, '', {})
def startvm(): dom = DomainQuery() domain_db = dom.get_data() vm_name = request.args.get('name') if vm_name is None: return render_template('startvm.html', error='VM not specified') else: DomainQuery.log('VM %s is turned off, turning it on' % vm_name) for d in domain_db: #DomainQuery.log('domain: %s' % str(d)) if d['name'] == vm_name: DomainQuery.log('create() = %d' % d['object'].create()) return render_template('startvm.html', error='%s started' % vm_name)
def restartvm(): dom = DomainQuery() domain_db = dom.get_data() vm_name = request.args.get('name') if vm_name is None: return render_template('restartvm.html', error='VM not specified') else: DomainQuery.log('Restarting VM: %s' % vm_name) for d in domain_db: #DomainQuery.log('domain: %s' % str(d)) if d['name'] == vm_name: DomainQuery.log('reset() = %d' % d['object'].reset()) return render_template('restartvm.html', error='%s reset' % vm_name)
def login(): if current_user.is_authenticated: flash('You are already logged in.') return redirect(url_for('auth.home')) form = LoginForm() if request.method == 'POST' and form.validate(): username = request.form.get('username') password = request.form.get('password') try: displayname = '' userdata = {} authenticated = False authenticated, displayname, userdata = User.try_login(username, password) except ldap.INVALID_CREDENTIALS: flash('Invalid username or password. Please try again.', 'danger') return render_template('login.html', form=form) if not authenticated: flash('Invalid username or password. Please try again.', 'danger') return render_template('login.html', form=form) user = User.query.filter_by(username=username).first() if user is None: user = User(username, password) DomainQuery.log('user not found, creating new record: user = %s, id = %s' % (user.username, user.id)) else: DomainQuery.log('found user record for user %s, id = %s' % (user.displayname, user.id)) user.set_displayname(displayname) DomainQuery.log('setting last IP to: %s' % str(request.remote_addr)) user.set_last_ip(str(request.remote_addr)) # commit the user record to the database db.session.add(user) db.session.flush() db.session.refresh(user) db.session.commit() login_user(user, force=True, remember=True) flash('You have successfully logged in.', 'success') return redirect(url_for('auth.home')) if form.errors: flash(form.errors, 'danger') return render_template('login.html', form=form)
def show_error(): error=process_exception(exception) DomainQuery.log('Fatal error: showing error page (%s)' % error['title']) return render_template('error.html', errorinfo=error)
def shutdown_session(exception=None): DomainQuery.close()
from flask import Flask import os import flask_login from flask_sqlalchemy import SQLAlchemy from backend.libvirtbridge import DomainQuery # translation support try: import gettext gettext.install('labvirtual', '/usr/share/locale') except FileNotFoundError as e: DomainQuery.log("no translation file available") app = Flask(__name__) app.secret_key = os.urandom(12) app.debug = False # load configuration import config as cfg for key, value in cfg.config.items(): app.config[key] = value login_manager = flask_login.LoginManager() login_manager.init_app(app) login_manager.login_view = 'auth.login' db = SQLAlchemy(app) from frontend.auth.views import auth app.register_blueprint(auth)