user_mail.go 4.5 KB

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