From 3daf76dbb0c52dd55671572880a9696bb2a3a1e9 Mon Sep 17 00:00:00 2001 From: yanzuoguang Date: Sun, 9 Nov 2025 18:58:31 +0800 Subject: [PATCH] =?UTF-8?q?feat(admin):=20=E6=B7=BB=E5=8A=A0=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E5=91=98=E9=A1=B5=E9=9D=A2=E7=94=A8=E6=88=B7=E8=AE=A4?= =?UTF-8?q?=E8=AF=81=E5=8A=9F=E8=83=BD-=20=E5=9C=A8=20admin.html=20?= =?UTF-8?q?=E4=B8=AD=E6=B7=BB=E5=8A=A0=E7=94=A8=E6=88=B7=E7=99=BB=E5=87=BA?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E6=98=BE=E7=A4=BA=E5=8C=BA=E5=9F=9F=20-=20?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=20sign=5Fin.html=20=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E7=94=A8=E6=88=B7=E7=99=BB=E5=BD=95=E7=95=8C?= =?UTF-8?q?=E9=9D=A2-=20=E5=AE=9E=E7=8E=B0=20SignOutHandler=20=E5=A4=84?= =?UTF-8?q?=E7=90=86=E7=94=A8=E6=88=B7=E7=99=BB=E5=87=BA=E9=80=BB=E8=BE=91?= =?UTF-8?q?=20-=20=E5=88=9B=E5=BB=BA=20AuthenticationHandler=20=E5=A4=84?= =?UTF-8?q?=E7=90=86=E7=94=A8=E6=88=B7=E7=99=BB=E5=BD=95=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=20-=20=E6=B7=BB=E5=8A=A0=20user=5Fwidget.html=20=E6=A8=A1?= =?UTF-8?q?=E6=9D=BF=E6=98=BE=E7=A4=BA=E7=99=BB=E5=87=BA=E6=8C=89=E9=92=AE?= =?UTF-8?q?=20-=20=E6=9B=B4=E6=96=B0=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AE=A4=E8=AF=81=E5=A4=B1=E8=B4=A5=E8=B7=B3?= =?UTF-8?q?=E8=BD=AC=E5=9C=B0=E5=9D=80=20-=20=E8=B0=83=E6=95=B4=E6=A8=A1?= =?UTF-8?q?=E6=9D=BF=E4=B8=AD=20handler=20=E8=B0=83=E7=94=A8=E7=9A=84?= =?UTF-8?q?=E7=BC=A9=E8=BF=9B=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sportsstore/admin/signout_handler.go | 24 ++++++++ 32-platform/sportsstore/admin/user_store.go | 56 +++++++++++++++++++ 32-platform/sportsstore/config.json | 3 + 32-platform/sportsstore/templates/admin.html | 5 +- .../sportsstore/templates/sign_in.html | 20 +++++++ .../sportsstore/templates/user_widget.html | 9 +++ 6 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 32-platform/sportsstore/admin/signout_handler.go create mode 100644 32-platform/sportsstore/admin/user_store.go create mode 100644 32-platform/sportsstore/templates/sign_in.html create mode 100644 32-platform/sportsstore/templates/user_widget.html diff --git a/32-platform/sportsstore/admin/signout_handler.go b/32-platform/sportsstore/admin/signout_handler.go new file mode 100644 index 0000000..f56b902 --- /dev/null +++ b/32-platform/sportsstore/admin/signout_handler.go @@ -0,0 +1,24 @@ +package admin + +import ( + "platform/authorization/identity" + "platform/http/actionresults" + "platform/http/handing" + "sportstore/store" +) + +type SignOutHandler struct { + identity.User + handing.URLGenerator +} + +func (handler SignOutHandler) GetUserWidget() actionresults.ActionResult { + context := struct { + identity.User + SignOutUrl string + }{ + User: handler.User, + SignOutUrl: store.MustGenerateUrl(handler.URLGenerator, AuthenticationHandler.PostSignOut), + } + return actionresults.NewTemplateAction("user_widget.html", context) +} diff --git a/32-platform/sportsstore/admin/user_store.go b/32-platform/sportsstore/admin/user_store.go new file mode 100644 index 0000000..72613b9 --- /dev/null +++ b/32-platform/sportsstore/admin/user_store.go @@ -0,0 +1,56 @@ +package admin + +import ( + "platform/authorization/identity" + "platform/config" + "platform/http/actionresults" + "platform/http/handing" + "platform/sessions" + "sportstore/store" +) + +type AuthenticationHandler struct { + identity.User + identity.SignInManager + identity.UserStore + sessions.Session + config.Configuration + handing.URLGenerator +} + +type Credentials struct { + UserName string + Password string +} + +const SignInMsgKey string = "sign_in_message" + +func (handler AuthenticationHandler) GetSignIn() actionresults.ActionResult { + message := handler.Session.GetValueDefault(SignInMsgKey, "").(string) + return actionresults.NewTemplateAction("sign_in.html", message) +} + +func (handler AuthenticationHandler) PostSignIn(cred Credentials) actionresults.ActionResult { + if cred.Password == "123456" { + user, ok := handler.UserStore.GetUserByName(cred.UserName) + if ok { + handler.Session.SetValue(SignInMsgKey, "") + err := handler.SignInManager.SignIn(user) + if err != nil { + handler.Session.SetValue(SignInMsgKey, "Sign in failed:"+err.Error()) + return actionresults.NewRedirectAction(store.MustGenerateUrl(handler.URLGenerator, AuthenticationHandler.GetSignIn)) + } + return actionresults.NewRedirectAction("/admin/section/") + } + } + handler.Session.SetValue(SignInMsgKey, "Invalid username or password") + return actionresults.NewRedirectAction(store.MustGenerateUrl(handler.URLGenerator, AuthenticationHandler.GetSignIn)) +} + +func (handler AuthenticationHandler) PostSignOut() actionresults.ActionResult { + err := handler.SignInManager.SignOut(handler.User) + if err != nil { + return actionresults.NewRedirectAction("/") + } + return actionresults.NewRedirectAction("/") +} diff --git a/32-platform/sportsstore/config.json b/32-platform/sportsstore/config.json index fc44b72..34e1e8d 100644 --- a/32-platform/sportsstore/config.json +++ b/32-platform/sportsstore/config.json @@ -38,5 +38,8 @@ "SaveCategory": "sql/save_category.sql", "UpdateCategory": "sql/update_category.sql" } + }, + "authorization": { + "failUrl": "/signin" } } \ No newline at end of file diff --git a/32-platform/sportsstore/templates/admin.html b/32-platform/sportsstore/templates/admin.html index 0975755..798c223 100644 --- a/32-platform/sportsstore/templates/admin.html +++ b/32-platform/sportsstore/templates/admin.html @@ -12,6 +12,9 @@
+
@@ -35,7 +38,7 @@ Welcome to the SportStore Administrations Features {{ else }} - {{ handler $context.ActiveSection "GetData" }} + {{ handler $context.ActiveSection "GetData" }} {{ end }} diff --git a/32-platform/sportsstore/templates/sign_in.html b/32-platform/sportsstore/templates/sign_in.html new file mode 100644 index 0000000..c89a07f --- /dev/null +++ b/32-platform/sportsstore/templates/sign_in.html @@ -0,0 +1,20 @@ +{{ layout "simple_layout.html" }} + +{{ if ne . "" }} +

{{ . }}

+{{ end }} + + +
+
+ + +
+
+ + +
+
+ +
+
\ No newline at end of file diff --git a/32-platform/sportsstore/templates/user_widget.html b/32-platform/sportsstore/templates/user_widget.html new file mode 100644 index 0000000..ee508f6 --- /dev/null +++ b/32-platform/sportsstore/templates/user_widget.html @@ -0,0 +1,9 @@ +{{ $context := . }} + +{{ if $context.User.IsAuthenticated }} +
+ +
+{{ end }} \ No newline at end of file