fix: Correções na página de administração e suas dependências
This commit is contained in:
206
app.py
206
app.py
@@ -128,7 +128,30 @@ def create_app():
|
|||||||
if 'user_id' not in session:
|
if 'user_id' not in session:
|
||||||
flash('Por favor, faça login para acessar esta página.', 'warning')
|
flash('Por favor, faça login para acessar esta página.', 'warning')
|
||||||
return redirect(url_for('login'))
|
return redirect(url_for('login'))
|
||||||
|
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
db = get_db_connection()
|
||||||
|
try:
|
||||||
|
# Carregar o usuário com suas roles e permissões
|
||||||
|
user = db.query(Usuario).options(
|
||||||
|
joinedload(Usuario.roles).joinedload(Role.permissions),
|
||||||
|
joinedload(Usuario.militante),
|
||||||
|
joinedload(Usuario.cr),
|
||||||
|
joinedload(Usuario.setor),
|
||||||
|
joinedload(Usuario.celula)
|
||||||
|
).get(session['user_id'])
|
||||||
|
|
||||||
|
if not user:
|
||||||
|
flash('Usuário não encontrado.', 'danger')
|
||||||
|
return redirect(url_for('login'))
|
||||||
|
|
||||||
|
# Atualiza timestamp da última atividade
|
||||||
|
user.update_last_activity()
|
||||||
|
db.commit()
|
||||||
|
|
||||||
return f(*args, **kwargs)
|
return f(*args, **kwargs)
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
return decorated_function
|
return decorated_function
|
||||||
|
|
||||||
# Decorator para verificar se a sessão expirou
|
# Decorator para verificar se a sessão expirou
|
||||||
@@ -222,6 +245,7 @@ def create_app():
|
|||||||
session['user_id'] = user.id
|
session['user_id'] = user.id
|
||||||
session['username'] = user.username
|
session['username'] = user.username
|
||||||
session['is_admin'] = user.is_admin
|
session['is_admin'] = user.is_admin
|
||||||
|
print(f"Login realizado: user_id={user.id}, username={user.username}, is_admin={user.is_admin}")
|
||||||
|
|
||||||
# Redirecionar para home
|
# Redirecionar para home
|
||||||
return redirect(url_for("home"))
|
return redirect(url_for("home"))
|
||||||
@@ -1361,34 +1385,36 @@ def create_app():
|
|||||||
@app.route('/usuarios/<int:user_id>/toggle_status', methods=['POST'])
|
@app.route('/usuarios/<int:user_id>/toggle_status', methods=['POST'])
|
||||||
@require_login
|
@require_login
|
||||||
def toggle_user_status(user_id):
|
def toggle_user_status(user_id):
|
||||||
user = db_session.query(Usuario).get_or_404(user_id)
|
if not current_user.is_admin:
|
||||||
|
return jsonify({
|
||||||
|
'success': False,
|
||||||
|
'error': 'Você não tem permissão para alterar o status de usuários.'
|
||||||
|
}), 403
|
||||||
|
|
||||||
# Verificar permissões baseado na hierarquia
|
db = get_db_connection()
|
||||||
if not current_user.has_permission('system_config'):
|
try:
|
||||||
if current_user.has_permission('manage_cr_sectors'):
|
usuario = db.query(Usuario).get(user_id)
|
||||||
# Secretário de CR só pode gerenciar membros do seu CR
|
if not usuario:
|
||||||
if user.cr_id != current_user.cr_id:
|
return jsonify({
|
||||||
flash('Você não tem permissão para gerenciar este usuário.', 'danger')
|
'success': False,
|
||||||
return redirect(url_for('dashboard_admin'))
|
'error': 'Usuário não encontrado.'
|
||||||
elif current_user.has_permission('manage_sector_cells'):
|
}), 404
|
||||||
# Secretário de Setor só pode gerenciar membros do seu setor
|
|
||||||
if user.setor_id != current_user.setor_id:
|
|
||||||
flash('Você não tem permissão para gerenciar este usuário.', 'danger')
|
|
||||||
return redirect(url_for('dashboard_admin'))
|
|
||||||
elif current_user.has_permission('manage_cell_members'):
|
|
||||||
# Secretário de Célula só pode gerenciar membros da sua célula
|
|
||||||
if user.celula_id != current_user.celula_id:
|
|
||||||
flash('Você não tem permissão para gerenciar este usuário.', 'danger')
|
|
||||||
return redirect(url_for('dashboard_admin'))
|
|
||||||
else:
|
|
||||||
# Militante básico não pode gerenciar ninguém
|
|
||||||
flash('Você não tem permissão para gerenciar usuários.', 'danger')
|
|
||||||
return redirect(url_for('dashboard_admin'))
|
|
||||||
|
|
||||||
user.ativo = not user.ativo
|
usuario.ativo = not usuario.ativo
|
||||||
db_session.commit()
|
db.commit()
|
||||||
|
|
||||||
return jsonify({'success': True, 'message': 'Status do usuário alterado com sucesso!'})
|
return jsonify({
|
||||||
|
'success': True,
|
||||||
|
'message': f'Usuário {"ativado" if usuario.ativo else "desativado"} com sucesso!'
|
||||||
|
})
|
||||||
|
except Exception as e:
|
||||||
|
db.rollback()
|
||||||
|
return jsonify({
|
||||||
|
'success': False,
|
||||||
|
'error': str(e)
|
||||||
|
}), 500
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
@app.route('/usuarios/<int:user_id>/alterar_nivel', methods=['POST'])
|
@app.route('/usuarios/<int:user_id>/alterar_nivel', methods=['POST'])
|
||||||
@require_login
|
@require_login
|
||||||
@@ -1619,6 +1645,136 @@ def create_app():
|
|||||||
finally:
|
finally:
|
||||||
db.close()
|
db.close()
|
||||||
|
|
||||||
|
@app.route('/dashboard_admin')
|
||||||
|
@login_required
|
||||||
|
def dashboard_admin():
|
||||||
|
"""Rota para o dashboard administrativo"""
|
||||||
|
if not current_user.is_admin:
|
||||||
|
flash('Você não tem permissão para acessar esta página.', 'danger')
|
||||||
|
return redirect(url_for('home'))
|
||||||
|
|
||||||
|
db = get_db_connection()
|
||||||
|
try:
|
||||||
|
# Busca usuários
|
||||||
|
usuarios = db.query(Usuario).all()
|
||||||
|
|
||||||
|
usuarios_data = []
|
||||||
|
for usuario in usuarios:
|
||||||
|
user_data = {
|
||||||
|
'id': usuario.id,
|
||||||
|
'username': usuario.username,
|
||||||
|
'email': usuario.email,
|
||||||
|
'nome': usuario.username, # Usar username como fallback
|
||||||
|
'ativo': usuario.ativo,
|
||||||
|
'is_admin': usuario.is_admin,
|
||||||
|
'last_login': usuario.ultimo_login.strftime('%d/%m/%Y %H:%M') if usuario.ultimo_login else 'Nunca',
|
||||||
|
'nivel': 'Administrador' if usuario.is_admin else 'Usuário'
|
||||||
|
}
|
||||||
|
usuarios_data.append(user_data)
|
||||||
|
|
||||||
|
return render_template(
|
||||||
|
'dashboard_admin.html',
|
||||||
|
usuarios=usuarios_data
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
import traceback
|
||||||
|
print(f"Erro no dashboard_admin: {traceback.format_exc()}")
|
||||||
|
flash('Erro ao carregar dados dos usuários. Por favor, tente novamente.', 'danger')
|
||||||
|
return redirect(url_for('home'))
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
@app.route('/reset_otp/<int:user_id>', methods=['POST'])
|
||||||
|
@login_required
|
||||||
|
def reset_otp(user_id):
|
||||||
|
if not current_user.is_admin:
|
||||||
|
return jsonify({
|
||||||
|
'success': False,
|
||||||
|
'error': 'Você não tem permissão para resetar OTP.'
|
||||||
|
}), 403
|
||||||
|
|
||||||
|
db = get_db_connection()
|
||||||
|
try:
|
||||||
|
usuario = db.query(Usuario).get(user_id)
|
||||||
|
if not usuario:
|
||||||
|
return jsonify({
|
||||||
|
'success': False,
|
||||||
|
'error': 'Usuário não encontrado.'
|
||||||
|
}), 404
|
||||||
|
|
||||||
|
usuario.otp_secret = pyotp.random_base32()
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
return jsonify({
|
||||||
|
'success': True
|
||||||
|
})
|
||||||
|
except Exception as e:
|
||||||
|
db.rollback()
|
||||||
|
return jsonify({
|
||||||
|
'success': False,
|
||||||
|
'error': str(e)
|
||||||
|
}), 500
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
@app.route('/reset_password/<int:user_id>', methods=['POST'])
|
||||||
|
@login_required
|
||||||
|
def reset_password(user_id):
|
||||||
|
if not current_user.is_admin:
|
||||||
|
return jsonify({
|
||||||
|
'success': False,
|
||||||
|
'error': 'Você não tem permissão para resetar senhas.'
|
||||||
|
}), 403
|
||||||
|
|
||||||
|
db = get_db_connection()
|
||||||
|
try:
|
||||||
|
usuario = db.query(Usuario).get(user_id)
|
||||||
|
if not usuario:
|
||||||
|
return jsonify({
|
||||||
|
'success': False,
|
||||||
|
'error': 'Usuário não encontrado.'
|
||||||
|
}), 404
|
||||||
|
|
||||||
|
nova_senha = ''.join(random.choices(string.ascii_letters + string.digits, k=12))
|
||||||
|
usuario.set_password(nova_senha)
|
||||||
|
|
||||||
|
try:
|
||||||
|
msg = Message(
|
||||||
|
'Nova Senha - Sistema de Controles',
|
||||||
|
recipients=[usuario.email]
|
||||||
|
)
|
||||||
|
msg.body = f'''Olá {usuario.nome},
|
||||||
|
|
||||||
|
Sua senha foi resetada por um administrador. Sua nova senha é:
|
||||||
|
|
||||||
|
{nova_senha}
|
||||||
|
|
||||||
|
Por favor, altere esta senha no seu próximo login.
|
||||||
|
|
||||||
|
Atenciosamente,
|
||||||
|
Sistema de Controles'''
|
||||||
|
|
||||||
|
mail.send(msg)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Erro ao enviar email: {str(e)}")
|
||||||
|
return jsonify({
|
||||||
|
'success': False,
|
||||||
|
'error': 'Erro ao enviar email com a nova senha.'
|
||||||
|
}), 500
|
||||||
|
|
||||||
|
db.commit()
|
||||||
|
return jsonify({
|
||||||
|
'success': True
|
||||||
|
})
|
||||||
|
except Exception as e:
|
||||||
|
db.rollback()
|
||||||
|
return jsonify({
|
||||||
|
'success': False,
|
||||||
|
'error': str(e)
|
||||||
|
}), 500
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
return app
|
return app
|
||||||
|
|
||||||
def init_system():
|
def init_system():
|
||||||
|
|||||||
@@ -70,6 +70,15 @@ def create_admin_user():
|
|||||||
admin.set_password("admin123")
|
admin.set_password("admin123")
|
||||||
admin.generate_otp_secret()
|
admin.generate_otp_secret()
|
||||||
|
|
||||||
|
# Buscar ou criar role de admin
|
||||||
|
admin_role = db.query(Role).filter_by(nome="admin").first()
|
||||||
|
if not admin_role:
|
||||||
|
admin_role = Role(nome="admin", nivel=0) # Nível 0 é o mais alto
|
||||||
|
db.add(admin_role)
|
||||||
|
|
||||||
|
# Adicionar role ao usuário
|
||||||
|
admin.roles.append(admin_role)
|
||||||
|
|
||||||
# Adicionar e fazer commit
|
# Adicionar e fazer commit
|
||||||
db.add(admin)
|
db.add(admin)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|||||||
@@ -441,6 +441,7 @@ class Usuario(Base, UserMixin):
|
|||||||
username = Column(String(50), unique=True, nullable=False)
|
username = Column(String(50), unique=True, nullable=False)
|
||||||
password_hash = Column(String(255), nullable=False)
|
password_hash = Column(String(255), nullable=False)
|
||||||
email = Column(String(100), unique=True, nullable=False)
|
email = Column(String(100), unique=True, nullable=False)
|
||||||
|
nome = Column(String(100)) # Nome completo do usuário
|
||||||
otp_secret = Column(String(32))
|
otp_secret = Column(String(32))
|
||||||
role_id = Column(Integer, ForeignKey('roles.id'))
|
role_id = Column(Integer, ForeignKey('roles.id'))
|
||||||
setor_id = Column(Integer, ForeignKey('setores.id'))
|
setor_id = Column(Integer, ForeignKey('setores.id'))
|
||||||
@@ -464,11 +465,11 @@ class Usuario(Base, UserMixin):
|
|||||||
cr = relationship('ComiteRegional', back_populates='usuarios')
|
cr = relationship('ComiteRegional', back_populates='usuarios')
|
||||||
celula = relationship('Celula', back_populates='usuarios')
|
celula = relationship('Celula', back_populates='usuarios')
|
||||||
|
|
||||||
def __init__(self, username, email=None, is_admin=False):
|
def __init__(self, username, email=None, is_admin=False, nome=None):
|
||||||
self.username = username
|
self.username = username
|
||||||
self.email = email
|
self.email = email
|
||||||
self.is_admin = is_admin
|
self.is_admin = is_admin
|
||||||
self.email = email
|
self.nome = nome
|
||||||
self.ativo = True
|
self.ativo = True
|
||||||
self.session_timeout = 30
|
self.session_timeout = 30
|
||||||
self.tipo = "USUARIO"
|
self.tipo = "USUARIO"
|
||||||
@@ -549,6 +550,10 @@ class Usuario(Base, UserMixin):
|
|||||||
self.motivo_logout = "Logout manual"
|
self.motivo_logout = "Logout manual"
|
||||||
self.ultima_atividade = None
|
self.ultima_atividade = None
|
||||||
|
|
||||||
|
def is_admin_user(self):
|
||||||
|
"""Verifica se o usuário é admin"""
|
||||||
|
return self.is_admin or any(role.nome == "admin" for role in self.roles)
|
||||||
|
|
||||||
class PagamentoCelula(Base):
|
class PagamentoCelula(Base):
|
||||||
__tablename__ = 'pagamentos_celula'
|
__tablename__ = 'pagamentos_celula'
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ from functools import wraps
|
|||||||
from flask import session, redirect, url_for, flash
|
from flask import session, redirect, url_for, flash
|
||||||
from flask_login import current_user, login_required
|
from flask_login import current_user, login_required
|
||||||
from sqlalchemy.orm import joinedload
|
from sqlalchemy.orm import joinedload
|
||||||
from .database import get_db_connection, Usuario
|
from .database import get_db_connection, Usuario, Role
|
||||||
from .rbac import Permission
|
from .rbac import Permission
|
||||||
|
|
||||||
def require_login(f):
|
def require_login(f):
|
||||||
@@ -15,9 +15,13 @@ def require_login(f):
|
|||||||
|
|
||||||
db = get_db_connection()
|
db = get_db_connection()
|
||||||
try:
|
try:
|
||||||
# Carregar o usuário com suas roles
|
# Carregar o usuário com suas roles e permissões
|
||||||
user = db.query(Usuario).options(
|
user = db.query(Usuario).options(
|
||||||
joinedload(Usuario.roles)
|
joinedload(Usuario.roles).joinedload(Role.permissions),
|
||||||
|
joinedload(Usuario.militante),
|
||||||
|
joinedload(Usuario.cr),
|
||||||
|
joinedload(Usuario.setor),
|
||||||
|
joinedload(Usuario.celula)
|
||||||
).get(current_user.id)
|
).get(current_user.id)
|
||||||
|
|
||||||
if not user:
|
if not user:
|
||||||
@@ -28,7 +32,15 @@ def require_login(f):
|
|||||||
user.update_last_activity()
|
user.update_last_activity()
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
|
# Substituir o current_user pelo usuário carregado
|
||||||
|
setattr(current_user, '_get_current_object', lambda: user)
|
||||||
|
|
||||||
|
# Executar a função com o usuário carregado
|
||||||
return f(*args, **kwargs)
|
return f(*args, **kwargs)
|
||||||
|
except Exception as e:
|
||||||
|
db.rollback()
|
||||||
|
flash('Erro ao carregar dados do usuário.', 'danger')
|
||||||
|
return redirect(url_for('login'))
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
db.close()
|
||||||
return decorated_function
|
return decorated_function
|
||||||
@@ -39,14 +51,38 @@ def require_permission(permission_name):
|
|||||||
@wraps(f)
|
@wraps(f)
|
||||||
def decorated_function(*args, **kwargs):
|
def decorated_function(*args, **kwargs):
|
||||||
if not current_user.is_authenticated:
|
if not current_user.is_authenticated:
|
||||||
flash('Por favor, faça login para acessar esta página.', 'danger')
|
flash('Você precisa estar logado para acessar esta página.', 'error')
|
||||||
return redirect(url_for('login'))
|
return redirect(url_for('login'))
|
||||||
|
|
||||||
if not current_user.has_permission(permission_name):
|
db = get_db_connection()
|
||||||
flash('Você não tem permissão para acessar esta página.', 'danger')
|
try:
|
||||||
return redirect(url_for('home'))
|
# Carregar o usuário com suas roles e permissões
|
||||||
|
user = db.query(Usuario).options(
|
||||||
|
joinedload(Usuario.roles).joinedload(Role.permissions),
|
||||||
|
joinedload(Usuario.militante),
|
||||||
|
joinedload(Usuario.cr),
|
||||||
|
joinedload(Usuario.setor),
|
||||||
|
joinedload(Usuario.celula)
|
||||||
|
).get(current_user.id)
|
||||||
|
|
||||||
|
if not user:
|
||||||
|
flash('Usuário não encontrado.', 'error')
|
||||||
|
return redirect(url_for('login'))
|
||||||
|
|
||||||
|
if not user.has_permission(permission_name):
|
||||||
|
flash('Você não tem permissão para acessar esta página.', 'error')
|
||||||
|
return redirect(url_for('index'))
|
||||||
|
|
||||||
|
# Atualiza timestamp da última atividade
|
||||||
|
user.update_last_activity()
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
# Substituir o current_user pelo usuário carregado
|
||||||
|
setattr(current_user, '_get_current_object', lambda: user)
|
||||||
|
|
||||||
return f(*args, **kwargs)
|
return f(*args, **kwargs)
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
return decorated_function
|
return decorated_function
|
||||||
return decorator
|
return decorator
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
<!-- Bootstrap 5 CSS -->
|
<!-- Bootstrap 5 CSS -->
|
||||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css?v=1" rel="stylesheet">
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css?v=1" rel="stylesheet">
|
||||||
<!-- Font Awesome 6 -->
|
<!-- Font Awesome 6 -->
|
||||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css?v=1">
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha512-DTOQO9RWCH3ppGqcWaEA1BIZOC6xxalwEsw9c2QQeAIftl+Vegovlnee1c9QX4TctnWMn13TZye+giMm8e2LwA==" crossorigin="anonymous" referrerpolicy="no-referrer" />
|
||||||
<!-- Componentes CSS -->
|
<!-- Componentes CSS -->
|
||||||
<link rel="stylesheet" href="{{ url_for('static', filename='css/components.css') }}">
|
<link rel="stylesheet" href="{{ url_for('static', filename='css/components.css') }}">
|
||||||
|
|
||||||
@@ -599,6 +599,11 @@
|
|||||||
<i class="fas fa-user-plus"></i>Novo Usuário
|
<i class="fas fa-user-plus"></i>Novo Usuário
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
|
<li>
|
||||||
|
<a class="dropdown-item" href="{{ url_for('dashboard_admin') }}">
|
||||||
|
<i class="fas fa-cog fa fa-cog fa-solid fa-cog" style="display: inline-block !important; visibility: visible !important;"></i>Administração
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
<li><hr class="dropdown-divider"></li>
|
<li><hr class="dropdown-divider"></li>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<li>
|
<li>
|
||||||
|
|||||||
@@ -1,63 +1,75 @@
|
|||||||
{% extends 'base.html' %}
|
{% extends "base.html" %}
|
||||||
|
|
||||||
{% block title %}Dashboard Administrativo{% endblock %}
|
{% block title %}Dashboard Administrativo{% endblock %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<div class="container">
|
<div class="container mt-4">
|
||||||
<h1 class="mb-4">Dashboard Administrativo</h1>
|
<div class="card">
|
||||||
|
<div class="card-header bg-dark text-white">
|
||||||
{% with messages = get_flashed_messages(with_categories=true) %}
|
<h3 class="mb-0"><i class="fas fa-users-cog"></i> Administração de Usuários</h3>
|
||||||
{% if messages %}
|
|
||||||
{% for category, message in messages %}
|
|
||||||
<div class="alert alert-{{ category }}">{{ message }}</div>
|
|
||||||
{% endfor %}
|
|
||||||
{% endif %}
|
|
||||||
{% endwith %}
|
|
||||||
|
|
||||||
<div class="card mb-4">
|
|
||||||
<div class="card-header">
|
|
||||||
<h5 class="card-title mb-0">Gerenciamento de Usuários</h5>
|
|
||||||
</div>
|
</div>
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
|
<div class="alert alert-info" role="alert">
|
||||||
|
<i class="fas fa-info-circle"></i> Aqui você pode gerenciar todos os usuários do sistema. Use os controles abaixo para ativar/desativar contas ou alterar níveis de acesso.
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="table-responsive">
|
<div class="table-responsive">
|
||||||
<table class="table table-striped">
|
<table class="table table-hover">
|
||||||
<thead>
|
<thead class="thead-light">
|
||||||
<tr>
|
<tr>
|
||||||
<th>ID</th>
|
|
||||||
<th>Usuário</th>
|
<th>Usuário</th>
|
||||||
<th>Email</th>
|
<th>Email</th>
|
||||||
<th>Admin</th>
|
<th>Nome</th>
|
||||||
<th>OTP Configurado</th>
|
<th>Último Acesso</th>
|
||||||
|
<th>Status</th>
|
||||||
|
<th>Nível</th>
|
||||||
<th>Ações</th>
|
<th>Ações</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{% for usuario in usuarios %}
|
{% for usuario in usuarios %}
|
||||||
<tr>
|
<tr>
|
||||||
<td>{{ usuario.id }}</td>
|
|
||||||
<td>{{ usuario.username }}</td>
|
<td>{{ usuario.username }}</td>
|
||||||
<td>{{ usuario.email }}</td>
|
<td>{{ usuario.email }}</td>
|
||||||
|
<td>{{ usuario.nome }}</td>
|
||||||
|
<td>{{ usuario.last_login }}</td>
|
||||||
|
<td>
|
||||||
|
<span class="badge {% if usuario.ativo %}badge-success{% else %}badge-danger{% endif %}">
|
||||||
|
{{ "Ativo" if usuario.ativo else "Inativo" }}
|
||||||
|
</span>
|
||||||
|
</td>
|
||||||
<td>
|
<td>
|
||||||
{% if usuario.is_admin %}
|
{% if usuario.is_admin %}
|
||||||
<span class="badge bg-success">Sim</span>
|
Administrador
|
||||||
{% else %}
|
{% else %}
|
||||||
<span class="badge bg-secondary">Não</span>
|
{{ usuario.nivel }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
{% if usuario.otp_secret %}
|
<div class="btn-group" role="group">
|
||||||
<span class="badge bg-success">Sim</span>
|
<button class="btn btn-sm btn-outline-primary"
|
||||||
{% else %}
|
onclick="toggleStatus('{{ usuario.id }}')"
|
||||||
<span class="badge bg-danger">Não</span>
|
data-toggle="tooltip"
|
||||||
{% endif %}
|
title="{{ 'Desativar' if usuario.ativo else 'Ativar' }} usuário">
|
||||||
</td>
|
<i class="fas {% if usuario.ativo %}fa-user-times{% else %}fa-user-check{% endif %}"></i>
|
||||||
<td>
|
|
||||||
<form action="{{ url_for('reset_otp', user_id=usuario.id) }}" method="POST" class="d-inline">
|
|
||||||
<button type="submit" class="btn btn-warning btn-sm"
|
|
||||||
onclick="return confirm('Tem certeza que deseja resetar o OTP deste usuário?')">
|
|
||||||
Resetar OTP
|
|
||||||
</button>
|
</button>
|
||||||
</form>
|
|
||||||
|
<button class="btn btn-sm btn-outline-warning"
|
||||||
|
onclick="resetarSenha('{{ usuario.id }}')"
|
||||||
|
data-toggle="tooltip"
|
||||||
|
title="Resetar senha">
|
||||||
|
<i class="fas fa-key"></i>
|
||||||
|
</button>
|
||||||
|
|
||||||
|
{% if not usuario.is_admin %}
|
||||||
|
<button class="btn btn-sm btn-outline-info"
|
||||||
|
onclick="alterarNivel('{{ usuario.id }}')"
|
||||||
|
data-toggle="tooltip"
|
||||||
|
title="Alterar nível">
|
||||||
|
<i class="fas fa-level-up-alt"></i>
|
||||||
|
</button>
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
@@ -66,18 +78,127 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="card">
|
<!-- Modal de Feedback -->
|
||||||
<div class="card-header">
|
<div class="modal fade" id="feedbackModal" tabindex="-1" role="dialog">
|
||||||
<h5 class="card-title mb-0">Ações Rápidas</h5>
|
<div class="modal-dialog" role="document">
|
||||||
|
<div class="modal-content">
|
||||||
|
<div class="modal-header">
|
||||||
|
<h5 class="modal-title">Aviso</h5>
|
||||||
|
<button type="button" class="close" data-dismiss="modal">
|
||||||
|
<span>×</span>
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<div class="card-body">
|
<div class="modal-body">
|
||||||
<div class="d-grid gap-2">
|
<p id="feedbackMessage"></p>
|
||||||
<a href="{{ url_for('novo_usuario') }}" class="btn btn-primary">
|
</div>
|
||||||
Criar Novo Usuário
|
<div class="modal-footer">
|
||||||
</a>
|
<button type="button" class="btn btn-secondary" data-dismiss="modal">Fechar</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block scripts %}
|
||||||
|
<script>
|
||||||
|
function showFeedback(message, type = 'info') {
|
||||||
|
const modal = document.getElementById('feedbackModal');
|
||||||
|
const messageElement = document.getElementById('feedbackMessage');
|
||||||
|
messageElement.textContent = message;
|
||||||
|
messageElement.className = `alert alert-${type}`;
|
||||||
|
$(modal).modal('show');
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleResponse(response) {
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`HTTP error! status: ${response.status}`);
|
||||||
|
}
|
||||||
|
return response.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
function toggleStatus(userId) {
|
||||||
|
if (!confirm('Tem certeza que deseja alterar o status deste usuário?')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fetch(`/usuarios/${userId}/toggle_status`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'X-CSRFToken': document.querySelector('meta[name="csrf-token"]').content
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(handleResponse)
|
||||||
|
.then(data => {
|
||||||
|
showFeedback(data.message || 'Status alterado com sucesso!', data.success ? 'success' : 'danger');
|
||||||
|
if (data.success) {
|
||||||
|
setTimeout(() => location.reload(), 1500);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
console.error('Error:', error);
|
||||||
|
showFeedback('Erro ao alterar status do usuário. Por favor, tente novamente.', 'danger');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function resetarSenha(userId) {
|
||||||
|
if (!confirm('Tem certeza que deseja resetar a senha deste usuário?')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fetch(`/reset_password/${userId}`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'X-CSRFToken': document.querySelector('meta[name="csrf-token"]').content
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(handleResponse)
|
||||||
|
.then(data => {
|
||||||
|
showFeedback(data.message || 'Senha resetada com sucesso!', data.success ? 'success' : 'danger');
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
console.error('Error:', error);
|
||||||
|
showFeedback('Erro ao resetar senha. Por favor, tente novamente.', 'danger');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function alterarNivel(userId) {
|
||||||
|
const novoNivel = prompt('Digite o novo nível do usuário (1-5):');
|
||||||
|
if (!novoNivel) return;
|
||||||
|
|
||||||
|
if (!/^[1-5]$/.test(novoNivel)) {
|
||||||
|
showFeedback('Por favor, insira um nível válido entre 1 e 5.', 'warning');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fetch(`/usuarios/${userId}/alterar_nivel`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'X-CSRFToken': document.querySelector('meta[name="csrf-token"]').content
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ nivel: parseInt(novoNivel) })
|
||||||
|
})
|
||||||
|
.then(handleResponse)
|
||||||
|
.then(data => {
|
||||||
|
showFeedback(data.message || 'Nível alterado com sucesso!', data.success ? 'success' : 'danger');
|
||||||
|
if (data.success) {
|
||||||
|
setTimeout(() => location.reload(), 1500);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
console.error('Error:', error);
|
||||||
|
showFeedback('Erro ao alterar nível. Por favor, tente novamente.', 'danger');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Inicializa os tooltips do Bootstrap
|
||||||
|
$(function () {
|
||||||
|
$('[data-toggle="tooltip"]').tooltip();
|
||||||
|
});
|
||||||
|
</script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
Reference in New Issue
Block a user