diff --git a/internal/app/api/user_key.go b/internal/app/api/user_key.go index 52ddfe1..1804efe 100644 --- a/internal/app/api/user_key.go +++ b/internal/app/api/user_key.go @@ -68,7 +68,7 @@ func (rs *UserKeyResource) create(c *gin.Context) { // @Failure 500 {object} httputil.JSONResponse // @Router /user/keys/{name} [get] func (rs *UserKeyResource) find(c *gin.Context) { - uk, err := rs.dUserKey.Find(c.Param("name")) + uk, err := rs.dUserKey.Find(authed.UidGet(c), c.Param("name")) if err != nil { ginutil.JSONBadRequest(c, err) return @@ -89,7 +89,7 @@ func (rs *UserKeyResource) find(c *gin.Context) { // @Failure 500 {object} httputil.JSONResponse // @Router /user/keys/{name}/secret [patch] func (rs *UserKeyResource) reset(c *gin.Context) { - uk, err := rs.dUserKey.Find(c.Param("name")) + uk, err := rs.dUserKey.Find(authed.UidGet(c), c.Param("name")) if err != nil { ginutil.JSONBadRequest(c, err) return @@ -115,7 +115,7 @@ func (rs *UserKeyResource) reset(c *gin.Context) { // @Failure 500 {object} httputil.JSONResponse // @Router /user/keys/{name} [delete] func (rs *UserKeyResource) remove(c *gin.Context) { - uk, err := rs.dUserKey.Find(c.Param("name")) + uk, err := rs.dUserKey.Find(authed.UidGet(c), c.Param("name")) if err != nil { ginutil.JSONBadRequest(c, err) return diff --git a/internal/app/dao/user_key.go b/internal/app/dao/user_key.go index e68ef00..6a1223b 100644 --- a/internal/app/dao/user_key.go +++ b/internal/app/dao/user_key.go @@ -17,9 +17,9 @@ func NewUserKey() *UserKey { return &UserKey{} } -func (u *UserKey) Find(name string) (*model.UserKey, error) { +func (u *UserKey) Find(uid int64, name string) (*model.UserKey, error) { uk := new(model.UserKey) - if err := gdb.Where("name=?", name).First(uk).Error; errors.Is(err, gorm.ErrRecordNotFound) { + if err := gdb.Where("uid=? and name=?", uid, name).First(uk).Error; errors.Is(err, gorm.ErrRecordNotFound) { return nil, fmt.Errorf("userKey not exist") } @@ -46,7 +46,7 @@ func (u *UserKey) FindAll(query *Query) (list []*model.UserKey, total int64, err } func (u *UserKey) Create(uk *model.UserKey) (*model.UserKey, error) { - if _, err := u.Find(uk.Name); err == nil { + if _, err := u.Find(uk.Uid, uk.Name); err == nil { return nil, fmt.Errorf("userKey already exist: %s", uk.Name) }