user_mail.go 4.5 KB

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