Skip to content

Commit

Permalink
feat: change current user's profile
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jul 23, 2022
1 parent fb65e98 commit 4f3129e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 32 deletions.
2 changes: 1 addition & 1 deletion internal/bootstrap/data/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func initSettings() {
}
}
// insert new items
for i, _ := range initialSettingItems {
for i := range initialSettingItems {
v := initialSettingItems[i]
_, err := db.GetSettingItemByKey(v.Key)
if err == nil {
Expand Down
20 changes: 18 additions & 2 deletions server/handles/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ var (
)

type LoginReq struct {
Username string `json:"username"`
Password string `json:"password"`
Username string `json:"username" binding:"required"`
Password string `json:"password" binding:"required"`
}

func Login(c *gin.Context) {
Expand Down Expand Up @@ -66,3 +66,19 @@ func CurrentUser(c *gin.Context) {
userResp.Password = ""
common.SuccessResp(c, userResp)
}

func UpdateCurrent(c *gin.Context) {
var req LoginReq
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
user := c.MustGet("user").(*model.User)
user.Username = req.Username
user.Password = req.Password
if err := db.UpdateUser(user); err != nil {
common.ErrorResp(c, err, 500)
} else {
common.SuccessResp(c)
}
}
62 changes: 33 additions & 29 deletions server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,55 @@ func Init(r *gin.Engine) {
r.GET("/d/*path", middlewares.Down, handles.Down)
r.GET("/p/*path", middlewares.Down, handles.Proxy)

r.POST("/api/auth/login", handles.Login)
api := r.Group("/api")
auth := api.Group("", middlewares.Auth)

api := r.Group("/api", middlewares.Auth)
api.GET("/auth/current", handles.CurrentUser)
api.POST("/auth/login", handles.Login)
auth.GET("/profile", handles.CurrentUser)
auth.POST("/profile/update", handles.UpdateCurrent)

admin := api.Group("/admin", middlewares.AuthAdmin)
// no need auth
public := api.Group("/public")
public.Any("/settings", handles.PublicSettings)

fs(auth.Group("/fs"))
admin(auth.Group("/admin", middlewares.AuthAdmin))
}

meta := admin.Group("/meta")
func admin(g *gin.RouterGroup) {
meta := g.Group("/meta")
meta.GET("/list", handles.ListMetas)
meta.POST("/create", handles.CreateMeta)
meta.POST("/update", handles.UpdateMeta)
meta.POST("/delete", handles.DeleteMeta)

user := admin.Group("/user")
user := g.Group("/user")
user.GET("/list", handles.ListUsers)
user.POST("/create", handles.CreateUser)
user.POST("/update", handles.UpdateUser)
user.POST("/delete", handles.DeleteUser)

storage := admin.Group("/storage")
storage := g.Group("/storage")
storage.GET("/list", handles.ListStorages)
storage.GET("/get", handles.GetStorage)
storage.POST("/create", handles.CreateStorage)
storage.POST("/update", handles.UpdateStorage)
storage.POST("/delete", handles.DeleteStorage)

driver := admin.Group("/driver")
driver := g.Group("/driver")
driver.GET("/list", handles.ListDriverItems)
driver.GET("/names", handles.ListDriverNames)
driver.GET("/items", handles.GetDriverItems)

setting := admin.Group("/setting")
setting := g.Group("/setting")
setting.GET("/get", handles.GetSetting)
setting.GET("/list", handles.ListSettings)
setting.POST("/save", handles.SaveSettings)
setting.POST("/delete", handles.DeleteSetting)
setting.POST("/reset_token", handles.ResetToken)
setting.POST("/set_aria2", handles.SetAria2)

task := admin.Group("/task")
task := g.Group("/task")
task.GET("/down/undone", handles.UndoneDownTask)
task.GET("/down/done", handles.DoneDownTask)
task.POST("/down/cancel", handles.CancelDownTask)
Expand All @@ -71,28 +80,23 @@ func Init(r *gin.Engine) {
task.GET("/copy/done", handles.DoneCopyTask)
task.POST("/copy/cancel", handles.CancelCopyTask)

ms := admin.Group("/message")
ms := g.Group("/message")
ms.GET("/get", message.PostInstance.GetHandle)
ms.POST("/send", message.PostInstance.SendHandle)
}

// guest can
public := api.Group("/public")
r.Any("/api/public/settings", handles.PublicSettings)
//public.GET("/settings", controllers.PublicSettings)
public.Any("/list", handles.FsList)
public.Any("/get", handles.FsGet)
public.Any("/dirs", handles.FsDirs)

// gust can't
fs := api.Group("/fs")
fs.POST("/mkdir", handles.FsMkdir)
fs.POST("/rename", handles.FsRename)
fs.POST("/move", handles.FsMove)
fs.POST("/copy", handles.FsCopy)
fs.POST("/remove", handles.FsRemove)
fs.POST("/put", handles.FsPut)
fs.POST("/link", middlewares.AuthAdmin, handles.Link)
fs.POST("/add_aria2", handles.AddAria2)
func fs(g *gin.RouterGroup) {
g.Any("/list", handles.FsList)
g.Any("/get", handles.FsGet)
g.Any("/dirs", handles.FsDirs)
g.POST("/mkdir", handles.FsMkdir)
g.POST("/rename", handles.FsRename)
g.POST("/move", handles.FsMove)
g.POST("/copy", handles.FsCopy)
g.POST("/remove", handles.FsRemove)
g.POST("/put", handles.FsPut)
g.POST("/link", middlewares.AuthAdmin, handles.Link)
g.POST("/add_aria2", handles.AddAria2)
}

func Cors(r *gin.Engine) {
Expand Down

0 comments on commit 4f3129e

Please sign in to comment.