From 249ee195ce52ee4a4defeb67a33ef353919d3a11 Mon Sep 17 00:00:00 2001 From: Gabriel Arakaki Giovanini Date: Sun, 25 Jun 2023 19:09:54 +0200 Subject: feat: Add user list UI Fill user settings UI with actual data. --- README.md | 2 ++ cmd/server/main.go | 2 +- pkg/components/auth/controller.go | 14 ------------ pkg/components/auth/model.go | 22 ------------------ pkg/components/user/controller.go | 1 + pkg/components/user/model.go | 33 +++++++++++++++++++++++++++ pkg/database/sql/user.go | 15 +++++++++---- pkg/view/auth.go | 20 ----------------- pkg/view/settings.go | 42 +++++++++++++++++++++++++--------- scss/main.scss | 9 ++++++-- templates/fs.html | 2 +- templates/settings.html | 47 ++++++++++++--------------------------- 12 files changed, 102 insertions(+), 107 deletions(-) create mode 100644 pkg/components/user/controller.go create mode 100644 pkg/components/user/model.go diff --git a/README.md b/README.md index 6103dbf..9ff9bfd 100644 --- a/README.md +++ b/README.md @@ -13,3 +13,5 @@ A read only file explorer with media capabilities. * Single image viewer and show exif info (not sure how yet) * User base root folder * Albuns +* Testing. Since I still on initial iteration phases I'm not adding as many + testing as I'd like to. Once I set on most of the design I'll add testing. diff --git a/cmd/server/main.go b/cmd/server/main.go index 4ca39de..d7c2fd6 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -103,7 +103,7 @@ func main() { for _, v := range []view.View{ view.NewAuthView(userController), view.NewFileSystemView(*fileSystemController, settingsRepository), - view.NewSettingsView(settingsRepository), + view.NewSettingsView(settingsRepository, userRepository), view.NewMediaView(mediaRepository), } { v.SetMyselfIn(extRouter) diff --git a/pkg/components/auth/controller.go b/pkg/components/auth/controller.go index 4da6071..a81a1c0 100644 --- a/pkg/components/auth/controller.go +++ b/pkg/components/auth/controller.go @@ -41,17 +41,3 @@ func (c *Controller) Login(ctx context.Context, username, password []byte) ([]by } return ext.WriteToken(token, c.key) } - -func (c *Controller) Register(ctx context.Context, username, password []byte) error { - hash, err := bcrypt.GenerateFromPassword(password, bcrypt.MinCost) - if err != nil { - return err - } - - _, err = c.repository.Create(ctx, &CreateUser{ - Username: string(username), - Password: hash, - }) - - return err -} diff --git a/pkg/components/auth/model.go b/pkg/components/auth/model.go index e46ef49..dd6ce50 100644 --- a/pkg/components/auth/model.go +++ b/pkg/components/auth/model.go @@ -3,30 +3,8 @@ package auth import "context" type ( - // TODO: move to user later - User struct { - ID uint - Username string - Name string - } - - // TODO: move to user later - UpdateUser struct { - Username string - Name string - } - - // TODO: move to user later - CreateUser struct { - Username string - Name string - Password []byte - } - Repository interface { GetIDByUsername(ctx context.Context, username string) (uint, error) GetPassword(ctx context.Context, id uint) ([]byte, error) - // TODO: move to user later - Create(ctx context.Context, createUser *CreateUser) (uint, error) } ) diff --git a/pkg/components/user/controller.go b/pkg/components/user/controller.go new file mode 100644 index 0000000..a00006b --- /dev/null +++ b/pkg/components/user/controller.go @@ -0,0 +1 @@ +package user diff --git a/pkg/components/user/model.go b/pkg/components/user/model.go new file mode 100644 index 0000000..f957c39 --- /dev/null +++ b/pkg/components/user/model.go @@ -0,0 +1,33 @@ +package user + +import "context" + +type ( + User struct { + ID uint + Username string + Name string + IsAdmin bool + Path string + } + + UpdateUser struct { + Username string + Name string + Password *string + } + + CreateUser struct { + Username string + Name string + Password string + IsAdmin bool + Path string + } + + Repository interface { + List(ctx context.Context) ([]*User, error) + Create(ctx context.Context, createUser *CreateUser) error + Update(ctx context.Context, id uint, updateUser *UpdateUser) error + } +) diff --git a/pkg/database/sql/user.go b/pkg/database/sql/user.go index d449b05..2d74162 100644 --- a/pkg/database/sql/user.go +++ b/pkg/database/sql/user.go @@ -7,7 +7,7 @@ import ( "gorm.io/gorm" "git.sr.ht/~gabrielgio/img/pkg/components/auth" - user "git.sr.ht/~gabrielgio/img/pkg/components/auth" + "git.sr.ht/~gabrielgio/img/pkg/components/user" ) type ( @@ -16,6 +16,8 @@ type ( Username string Name string Password string + IsAdmin bool + Path string } Users []*User @@ -26,6 +28,7 @@ type ( ) var _ auth.Repository = &UserRepository{} +var _ user.Repository = &UserRepository{} func NewUserRepository(db *gorm.DB) *UserRepository { return &UserRepository{ @@ -38,6 +41,8 @@ func (self *User) ToModel() *user.User { ID: self.Model.ID, Name: self.Name, Username: self.Username, + Path: self.Path, + IsAdmin: self.IsAdmin, } } @@ -63,6 +68,8 @@ func (self *UserRepository) EnsureAdmin(ctx context.Context) { hash, _ := bcrypt.GenerateFromPassword([]byte("admin"), bcrypt.MinCost) self.db.Save(&User{ Username: "admin", + Path: "/", + IsAdmin: true, Password: string(hash), }) } @@ -130,7 +137,7 @@ func (self *UserRepository) GetPassword(ctx context.Context, id uint) ([]byte, e return userPassword.Password, nil } -func (self *UserRepository) Create(ctx context.Context, createUser *user.CreateUser) (uint, error) { +func (self *UserRepository) Create(ctx context.Context, createUser *user.CreateUser) error { user := &User{ Username: createUser.Username, Name: createUser.Name, @@ -141,10 +148,10 @@ func (self *UserRepository) Create(ctx context.Context, createUser *user.CreateU WithContext(ctx). Create(user) if result.Error != nil { - return 0, result.Error + return result.Error } - return user.Model.ID, nil + return nil } func (self *UserRepository) Update(ctx context.Context, id uint, update *user.UpdateUser) error { diff --git a/pkg/view/auth.go b/pkg/view/auth.go index 5c83eba..d44424d 100644 --- a/pkg/view/auth.go +++ b/pkg/view/auth.go @@ -64,23 +64,6 @@ func (v *AuthView) Login(ctx *fasthttp.RequestCtx) error { return nil } -func (v *AuthView) RegisterView(ctx *fasthttp.RequestCtx) error { - return img.Render[interface{}](ctx, "register.html", nil) -} - -func (v *AuthView) Register(ctx *fasthttp.RequestCtx) error { - username := ctx.FormValue("username") - password := ctx.FormValue("password") - - err := v.userController.Register(ctx, username, password) - if err != nil { - return err - } - - ctx.Redirect("/login", 307) - return nil -} - func Index(ctx *fasthttp.RequestCtx) { ctx.Redirect("/login", 307) } @@ -89,9 +72,6 @@ func (v *AuthView) SetMyselfIn(r *ext.Router) { r.GET("/login", v.LoginView) r.POST("/login", v.Login) - r.GET("/register", v.RegisterView) - r.POST("/register", v.Register) - r.GET("/logout", v.Logout) r.POST("/logout", v.Logout) } diff --git a/pkg/view/settings.go b/pkg/view/settings.go index 746dee4..e5acb1b 100644 --- a/pkg/view/settings.go +++ b/pkg/view/settings.go @@ -5,28 +5,50 @@ import ( "git.sr.ht/~gabrielgio/img" "git.sr.ht/~gabrielgio/img/pkg/components/settings" + "git.sr.ht/~gabrielgio/img/pkg/components/user" "git.sr.ht/~gabrielgio/img/pkg/ext" ) -type SettingsView struct { - // there is not need to create a controller for this - repository settings.Repository -} +type ( + SettingsView struct { + // there is not need to create a controller for this + settingsRepository settings.Repository + userRepository user.Repository + } + + SettingsPage struct { + Settings *settings.Settings + Users []*user.User + } +) -func NewSettingsView(respository settings.Repository) *SettingsView { +func NewSettingsView( + settingsRespository settings.Repository, + userRepository user.Repository, +) *SettingsView { return &SettingsView{ - repository: respository, + settingsRepository: settingsRespository, + userRepository: userRepository, } } func (self *SettingsView) Index(ctx *fasthttp.RequestCtx) error { - s, err := self.repository.Load(ctx) + s, err := self.settingsRepository.Load(ctx) if err != nil { return err } - return img.Render(ctx, "settings.html", &img.HTMLView[*settings.Settings]{ + + users, err := self.userRepository.List(ctx) + if err != nil { + return err + } + + return img.Render(ctx, "settings.html", &img.HTMLView[*SettingsPage]{ Title: "Settings", - Data: s, + Data: &SettingsPage{ + Settings: s, + Users: users, + }, }) } @@ -36,7 +58,7 @@ func (self *SettingsView) Save(ctx *fasthttp.RequestCtx) error { showOwner = string(ctx.FormValue("showOwner")) == "on" ) - err := self.repository.Save(ctx, &settings.Settings{ + err := self.settingsRepository.Save(ctx, &settings.Settings{ ShowMode: showMode, ShowOwner: showOwner, }) diff --git a/scss/main.scss b/scss/main.scss index 033315f..7028622 100644 --- a/scss/main.scss +++ b/scss/main.scss @@ -34,9 +34,10 @@ body { .input, .button{ border-radius: 0; } - -.file-row { +.wide-column { width: 100%; +} +.is-mono { font-family: monospace; } @@ -45,6 +46,10 @@ nav { margin: auto; } +a.is-danger { + color: $danger +} + .container { margin-top: 15px; diff --git a/templates/fs.html b/templates/fs.html index 608289d..a44d78f 100644 --- a/templates/fs.html +++ b/templates/fs.html @@ -11,7 +11,7 @@ {{range .Data.Page.Files}}
-
+
{{if $.Data.ShowMode}}{{.Info.Mode}} {{end}} {{if $.Data.ShowOwner}}{{.Info.Sys.Gid}}:{{.Info.Sys.Uid}} {{end}} diff --git a/templates/settings.html b/templates/settings.html index 2aa1a80..8c08773 100644 --- a/templates/settings.html +++ b/templates/settings.html @@ -7,7 +7,7 @@
@@ -15,7 +15,7 @@
@@ -26,38 +26,19 @@
-
- - - - - - - - - - - - - - - - - - - - - - - -
UsernamePathIs Admin?Action
gabrielgio/home/gabrielgioyes - Edit - Delete -
beatriz/home/beatrizno - Edit - Delete -
+ {{range .Data.Users}} +
+
+
+ {{.Username}} +
+
+ {{.Path}} +
+ +
+ {{end}}
{{end}} -- cgit v1.2.3