@admin_bp.route('/setting/authentication', methods=['GET', 'POST'])
@login_required
@admin_role_required
def setting_authentication():
    if (request.method == 'GET'):
        return render_template('admin_setting_authentication.html')
    elif (request.method == 'POST'):
        conf_type = request.form.get('config_tab')
        result = None
        if (conf_type == 'general'):
            local_db_enabled = (True if request.form.get('local_db_enabled') else False)
            signup_enabled = (True if request.form.get('signup_enabled') else False)
            if (not has_an_auth_method(local_db_enabled=local_db_enabled)):
                result = {'status': False, 'msg': 'Must have at least one authentication method enabled.'}
            else:
                Setting().set('local_db_enabled', local_db_enabled)
                Setting().set('signup_enabled', signup_enabled)
                result = {'status': True, 'msg': 'Saved successfully'}
        elif (conf_type == 'ldap'):
            ldap_enabled = (True if request.form.get('ldap_enabled') else False)
            if (not has_an_auth_method(ldap_enabled=ldap_enabled)):
                result = {'status': False, 'msg': 'Must have at least one authentication method enabled.'}
            else:
                Setting().set('ldap_enabled', ldap_enabled)
                Setting().set('ldap_type', request.form.get('ldap_type'))
                Setting().set('ldap_uri', request.form.get('ldap_uri'))
                Setting().set('ldap_base_dn', request.form.get('ldap_base_dn'))
                Setting().set('ldap_admin_username', request.form.get('ldap_admin_username'))
                Setting().set('ldap_admin_password', request.form.get('ldap_admin_password'))
                Setting().set('ldap_filter_basic', request.form.get('ldap_filter_basic'))
                Setting().set('ldap_filter_group', request.form.get('ldap_filter_group'))
                Setting().set('ldap_filter_username', request.form.get('ldap_filter_username'))
                Setting().set('ldap_filter_groupname', request.form.get('ldap_filter_groupname'))
                Setting().set('ldap_sg_enabled', (True if (request.form.get('ldap_sg_enabled') == 'ON') else False))
                Setting().set('ldap_admin_group', request.form.get('ldap_admin_group'))
                Setting().set('ldap_operator_group', request.form.get('ldap_operator_group'))
                Setting().set('ldap_user_group', request.form.get('ldap_user_group'))
                Setting().set('ldap_domain', request.form.get('ldap_domain'))
                Setting().set('autoprovisioning', (True if (request.form.get('autoprovisioning') == 'ON') else False))
                Setting().set('autoprovisioning_attribute', request.form.get('autoprovisioning_attribute'))
                if (request.form.get('autoprovisioning') == 'ON'):
                    if validateURN(request.form.get('urn_value')):
                        Setting().set('urn_value', request.form.get('urn_value'))
                    else:
                        return render_template('admin_setting_authentication.html', error='Invalid urn')
                else:
                    Setting().set('urn_value', request.form.get('urn_value'))
                Setting().set('purge', (True if (request.form.get('purge') == 'ON') else False))
                result = {'status': True, 'msg': 'Saved successfully'}
        elif (conf_type == 'google'):
            google_oauth_enabled = (True if request.form.get('google_oauth_enabled') else False)
            if (not has_an_auth_method(google_oauth_enabled=google_oauth_enabled)):
                result = {'status': False, 'msg': 'Must have at least one authentication method enabled.'}
            else:
                Setting().set('google_oauth_enabled', google_oauth_enabled)
                Setting().set('google_oauth_client_id', request.form.get('google_oauth_client_id'))
                Setting().set('google_oauth_client_secret', request.form.get('google_oauth_client_secret'))
                Setting().set('google_token_url', request.form.get('google_token_url'))
                Setting().set('google_oauth_scope', request.form.get('google_oauth_scope'))
                Setting().set('google_authorize_url', request.form.get('google_authorize_url'))
                Setting().set('google_base_url', request.form.get('google_base_url'))
                result = {'status': True, 'msg': 'Saved successfully. Please reload PDA to take effect.'}
        elif (conf_type == 'github'):
            github_oauth_enabled = (True if request.form.get('github_oauth_enabled') else False)
            if (not has_an_auth_method(github_oauth_enabled=github_oauth_enabled)):
                result = {'status': False, 'msg': 'Must have at least one authentication method enabled.'}
            else:
                Setting().set('github_oauth_enabled', github_oauth_enabled)
                Setting().set('github_oauth_key', request.form.get('github_oauth_key'))
                Setting().set('github_oauth_secret', request.form.get('github_oauth_secret'))
                Setting().set('github_oauth_scope', request.form.get('github_oauth_scope'))
                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_authorize_url', request.form.get('github_oauth_authorize_url'))
                result = {'status': True, 'msg': 'Saved successfully. Please reload PDA to take effect.'}
        elif (conf_type == 'azure'):
            azure_oauth_enabled = (True if request.form.get('azure_oauth_enabled') else False)
            if (not has_an_auth_method(azure_oauth_enabled=azure_oauth_enabled)):
                result = {'status': False, 'msg': 'Must have at least one authentication method enabled.'}
            else:
                Setting().set('azure_oauth_enabled', azure_oauth_enabled)
                Setting().set('azure_oauth_key', request.form.get('azure_oauth_key'))
                Setting().set('azure_oauth_secret', request.form.get('azure_oauth_secret'))
                Setting().set('azure_oauth_scope', request.form.get('azure_oauth_scope'))
                Setting().set('azure_oauth_api_url', request.form.get('azure_oauth_api_url'))
                Setting().set('azure_oauth_token_url', request.form.get('azure_oauth_token_url'))
                Setting().set('azure_oauth_authorize_url', request.form.get('azure_oauth_authorize_url'))
                Setting().set('azure_sg_enabled', (True if (request.form.get('azure_sg_enabled') == 'ON') else False))
                Setting().set('azure_admin_group', request.form.get('azure_admin_group'))
                Setting().set('azure_operator_group', request.form.get('azure_operator_group'))
                Setting().set('azure_user_group', request.form.get('azure_user_group'))
                Setting().set('azure_group_accounts_enabled', (True if (request.form.get('azure_group_accounts_enabled') == 'ON') else False))
                Setting().set('azure_group_accounts_name', request.form.get('azure_group_accounts_name'))
                Setting().set('azure_group_accounts_name_re', request.form.get('azure_group_accounts_name_re'))
                Setting().set('azure_group_accounts_description', request.form.get('azure_group_accounts_description'))
                Setting().set('azure_group_accounts_description_re', request.form.get('azure_group_accounts_description_re'))
                result = {'status': True, 'msg': 'Saved successfully. Please reload PDA to take effect.'}
        elif (conf_type == 'oidc'):
            oidc_oauth_enabled = (True if request.form.get('oidc_oauth_enabled') else False)
            if (not has_an_auth_method(oidc_oauth_enabled=oidc_oauth_enabled)):
                result = {'status': False, 'msg': 'Must have at least one authentication method enabled.'}
            else:
                Setting().set('oidc_oauth_enabled', (True if request.form.get('oidc_oauth_enabled') else False))
                Setting().set('oidc_oauth_key', request.form.get('oidc_oauth_key'))
                Setting().set('oidc_oauth_secret', request.form.get('oidc_oauth_secret'))
                Setting().set('oidc_oauth_scope', request.form.get('oidc_oauth_scope'))
                Setting().set('oidc_oauth_api_url', request.form.get('oidc_oauth_api_url'))
                Setting().set('oidc_oauth_token_url', request.form.get('oidc_oauth_token_url'))
                Setting().set('oidc_oauth_authorize_url', request.form.get('oidc_oauth_authorize_url'))
                Setting().set('oidc_oauth_logout_url', request.form.get('oidc_oauth_logout_url'))
                Setting().set('oidc_oauth_username', request.form.get('oidc_oauth_username'))
                Setting().set('oidc_oauth_firstname', request.form.get('oidc_oauth_firstname'))
                Setting().set('oidc_oauth_last_name', request.form.get('oidc_oauth_last_name'))
                Setting().set('oidc_oauth_email', request.form.get('oidc_oauth_email'))
                Setting().set('oidc_oauth_account_name_property', request.form.get('oidc_oauth_account_name_property'))
                Setting().set('oidc_oauth_account_description_property', request.form.get('oidc_oauth_account_description_property'))
                result = {'status': True, 'msg': 'Saved successfully. Please reload PDA to take effect.'}
        else:
            return abort(400)
        return render_template('admin_setting_authentication.html', result=result)
