2019-04-16 15:24:59 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
|
|
|
|
'''
|
|
|
|
Flask functions and routes for the settings modules page
|
|
|
|
'''
|
2022-11-28 14:01:40 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2019-04-16 15:24:59 +00:00
|
|
|
from flask import Flask, render_template, jsonify, request, Blueprint, redirect, url_for
|
2019-06-07 15:14:11 +00:00
|
|
|
from flask_login import login_required, current_user
|
2019-04-16 15:24:59 +00:00
|
|
|
|
2019-11-20 15:15:08 +00:00
|
|
|
from Role_Manager import login_admin, login_analyst, login_user, login_read_only
|
2019-06-11 15:37:20 +00:00
|
|
|
|
2022-11-28 14:01:40 +00:00
|
|
|
sys.path.append(os.environ['AIL_BIN'])
|
|
|
|
##################################
|
|
|
|
# Import Project packages
|
|
|
|
##################################
|
|
|
|
from lib import d4
|
|
|
|
from lib import Users
|
2022-07-08 07:47:47 +00:00
|
|
|
|
2019-04-16 15:24:59 +00:00
|
|
|
|
|
|
|
# ============ VARIABLES ============
|
|
|
|
import Flask_config
|
|
|
|
|
|
|
|
app = Flask_config.app
|
|
|
|
baseUrl = Flask_config.baseUrl
|
|
|
|
r_serv_db = Flask_config.r_serv_db
|
|
|
|
REPO_ORIGIN = Flask_config.REPO_ORIGIN
|
2019-04-18 08:56:00 +00:00
|
|
|
dict_update_description = Flask_config.dict_update_description
|
2019-06-20 08:56:31 +00:00
|
|
|
email_regex = Flask_config.email_regex
|
2019-04-17 12:39:29 +00:00
|
|
|
|
2019-04-16 15:24:59 +00:00
|
|
|
settings = Blueprint('settings', __name__, template_folder='templates')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ============ FUNCTIONS ============
|
|
|
|
|
2019-06-20 08:56:31 +00:00
|
|
|
def check_email(email):
|
|
|
|
result = email_regex.match(email)
|
|
|
|
if result:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2019-04-16 15:24:59 +00:00
|
|
|
# ============= ROUTES ==============
|
|
|
|
|
2019-06-07 15:14:11 +00:00
|
|
|
@settings.route("/settings/edit_profile", methods=['GET'])
|
|
|
|
@login_required
|
2019-11-20 15:15:08 +00:00
|
|
|
@login_read_only
|
2019-06-07 15:14:11 +00:00
|
|
|
def edit_profile():
|
2022-07-08 07:47:47 +00:00
|
|
|
user_metadata = Users.get_user_metadata(current_user.get_id())
|
2019-06-20 09:15:56 +00:00
|
|
|
admin_level = current_user.is_in_role('admin')
|
|
|
|
return render_template("edit_profile.html", user_metadata=user_metadata,
|
|
|
|
admin_level=admin_level)
|
2019-06-07 15:14:11 +00:00
|
|
|
|
|
|
|
@settings.route("/settings/new_token", methods=['GET'])
|
|
|
|
@login_required
|
2019-11-20 15:15:08 +00:00
|
|
|
@login_user
|
2019-06-07 15:14:11 +00:00
|
|
|
def new_token():
|
2022-07-08 07:47:47 +00:00
|
|
|
Users.generate_new_token(current_user.get_id())
|
2019-06-07 15:14:11 +00:00
|
|
|
return redirect(url_for('settings.edit_profile'))
|
|
|
|
|
2020-09-21 12:55:48 +00:00
|
|
|
@settings.route("/settings/new_token_user", methods=['POST'])
|
2019-06-11 15:37:20 +00:00
|
|
|
@login_required
|
2019-06-19 15:02:09 +00:00
|
|
|
@login_admin
|
2019-06-11 15:37:20 +00:00
|
|
|
def new_token_user():
|
2020-09-21 12:55:48 +00:00
|
|
|
user_id = request.form.get('user_id')
|
2022-07-08 07:47:47 +00:00
|
|
|
if Users.exists_user(user_id):
|
|
|
|
Users.generate_new_token(user_id)
|
2019-06-11 15:37:20 +00:00
|
|
|
return redirect(url_for('settings.users_list'))
|
|
|
|
|
|
|
|
@settings.route("/settings/create_user", methods=['GET'])
|
|
|
|
@login_required
|
2019-06-19 15:02:09 +00:00
|
|
|
@login_admin
|
2019-06-11 15:37:20 +00:00
|
|
|
def create_user():
|
|
|
|
user_id = request.args.get('user_id')
|
2019-06-20 08:56:31 +00:00
|
|
|
error = request.args.get('error')
|
|
|
|
error_mail = request.args.get('error_mail')
|
2019-06-11 15:37:20 +00:00
|
|
|
role = None
|
|
|
|
if r_serv_db.exists('user_metadata:{}'.format(user_id)):
|
|
|
|
role = r_serv_db.hget('user_metadata:{}'.format(user_id), 'role')
|
|
|
|
else:
|
|
|
|
user_id = None
|
2022-07-08 07:47:47 +00:00
|
|
|
all_roles = Users.get_all_roles()
|
2019-06-20 09:15:56 +00:00
|
|
|
return render_template("create_user.html", all_roles=all_roles, user_id=user_id, user_role=role,
|
|
|
|
error=error, error_mail=error_mail,
|
|
|
|
admin_level=True)
|
2019-06-11 15:37:20 +00:00
|
|
|
|
|
|
|
@settings.route("/settings/create_user_post", methods=['POST'])
|
|
|
|
@login_required
|
2019-06-19 15:02:09 +00:00
|
|
|
@login_admin
|
2019-06-11 15:37:20 +00:00
|
|
|
def create_user_post():
|
|
|
|
email = request.form.get('username')
|
|
|
|
role = request.form.get('user_role')
|
|
|
|
password1 = request.form.get('password1')
|
|
|
|
password2 = request.form.get('password2')
|
|
|
|
|
2022-07-08 07:47:47 +00:00
|
|
|
all_roles = Users.get_all_roles()
|
2019-06-11 15:37:20 +00:00
|
|
|
|
2019-06-20 08:56:31 +00:00
|
|
|
if email and len(email)< 300 and check_email(email) and role:
|
2019-06-11 15:37:20 +00:00
|
|
|
if role in all_roles:
|
|
|
|
# password set
|
|
|
|
if password1 and password2:
|
|
|
|
if password1==password2:
|
2022-07-08 07:47:47 +00:00
|
|
|
if Users.check_password_strength(password1):
|
2019-06-11 15:37:20 +00:00
|
|
|
password = password1
|
|
|
|
else:
|
2019-06-20 09:15:56 +00:00
|
|
|
return render_template("create_user.html", all_roles=all_roles, error="Incorrect Password", admin_level=True)
|
2019-06-11 15:37:20 +00:00
|
|
|
else:
|
2019-06-20 09:15:56 +00:00
|
|
|
return render_template("create_user.html", all_roles=all_roles, error="Passwords don't match", admin_level=True)
|
2019-06-11 15:37:20 +00:00
|
|
|
# generate password
|
|
|
|
else:
|
2022-07-08 07:47:47 +00:00
|
|
|
password = Users.gen_password()
|
2019-06-11 15:37:20 +00:00
|
|
|
|
|
|
|
if current_user.is_in_role('admin'):
|
2022-07-08 07:47:47 +00:00
|
|
|
str_password = password
|
|
|
|
if Users.exists_user(email):
|
|
|
|
if not password1 and not password2:
|
|
|
|
password = None
|
|
|
|
str_password = 'Password not changed'
|
|
|
|
Users.create_user(email, password=password, role=role)
|
|
|
|
return redirect(url_for('settings.users_list', new_user=email, new_user_password=str_password))
|
2019-06-11 15:37:20 +00:00
|
|
|
|
|
|
|
else:
|
2019-06-20 09:15:56 +00:00
|
|
|
return render_template("create_user.html", all_roles=all_roles, admin_level=True)
|
2019-06-11 15:37:20 +00:00
|
|
|
else:
|
2019-06-20 09:15:56 +00:00
|
|
|
return render_template("create_user.html", all_roles=all_roles, error_mail=True, admin_level=True)
|
2019-06-11 15:37:20 +00:00
|
|
|
|
|
|
|
@settings.route("/settings/users_list", methods=['GET'])
|
|
|
|
@login_required
|
2019-06-19 15:02:09 +00:00
|
|
|
@login_admin
|
2019-06-11 15:37:20 +00:00
|
|
|
def users_list():
|
2022-07-08 07:47:47 +00:00
|
|
|
all_users = Users.get_users_metadata(Users.get_all_users())
|
2019-06-11 15:37:20 +00:00
|
|
|
new_user = request.args.get('new_user')
|
|
|
|
new_user_dict = {}
|
|
|
|
if new_user:
|
|
|
|
new_user_dict['email'] = new_user
|
|
|
|
new_user_dict['edited'] = request.args.get('new_user_edited')
|
|
|
|
new_user_dict['password'] = request.args.get('new_user_password')
|
2019-06-20 09:15:56 +00:00
|
|
|
return render_template("users_list.html", all_users=all_users, new_user=new_user_dict, admin_level=True)
|
2019-06-11 15:37:20 +00:00
|
|
|
|
2020-09-21 12:55:48 +00:00
|
|
|
@settings.route("/settings/edit_user", methods=['POST'])
|
2019-06-11 15:37:20 +00:00
|
|
|
@login_required
|
2019-06-19 15:02:09 +00:00
|
|
|
@login_admin
|
2019-06-11 15:37:20 +00:00
|
|
|
def edit_user():
|
2020-09-21 12:55:48 +00:00
|
|
|
user_id = request.form.get('user_id')
|
2019-06-11 15:37:20 +00:00
|
|
|
return redirect(url_for('settings.create_user', user_id=user_id))
|
|
|
|
|
2020-09-21 12:55:48 +00:00
|
|
|
@settings.route("/settings/delete_user", methods=['POST'])
|
2019-06-11 15:37:20 +00:00
|
|
|
@login_required
|
2019-06-19 15:02:09 +00:00
|
|
|
@login_admin
|
2019-06-11 15:37:20 +00:00
|
|
|
def delete_user():
|
2020-09-21 12:55:48 +00:00
|
|
|
user_id = request.form.get('user_id')
|
2022-07-08 07:47:47 +00:00
|
|
|
Users.delete_user(user_id)
|
2019-06-11 15:37:20 +00:00
|
|
|
return redirect(url_for('settings.users_list'))
|
|
|
|
|
2019-04-17 12:39:29 +00:00
|
|
|
|
2021-03-31 09:25:09 +00:00
|
|
|
@settings.route("/settings/passivedns", methods=['GET'])
|
|
|
|
@login_required
|
|
|
|
@login_read_only
|
|
|
|
def passive_dns():
|
|
|
|
passivedns_enabled = d4.is_passive_dns_enabled()
|
|
|
|
return render_template("passive_dns.html", passivedns_enabled=passivedns_enabled)
|
|
|
|
|
|
|
|
@settings.route("/settings/passivedns/change_state", methods=['GET'])
|
|
|
|
@login_required
|
|
|
|
@login_admin
|
|
|
|
def passive_dns_change_state():
|
|
|
|
new_state = request.args.get('state') == 'enable'
|
|
|
|
passivedns_enabled = d4.change_passive_dns_state(new_state)
|
|
|
|
return redirect(url_for('settings.passive_dns'))
|
|
|
|
|
2022-03-07 14:12:01 +00:00
|
|
|
@settings.route("/settings/ail", methods=['GET'])
|
|
|
|
@login_required
|
|
|
|
@login_admin
|
|
|
|
def ail_configs():
|
|
|
|
return render_template("ail_configs.html", passivedns_enabled=None)
|
|
|
|
|
2019-04-16 15:24:59 +00:00
|
|
|
# ========= REGISTRATION =========
|
|
|
|
app.register_blueprint(settings, url_prefix=baseUrl)
|