// FindByEmail find user in Database by email
766018
func (repo *repo) FindByEmail(email string) (user service.Entity, err error) {
	if err = dbconn.DB.Where("email = ?", email).First(&user).Error; err != nil {
		return service.Entity{}, err
	}
	return user, nil
}
19223
func (s *UserService) FindUser(ctx context.Context, filter platform.UserFilter) (*platform.User, error) {
	users, n, err := s.FindUsers(ctx, filter)
	if err != nil {
		return nil, &platform.Error{
			Op:  s.OpPrefix + platform.OpFindUser,
			Err: err,
		}
	}

	if n == 0 {
		return nil, &platform.Error{
			Code: platform.ENotFound,
			Op:   s.OpPrefix + platform.OpFindUser,
			Msg:  "no results found",
		}
	}

	return users[0], nil
}
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
}
1034138
func (state *UserState) HasEmail(email string) (string, error) {
	if email == "" {
		return "", ErrNotFound
	}
	usernames, err := state.AllUsernames()
	if err != nil {
		return "", err
	}
	for _, username := range usernames {
		if user_email, err := state.Email(username); err != nil {
			return "", err
		} else {
			if user_email == email {
				return username, nil
			}
		}
	}
	return "", ErrNotFound
}
611483
func UserFilterByEmailPrivacy(privateEmails bool) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Where(fmt.Sprintf("email_private IS %v", privateEmails))
	}
}
1303770
func isEmail(v interface{}) bool {
	s, ok := v.(string)
	if !ok {
		return false
	}
	// entire email address to be no more than 254 characters long
	if len(s) > 254 {
		return false
	}

	// email address is generally recognized as having two parts joined with an at-sign
	at := strings.LastIndexByte(s, '@')
	if at == -1 {
		return false
	}
	local := s[0:at]
	domain := s[at+1:]

	// local part may be up to 64 characters long
	if len(local) > 64 {
		return false
	}

	// domain must match the requirements for a hostname
	if !isHostname(domain) {
		return false
	}

	_, err := mail.ParseAddress(s)
	return err == nil
}
288806
func (user *User) AddEmail(emailAddr string, isPrimary bool) error {
	emailAddrCanonical := strings.ToLower(strings.TrimSpace(emailAddr))
	if len(emailAddr) < 1 {
		return fmt.Errorf("Invalid Email Address: %v", emailAddr)
	}
	email := Item{
		Value:   emailAddrCanonical,
		Primary: isPrimary}
	user.Emails = append(user.Emails, email)
	return nil
}
411096
func (m *DummyMailbox) MessageByUID(uidno uint32) Message {
	for _, message := range m.messages {
		if message.UID() == uidno {
			return message
		}
	}

	// No message found
	return nil
}
656219
func (s *Service) FindUserByID(ctx context.Context, id platform.ID) (u *platform.User, err error) {
	var pe *platform.Error
	u, pe = s.loadUser(id)
	if pe != nil {
		err = &platform.Error{
			Op:  OpPrefix + platform.OpFindUserByID,
			Err: pe,
		}
	}
	return u, err
}
460749
func LoadUserEmails(m MetaContext) (emails []keybase1.Email, err error) {
	uid := m.G().GetMyUID()
	res, err := m.G().API.Get(m, APIArg{
		Endpoint:    "user/lookup",
		SessionType: APISessionTypeREQUIRED,
		Args: HTTPArgs{
			"uid": UIDArg(uid),
		},
	})
	if err != nil {
		return
	}

	emailPayloads, err := jsonhelpers.JSONGetChildren(res.Body.AtKey("them").AtKey("emails").AtKey("emails"))
	if err != nil {
		return nil, err
	}
	for _, emailPayload := range emailPayloads {
		email, err := emailPayload.AtKey("email").GetString()
		if err != nil {
			return nil, err
		}
		isPrimary, err := emailPayload.AtKey("is_primary").GetInt()
		if err != nil {
			return nil, err
		}
		isVerified, err := emailPayload.AtKey("is_verified").GetInt()
		if err != nil {
			return nil, err
		}
		visibilityCode, err := emailPayload.AtKey("visibility").GetInt()
		if err != nil {
			return nil, err
		}
		emails = append(emails, keybase1.Email{
			Email:      keybase1.EmailAddress(email),
			IsVerified: isVerified == 1,
			IsPrimary:  isPrimary == 1,
			Visibility: keybase1.IdentityVisibility(visibilityCode),
		})
	}

	return
}
305986
func (o *InvitationManager) FilterByUser(username string, status string) ([]JoinOrganizationInvitation, error) {
	orgRequests := []JoinOrganizationInvitation{}
	if status == "" {
		return o.GetByUser(username)
	}
	qry := bson.M{
		"user":   username,
		"status": status,
	}
	err := o.collection.Find(qry).All(&orgRequests)
	return orgRequests, err
}
342531
func IsEmail(field, email string) []ValidationMsg {
	if emailRegex.MatchString(email) {
		return []ValidationMsg{}
	}

	return []ValidationMsg{ValidationMsg{Field: field, Msg: NotEmailMsg}}
}
1232902
func (r *EmailResource) Find(address string) (*Email, error) {
	emails, err := r.List()
	if err != nil {
		return nil, err
	}

	for _, email := range emails {
		if email.Email == address {
			return email, nil
		}
	}

	return nil, ErrNotFound
}
56741
func (r *EmailResource) Find(account, address string) (*Email, error) {
	email := Email{}
	path := fmt.Sprintf("/users/%s/emails/%s", account, address)

	if err := r.client.do("GET", path, nil, nil, &email); err != nil {
		return nil, err
	}

	return &email, nil
}
1098327
func (s *DbSession) FindByID(id string, result Document) error {
	if !bson.IsObjectIdHex(id) {
		return errors.New("invalid id")
	}
	coll := s.collection(result.CollectionName())
	if err := coll.FindId(bson.ObjectIdHex(id)).One(result); err != nil {
		if err.Error() != mgo.ErrNotFound.Error() {
			log.Printf("Error fetching %s with id %s. Error: %s\n", result.CollectionName(), id, err)
		}
		return err
	}
	return nil
}
222290
func (m *Manager) SaveEmail(username string, email EmailAddress) error {
	if err := m.RemoveEmail(username, email.Label); err != nil {
		return err
	}
	return m.getUserCollection().Update(
		bson.M{"username": username},
		bson.M{"$push": bson.M{"emailaddresses": email}})
}
142133
func emailUsername(email string) string {
	at := strings.Index(email, "@")
	if at == -1 {
		return email
	} else if at == 0 {
		return email[1:]
	}
	return email[:at]
}