diff --git a/model-passwd.go b/model-passwd.go index 8e1da99..8ab3839 100644 --- a/model-passwd.go +++ b/model-passwd.go @@ -165,5 +165,7 @@ func passwordFound(user User, config *ConfigFile, ldapConn *ldap.Conn) (string, log.Printf("passwordFound %v", searchRes) return "", err } + delReq := ldap.NewDelRequest("uid="+user.CN+","+config.InvitationBaseDN, nil) + ldapConn.Del(delReq) return searchRes.Entries[0].GetAttributeValue("seeAlso"), err } diff --git a/model-user.go b/model-user.go index 99e2c2a..e3aecb8 100644 --- a/model-user.go +++ b/model-user.go @@ -86,6 +86,9 @@ func add(user User, config *ConfigFile, ldapConn *ldap.Conn) error { if user.SN != "" { req.Attribute("sn", []string{user.SN}) } + if user.OtherMailbox != "" { + req.Attribute("carLicense", []string{user.OtherMailbox}) + } if user.Description != "" { req.Attribute("description", []string{user.Description}) } @@ -112,8 +115,18 @@ func add(user User, config *ConfigFile, ldapConn *ldap.Conn) error { if err != nil { return err } - return nil } + // Send the email + err = sendMail(SendMailTplData{ + From: "alice@resdigita.org", + To: user.OtherMailbox, + ContentVars: map[string]string{ + "InviteFrom": "alice@resdigita.org", + "SebAddress": "https://www.gvoisins.org", + "Code": "...", + }, + }) + return err } func modify(user User, config *ConfigFile, ldapConn *ldap.Conn) error { diff --git a/utils.go b/utils.go index 16b8afd..e411271 100644 --- a/utils.go +++ b/utils.go @@ -1,12 +1,15 @@ package main import ( + "bytes" "crypto/tls" "log" "net" "math/rand" + "html/template" + "github.com/go-ldap/ldap/v3" // "golang.org/x/text/encoding/unicode" ) @@ -47,3 +50,11 @@ func suggestPassword() string { } return password } + +// Sends an email according to the enclosed information +func sendMail(sendMailTplData SendMailTplData) error { + templateMail := template.Must(template.ParseFiles(templatePath + "/" + sendMailTplData.RelTemplatePath)) + buf := bytes.NewBuffer([]byte{}) + err := templateMail.Execute(buf, sendMailTplData) + return err +} diff --git a/view.go b/view.go index c11a2a3..f217d52 100644 --- a/view.go +++ b/view.go @@ -218,6 +218,19 @@ type LoginFormData struct { Common NestedCommonTplData } +// Data to be passed to an email for sending +type SendMailTplData struct { + // Sender of the email + To string + // Receiver of the email + From string + // Relative path (without leading /) to the email template in the templates folder + // usually ending in .txt + RelTemplatePath string + // Variables to be included in the template of the email + ContentVars map[string]string +} + var templatePath = "./templates" func getTemplate(name string) *template.Template {