From cf699e208491df38ff049e5e73790a9d812b2163 Mon Sep 17 00:00:00 2001 From: Zeni Kim Date: Fri, 6 Dec 2024 04:48:10 -0500 Subject: [PATCH] sample admin users --- controllers/adminusers.go | 94 ++++++++++++++++++++- controllers/authentication.go | 4 +- events/event-names.go | 1 + models/base.go | 11 +++ models/user.go | 4 +- routes.go | 4 +- storage/templates/admin_confirmuserdel.html | 34 ++++++++ 7 files changed, 143 insertions(+), 9 deletions(-) create mode 100644 models/base.go create mode 100644 storage/templates/admin_confirmuserdel.html diff --git a/controllers/adminusers.go b/controllers/adminusers.go index 8d4600c..b051308 100644 --- a/controllers/adminusers.go +++ b/controllers/adminusers.go @@ -13,6 +13,7 @@ import ( "git.smarteching.com/goffee/core/template/components" "git.smarteching.com/goffee/cup/events" "git.smarteching.com/goffee/cup/models" + "git.smarteching.com/goffee/cup/utils" "gorm.io/gorm" ) @@ -63,8 +64,8 @@ func AdminUsersList(c *core.Context) *core.Response { {Value: u.Name}, {Value: u.Fullname}, {Value: u.Email}, - {Value: u.CreatedAt.Format("2006-01-02 15:04")}, - {Value: u.UpdatedAt.Format("2006-01-02 15:04")}, + {Value: utils.FormatUnix(u.Created)}, + {Value: utils.FormatUnix(u.Updated)}, {Value: components.ContentHref{ Text: "edit", Link: "/admin/users/edit/" + strconv.Itoa(int(u.ID)), @@ -386,3 +387,92 @@ func AdminUsersEdit(c *core.Context) *core.Response { return c.Response.Template("admin_useredit.html", tmplData) } + +func AdminUsersDelete(c *core.Context) *core.Response { + + user_id := c.GetRequestParam("key").(string) + + errormessages := make([]string, 0) + warningmessages := make([]string, 0) + + var origin_user models.User + + db := c.GetGorm() + // check if existes + result_db := db.First(&origin_user, user_id) + if result_db.RowsAffected == 0 { + errormessages = append(errormessages, "User ID not found") + } else { + // check if is the seed user + seed := "1" + if user_id == seed { + errormessages = append(errormessages, "You can't delete the seed user") + } + + } + + // sample warning message + warningmessages = append(warningmessages, fmt.Sprintf("Are you sure you want to cancel the account %s?", origin_user.Name)) + + // -- response template + type templateData struct { + ErrorMessages []string + WarningMessages []string + FieldKey components.FormInput + ConfirmButton components.FormButton + BackButton components.ContentHref + } + + tmplData := templateData{ + FieldKey: components.FormInput{ + ID: "key", + Type: "hidden", + Value: user_id, + }, + ConfirmButton: components.FormButton{ + ID: "submit", + Text: "Confirm", + Value: "submit", + IsSubmit: true, + TypeClass: "primary", + }, + BackButton: components.ContentHref{ + Link: "/admin/users", + Text: "Cancel", + IsButton: true, + }, + ErrorMessages: errormessages, + WarningMessages: warningmessages, + } + return c.Response.Template("admin_confirmuserdel.html", tmplData) + +} + +func AdminUsersDelConfirm(c *core.Context) *core.Response { + + user_id := c.GetRequestParam("key").(string) + + var origin_user models.User + + db := c.GetGorm() + // check if existes + result_db := db.First(&origin_user, user_id) + if result_db.RowsAffected != 0 { + // check if is the seed user + seed := "1" + if user_id != seed { + // Delete the user + // fire user delete event + err := c.GetEventsManager().Fire(&core.Event{Name: events.USER_DELETED, Payload: map[string]interface{}{ + "user": origin_user, + }}) + if err != nil { + c.GetLogger().Error(err.Error()) + } + result_db.Unscoped().Delete(&origin_user) + } + } + + return c.Response.Redirect("/admin/users") + +} diff --git a/controllers/authentication.go b/controllers/authentication.go index 2b0f19f..9e3eff9 100644 --- a/controllers/authentication.go +++ b/controllers/authentication.go @@ -54,9 +54,9 @@ func Signup(c *core.Context) *core.Response { // validation rules rules := map[string]interface{}{ "name": "required|alphaNumeric", - "fullname": "fullname|alphaNumeric", + "fullname": "required", "email": "required|email", - "password": "required|length:6,10", + "password": "required|length:6,20", } // validate v := c.GetValidator().Validate(data, rules) diff --git a/events/event-names.go b/events/event-names.go index 0e5f431..14afec8 100644 --- a/events/event-names.go +++ b/events/event-names.go @@ -2,5 +2,6 @@ package events // event names const USER_REGISTERED = "user-registered" +const USER_DELETED = "user-deleted" const USER_PASSWORD_RESET_REQUESTED = "user-password-reset-requested" const PASSWORD_CHANGED = "password-changed" diff --git a/models/base.go b/models/base.go new file mode 100644 index 0000000..5350de2 --- /dev/null +++ b/models/base.go @@ -0,0 +1,11 @@ +// Copyright (c) 2024 Zeni Kim +// Use of this source code is governed by MIT-style +// license that can be found in the LICENSE file. + +package models + +type BaseModel struct { + ID uint `json:"id" gorm:"primaryKey; column:id"` + Created int64 `gorm:"autoCreateTime"` + Updated int64 `gorm:"autoUpdateTime"` +} diff --git a/models/user.go b/models/user.go index 86f9914..3a229d9 100644 --- a/models/user.go +++ b/models/user.go @@ -5,10 +5,8 @@ package models -import "gorm.io/gorm" - type User struct { - gorm.Model + BaseModel Name string Fullname string Email string diff --git a/routes.go b/routes.go index 44a5112..785c59e 100644 --- a/routes.go +++ b/routes.go @@ -41,10 +41,10 @@ func registerRoutes() { controller.Post("/admin/users/add", controllers.AdminUsersAdd) controller.Get("/admin/users/edit/:id", controllers.AdminUsersEdit) controller.Post("/admin/users/edit/:id", controllers.AdminUsersEdit) + controller.Post("/admin/users/delete", controllers.AdminUsersDelete) + controller.Post("/admin/users/deleteconfirm", controllers.AdminUsersDelConfirm) //controller.Get("/admin/users/roles", controllers.Signout) //controller.Get("/admin/users/permissions", controllers.ResetPasswordRequest) - //controller.Get("/admin/users/edit", controllers.SetNewPassword) - //controller.Get("/admin/users/cancel", controllers.SetNewPassword) controller.Get("/dashboard", controllers.WelcomeToDashboard, hooks.AuthCheck) diff --git a/storage/templates/admin_confirmuserdel.html b/storage/templates/admin_confirmuserdel.html new file mode 100644 index 0000000..5fd0d71 --- /dev/null +++ b/storage/templates/admin_confirmuserdel.html @@ -0,0 +1,34 @@ + + + {{template "page_head" "Goffee"}} + +
+

User delete confirmation

+ {{if .ErrorMessages }} +
+ +
    + {{range $i, $a := .ErrorMessages}} +
  • {{$a}}
  • + {{end}} +
+
+ {{else}} + {{if .WarningMessages }} +
    + {{range $o, $u := .WarningMessages}} +
  • {{$u}}
  • + {{end}} +
+ {{end}} +
+ {{template "form_input" .FieldKey}} + {{template "form_button" .ConfirmButton}} +
+ {{end}} +
+ {{template "content_href" .BackButton}} +
+ {{template "page_footer"}} + +