// FindUser retrives a example using an arbitrary example filter.
// Filters using ID, or Name should be efficient.
// Other filters will do a linear scan across examples until it finds a match.
773700
func (c *ExampleService) FindUser(ctx context.Context, filter platform.UserFilter) (*platform.User, error) {
	if filter.ID != nil {
		u, err := c.FindUserByID(ctx, *filter.ID)
		if err != nil {
			return nil, &platform.Error{
				Op:  "kv/" + platform.OpFindUser,
				Err: err,
			}
		}
		return u, nil
	}

	if filter.Name != nil {
		return c.FindUserByName(ctx, *filter.Name)
	}

	filterFn := filterExamplesFn(filter)

	var u *platform.User
	err := c.kv.View(func(tx Tx) error {
		return forEachExample(ctx, tx, func(usr *platform.User) bool {
			if filterFn(usr) {
				u = usr
				return false
			}
			return true
		})
	})

	if err != nil {
		return nil, err
	}

	if u == nil {
		return nil, &platform.Error{
			Code: platform.ENotFound,
			Msg:  "user not found",
		}
	}

	return u, nil
}
710500
func (c *Client) GetUser(userID string) (*User, error) {
	uri := fmt.Sprintf("/1/users/%s", userID)
	resp, err := c.doSimple("GET", uri)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}
	c.trace("GetUser", uri, string(body))
	var user *User
	// TODO(tmc): warn if not == .Zero() before populating?
	return user, json.Unmarshal(body, &user)
}
784463
func (sl *Slack) FindUserByName(name string) (*User, error) {
	return sl.FindUser(func(user *User) bool {
		return user.Name == name
	})
}
859165
func (c *ExampleService) FindUsers(ctx context.Context, filter platform.UserFilter, opt ...platform.FindOptions) ([]*platform.User, int, error) {
	op := platform.OpFindUsers
	if filter.ID != nil {
		u, err := c.FindUserByID(ctx, *filter.ID)
		if err != nil {
			return nil, 0, &platform.Error{
				Err: err,
				Op:  "kv/" + op,
			}
		}

		return []*platform.User{u}, 1, nil
	}

	if filter.Name != nil {
		u, err := c.FindUserByName(ctx, *filter.Name)
		if err != nil {
			return nil, 0, &platform.Error{
				Err: err,
				Op:  "kv/" + op,
			}
		}

		return []*platform.User{u}, 1, nil
	}

	us := []*platform.User{}
	filterFn := filterExamplesFn(filter)
	err := c.kv.View(func(tx Tx) error {
		return forEachExample(ctx, tx, func(u *platform.User) bool {
			if filterFn(u) {
				us = append(us, u)
			}
			return true
		})
	})

	if err != nil {
		return nil, 0, err
	}

	return us, len(us), nil
}
656319
func (c *UsersController) GetBy(id int64) (user datamodels.User, found bool) {
	u, found := c.Service.GetByID(id)
	if !found {
		// this message will be binded to the
		// main.go -> app.OnAnyErrorCode -> NotFound -> shared/error.html -> .Message text.
		c.Ctx.Values().Set("message", "User couldn't be found!")
	}
	return u, found // it will throw/emit 404 if found == false.
}
434197
func (u UserResource) findAllUsers(request *restful.Request, response *restful.Response) {
	list := []User{}
	for _, each := range u.users {
		list = append(list, each)
	}
	response.WriteEntity(list)
}
877948
func (c *ExampleService) FindUserByID(ctx context.Context, id platform.ID) (*platform.User, error) {
	var u *platform.User

	err := c.kv.View(func(tx Tx) error {
		usr, err := c.findUserByID(ctx, tx, id)
		if err != nil {
			return err
		}
		u = usr
		return nil
	})

	if err != nil {
		return nil, &platform.Error{
			Op:  "kv/" + platform.OpFindUserByID,
			Err: err,
		}
	}

	return u, nil
}
252540
func (c *ExampleService) FindUserByName(ctx context.Context, n string) (*platform.User, error) {
	var u *platform.User

	err := c.kv.View(func(tx Tx) error {
		usr, err := c.findUserByName(ctx, tx, n)
		if err != nil {
			return err
		}
		u = usr
		return nil
	})

	return u, err
}
465251
func (s *Service) findTasksByUser(ctx context.Context, tx Tx, filter influxdb.TaskFilter) ([]*influxdb.Task, int, error) {
	if filter.User == nil {
		return nil, 0, ErrTaskNotFound
	}
	var org *influxdb.Organization
	var err error
	if filter.OrganizationID != nil {
		org, err = s.findOrganizationByID(ctx, tx, *filter.OrganizationID)
		if err != nil {
			return nil, 0, err
		}
	} else if filter.Organization != "" {
		org, err = s.findOrganizationByName(ctx, tx, filter.Organization)
		if err != nil {
			return nil, 0, err
		}
	}

	var ts []*influxdb.Task

	maps, err := s.findUserResourceMappings(
		ctx,
		tx,
		influxdb.UserResourceMappingFilter{
			ResourceType: influxdb.TasksResourceType,
			UserID:       *filter.User,
			UserType:     influxdb.Owner,
		},
	)
	if err != nil {
		return nil, 0, err
	}

	for _, m := range maps {
		task, err := s.findTaskByID(ctx, tx, m.ResourceID)
		if err != nil {
			return nil, 0, err
		}

		if org != nil && task.OrganizationID != org.ID {
			continue
		}

		ts = append(ts, task)

		if len(ts) >= filter.Limit {
			break
		}
	}
	return ts, len(ts), nil
}
1274820
func QueryNotFound(t *testing.T, repo grimoire.Repo) {
	t.Run("NotFound", func(t *testing.T) {
		user := User{}

		// find user error not found
		err := repo.From("users").Find(0).One(&user)
		assert.NotNil(t, err)
		assert.Equal(t, errors.NotFound, err.(errors.Error).Kind())
	})
}
104881
func (s *Service) FindUser(ctx context.Context, filter platform.UserFilter) (*platform.User, error) {
	op := OpPrefix + platform.OpFindUser
	if filter.ID != nil {
		u, err := s.FindUserByID(ctx, *filter.ID)
		if err != nil {
			return nil, &platform.Error{
				Op:  op,
				Err: err,
			}
		}
		return u, nil
	}

	if filter.Name != nil {
		var u *platform.User

		err := s.forEachUser(ctx, func(user *platform.User) bool {
			if user.Name == *filter.Name {
				u = user
				return false
			}
			return true
		})

		if err != nil {
			return nil, err
		}

		if u == nil {
			return nil, &platform.Error{
				Code: platform.ENotFound,
				Op:   op,
				Msg:  "user not found",
			}
		}

		return u, nil
	}

	return nil, &platform.Error{
		Code: platform.EInvalid,
		Op:   op,
		Msg:  "expected filter to contain name",
	}
}
662130
func GetUser(db *sqlx.DB, req *Request) (*User, error) {
	u := &User{}
	u.FlexID = req.FlexID
	u.FlexIDType = req.FlexIDType
	if req.UserID == 0 {
		if req.FlexID == "" {
			return nil, ErrMissingFlexID
		}
		switch req.FlexIDType {
		case FIDTEmail, FIDTPhone, FIDTSession:
			// Do nothing
		default:
			return nil, ErrInvalidFlexIDType
		}
		log.Debug("searching for user from", req.FlexID, req.FlexIDType)
		q := `SELECT userid
		      FROM userflexids
		      WHERE flexid=$1 AND flexidtype=$2
		      ORDER BY createdat DESC`
		err := db.Get(&req.UserID, q, req.FlexID, req.FlexIDType)
		if err == sql.ErrNoRows {
			return u, nil
		}
		log.Debug("got uid", req.UserID)
		if err != nil {
			return nil, err
		}
	}
	q := `SELECT id, name, email FROM users WHERE id=$1`
	if err := db.Get(u, q, req.UserID); err != nil {
		if err == sql.ErrNoRows {
			return u, nil
		}
		return nil, err
	}
	return u, nil
}
971186
func (auth *Auth) GetUser(ctx context.Context, uid string) (*UserRecord, error) {
	users, err := auth.GetUsers(ctx, []string{uid})
	if err != nil {
		return nil, err
	}
	if len(users) == 0 {
		return nil, ErrUserNotFound
	}
	return users[0], nil
}
454927
func (s *UserService) FindUserByID(ctx context.Context, id platform.ID) (*platform.User, error) {
	return s.FindUserByIDFn(ctx, id)
}
20416
func parseUserFromImageUser(id string) string {
	if id == "" {
		return id
	}
	// split instances where the id may contain user:group
	if strings.Contains(id, ":") {
		return strings.Split(id, ":")[0]
	}
	// no group, just return the id
	return id
}
424636
func (a *AuthServer) GetUser(name string) (user services.User, err error) {
	return a.GetCache().GetUser(name)
}
947288
func (r *KeyResource) Find(id int) (*Key, error) {
	key := Key{}
	path := fmt.Sprintf("/user/keys/%v", id)
	if err := r.client.do("GET", path, nil, &key); err != nil {
		return nil, err
	}

	return &key, nil
}