user_mail.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. return e.Get(&EmailAddress{Email: email})
  53. }
  54. // IsEmailUsed returns true if the email has been used.
  55. func IsEmailUsed(email string) (bool, error) {
  56. return isEmailUsed(x, email)
  57. }
  58. func addEmailAddress(e Engine, email *EmailAddress) error {
  59. email.Email = strings.ToLower(strings.TrimSpace(email.Email))
  60. used, err := isEmailUsed(e, email.Email)
  61. if err != nil {
  62. return err
  63. } else if used {
  64. return ErrEmailAlreadyUsed{email.Email}
  65. }
  66. _, err = e.Insert(email)
  67. return err
  68. }
  69. func AddEmailAddress(email *EmailAddress) error {
  70. return addEmailAddress(x, email)
  71. }
  72. func AddEmailAddresses(emails []*EmailAddress) error {
  73. if len(emails) == 0 {
  74. return nil
  75. }
  76. // Check if any of them has been used
  77. for i := range emails {
  78. emails[i].Email = strings.ToLower(strings.TrimSpace(emails[i].Email))
  79. used, err := IsEmailUsed(emails[i].Email)
  80. if err != nil {
  81. return err
  82. } else if used {
  83. return ErrEmailAlreadyUsed{emails[i].Email}
  84. }
  85. }
  86. if _, err := x.Insert(emails); err != nil {
  87. return fmt.Errorf("Insert: %v", err)
  88. }
  89. return nil
  90. }
  91. func (email *EmailAddress) Activate() error {
  92. user, err := GetUserByID(email.UID)
  93. if err != nil {
  94. return err
  95. }
  96. user.Rands = GetUserSalt()
  97. sess := x.NewSession()
  98. defer sessionRelease(sess)
  99. if err = sess.Begin(); err != nil {
  100. return err
  101. }
  102. email.IsActivated = true
  103. if _, err := sess.Id(email.ID).AllCols().Update(email); err != nil {
  104. return err
  105. } else if err = updateUser(sess, user); err != nil {
  106. return err
  107. }
  108. return sess.Commit()
  109. }
  110. func DeleteEmailAddress(email *EmailAddress) (err error) {
  111. if email.ID > 0 {
  112. _, err = x.Id(email.ID).Delete(new(EmailAddress))
  113. } else {
  114. _, err = x.Where("email=?", email.Email).Delete(new(EmailAddress))
  115. }
  116. return err
  117. }
  118. func DeleteEmailAddresses(emails []*EmailAddress) (err error) {
  119. for i := range emails {
  120. if err = DeleteEmailAddress(emails[i]); err != nil {
  121. return err
  122. }
  123. }
  124. return nil
  125. }
  126. func MakeEmailPrimary(email *EmailAddress) error {
  127. has, err := x.Get(email)
  128. if err != nil {
  129. return err
  130. } else if !has {
  131. return ErrEmailNotExist
  132. }
  133. if !email.IsActivated {
  134. return ErrEmailNotActivated
  135. }
  136. user := &User{ID: email.UID}
  137. has, err = x.Get(user)
  138. if err != nil {
  139. return err
  140. } else if !has {
  141. return ErrUserNotExist{email.UID, ""}
  142. }
  143. // Make sure the former primary email doesn't disappear.
  144. formerPrimaryEmail := &EmailAddress{Email: user.Email}
  145. has, err = x.Get(formerPrimaryEmail)
  146. if err != nil {
  147. return err
  148. }
  149. sess := x.NewSession()
  150. defer sessionRelease(sess)
  151. if err = sess.Begin(); err != nil {
  152. return err
  153. }
  154. if !has {
  155. formerPrimaryEmail.UID = user.ID
  156. formerPrimaryEmail.IsActivated = user.IsActive
  157. if _, err = sess.Insert(formerPrimaryEmail); err != nil {
  158. return err
  159. }
  160. }
  161. user.Email = email.Email
  162. if _, err = sess.Id(user.ID).AllCols().Update(user); err != nil {
  163. return err
  164. }
  165. return sess.Commit()
  166. }