mirror of
https://github.com/anyproto/any-sync.git
synced 2025-06-08 05:57:03 +09:00
aclService: hasRecord method
This commit is contained in:
parent
ccbb8caecd
commit
b731b57ed9
3 changed files with 62 additions and 0 deletions
18
acl/acl.go
18
acl/acl.go
|
@ -40,6 +40,7 @@ type AclService interface {
|
|||
Permissions(ctx context.Context, identity crypto.PubKey, spaceId string) (res list.AclPermissions, err error)
|
||||
OwnerPubKey(ctx context.Context, spaceId string) (ownerIdentity crypto.PubKey, err error)
|
||||
ReadState(ctx context.Context, spaceId string, f func(s *list.AclState) error) (err error)
|
||||
HasRecord(ctx context.Context, spaceId, recordId string) (has bool, err error)
|
||||
app.ComponentRunnable
|
||||
}
|
||||
|
||||
|
@ -170,6 +171,23 @@ func (as *aclService) ReadState(ctx context.Context, spaceId string, f func(s *l
|
|||
return f(acl.AclState())
|
||||
}
|
||||
|
||||
func (as *aclService) HasRecord(ctx context.Context, spaceId, recordId string) (has bool, err error) {
|
||||
acl, err := as.get(ctx, spaceId)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
acl.RLock()
|
||||
defer acl.RUnlock()
|
||||
acl.Iterate(func(record *list.AclRecord) (isContinue bool) {
|
||||
if record.Id == recordId {
|
||||
has = true
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (as *aclService) Run(ctx context.Context) (err error) {
|
||||
return
|
||||
}
|
||||
|
|
|
@ -175,6 +175,35 @@ func TestAclService_ReadState(t *testing.T) {
|
|||
}))
|
||||
}
|
||||
|
||||
func TestAclService_HasRecord(t *testing.T) {
|
||||
ownerKeys, err := accountdata.NewRandom()
|
||||
require.NoError(t, err)
|
||||
spaceId := "spaceId"
|
||||
ownerAcl, err := list.NewTestDerivedAcl(spaceId, ownerKeys)
|
||||
require.NoError(t, err)
|
||||
|
||||
fx := newFixture(t)
|
||||
defer fx.finish(t)
|
||||
|
||||
fx.consCl.EXPECT().Watch(spaceId, gomock.Any()).DoAndReturn(func(spaceId string, w consensusclient.Watcher) error {
|
||||
go func() {
|
||||
w.AddConsensusRecords([]*consensusproto.RawRecordWithId{
|
||||
ownerAcl.Root(),
|
||||
})
|
||||
}()
|
||||
return nil
|
||||
})
|
||||
fx.consCl.EXPECT().UnWatch(spaceId)
|
||||
|
||||
has, err := fx.HasRecord(ctx, spaceId, ownerAcl.Root().Id)
|
||||
require.NoError(t, err)
|
||||
assert.True(t, has)
|
||||
|
||||
has, err = fx.HasRecord(ctx, spaceId, "non-exists")
|
||||
require.NoError(t, err)
|
||||
assert.False(t, has)
|
||||
}
|
||||
|
||||
func newFixture(t *testing.T) *fixture {
|
||||
ctrl := gomock.NewController(t)
|
||||
fx := &fixture{
|
||||
|
|
|
@ -73,6 +73,21 @@ func (mr *MockAclServiceMockRecorder) Close(arg0 any) *gomock.Call {
|
|||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Close", reflect.TypeOf((*MockAclService)(nil).Close), arg0)
|
||||
}
|
||||
|
||||
// HasRecord mocks base method.
|
||||
func (m *MockAclService) HasRecord(arg0 context.Context, arg1, arg2 string) (bool, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "HasRecord", arg0, arg1, arg2)
|
||||
ret0, _ := ret[0].(bool)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// HasRecord indicates an expected call of HasRecord.
|
||||
func (mr *MockAclServiceMockRecorder) HasRecord(arg0, arg1, arg2 any) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "HasRecord", reflect.TypeOf((*MockAclService)(nil).HasRecord), arg0, arg1, arg2)
|
||||
}
|
||||
|
||||
// Init mocks base method.
|
||||
func (m *MockAclService) Init(arg0 *app.App) error {
|
||||
m.ctrl.T.Helper()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue