github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/apiv3/servicecontext/user.go (about)

     1  package servicecontext
     2  
     3  import (
     4  	"github.com/evergreen-ci/evergreen/auth"
     5  	"github.com/evergreen-ci/evergreen/model/user"
     6  )
     7  
     8  // DBUserConnector is a struct that implements the User related interface
     9  // of the ServiceContext interface through interactions with the backing database.
    10  type DBUserConnector struct{}
    11  
    12  // FindUserById uses the service layer's user type to query the backing database for
    13  // the user with the given Id.
    14  func (tc *DBUserConnector) FindUserById(userId string) (auth.APIUser, error) {
    15  	t, err := user.FindOne(user.ById(userId))
    16  	if err != nil {
    17  		return nil, err
    18  	}
    19  	return t, nil
    20  }
    21  
    22  // MockUserConnector stores a cached set of users that are queried against by the
    23  // implementations of the UserConnector interface's functions.
    24  type MockUserConnector struct {
    25  	CachedUsers map[string]*user.DBUser
    26  }
    27  
    28  // FindUserById provides a mock implementation of the User functions
    29  // from the ServiceContext that does not need to use a database.
    30  // It returns results based on the cached users in the MockUserConnector.
    31  func (muc *MockUserConnector) FindUserById(userId string) (auth.APIUser, error) {
    32  	u := muc.CachedUsers[userId]
    33  	return u, nil
    34  }