@admin_bp.route('/setting/authentication', methods=['GET', 'POST'])
@login_required
@admin_role_required
def setting_authentication():
    if (request.method == 'GET'):
        return render_template('admin_setting_authentication.html')
    elif (request.method == 'POST'):
        conf_type = request.form.get('config_tab')
        result = None
        if (conf_type == 'general'):
            local_db_enabled = (True if request.form.get('local_db_enabled') else False)
            signup_enabled = (True if request.form.get('signup_enabled') else False)
            if (not has_an_auth_method(local_db_enabled=local_db_enabled)):
                result = {'status': False, 'msg': 'Must have at least one authentication method enabled.'}
            else:
                Setting().set('local_db_enabled', local_db_enabled)
                Setting().set('signup_enabled', signup_enabled)
                result = {'status': True, 'msg': 'Saved successfully'}
        elif (conf_type == 'ldap'):
            ldap_enabled = (True if request.form.get('ldap_enabled') else False)
            if (not has_an_auth_method(ldap_enabled=ldap_enabled)):
                result = {'status': False, 'msg': 'Must have at least one authentication method enabled.'}
            else:
                Setting().set('ldap_enabled', ldap_enabled)
                Setting().set('ldap_type', request.form.get('ldap_type'))
                Setting().set('ldap_uri', request.form.get('ldap_uri'))
                Setting().set('ldap_base_dn', request.form.get('ldap_base_dn'))
                Setting().set('ldap_admin_username', request.form.get('ldap_admin_username'))
                Setting().set('ldap_admin_password', request.form.get('ldap_admin_password'))
                Setting().set('ldap_filter_basic', request.form.get('ldap_filter_basic'))
                Setting().set('ldap_filter_group', request.form.get('ldap_filter_group'))
                Setting().set('ldap_filter_username', request.form.get('ldap_filter_username'))
                Setting().set('ldap_filter_groupname', request.form.get('ldap_filter_groupname'))
                Setting().set('ldap_sg_enabled', (True if (request.form.get('ldap_sg_enabled') == 'ON') else False))
                Setting().set('ldap_admin_group', request.form.get('ldap_admin_group'))
                Setting().set('ldap_operator_group', request.form.get('ldap_operator_group'))
                Setting().set('ldap_user_group', request.form.get('ldap_user_group'))
                Setting().set('ldap_domain', request.form.get('ldap_domain'))
                Setting().set('autoprovisioning', (True if (request.form.get('autoprovisioning') == 'ON') else False))
                Setting().set('autoprovisioning_attribute', request.form.get('autoprovisioning_attribute'))
                if (request.form.get('autoprovisioning') == 'ON'):
                    if validateURN(request.form.get('urn_value')):
                        Setting().set('urn_value', request.form.get('urn_value'))
                    else:
                        return render_template('admin_setting_authentication.html', error='Invalid urn')
                else:
                    Setting().set('urn_value', request.form.get('urn_value'))
                Setting().set('purge', (True if (request.form.get('purge') == 'ON') else False))
                result = {'status': True, 'msg': 'Saved successfully'}
        elif (conf_type == 'google'):
            google_oauth_enabled = (True if request.form.get('google_oauth_enabled') else False)
            if (not has_an_auth_method(google_oauth_enabled=google_oauth_enabled)):
                result = {'status': False, 'msg': 'Must have at least one authentication method enabled.'}
            else:
                Setting().set('google_oauth_enabled', google_oauth_enabled)
                Setting().set('google_oauth_client_id', request.form.get('google_oauth_client_id'))
                Setting().set('google_oauth_client_secret', request.form.get('google_oauth_client_secret'))
                Setting().set('google_token_url', request.form.get('google_token_url'))
                Setting().set('google_oauth_scope', request.form.get('google_oauth_scope'))
                Setting().set('google_authorize_url', request.form.get('google_authorize_url'))
                Setting().set('google_base_url', request.form.get('google_base_url'))
                result = {'status': True, 'msg': 'Saved successfully. Please reload PDA to take effect.'}
        elif (conf_type == 'github'):
            github_oauth_enabled = (True if request.form.get('github_oauth_enabled') else False)
            if (not has_an_auth_method(github_oauth_enabled=github_oauth_enabled)):
                result = {'status': False, 'msg': 'Must have at least one authentication method enabled.'}
            else:
                Setting().set('github_oauth_enabled', github_oauth_enabled)
                Setting().set('github_oauth_key', request.form.get('github_oauth_key'))
                Setting().set('github_oauth_secret', request.form.get('github_oauth_secret'))
                Setting().set('github_oauth_scope', request.form.get('github_oauth_scope'))
                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_authorize_url', request.form.get('github_oauth_authorize_url'))
                result = {'status': True, 'msg': 'Saved successfully. Please reload PDA to take effect.'}
        elif (conf_type == 'azure'):
            azure_oauth_enabled = (True if request.form.get('azure_oauth_enabled') else False)
            if (not has_an_auth_method(azure_oauth_enabled=azure_oauth_enabled)):
                result = {'status': False, 'msg': 'Must have at least one authentication method enabled.'}
            else:
                Setting().set('azure_oauth_enabled', azure_oauth_enabled)
                Setting().set('azure_oauth_key', request.form.get('azure_oauth_key'))
                Setting().set('azure_oauth_secret', request.form.get('azure_oauth_secret'))
                Setting().set('azure_oauth_scope', request.form.get('azure_oauth_scope'))
                Setting().set('azure_oauth_api_url', request.form.get('azure_oauth_api_url'))
                Setting().set('azure_oauth_token_url', request.form.get('azure_oauth_token_url'))
                Setting().set('azure_oauth_authorize_url', request.form.get('azure_oauth_authorize_url'))
                Setting().set('azure_sg_enabled', (True if (request.form.get('azure_sg_enabled') == 'ON') else False))
                Setting().set('azure_admin_group', request.form.get('azure_admin_group'))
                Setting().set('azure_operator_group', request.form.get('azure_operator_group'))
                Setting().set('azure_user_group', request.form.get('azure_user_group'))
                Setting().set('azure_group_accounts_enabled', (True if (request.form.get('azure_group_accounts_enabled') == 'ON') else False))
                Setting().set('azure_group_accounts_name', request.form.get('azure_group_accounts_name'))
                Setting().set('azure_group_accounts_name_re', request.form.get('azure_group_accounts_name_re'))
                Setting().set('azure_group_accounts_description', request.form.get('azure_group_accounts_description'))
                Setting().set('azure_group_accounts_description_re', request.form.get('azure_group_accounts_description_re'))
                result = {'status': True, 'msg': 'Saved successfully. Please reload PDA to take effect.'}
        elif (conf_type == 'oidc'):
            oidc_oauth_enabled = (True if request.form.get('oidc_oauth_enabled') else False)
            if (not has_an_auth_method(oidc_oauth_enabled=oidc_oauth_enabled)):
                result = {'status': False, 'msg': 'Must have at least one authentication method enabled.'}
            else:
                Setting().set('oidc_oauth_enabled', (True if request.form.get('oidc_oauth_enabled') else False))
                Setting().set('oidc_oauth_key', request.form.get('oidc_oauth_key'))
                Setting().set('oidc_oauth_secret', request.form.get('oidc_oauth_secret'))
                Setting().set('oidc_oauth_scope', request.form.get('oidc_oauth_scope'))
                Setting().set('oidc_oauth_api_url', request.form.get('oidc_oauth_api_url'))
                Setting().set('oidc_oauth_token_url', request.form.get('oidc_oauth_token_url'))
                Setting().set('oidc_oauth_authorize_url', request.form.get('oidc_oauth_authorize_url'))
                Setting().set('oidc_oauth_logout_url', request.form.get('oidc_oauth_logout_url'))
                Setting().set('oidc_oauth_username', request.form.get('oidc_oauth_username'))
                Setting().set('oidc_oauth_firstname', request.form.get('oidc_oauth_firstname'))
                Setting().set('oidc_oauth_last_name', request.form.get('oidc_oauth_last_name'))
                Setting().set('oidc_oauth_email', request.form.get('oidc_oauth_email'))
                Setting().set('oidc_oauth_account_name_property', request.form.get('oidc_oauth_account_name_property'))
                Setting().set('oidc_oauth_account_description_property', request.form.get('oidc_oauth_account_description_property'))
                result = {'status': True, 'msg': 'Saved successfully. Please reload PDA to take effect.'}
        else:
            return abort(400)
        return render_template('admin_setting_authentication.html', result=result)