Fix db migration issue

This commit is contained in:
Khanh Ngo 2018-08-23 09:23:21 +07:00
parent 66e9be8c7a
commit 32dcc6482f
No known key found for this signature in database
GPG Key ID: B9AE3BAF6D5A7B22
3 changed files with 18 additions and 12 deletions

View File

@ -37,8 +37,4 @@ if app.config.get('SAML_ENABLED') and app.config.get('SAML_ENCRYPT'):
certutil.create_self_signed_cert() certutil.create_self_signed_cert()
from app import models from app import models
from app import views
try:
from app import views
except:
logging.error("You have not initialized the DB yet or DB migration is running...")

View File

@ -7,7 +7,6 @@ from app.models import Setting
# TODO: # TODO:
# - Replace Flask-OAuthlib by authlib # - Replace Flask-OAuthlib by authlib
# - Fix flask-migrate issue with calling Setting() class during downgrade / upgrade
# - Fix github/google enabling (Currently need to reload the flask app) # - Fix github/google enabling (Currently need to reload the flask app)
def github_oauth(): def github_oauth():

View File

@ -28,9 +28,10 @@ if app.config['SAML_ENABLED']:
from onelogin.saml2.auth import OneLogin_Saml2_Auth from onelogin.saml2.auth import OneLogin_Saml2_Auth
from onelogin.saml2.utils import OneLogin_Saml2_Utils from onelogin.saml2.utils import OneLogin_Saml2_Utils
google = None
github = None
logging = logger.getLogger(__name__) logging = logger.getLogger(__name__)
google = google_oauth()
github = github_oauth()
# FILTERS # FILTERS
app.jinja_env.filters['display_record_name'] = utils.display_record_name app.jinja_env.filters['display_record_name'] = utils.display_record_name
@ -50,6 +51,14 @@ def inject_setting():
return dict(SETTING=setting) return dict(SETTING=setting)
@app.before_first_request
def register_modules():
global google
global github
google = google_oauth()
github = github_oauth()
# START USER AUTHENTICATION HANDLER # START USER AUTHENTICATION HANDLER
@app.before_request @app.before_request
def before_request(): def before_request():
@ -143,7 +152,8 @@ def register():
@app.route('/google/login') @app.route('/google/login')
def google_login(): def google_login():
if not Setting().get('google_oauth_enabled'): if not Setting().get('google_oauth_enabled') or google is None:
logging.error('Google OAuth is disabled or you have not yet reloaded the pda application after enabling.')
return abort(400) return abort(400)
else: else:
return google.authorize(callback=url_for('google_authorized', _external=True)) return google.authorize(callback=url_for('google_authorized', _external=True))
@ -151,7 +161,8 @@ def google_login():
@app.route('/github/login') @app.route('/github/login')
def github_login(): def github_login():
if not Setting().get('github_oauth_enabled'): if not Setting().get('github_oauth_enabled') or github is None:
logging.error('Github OAuth is disabled or you have not yet reloaded the pda application after enabling.')
return abort(400) return abort(400)
else: else:
return github.authorize(callback=url_for('github_authorized', _external=True)) return github.authorize(callback=url_for('github_authorized', _external=True))
@ -1445,7 +1456,7 @@ def admin_setting_authentication():
Setting().set('google_token_params', request.form.get('google_token_params')) Setting().set('google_token_params', request.form.get('google_token_params'))
Setting().set('google_authorize_url', request.form.get('google_authorize_url')) Setting().set('google_authorize_url', request.form.get('google_authorize_url'))
Setting().set('google_base_url', request.form.get('google_base_url')) Setting().set('google_base_url', request.form.get('google_base_url'))
result = {'status': True, 'msg': 'Saved successfully'} result = {'status': True, 'msg': 'Saved successfully. Please reload PDA to take effect.'}
elif conf_type == 'github': elif conf_type == 'github':
Setting().set('github_oauth_enabled', True if request.form.get('github_oauth_enabled') else False) Setting().set('github_oauth_enabled', True if request.form.get('github_oauth_enabled') else False)
Setting().set('github_oauth_key', request.form.get('github_oauth_key')) Setting().set('github_oauth_key', request.form.get('github_oauth_key'))
@ -1454,7 +1465,7 @@ def admin_setting_authentication():
Setting().set('github_oauth_api_url', request.form.get('github_oauth_api_url')) Setting().set('github_oauth_api_url', request.form.get('github_oauth_api_url'))
Setting().set('github_oauth_token_url', request.form.get('github_oauth_token_url')) Setting().set('github_oauth_token_url', request.form.get('github_oauth_token_url'))
Setting().set('github_oauth_authorize_url', request.form.get('github_oauth_authorize_url')) Setting().set('github_oauth_authorize_url', request.form.get('github_oauth_authorize_url'))
result = {'status': True, 'msg': 'Saved successfully'} result = {'status': True, 'msg': 'Saved successfully. Please reload PDA to take effect.'}
else: else:
return abort(400) return abort(400)