Skip to content

Commit

Permalink
fix: error permission for the admin api
Browse files Browse the repository at this point in the history
  • Loading branch information
saltbo committed Feb 20, 2021
1 parent cc59d4c commit 66034d6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
14 changes: 11 additions & 3 deletions internal/app/dao/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ func (s *Storage) Find(id interface{}) (*model.Storage, error) {
return storage, nil
}

func (s *Storage) FindAll(offset, limit int) (list []model.Storage, total int64, err error) {
func (s *Storage) FindAll(offset, limit int) (storages []model.Storage, total int64, err error) {
gdb.Model(model.Storage{}).Count(&total)
err = gdb.Find(&list).Offset(offset).Limit(limit).Error
err = gdb.Find(&storages).Offset(offset).Limit(limit).Error
for idx, storage := range storages {
storages[idx].SecretKey = storage.SKAsterisk() // 对外隐藏SK
}
return
}

Expand All @@ -41,11 +44,16 @@ func (s *Storage) Create(storage *model.Storage) error {
}

func (s *Storage) Update(id string, storage *model.Storage) error {
if err := gdb.First(&model.Storage{}, id).Error; errors.Is(err, gorm.ErrRecordNotFound) {
existStorage := new(model.Storage)
if err := gdb.First(existStorage, id).Error; errors.Is(err, gorm.ErrRecordNotFound) {
return fmt.Errorf("storage not found")
}

storage.Id, _ = strconv.ParseInt(id, 10, 64)
// 如果SK没有发生改变则不允许更新SK,避免改错SK
if storage.SecretKey == existStorage.SKAsterisk() {
storage.SecretKey = existStorage.SecretKey
}
return gdb.Save(storage).Error
}

Expand Down
7 changes: 7 additions & 0 deletions internal/app/model/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,10 @@ func (Storage) TableName() string {
func (s *Storage) PublicRead() bool {
return s.Mode == StorageModeFileDisk
}

func (s *Storage) SKAsterisk() (sk string) {
for range s.SecretKey {
sk += "*"
}
return
}
18 changes: 16 additions & 2 deletions internal/pkg/middleware/auth_rbac.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,34 @@
- id: 102
host: "*"
path: "/api/storages/**"
method: "{PUT,DELETE}"
method: "{PUT,PATCH,DELETE}"
authorized_roles:
- "admin"

- id: 103
host: "*"
path: "/api/users"
method: "GET"
authorized_roles:
- "admin"

- id: 104
host: "*"
path: "/api/users/**"
method: "{PUT,DELETE}"
authorized_roles:
- "admin"

- id: 104
- id: 105
host: "*"
path: "/api/system/options/*"
method: "PUT"
authorized_roles:
- "admin"

- id: 106
host: "*"
path: "/api/system/options/core.email"
method: "GET"
authorized_roles:
- "admin"

0 comments on commit 66034d6

Please sign in to comment.