Require access to Still via method

This commit adds a safeguard to accessing the still from a specific
component by requiring access via the component.GetStill method.
This commit is contained in:
Tom Wiesing 2024-04-08 22:39:32 +02:00
parent 81fa84c244
commit 8235ea9105
No known key found for this signature in database
63 changed files with 288 additions and 197 deletions

View file

@ -26,26 +26,30 @@ var Locked = exit.Error{
// TryLock attemps to lock this WissKI and returns if it suceeded
func (lock *Locker) TryLock(ctx context.Context) bool {
table, err := lock.Malt.SQL.QueryTable(ctx, lock.Malt.LockTable)
liquid := ingredient.GetLiquid(lock)
table, err := liquid.SQL.QueryTable(ctx, liquid.LockTable)
if err != nil {
return false
}
result := table.FirstOrCreate(&models.Lock{}, models.Lock{Slug: lock.Slug})
result := table.FirstOrCreate(&models.Lock{}, models.Lock{Slug: liquid.Slug})
return result.Error == nil && result.RowsAffected == 1
}
// TryUnlock attempts to unlock this WissKI and reports if it succeeded.
// An Unlock is also attempted when ctx is cancelled.
func (lock *Locker) TryUnlock(ctx context.Context) bool {
liquid := ingredient.GetLiquid(lock)
ctx, close := contextx.Anyways(ctx, time.Second)
defer close()
table, err := lock.Malt.SQL.QueryTable(ctx, lock.Malt.LockTable)
table, err := liquid.SQL.QueryTable(ctx, liquid.LockTable)
if err != nil {
return false
}
result := table.Where("slug = ?", lock.Slug).Delete(&models.Lock{})
result := table.Where("slug = ?", liquid.Slug).Delete(&models.Lock{})
return result.Error == nil && result.RowsAffected == 1
}

View file

@ -10,13 +10,15 @@ import (
// Locked checks if this WissKI is currently locked.
// If an error occurs, the instance is considered not locked.
func (lock *Locker) Locked(ctx context.Context) (locked bool) {
table, err := lock.Malt.SQL.QueryTable(ctx, lock.Malt.LockTable)
liquid := ingredient.GetLiquid(lock)
table, err := liquid.SQL.QueryTable(ctx, liquid.LockTable)
if err != nil {
return false
}
// check if this instance is locked
table.Select("count(*) > 0").Where("slug = ?", lock.Slug).Find(&locked)
table.Select("count(*) > 0").Where("slug = ?", liquid.Slug).Find(&locked)
return
}