You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
4.6 KiB

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