Fix another locking issue

This commit is contained in:
Daniel 2020-10-13 14:58:27 +02:00
parent 6a58ce5a7a
commit 34e12860e4

View file

@ -92,11 +92,28 @@ func (o *Options) Apply(r record.Record) {
} }
} }
// HasAllPermissions returns whether the options specify the highest possible permissions for operations. // HasAllPermissions returns whether the options specify the highest possible
// permissions for operations.
func (o *Options) HasAllPermissions() bool { func (o *Options) HasAllPermissions() bool {
return o.Local && o.Internal return o.Local && o.Internal
} }
// hasAccessPermission checks if the interface options permit access to the
// given record, locking the record for accessing it's attributes.
func (o *Options) hasAccessPermission(r record.Record) bool {
// Check if the options specify all permissions, which makes checking the
// record unnecessary.
if o.HasAllPermissions() {
return true
}
r.Lock()
defer r.Unlock()
// Check permissions against record.
return r.Meta().CheckPermission(o.Local, o.Internal)
}
// NewInterface returns a new Interface to the database. // NewInterface returns a new Interface to the database.
func NewInterface(opts *Options) *Interface { func NewInterface(opts *Options) *Interface {
if opts == nil { if opts == nil {
@ -156,13 +173,8 @@ func (i *Interface) getRecord(dbName string, dbKey string, mustBeWriteable bool)
r = i.checkCache(dbName + ":" + dbKey) r = i.checkCache(dbName + ":" + dbKey)
if r != nil { if r != nil {
if !i.options.HasAllPermissions() { if !i.options.hasAccessPermission(r) {
r.Lock() return nil, db, ErrPermissionDenied
permitted := r.Meta().CheckPermission(i.options.Local, i.options.Internal)
r.Unlock()
if !permitted {
return nil, db, ErrPermissionDenied
}
} }
return r, db, nil return r, db, nil
} }
@ -172,7 +184,7 @@ func (i *Interface) getRecord(dbName string, dbKey string, mustBeWriteable bool)
return nil, db, err return nil, db, err
} }
if !r.Meta().CheckPermission(i.options.Local, i.options.Internal) { if !i.options.hasAccessPermission(r) {
return nil, db, ErrPermissionDenied return nil, db, ErrPermissionDenied
} }