This commit is contained in:
Lizandro Guarnizo
2026-05-15 14:46:11 -05:00
parent b340d29583
commit d7fe292f05
4 changed files with 104 additions and 2 deletions
+30
View File
@@ -77,3 +77,33 @@ func GeneratePasswordResetURL(email string, baseURL string) string {
uri := fmt.Sprintf("%s/reset-password?t=%s", baseURL, token)
return uri
}
// SendPortalCredentialsEmail envía al usuario del portal su URL de acceso y contraseña inicial.
func SendPortalCredentialsEmail(email, nombre, password string) {
loginURL := fmt.Sprintf("%s/portal/login", app.Http.Server.Url)
htmlBody := fmt.Sprintf(`<!DOCTYPE html>
<html><body style="font-family:Inter,sans-serif;background:#f1f5f9;padding:32px">
<div style="max-width:480px;margin:0 auto;background:#fff;border-radius:16px;padding:32px;border:1px solid #e2e8f0">
<div style="text-align:center;margin-bottom:24px">
<div style="width:48px;height:48px;border-radius:50%%;background:#8eb02f;display:inline-flex;align-items:center;justify-content:center;color:#fff;font-weight:700;font-size:20px">U</div>
<h2 style="margin:12px 0 4px;color:#1e293b">Portal de Clientes</h2>
<p style="color:#64748b;font-size:14px;margin:0">Acceso a tu espacio de trabajo</p>
</div>
<p style="color:#334155;font-size:15px">Hola <strong>%s</strong>,</p>
<p style="color:#334155;font-size:14px">Tu cuenta en el Portal de Clientes ha sido creada. Usa los siguientes datos para ingresar:</p>
<div style="background:#f8fafc;border-radius:10px;padding:16px;margin:20px 0;border:1px solid #e2e8f0">
<p style="margin:0 0 8px;font-size:13px;color:#64748b"><strong>Email:</strong> %s</p>
<p style="margin:0 0 8px;font-size:13px;color:#64748b"><strong>Contraseña:</strong> %s</p>
<p style="margin:0;font-size:13px;color:#64748b"><strong>Enlace de acceso:</strong> <a href="%s" style="color:#8eb02f">%s</a></p>
</div>
<a href="%s" style="display:block;text-align:center;background:#8eb02f;color:#fff;padding:12px 24px;border-radius:10px;font-weight:600;text-decoration:none;font-size:15px">Ingresar al Portal</a>
<p style="font-size:12px;color:#94a3b8;margin-top:24px;text-align:center">Si no esperabas este correo, puedes ignorarlo.</p>
</div>
</body></html>`, nombre, email, password, loginURL, loginURL, loginURL)
go func() {
if err := app.Http.Mail.Send(email, "Acceso a tu Portal de Clientes - U-site", htmlBody, ""); err != nil {
log.Printf("[Portal] Error enviando credenciales a %s: %v", email, err)
}
}()
}
+46 -2
View File
@@ -1,6 +1,6 @@
<div x-data="portalUsuariosApp()" x-init="init()" class="p-6">
<div class="flex items-center justify-between mb-6">
<div class="flex items-center justify-between mb-4">
<div>
<h1 class="text-2xl font-bold text-slate-800">Usuarios del Portal</h1>
<p class="text-sm text-slate-500 mt-1">Accesos del portal de clientes</p>
@@ -11,6 +11,15 @@
</button>
</div>
<!-- Banner URL del portal -->
<div class="flex items-center gap-3 bg-blue-50 border border-blue-200 rounded-xl px-4 py-3 mb-6 text-sm">
<svg class="w-4 h-4 text-blue-500 shrink-0" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M13.828 10.172a4 4 0 00-5.656 0l-4 4a4 4 0 105.656 5.656l1.102-1.101m-.758-4.899a4 4 0 005.656 0l4-4a4 4 0 00-5.656-5.656l-1.1 1.1"/></svg>
<span class="text-blue-700">Los usuarios inician sesión en:</span>
<a href="/portal/login" target="_blank" class="font-semibold text-blue-600 hover:underline">/portal/login</a>
<button @click="navigator.clipboard.writeText(window.location.origin+'/portal/login').then(()=>alert('URL copiada'))"
class="ml-auto text-xs text-blue-500 hover:text-blue-700 border border-blue-300 rounded px-2 py-0.5">Copiar enlace</button>
</div>
<!-- Tabla -->
<div class="bg-white rounded-xl shadow-sm border border-slate-200 overflow-x-auto">
<table class="w-full text-sm">
@@ -58,6 +67,9 @@
<button @click="openEdit(u)" class="btn-icon text-yellow-500" title="Editar">
<svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/></svg>
</button>
<button @click="openSendCredentials(u)" class="btn-icon text-blue-500" title="Enviar credenciales">
<svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z"/></svg>
</button>
<button @click="confirmDelete(u)" class="btn-icon text-red-500" title="Eliminar">
<svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/></svg>
</button>
@@ -145,15 +157,33 @@
</div>
</div>
<!-- Modal enviar credenciales -->
<div x-show="showCredModal" x-cloak class="fixed inset-0 z-50 flex items-center justify-center bg-black/40">
<div @click.outside="showCredModal=false" class="bg-white rounded-2xl shadow-2xl w-full max-w-sm mx-4 p-6">
<h2 class="text-lg font-bold mb-1">Enviar credenciales</h2>
<p class="text-slate-500 text-sm mb-4">Se enviará el correo con los datos de acceso al portal a <strong x-text="credEmail"></strong>.</p>
<div class="mb-4">
<label class="label">Contraseña (opcional — se muestra en el correo)</label>
<input x-model="credPassword" type="text" class="input-field w-full" placeholder="Dejar vacío para omitir la contraseña">
</div>
<p x-show="credMsg" x-text="credMsg" class="text-green-600 text-sm mb-3"></p>
<div class="flex justify-end gap-3">
<button @click="showCredModal=false" class="btn-secondary">Cancelar</button>
<button @click="doSendCredentials()" :disabled="saving" class="btn-primary" x-text="saving?'Enviando...':'Enviar correo'"></button>
</div>
</div>
</div>
</div>
<script>
function portalUsuariosApp() {
return {
items: [], clientes: [], portalRoles: [], loading: false, saving: false,
showModal: false, showDelete: false, showAccesoModal: false,
showModal: false, showDelete: false, showAccesoModal: false, showCredModal: false,
editId: null, deleteId: null, error: '',
accesoUserId: null, accesoClienteId: '',
credUserId: null, credEmail: '', credPassword: '', credMsg: '',
form: { nombre:'', email:'', password:'', role_id:'', cliente_id:'', activo:true, notas:'' },
async init() { await this.load(); },
@@ -207,6 +237,20 @@ function portalUsuariosApp() {
await axios.delete(`/app/portal-usuarios/${u.ID}/acceso/${clienteId}`);
await this.load();
},
openSendCredentials(u) {
this.credUserId=u.ID; this.credEmail=u.email; this.credPassword=''; this.credMsg='';
this.showCredModal=true;
},
async doSendCredentials() {
this.saving=true; this.credMsg='';
try {
const r = await axios.post(`/app/portal-usuarios/${this.credUserId}/send-credentials`, {password: this.credPassword});
this.credMsg = r.data.message || 'Correo enviado';
setTimeout(()=>{ this.showCredModal=false; }, 1800);
} catch(e) { this.credMsg = e.response?.data?.error || 'Error al enviar'; }
finally { this.saving=false; }
},
}
}
</script>
@@ -8,6 +8,7 @@ import (
"github.com/gofiber/fiber/v2"
"github.com/sujit-baniya/fiber-boilerplate/app"
"github.com/sujit-baniya/fiber-boilerplate/pkg/models"
"github.com/sujit-baniya/fiber-boilerplate/pkg/services"
)
func PortalUsuariosIndex(c *fiber.Ctx) error {
@@ -172,3 +173,29 @@ func RemovePortalAcceso(c *fiber.Ctx) error {
}
return c.JSON(fiber.Map{"ok": true})
}
// SendPortalCredentials envía las credenciales de acceso al portal al correo del usuario.
func SendPortalCredentials(c *fiber.Ctx) error {
id, err := strconv.ParseUint(c.Params("id"), 10, 32)
if err != nil {
return c.Status(400).JSON(fiber.Map{"error": "ID inválido"})
}
u, err := models.GetPortalUserByID(uint(id))
if err != nil {
return c.Status(404).JSON(fiber.Map{"error": "Usuario no encontrado"})
}
type Req struct {
Password string `json:"password"`
}
var req Req
_ = c.BodyParser(&req)
password := req.Password
if password == "" {
password = "(la contraseña que configuraste)"
}
services.SendPortalCredentialsEmail(u.Email, u.Nombre, password)
return c.JSON(fiber.Map{"ok": true, "message": "Correo enviado a " + u.Email})
}
+1
View File
@@ -254,6 +254,7 @@ func UserRoutes(app fiber.Router) {
protected.Delete("/portal-usuarios/:id", controllers.DeletePortalUsuario)
protected.Post("/portal-usuarios/:id/acceso", controllers.AddPortalAcceso)
protected.Delete("/portal-usuarios/:id/acceso/:clienteID", controllers.RemovePortalAcceso)
protected.Post("/portal-usuarios/:id/send-credentials", controllers.SendPortalCredentials)
protected.Get("/facturas", middlewares.MenuMiddleware, controllers.FacturasIndex)
protected.Get("/loadfacturas", controllers.LoadFacturas)