Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit 9ffcaa2

Browse files
author
ggmolly
committedJul 14, 2024·
fix: check if commander has the ship before updating the fleet
1 parent 237e233 commit 9ffcaa2

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed
 

‎orm/fleet.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
package orm
22

3-
import "github.com/lib/pq"
3+
import (
4+
"errors"
5+
6+
"github.com/lib/pq"
7+
)
8+
9+
var (
10+
ErrInvalidShipID = errors.New("invalid ship id")
11+
)
412

513
type Fleet struct {
614
ID uint32 `gorm:"primary_key"` // uniquely identifies the fleet
@@ -17,6 +25,10 @@ func CreateFleet(owner *Commander, id uint32, name string, ships []uint32) error
1725
fleet.CommanderID = owner.CommanderID
1826
fleet.GameID = id
1927
for _, shipID := range ships {
28+
// check if the commander has this ship
29+
if _, ok := owner.OwnedShipsMap[shipID]; !ok {
30+
return ErrInvalidShipID
31+
}
2032
fleet.ShipList = append(fleet.ShipList, int64(shipID))
2133
}
2234
if err := GormDB.Create(&fleet).Error; err != nil {
@@ -35,6 +47,10 @@ func (f *Fleet) RenameFleet(name string) error {
3547
func (f *Fleet) UpdateShipList(owner *Commander, ships []uint32) error {
3648
f.ShipList = make([]int64, len(ships))
3749
for i, shipID := range ships {
50+
// check if the commander has this ship
51+
if _, ok := owner.OwnedShipsMap[shipID]; !ok {
52+
return ErrInvalidShipID
53+
}
3854
f.ShipList[i] = int64(shipID)
3955
}
4056
if err := GormDB.Save(f).Error; err != nil {

0 commit comments

Comments
 (0)
This repository has been archived.