sample admin users
This commit is contained in:
parent
0f520e67af
commit
cf699e2084
7 changed files with 143 additions and 9 deletions
|
@ -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")
|
||||
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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"
|
||||
|
|
11
models/base.go
Normal file
11
models/base.go
Normal file
|
@ -0,0 +1,11 @@
|
|||
// Copyright (c) 2024 Zeni Kim <zenik@smarteching.com>
|
||||
// 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"`
|
||||
}
|
|
@ -5,10 +5,8 @@
|
|||
|
||||
package models
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type User struct {
|
||||
gorm.Model
|
||||
BaseModel
|
||||
Name string
|
||||
Fullname string
|
||||
Email string
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
34
storage/templates/admin_confirmuserdel.html
Normal file
34
storage/templates/admin_confirmuserdel.html
Normal file
|
@ -0,0 +1,34 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
{{template "page_head" "Goffee"}}
|
||||
<body>
|
||||
<div class="container">
|
||||
<h2>User delete confirmation</h2>
|
||||
{{if .ErrorMessages }}
|
||||
<div class="alert alert-dismissible alert-warning">
|
||||
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
|
||||
<ul>
|
||||
{{range $i, $a := .ErrorMessages}}
|
||||
<li>{{$a}}</li>
|
||||
{{end}}
|
||||
</ul>
|
||||
</div>
|
||||
{{else}}
|
||||
{{if .WarningMessages }}
|
||||
<ul class="warningmessages">
|
||||
{{range $o, $u := .WarningMessages}}
|
||||
<li>{{$u}}</li>
|
||||
{{end}}
|
||||
</ul>
|
||||
{{end}}
|
||||
<form method="POST" id="confirmdelete_user" action="/admin/users/deleteconfirm">
|
||||
{{template "form_input" .FieldKey}}
|
||||
{{template "form_button" .ConfirmButton}}
|
||||
</form>
|
||||
{{end}}
|
||||
<hr>
|
||||
{{template "content_href" .BackButton}}
|
||||
</div>
|
||||
{{template "page_footer"}}
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in a new issue