From 2809df41c2e11bd65b62788bfd917e379b7f3b9d Mon Sep 17 00:00:00 2001 From: Chris Mann Date: Tue, 25 Jul 2023 21:37:46 +0200 Subject: [PATCH] Refactoring --- Makefile | 2 +- controller.go | 102 ++++++++++++++++++++++ garage.go | 7 +- model.go | 31 +++++++ view.go | 230 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 368 insertions(+), 4 deletions(-) create mode 100644 controller.go create mode 100644 model.go create mode 100644 view.go diff --git a/Makefile b/Makefile index 9025129..c8eeba0 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ BIN=guichet -SRC=main.go ssha.go profile.go admin.go invite.go directory.go utils.go picture.go login.go config.go http-utils.go home.go model-user.go gpas.go session.go +SRC=main.go ssha.go profile.go admin.go invite.go directory.go utils.go picture.go login.go config.go http-utils.go home.go model-user.go gpas.go session.go model.go view.go controller.go DOCKER=lxpz/guichet_amd64 all: $(BIN) diff --git a/controller.go b/controller.go new file mode 100644 index 0000000..06e9d49 --- /dev/null +++ b/controller.go @@ -0,0 +1,102 @@ +/* +Routes the requests to the app +*/ +package main + +import ( + "net/http" + + "github.com/gorilla/mux" +) + +type ConfigFile struct { + HttpBindAddr string `json:"http_bind_addr"` + LdapServerAddr string `json:"ldap_server_addr"` + LdapTLS bool `json:"ldap_tls"` + + BaseDN string `json:"base_dn"` + UserBaseDN string `json:"user_base_dn"` + UserNameAttr string `json:"user_name_attr"` + GroupBaseDN string `json:"group_base_dn"` + GroupNameAttr string `json:"group_name_attr"` + + MailingBaseDN string `json:"mailing_list_base_dn"` + MailingNameAttr string `json:"mailing_list_name_attr"` + MailingGuestsBaseDN string `json:"mailing_list_guest_user_base_dn"` + + InvitationBaseDN string `json:"invitation_base_dn"` + InvitationNameAttr string `json:"invitation_name_attr"` + InvitedMailFormat string `json:"invited_mail_format"` + InvitedAutoGroups []string `json:"invited_auto_groups"` + + WebAddress string `json:"web_address"` + MailFrom string `json:"mail_from"` + SMTPServer string `json:"smtp_server"` + SMTPUsername string `json:"smtp_username"` + SMTPPassword string `json:"smtp_password"` + + AdminAccount string `json:"admin_account"` + GroupCanInvite string `json:"group_can_invite"` + GroupCanAdmin string `json:"group_can_admin"` + + S3AdminEndpoint string `json:"s3_admin_endpoint"` + S3AdminToken string `json:"s3_admin_token"` + + S3Endpoint string `json:"s3_endpoint"` + S3AccessKey string `json:"s3_access_key"` + S3SecretKey string `json:"s3_secret_key"` + S3Region string `json:"s3_region"` + S3Bucket string `json:"s3_bucket"` + + Org string `json:"org"` + DomainName string `json:"domain_name"` + NewUserDN string `json:"new_user_dn"` + NewUserPassword string `json:"new_user_password"` +} + +var staticPath = "./static" + +/* +Create the different routes +*/ +func makeGVRouter() (*mux.Router, error) { + r := mux.NewRouter() + r.HandleFunc("/", handleHome) + r.HandleFunc("/logout", handleLogout) + + r.HandleFunc("/profile", handleProfile) + r.HandleFunc("/passwd", handlePasswd) + r.HandleFunc("/picture/{name}", handleDownloadPicture) + + r.HandleFunc("/admin-activate", handleAdminActivateUsers) + r.HandleFunc("/admin-unactivate/{cn}", handleAdminUnactivateUser) + r.HandleFunc("/admin-activate/{cn}", handleAdminActivateUser) + + // r.HandleFunc("/directory/search", handleDirectorySearch) + // r.HandleFunc("/directory", handleDirectory) + // r.HandleFunc("/garage/key", handleGarageKey) + // r.HandleFunc("/garage/website", handleGarageWebsiteList) + // r.HandleFunc("/garage/website/new", handleGarageWebsiteNew) + // r.HandleFunc("/garage/website/b/{bucket}", handleGarageWebsiteInspect) + + r.HandleFunc("/inscription", handleInviteNewAccount) + // r.HandleFunc("/invite/send_code", handleInviteSendCode) + r.HandleFunc("/gpassword/{code}", handleFoundPassword) + r.HandleFunc("/gpas", handleLostPassword) + // r.HandleFunc("/invitation/{code}", handleInvitationCode) + + r.HandleFunc("/admin-users", handleAdminUsers) + r.HandleFunc("/admin-groups", handleAdminGroups) + r.HandleFunc("/admin-mailing", handleAdminMailing) + // r.HandleFunc("/admin/mailing/{id}", handleAdminMailingList) + r.HandleFunc("/admin-ldap/{dn}", handleAdminLDAP) + r.HandleFunc("/admin-create/{template}/{super_dn}", handleAdminCreate) + + staticFiles := http.FileServer(http.Dir(staticPath)) + r.Handle("/static/{file:.*}", http.StripPrefix("/static/", staticFiles)) + + // log.Printf("Starting HTTP server on %s", config.HttpBindAddr) + err := http.ListenAndServe(config.HttpBindAddr, logRequest(r)) + + return r, err +} diff --git a/garage.go b/garage.go index 1ae02e4..c72f118 100644 --- a/garage.go +++ b/garage.go @@ -4,12 +4,13 @@ import ( "context" "errors" "fmt" - garage "git.deuxfleurs.fr/garage-sdk/garage-admin-sdk-golang" - "github.com/go-ldap/ldap/v3" - "github.com/gorilla/mux" "log" "net/http" "strings" + + garage "git.deuxfleurs.fr/garage-sdk/garage-admin-sdk-golang" + "github.com/go-ldap/ldap/v3" + "github.com/gorilla/mux" ) func gadmin() (*garage.APIClient, context.Context) { diff --git a/model.go b/model.go new file mode 100644 index 0000000..2726f79 --- /dev/null +++ b/model.go @@ -0,0 +1,31 @@ +/* +Centralises the models used in this application +*/ + +package main + +import ( + "github.com/go-ldap/ldap/v3" +) + +/* +Represents a user +*/ +type User struct { + DN string + CN string + GivenName string + DisplayName string + Mail string + SN string + UID string + Description string + Password string + OtherMailbox string + CanAdmin bool + CanInvite bool + UserEntry *ldap.Entry + SeeAlso string +} + +type EntryList []*ldap.Entry diff --git a/view.go b/view.go new file mode 100644 index 0000000..c11a2a3 --- /dev/null +++ b/view.go @@ -0,0 +1,230 @@ +/* +Creates the webpages to be processed by Guichet +*/ +package main + +import ( + "html/template" + "strings" + + "github.com/go-ldap/ldap/v3" +) + +type NestedCommonTplData struct { + Error string + ErrorMessage string + CanAdmin bool + CanInvite bool + LoggedIn bool + Success bool + WarningMessage string +} +type NestedLoginTplData struct { + Login *LoginStatus + Username string + Status *LoginStatus +} + +type AdminUsersTplData struct { + UserNameAttr string + UserBaseDN string + Users EntryList + Common NestedCommonTplData + Login NestedLoginTplData +} +type AdminLDAPTplData struct { + DN string + + Path []PathItem + ChildrenOU []Child + ChildrenOther []Child + CanAddChild bool + Props map[string]*PropValues + CanDelete bool + + HasMembers bool + Members []EntryName + PossibleNewMembers []EntryName + HasGroups bool + Groups []EntryName + PossibleNewGroups []EntryName + + ListMemGro map[string]string + + Common NestedCommonTplData + Login NestedLoginTplData +} +type AdminMailingListTplData struct { + Common NestedCommonTplData + Login NestedLoginTplData + MailingNameAttr string + MailingBaseDN string + MailingList *ldap.Entry + Members EntryList + PossibleNewMembers EntryList + AllowGuest bool +} +type AdminMailingTplData struct { + Common NestedCommonTplData + Login NestedLoginTplData + MailingNameAttr string + MailingBaseDN string + MailingLists EntryList +} +type AdminGroupsTplData struct { + Common NestedCommonTplData + Login NestedLoginTplData + GroupNameAttr string + GroupBaseDN string + Groups EntryList +} +type EntryName struct { + DN string + Name string +} +type Child struct { + DN string + Identifier string + Name string +} +type PathItem struct { + DN string + Identifier string + Active bool +} +type PropValues struct { + Name string + Values []string + Editable bool + Deletable bool +} +type CreateData struct { + SuperDN string + Path []PathItem + Template string + + IdType string + IdValue string + DisplayName string + GivenName string + Member string + Mail string + Description string + StructuralObjectClass string + ObjectClass string + SN string + + Common NestedCommonTplData + Login NestedLoginTplData +} +type SearchResult struct { + DN string + Id string + DisplayName string + Email string + Description string + ProfilePicture string +} +type SearchResults struct { + Results []SearchResult +} +type HomePageData struct { + Common NestedCommonTplData + Login NestedLoginTplData + BaseDN string + Org string +} +type PasswordFoundData struct { + Common NestedCommonTplData + Login NestedLoginTplData + Username string + Mail string + OtherMailbox string +} +type PasswordLostData struct { + Common NestedCommonTplData + ErrorMessage string + Success bool + Username string + Mail string + OtherMailbox string +} +type NewAccountData struct { + Username string + DisplayName string + GivenName string + Surname string + Mail string + SuggestPW string + OtherEmail string + + ErrorUsernameTaken bool + ErrorInvalidUsername bool + ErrorPasswordTooShort bool + ErrorPasswordMismatch bool + Common NestedCommonTplData +} +type SendCodeData struct { + Common NestedCommonTplData + ErrorInvalidEmail bool + + CodeDisplay string + CodeSentTo string + WebBaseAddress string +} + +type CodeMailFields struct { + From string + To string + Code string + InviteFrom string + WebBaseAddress string + Common NestedCommonTplData +} +type ProfileTplData struct { + Mail string + DisplayName string + GivenName string + Surname string + Description string + Common NestedCommonTplData + Login NestedLoginTplData +} + +//ProfilePicture string +//Visibility string + +type PasswdTplData struct { + Common NestedCommonTplData + Login NestedLoginTplData + TooShortError bool + NoMatchError bool +} +type LoginInfo struct { + Username string + DN string + Password string +} +type LoginStatus struct { + Info *LoginInfo + conn *ldap.Conn + UserEntry *ldap.Entry + Common NestedCommonTplData +} +type LoginFormData struct { + Username string + WrongUser bool + WrongPass bool + Common NestedCommonTplData +} + +var templatePath = "./templates" + +func getTemplate(name string) *template.Template { + return template.Must(template.New("layout.html").Funcs(template.FuncMap{ + "contains": strings.Contains, + }).ParseFiles( + templatePath+"/layout.html", + templatePath+"/"+name, + )) +}