user_mail.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package db
  5. import (
  6. "fmt"
  7. "strings"
  8. "gogs.io/gogs/internal/db/errors"
  9. "gogs.io/gogs/internal/errutil"
  10. )
  11. // EmailAddresses is the list of all email addresses of a user. Can contain the
  12. // primary email address, but is not obligatory.
  13. type EmailAddress struct {
  14. ID int64
  15. UID int64 `xorm:"INDEX NOT NULL" gorm:"INDEX"`
  16. Email string `xorm:"UNIQUE NOT NULL" gorm:"UNIQUE"`
  17. IsActivated bool `gorm:"NOT NULL;DEFAULT:FALSE"`
  18. IsPrimary bool `xorm:"-" gorm:"-" json:"-"`
  19. }
  20. // GetEmailAddresses returns all email addresses belongs to given user.
  21. func GetEmailAddresses(uid int64) ([]*EmailAddress, error) {
  22. emails := make([]*EmailAddress, 0, 5)
  23. if err := x.Where("uid=?", uid).Find(&emails); err != nil {
  24. return nil, err
  25. }
  26. u, err := GetUserByID(uid)
  27. if err != nil {
  28. return nil, err
  29. }
  30. isPrimaryFound := false
  31. for _, email := range emails {
  32. if email.Email == u.Email {
  33. isPrimaryFound = true
  34. email.IsPrimary = true
  35. } else {
  36. email.IsPrimary = false
  37. }
  38. }
  39. // We alway want the primary email address displayed, even if it's not in
  40. // the emailaddress table (yet).
  41. if !isPrimaryFound {
  42. emails = append(emails, &EmailAddress{
  43. Email: u.Email,
  44. IsActivated: true,
  45. IsPrimary: true,
  46. })
  47. }
  48. return emails, nil
  49. }
  50. func isEmailUsed(e Engine, email string) (bool, error) {
  51. if len(email) == 0 {
  52. return true, nil
  53. }
  54. has, err := e.Get(&EmailAddress{Email: email})
  55. if err != nil {
  56. return false, err
  57. } else if has {
  58. return true, nil
  59. }
  60. // We need to check primary email of users as well.
  61. return e.Where("type=?", UserIndividual).And("email=?", email).Get(new(User))
  62. }
  63. // IsEmailUsed returns true if the email has been used.
  64. func IsEmailUsed(email string) (bool, error) {
  65. return isEmailUsed(x, email)
  66. }
  67. func addEmailAddress(e Engine, email *EmailAddress) error {
  68. email.Email = strings.ToLower(strings.TrimSpace(email.Email))
  69. used, err := isEmailUsed(e, email.Email)
  70. if err != nil {
  71. return err
  72. } else if used {
  73. return ErrEmailAlreadyUsed{args: errutil.Args{"email": email.Email}}
  74. }
  75. _, err = e.Insert(email)
  76. return err
  77. }
  78. func AddEmailAddress(email *EmailAddress) error {
  79. return addEmailAddress(x, email)
  80. }
  81. func AddEmailAddresses(emails []*EmailAddress) error {
  82. if len(emails) == 0 {
  83. return nil
  84. }
  85. // Check if any of them has been used
  86. for i := range emails {
  87. emails[i].Email = strings.ToLower(strings.TrimSpace(emails[i].Email))
  88. used, err := IsEmailUsed(emails[i].Email)
  89. if err != nil {
  90. return err
  91. } else if used {
  92. return ErrEmailAlreadyUsed{args: errutil.Args{"email": emails[i].Email}}
  93. }
  94. }
  95. if _, err := x.Insert(emails); err != nil {
  96. return fmt.Errorf("Insert: %v", err)
  97. }
  98. return nil
  99. }
  100. func (email *EmailAddress) Activate() error {
  101. user, err := GetUserByID(email.UID)
  102. if err != nil {
  103. return err
  104. }
  105. if user.Rands, err = GetUserSalt(); err != nil {
  106. return err
  107. }
  108. sess := x.NewSession()
  109. defer sess.Close()
  110. if err = sess.Begin(); err != nil {
  111. return err
  112. }
  113. email.IsActivated = true
  114. if _, err := sess.ID(email.ID).AllCols().Update(email); err != nil {
  115. return err
  116. } else if err = updateUser(sess, user); err != nil {
  117. return err
  118. }
  119. return sess.Commit()
  120. }
  121. func DeleteEmailAddress(email *EmailAddress) (err error) {
  122. if email.ID > 0 {
  123. _, err = x.Id(email.ID).Delete(new(EmailAddress))
  124. } else {
  125. _, err = x.Where("email=?", email.Email).Delete(new(EmailAddress))
  126. }
  127. return err
  128. }
  129. func DeleteEmailAddresses(emails []*EmailAddress) (err error) {
  130. for i := range emails {
  131. if err = DeleteEmailAddress(emails[i]); err != nil {
  132. return err
  133. }
  134. }
  135. return nil
  136. }
  137. func MakeEmailPrimary(userID int64, email *EmailAddress) error {
  138. has, err := x.Get(email)
  139. if err != nil {
  140. return err
  141. } else if !has {
  142. return errors.EmailNotFound{Email: email.Email}
  143. }
  144. if email.UID != userID {
  145. return errors.New("not the owner of the email")
  146. }
  147. if !email.IsActivated {
  148. return errors.EmailNotVerified{Email: email.Email}
  149. }
  150. user := &User{ID: email.UID}
  151. has, err = x.Get(user)
  152. if err != nil {
  153. return err
  154. } else if !has {
  155. return ErrUserNotExist{args: map[string]interface{}{"userID": email.UID}}
  156. }
  157. // Make sure the former primary email doesn't disappear.
  158. formerPrimaryEmail := &EmailAddress{Email: user.Email}
  159. has, err = x.Get(formerPrimaryEmail)
  160. if err != nil {
  161. return err
  162. }
  163. sess := x.NewSession()
  164. defer sess.Close()
  165. if err = sess.Begin(); err != nil {
  166. return err
  167. }
  168. if !has {
  169. formerPrimaryEmail.UID = user.ID
  170. formerPrimaryEmail.IsActivated = user.IsActive
  171. if _, err = sess.Insert(formerPrimaryEmail); err != nil {
  172. return err
  173. }
  174. }
  175. user.Email = email.Email
  176. if _, err = sess.ID(user.ID).AllCols().Update(user); err != nil {
  177. return err
  178. }
  179. return sess.Commit()
  180. }