示例#1
0
文件: curator.go 项目: varver/leaps
/*
CreateDocument - Creates a fresh Binder for a new document, which is subsequently stored, returns an
error if either the document ID is already currently in use, or if there is a problem storing the
new document. May require authentication, if so a userID is supplied.
*/
func (c *Curator) CreateDocument(token string, userID string, doc store.Document) (BinderPortal, error) {
	c.log.Debugf("Creating new document with token %v\n", token)

	if !c.authenticator.AuthoriseCreate(token, userID) {
		c.stats.Incr("curator.create.rejected_client", 1)
		return BinderPortal{}, fmt.Errorf("failed to gain permission to create with token: %v\n", token)
	}
	c.stats.Incr("curator.create.accepted_client", 1)

	// Always generate a fresh ID
	doc.ID = util.GenerateStampedUUID()

	if err := c.store.Create(doc.ID, doc); err != nil {
		c.stats.Incr("curator.create_new.failed", 1)
		c.log.Errorf("Failed to create new document: %v\n", err)
		return BinderPortal{}, err
	}
	binder, err := NewBinder(doc.ID, c.store, c.config.BinderConfig, c.errorChan, c.log, c.stats)
	if err != nil {
		c.stats.Incr("curator.bind_new.failed", 1)
		c.log.Errorf("Failed to bind to new document: %v\n", err)
		return BinderPortal{}, err
	}
	c.binderMutex.Lock()
	c.openBinders[doc.ID] = binder
	c.binderMutex.Unlock()
	c.stats.Incr("curator.open_binders", 1)

	return binder.Subscribe(token), nil
}
示例#2
0
文件: curator.go 项目: Jeffail/leaps
/*
CreateDocument - Creates a fresh Binder for a new document, which is subsequently stored, returns an
error if either the document ID is already currently in use, or if there is a problem storing the
new document. May require authentication, if so a userID is supplied.
*/
func (c *impl) CreateDocument(
	userID, token string, doc store.Document, timeout time.Duration,
) (binder.Portal, error) {
	c.log.Debugf("Creating new document with userID %v token %v\n", userID, token)

	if c.auth.Authenticate(userID, token, "") < acl.CreateAccess {
		c.stats.Incr("curator.create.rejected_client", 1)
		return nil, fmt.Errorf("failed to gain permission to create with token: %v\n", token)
	}
	c.stats.Incr("curator.create.accepted_client", 1)

	// Always generate a fresh ID
	doc.ID = util.GenerateStampedUUID()

	if err := c.store.Create(doc); err != nil {
		c.stats.Incr("curator.create_new.failed", 1)
		c.log.Errorf("Failed to create new document: %v\n", err)
		return nil, err
	}
	openBinder, err := binder.New(
		doc.ID, c.store, c.config.BinderConfig, c.errorChan, c.log, c.stats,
	)
	if err != nil {
		c.stats.Incr("curator.bind_new.failed", 1)
		c.log.Errorf("Failed to bind to new document: %v\n", err)
		return nil, err
	}
	c.binderMutex.Lock()
	c.openBinders[doc.ID] = openBinder
	c.binderMutex.Unlock()
	c.stats.Incr("curator.open_binders", 1)

	return openBinder.Subscribe(userID, timeout)
}
示例#3
0
func TestGetUsers(t *testing.T) {
	log, stats := loggerAndStats()
	auth, storage := authAndStore(log, stats)

	testCases := map[string][]string{
		"doc1": []string{
			"user1", "user2", "user3",
		},
		"doc2": []string{
			"user4", "user5", "user6",
		},
		"doc3": []string{
			"user1", "user4", "user7",
		},
	}

	for docID := range testCases {
		doc := store.Document{}
		doc.ID = docID
		doc.Content = "hello world"
		if err := storage.Create(doc); err != nil {
			t.Error(err)
		}
	}

	curator, err := New(NewConfig(), log, stats, auth, storage)
	if err != nil {
		t.Error(err)
		return
	}
	defer curator.Close()

	for docID, users := range testCases {
		for _, userID := range users {
			if _, err := curator.EditDocument(userID, "", docID, time.Second); err != nil {
				t.Error(err)
			}
		}
	}

	getUsers, err := curator.GetUsers(time.Second)
	if err != nil {
		t.Error(err)
		return
	}

	if !reflect.DeepEqual(getUsers, testCases) {
		t.Errorf("GetUsers not matched, %v != %v", getUsers, testCases)
	}
}