-
-
Notifications
You must be signed in to change notification settings - Fork 522
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #840 from 0xJacky/fix/pr-831
fix/pr-831
- Loading branch information
Showing
20 changed files
with
1,601 additions
and
1,872 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,132 +1,53 @@ | ||
package user | ||
|
||
import ( | ||
"github.com/0xJacky/Nginx-UI/api" | ||
"github.com/0xJacky/Nginx-UI/internal/user" | ||
"github.com/0xJacky/Nginx-UI/model" | ||
"github.com/0xJacky/Nginx-UI/query" | ||
"github.com/0xJacky/Nginx-UI/settings" | ||
"github.com/gin-gonic/gin" | ||
"github.com/spf13/cast" | ||
"github.com/uozi-tech/cosy" | ||
"golang.org/x/crypto/bcrypt" | ||
"net/http" | ||
) | ||
|
||
func GetUsers(c *gin.Context) { | ||
cosy.Core[model.User](c).SetFussy("name").PagingList() | ||
} | ||
|
||
func GetUser(c *gin.Context) { | ||
id := cast.ToUint64(c.Param("id")) | ||
|
||
u := query.User | ||
|
||
user, err := u.FirstByID(id) | ||
|
||
if err != nil { | ||
api.ErrHandler(c, err) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, user) | ||
} | ||
|
||
type UserJson struct { | ||
Name string `json:"name" binding:"required,max=255"` | ||
Password string `json:"password" binding:"max=255"` | ||
} | ||
|
||
func AddUser(c *gin.Context) { | ||
var json UserJson | ||
ok := cosy.BindAndValid(c, &json) | ||
if !ok { | ||
return | ||
} | ||
|
||
u := query.User | ||
|
||
pwd, err := bcrypt.GenerateFromPassword([]byte(json.Password), bcrypt.DefaultCost) | ||
if err != nil { | ||
api.ErrHandler(c, err) | ||
return | ||
} | ||
json.Password = string(pwd) | ||
|
||
user := model.User{ | ||
Name: json.Name, | ||
Password: json.Password, | ||
} | ||
|
||
err = u.Create(&user) | ||
|
||
if err != nil { | ||
api.ErrHandler(c, err) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, user) | ||
|
||
} | ||
|
||
func EditUser(c *gin.Context) { | ||
userId := cast.ToUint64(c.Param("id")) | ||
|
||
if settings.NodeSettings.Demo && userId == 1 { | ||
c.JSON(http.StatusNotAcceptable, gin.H{ | ||
"message": "Changing user password is forbidden in demo mode", | ||
}) | ||
return | ||
} | ||
|
||
var json UserJson | ||
ok := cosy.BindAndValid(c, &json) | ||
if !ok { | ||
return | ||
} | ||
|
||
u := query.User | ||
user, err := u.FirstByID(userId) | ||
|
||
if err != nil { | ||
api.ErrHandler(c, err) | ||
func encryptPassword(ctx *cosy.Ctx[model.User]) { | ||
if ctx.Payload["password"] == nil { | ||
return | ||
} | ||
edit := &model.User{ | ||
Name: json.Name, | ||
} | ||
|
||
// encrypt password | ||
if json.Password != "" { | ||
var pwd []byte | ||
pwd, err = bcrypt.GenerateFromPassword([]byte(json.Password), bcrypt.DefaultCost) | ||
pwd := ctx.Payload["password"].(string) | ||
if pwd != "" { | ||
pwdBytes, err := bcrypt.GenerateFromPassword([]byte(pwd), bcrypt.DefaultCost) | ||
if err != nil { | ||
api.ErrHandler(c, err) | ||
ctx.AbortWithError(err) | ||
return | ||
} | ||
edit.Password = string(pwd) | ||
ctx.Model.Password = string(pwdBytes) | ||
} else { | ||
delete(ctx.Payload, "password") | ||
} | ||
} | ||
|
||
_, err = u.Where(u.ID.Eq(userId)).Updates(&edit) | ||
|
||
if err != nil { | ||
api.ErrHandler(c, err) | ||
return | ||
} | ||
func InitManageUserRouter(g *gin.RouterGroup) { | ||
c := cosy.Api[model.User]("users") | ||
|
||
c.JSON(http.StatusOK, user) | ||
} | ||
c.CreateHook(func(c *cosy.Ctx[model.User]) { | ||
c.BeforeDecodeHook(encryptPassword) | ||
}) | ||
|
||
func DeleteUser(c *gin.Context) { | ||
id := cast.ToInt(c.Param("id")) | ||
if cast.ToInt(id) == 1 { | ||
c.JSON(http.StatusNotAcceptable, gin.H{ | ||
"message": "Prohibit deleting the default user", | ||
c.ModifyHook(func(c *cosy.Ctx[model.User]) { | ||
c.BeforeDecodeHook(func(ctx *cosy.Ctx[model.User]) { | ||
if ctx.ID == 1 { | ||
ctx.AbortWithError(user.ErrChangeInitUserPwdInDemo) | ||
} | ||
}) | ||
return | ||
} | ||
cosy.Core[model.User](c).Destroy() | ||
} | ||
c.BeforeDecodeHook(encryptPassword) | ||
}) | ||
|
||
c.DestroyHook(func(c *cosy.Ctx[model.User]) { | ||
c.BeforeExecuteHook(func(ctx *cosy.Ctx[model.User]) { | ||
if ctx.ID == 1 { | ||
ctx.AbortWithError(user.ErrCannotRemoveInitUser) | ||
} | ||
}) | ||
}) | ||
|
||
func RecoverUser(c *gin.Context) { | ||
cosy.Core[model.User](c).Recover() | ||
c.InitRouter(g) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.