Skip to content

Commit

Permalink
feat(shortUrl): 删除数据时验证用户, 非管理员仅可以删除自己创建的数据
Browse files Browse the repository at this point in the history
  • Loading branch information
vaebe committed May 17, 2024
1 parent 705913e commit 39200e1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
3 changes: 2 additions & 1 deletion internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func mainFunc(ctx context.Context, parser *gcmd.Parser) (err error) {
login.NewV1().RefreshToken,
shortUrlCode.NewV1(),
common.NewV1().UploadFile,
shortUrl.NewV1(),
)
})

Expand All @@ -79,7 +80,7 @@ func mainFunc(ctx context.Context, parser *gcmd.Parser) (err error) {
group.Middleware(middlewares.UserIsAdmin)

group.Bind(
shortUrl.NewV1(),

user.NewV1(),
)
})
Expand Down
12 changes: 11 additions & 1 deletion internal/controller/shortUrl/shortUrl_v1_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ import (
)

func (c *ControllerV1) Delete(ctx context.Context, req *v1.DeleteReq) (res *v1.DeleteRes, err error) {
err = service.ShortUrl().Delete(ctx, req.Id)
loginUserInfo, err := service.Auth().GetLoginUserInfo(ctx)
if err != nil {
return nil, err
}

userId := ""
if loginUserInfo.Role == "01" {
userId = loginUserInfo.Id
}

err = service.ShortUrl().Delete(ctx, req.Id, userId)
return
}
11 changes: 9 additions & 2 deletions internal/logic/shortUrl/shortUrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,15 @@ func (s *sShortUrl) GetList(ctx context.Context, in v1.GetListReq, userId string
}

// Delete 删除短链
func (s *sShortUrl) Delete(ctx context.Context, id string) error {
res, err := dao.ShortUrl.Ctx(ctx).Where(dao.ShortUrl.Columns().Id, id).Delete()
func (s *sShortUrl) Delete(ctx context.Context, id string, userId string) error {
db := dao.ShortUrl.Ctx(ctx).Where(dao.ShortUrl.Columns().Id, id)

// 用户 id 存在只查询当前用户的数据
if userId != "" {
db = db.Where(dao.ShortUrl.Columns().UserId, userId)
}

res, err := db.Delete()

if num, _ := res.RowsAffected(); num == 0 {
return gerror.New("需要删除的数据不存在!")
Expand Down
2 changes: 1 addition & 1 deletion internal/service/short_url.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 39200e1

Please sign in to comment.