GetByEmail returns the user (not organization) with given email. It ignores records with unverified emails and returns ErrUserNotExist when not found.
(ctx context.Context, email string)
| 691 | // GetByEmail returns the user (not organization) with given email. It ignores |
| 692 | // records with unverified emails and returns ErrUserNotExist when not found. |
| 693 | func (s *UsersStore) GetByEmail(ctx context.Context, email string) (*User, error) { |
| 694 | if email == "" { |
| 695 | return nil, ErrUserNotExist{args: errutil.Args{"email": email}} |
| 696 | } |
| 697 | email = strings.ToLower(email) |
| 698 | |
| 699 | /* |
| 700 | Equivalent SQL for PostgreSQL: |
| 701 | |
| 702 | SELECT * FROM "user" |
| 703 | LEFT JOIN email_address ON email_address.uid = "user".id |
| 704 | WHERE |
| 705 | "user".type = @userType |
| 706 | AND ( |
| 707 | "user".email = @email AND "user".is_active = TRUE |
| 708 | OR email_address.email = @email AND email_address.is_activated = TRUE |
| 709 | ) |
| 710 | */ |
| 711 | user := new(User) |
| 712 | err := s.db.WithContext(ctx). |
| 713 | Joins(dbutil.Quote("LEFT JOIN email_address ON email_address.uid = %s.id", "user"), true). |
| 714 | Where(dbutil.Quote("%s.type = ?", "user"), UserTypeIndividual). |
| 715 | Where(s.db. |
| 716 | Where(dbutil.Quote("%[1]s.email = ? AND %[1]s.is_active = ?", "user"), email, true). |
| 717 | Or("email_address.email = ? AND email_address.is_activated = ?", email, true), |
| 718 | ). |
| 719 | First(&user). |
| 720 | Error |
| 721 | if err != nil { |
| 722 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 723 | return nil, ErrUserNotExist{args: errutil.Args{"email": email}} |
| 724 | } |
| 725 | return nil, err |
| 726 | } |
| 727 | return user, nil |
| 728 | } |
| 729 | |
| 730 | // GetByID returns the user with given ID. It returns ErrUserNotExist when not |
| 731 | // found. |