DomainExpiration/app/routes/clients.py

247 lines
11 KiB
Python

from flask import Blueprint, render_template, request, redirect, url_for, flash
from app.models import Client, ClientPassword, Domain, PasswordCategory, RemoteCategory, ClientRemote
from app import db
clients_bp = Blueprint('clients', __name__)
@clients_bp.route('/clients')
def clients():
clients_list = Client.query.order_by(Client.name.asc()).all()
return render_template('clients.html', clients=clients_list)
@clients_bp.route('/clients/add', methods=['GET', 'POST'])
def add_client():
if request.method == 'POST':
names = request.form.getlist('name')
for name in names:
if name.strip():
new_client = Client(name=name.strip())
db.session.add(new_client)
db.session.commit()
return redirect(url_for('clients.clients'))
return render_template('add_client.html')
@clients_bp.route('/clients/<int:client_id>')
def client_detail(client_id):
client = Client.query.get_or_404(client_id)
available_domains = Domain.query.filter_by(ssl_only=False).all()
available_ssl = Domain.query.filter_by(ssl_only=True).all()
available_categories = PasswordCategory.query.all()
available_remote_categories = RemoteCategory.query.all()
# Group passwords by category.
passwords_by_category = {}
for p in client.passwords:
cat = p.category.name if p.category else "Uncategorized"
if cat not in passwords_by_category:
passwords_by_category[cat] = []
passwords_by_category[cat].append(p)
# Group remotes by remote category.
remotes_by_category = {}
for r in client.remotes:
cat_name = r.remote_category.name
if cat_name not in remotes_by_category:
remotes_by_category[cat_name] = []
remotes_by_category[cat_name].append(r)
return render_template('client_detail.html', client=client,
available_domains=available_domains,
available_ssl=available_ssl,
available_categories=available_categories,
available_remote_categories=available_remote_categories,
passwords_by_category=passwords_by_category,
remotes_by_category=remotes_by_category)
@clients_bp.route('/clients/<int:client_id>/update_details', methods=['POST'])
def update_client_details(client_id):
client = Client.query.get_or_404(client_id)
client.phone = request.form.get('phone')
client.address = request.form.get('address')
client.ip_address = request.form.get('ip_address')
client.subnet_mask = request.form.get('subnet_mask')
client.gateway = request.form.get('gateway')
db.session.commit()
return redirect(url_for('clients.client_detail', client_id=client.id))
@clients_bp.route('/clients/<int:client_id>/add_password', methods=['POST'])
def add_client_password(client_id):
client = Client.query.get_or_404(client_id)
item = request.form.get('item')
username = request.form.get('username')
password_text = request.form.get('password')
ip_address = request.form.get('ip_address') # Capture the IP address
category_id = request.form.get('category_id')
if item and username and password_text:
new_pass = ClientPassword(
item=item,
username=username,
password=password_text,
ip_address=ip_address, # Set the IP address
client_id=client.id
)
if category_id:
new_pass.category_id = int(category_id)
db.session.add(new_pass)
db.session.commit()
return redirect(url_for('clients.client_detail', client_id=client.id))
@clients_bp.route('/clients/<int:client_id>/attach_domain', methods=['POST'])
def attach_domain(client_id):
client = Client.query.get_or_404(client_id)
domain_id = request.form.get('domain_id')
if domain_id:
domain = Domain.query.get(int(domain_id))
if domain and domain not in client.domains:
client.domains.append(domain)
db.session.commit()
return redirect(url_for('clients.client_detail', client_id=client.id))
@clients_bp.route('/clients/<int:client_id>/attach_ssl', methods=['POST'])
def attach_ssl(client_id):
client = Client.query.get_or_404(client_id)
domain_id = request.form.get('ssl_domain_id')
if domain_id:
domain = Domain.query.get(int(domain_id))
if domain and domain not in client.ssl_domains:
client.ssl_domains.append(domain)
db.session.commit()
return redirect(url_for('clients.client_detail', client_id=client.id))
# Removal routes for associations
@clients_bp.route('/clients/<int:client_id>/remove_domain/<int:domain_id>', methods=['POST'])
def remove_domain(client_id, domain_id):
client = Client.query.get_or_404(client_id)
domain = Domain.query.get_or_404(domain_id)
if domain in client.domains:
client.domains.remove(domain)
db.session.commit()
return redirect(url_for('clients.client_detail', client_id=client.id))
@clients_bp.route('/clients/<int:client_id>/remove_ssl/<int:domain_id>', methods=['POST'])
def remove_ssl(client_id, domain_id):
client = Client.query.get_or_404(client_id)
domain = Domain.query.get_or_404(domain_id)
if domain in client.ssl_domains:
client.ssl_domains.remove(domain)
db.session.commit()
return redirect(url_for('clients.client_detail', client_id=client.id))
@clients_bp.route('/clients/<int:client_id>/remove_password/<int:password_id>', methods=['POST'])
def remove_password(client_id, password_id):
password = ClientPassword.query.get_or_404(password_id)
db.session.delete(password)
db.session.commit()
return redirect(url_for('clients.client_detail', client_id=client_id))
@clients_bp.route('/clients/<int:client_id>/delete', methods=['POST'])
def delete_client(client_id):
client = Client.query.get_or_404(client_id)
db.session.delete(client)
db.session.commit()
flash("Client deleted successfully.", "success")
return redirect(url_for('clients.clients'))
@clients_bp.route('/clients/<int:client_id>/add_remote', methods=['POST'])
def add_remote(client_id):
from app.models import RemoteCategory, ClientRemote
client = Client.query.get_or_404(client_id)
remote_category_id = request.form.get('remote_category_id')
if not remote_category_id:
flash("Please select a remote category.", "warning")
return redirect(url_for('clients.client_detail', client_id=client.id))
remote_category = RemoteCategory.query.get(remote_category_id)
new_remote = ClientRemote(client_id=client.id, remote_category_id=remote_category.id)
# Fill in fields depending on category name.
if remote_category.name == "RDP":
new_remote.name = request.form.get('remote_name')
new_remote.ip_address = request.form.get('ip_address')
new_remote.port = request.form.get('port')
elif remote_category.name == "SSL-VPN":
new_remote.ip_address = request.form.get('ip_address')
new_remote.port = request.form.get('port')
elif remote_category.name == "VPN":
new_remote.ip_address = request.form.get('ip_address')
new_remote.psk = request.form.get('psk')
elif remote_category.name == "MGMT":
new_remote.name = request.form.get('remote_name')
new_remote.address = request.form.get('address')
else:
new_remote.details = request.form.get('details')
db.session.add(new_remote)
db.session.commit()
return redirect(url_for('clients.client_detail', client_id=client.id))
@clients_bp.route('/clients/<int:client_id>/remove_remote/<int:remote_id>', methods=['POST'])
def remove_remote(client_id, remote_id):
from app.models import ClientRemote
remote = ClientRemote.query.get_or_404(remote_id)
db.session.delete(remote)
db.session.commit()
return redirect(url_for('clients.client_detail', client_id=client_id))
@clients_bp.route('/clients/<int:client_id>/print', methods=['GET'])
def print_client(client_id):
client = Client.query.get_or_404(client_id)
available_domains = Domain.query.filter_by(ssl_only=False).all()
available_ssl = Domain.query.filter_by(ssl_only=True).all()
available_categories = PasswordCategory.query.all()
available_remote_categories = RemoteCategory.query.all()
# Group passwords by category.
passwords_by_category = {}
for p in client.passwords:
cat = p.category.name if p.category else "Uncategorized"
if cat not in passwords_by_category:
passwords_by_category[cat] = []
passwords_by_category[cat].append(p)
# Group remotes by category.
remotes_by_category = {}
for r in client.remotes:
cat_name = r.remote_category.name
if cat_name not in remotes_by_category:
remotes_by_category[cat_name] = []
remotes_by_category[cat_name].append(r)
return render_template('client_detail_print.html', client=client,
available_domains=available_domains,
available_ssl=available_ssl,
available_categories=available_categories,
available_remote_categories=available_remote_categories,
passwords_by_category=passwords_by_category,
remotes_by_category=remotes_by_category)
@clients_bp.route('/clients/<int:client_id>/edit_password/<int:password_id>', methods=['GET', 'POST'])
def edit_password(client_id, password_id):
password_item = ClientPassword.query.get_or_404(password_id)
if request.method == 'POST':
password_item.item = request.form.get('item')
password_item.username = request.form.get('username')
password_item.password = request.form.get('password')
password_item.ip_address = request.form.get('ip_address')
category_id = request.form.get('category_id')
if category_id:
password_item.category_id = int(category_id)
else:
password_item.category_id = None
db.session.commit()
flash("Password updated successfully", "success")
return redirect(url_for('clients.client_detail', client_id=client_id))
available_categories = PasswordCategory.query.all()
return render_template('edit_password.html', client_id=client_id, password=password_item, available_categories=available_categories)
@clients_bp.route('/clients/<int:client_id>/edit_remote/<int:remote_id>', methods=['GET', 'POST'])
def edit_remote(client_id, remote_id):
remote = ClientRemote.query.get_or_404(remote_id)
if request.method == 'POST':
# For remote editing, update fields based on the remote category.
remote.name = request.form.get('remote_name')
remote.ip_address = request.form.get('ip_address')
remote.port = request.form.get('port')
remote.psk = request.form.get('psk')
remote.address = request.form.get('address')
remote.details = request.form.get('details')
db.session.commit()
flash("Remote updated successfully", "success")
return redirect(url_for('clients.client_detail', client_id=client_id))
return render_template('edit_remote.html', client_id=client_id, remote=remote)