diff --git a/pkg/server/admin/index.go b/pkg/server/admin/index.go index e3a15fd..ad11edc 100644 --- a/pkg/server/admin/index.go +++ b/pkg/server/admin/index.go @@ -1,12 +1,11 @@ package admin import ( - "git.solsynth.dev/hydrogen/passport/pkg/utils" "github.com/gofiber/fiber/v2" ) -func MapAdminEndpoints(A *fiber.App, authFunc utils.AuthFunc) { - admin := A.Group("/api/admin").Use(authFunc) +func MapAdminEndpoints(A *fiber.App, authMiddleware fiber.Handler) { + admin := A.Group("/api/admin").Use(authMiddleware) { admin.Post("/badges", grantBadge) admin.Delete("/badges/:badgeId", revokeBadge) diff --git a/pkg/server/startup.go b/pkg/server/startup.go index c2f8453..021ffe8 100644 --- a/pkg/server/startup.go +++ b/pkg/server/startup.go @@ -144,7 +144,7 @@ func NewServer() { URL: "/favicon.png", })) - admin.MapAdminEndpoints(A, authFunc) + admin.MapAdminEndpoints(A, authMiddleware) ui.MapUserInterface(A, authFunc) } diff --git a/pkg/server/ui/directory.go b/pkg/server/ui/directory.go deleted file mode 100644 index dc64950..0000000 --- a/pkg/server/ui/directory.go +++ /dev/null @@ -1,51 +0,0 @@ -package ui - -import ( - "fmt" - "html/template" - "time" - - "git.solsynth.dev/hydrogen/passport/pkg/database" - "git.solsynth.dev/hydrogen/passport/pkg/models" - "github.com/gofiber/fiber/v2" - "github.com/gomarkdown/markdown" - "github.com/gomarkdown/markdown/html" - "github.com/gomarkdown/markdown/parser" - "github.com/sujit-baniya/flash" -) - -func otherUserinfoPage(c *fiber.Ctx) error { - name := c.Params("account") - - var data models.Account - if err := database.C. - Where(&models.Account{Name: name}). - Preload("Profile"). - Preload("PersonalPage"). - Preload("Contacts"). - First(&data).Error; err != nil { - return fiber.NewError(fiber.StatusInternalServerError, err.Error()) - } - - birthday := "Unknown" - if data.Profile.Birthday != nil { - birthday = data.Profile.Birthday.Format(time.RFC822) - } - - doc := parser. - NewWithExtensions(parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock). - Parse([]byte(data.PersonalPage.Content)) - - renderer := html.NewRenderer(html.RendererOptions{Flags: html.CommonFlags | html.HrefTargetBlank}) - - return c.Render("views/users/directory/userinfo", fiber.Map{ - "info": flash.Get(c)["message"], - "uid": fmt.Sprintf("%08d", data.ID), - "joined_at": data.CreatedAt.Format(time.RFC822), - "birthday_at": birthday, - "personal_page": template.HTML(markdown.Render(doc, renderer)), - "userinfo": data, - "avatar": data.GetAvatar(), - "banner": data.GetBanner(), - }, "views/layouts/user-center") -} diff --git a/pkg/server/ui/index.go b/pkg/server/ui/index.go index 7f0fc62..eb8b19f 100644 --- a/pkg/server/ui/index.go +++ b/pkg/server/ui/index.go @@ -43,10 +43,5 @@ func MapUserInterface(A *fiber.App, authFunc utils.AuthFunc) { pages.Post("/mfa/apply", mfaApplyAction) pages.Post("/authorize", authCheckWare, authorizeAction) - pages.Get("/@:account", otherUserinfoPage) - pages.Get("/users/me", authCheckWare, selfUserinfoPage) - pages.Get("/users/me/personalize", authCheckWare, personalizePage) - - pages.Post("/users/me/personalize", authCheckWare, personalizeAction) } diff --git a/pkg/server/ui/personalize.go b/pkg/server/ui/personalize.go deleted file mode 100644 index 12b8797..0000000 --- a/pkg/server/ui/personalize.go +++ /dev/null @@ -1,101 +0,0 @@ -package ui - -import ( - "fmt" - "git.solsynth.dev/hydrogen/passport/pkg/database" - "git.solsynth.dev/hydrogen/passport/pkg/models" - "git.solsynth.dev/hydrogen/passport/pkg/services" - "git.solsynth.dev/hydrogen/passport/pkg/utils" - "github.com/gofiber/fiber/v2" - "github.com/nicksnyder/go-i18n/v2/i18n" - "github.com/samber/lo" - "github.com/sujit-baniya/flash" - "strings" - "time" -) - -func personalizePage(c *fiber.Ctx) error { - user := c.Locals("principal").(models.Account) - localizer := c.Locals("localizer").(*i18n.Localizer) - - var data models.Account - if err := database.C. - Where(&models.Account{BaseModel: models.BaseModel{ID: user.ID}}). - Preload("Profile"). - Preload("PersonalPage"). - Preload("Contacts"). - First(&data).Error; err != nil { - return fiber.NewError(fiber.StatusInternalServerError, err.Error()) - } - - var birthday any - if data.Profile.Birthday != nil { - birthday = strings.SplitN(data.Profile.Birthday.Format(time.RFC3339), "T", 1)[0] - } - - apply, _ := localizer.LocalizeMessage(&i18n.Message{ID: "apply"}) - back, _ := localizer.LocalizeMessage(&i18n.Message{ID: "back"}) - - return c.Render("views/users/personalize", fiber.Map{ - "info": flash.Get(c)["message"], - "birthday_at": birthday, - "userinfo": data, - "i18n": fiber.Map{ - "apply": apply, - "back": back, - }, - }, "views/layouts/user-center") -} - -func personalizeAction(c *fiber.Ctx) error { - user := c.Locals("principal").(models.Account) - - var data struct { - Nick string `form:"nick" validate:"required,min=4,max=24"` - Description string `form:"description"` - FirstName string `form:"first_name"` - LastName string `form:"last_name"` - Birthday string `form:"birthday"` - } - - if err := utils.BindAndValidate(c, &data); err != nil { - return flash.WithInfo(c, fiber.Map{ - "message": err.Error(), - }).Redirect("/users/me/personalize") - } - - var account models.Account - if err := database.C. - Where(&models.Account{BaseModel: models.BaseModel{ID: user.ID}}). - Preload("Profile"). - First(&account).Error; err != nil { - return flash.WithInfo(c, fiber.Map{ - "message": fmt.Sprintf("unable to get your userinfo: %v", err), - }).Redirect("/users/me/personalize") - } - - account.Nick = data.Nick - account.Description = data.Description - account.Profile.FirstName = data.FirstName - account.Profile.LastName = data.LastName - - if birthday, err := time.Parse(time.DateOnly, data.Birthday); err == nil { - account.Profile.Birthday = lo.ToPtr(birthday) - } - - if err := database.C.Save(&account).Error; err != nil { - return flash.WithInfo(c, fiber.Map{ - "message": fmt.Sprintf("unable to personalize your account: %v", err), - }).Redirect("/users/me/personalize") - } else if err := database.C.Save(&account.Profile).Error; err != nil { - return flash.WithInfo(c, fiber.Map{ - "message": fmt.Sprintf("unable to personalize your profile: %v", err), - }).Redirect("/users/me/personalize") - } - - services.InvalidAuthCacheWithUser(account.ID) - - return flash.WithInfo(c, fiber.Map{ - "message": "your account has been personalized", - }).Redirect("/users/me") -} diff --git a/pkg/views/authorize.gohtml b/pkg/views/authorize.gohtml index 2527308..c98e6a4 100644 --- a/pkg/views/authorize.gohtml +++ b/pkg/views/authorize.gohtml @@ -1,50 +1,56 @@