db.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2020 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of gostfix project https://git.semlanik.org/semlanik/gostfix
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. package db
  26. import (
  27. "context"
  28. "errors"
  29. "time"
  30. common "git.semlanik.org/semlanik/gostfix/common"
  31. bcrypt "golang.org/x/crypto/bcrypt"
  32. bson "go.mongodb.org/mongo-driver/bson"
  33. mongo "go.mongodb.org/mongo-driver/mongo"
  34. options "go.mongodb.org/mongo-driver/mongo/options"
  35. config "git.semlanik.org/semlanik/gostfix/config"
  36. )
  37. type Storage struct {
  38. usersCollection *mongo.Collection
  39. tokensCollection *mongo.Collection
  40. emailsCollection *mongo.Collection
  41. }
  42. func NewStorage() (s *Storage, err error) {
  43. fullUrl := "mongodb://"
  44. if config.ConfigInstance().MongoUser != "" {
  45. fullUrl += config.ConfigInstance().MongoUser
  46. if config.ConfigInstance().MongoPassword != "" {
  47. fullUrl += ":" + config.ConfigInstance().MongoPassword
  48. }
  49. fullUrl += "@"
  50. }
  51. fullUrl += config.ConfigInstance().MongoAddress
  52. client, err := mongo.NewClient(options.Client().ApplyURI(fullUrl))
  53. if err != nil {
  54. return nil, err
  55. }
  56. ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
  57. defer cancel()
  58. err = client.Connect(ctx)
  59. if err != nil {
  60. return nil, err
  61. }
  62. db := client.Database("gostfix")
  63. index := mongo.IndexModel{
  64. Keys: bson.M{
  65. "user": 1,
  66. },
  67. Options: options.Index().SetUnique(true),
  68. }
  69. s = &Storage{
  70. usersCollection: db.Collection("users"),
  71. tokensCollection: db.Collection("tokens"),
  72. emailsCollection: db.Collection("emails"),
  73. }
  74. //Initial database setup
  75. s.usersCollection.Indexes().CreateOne(context.Background(), index)
  76. s.tokensCollection.Indexes().CreateOne(context.Background(), index)
  77. s.emailsCollection.Indexes().CreateOne(context.Background(), index)
  78. return
  79. }
  80. func (s *Storage) AddUser(user, password, fullName string) error {
  81. hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
  82. if err != nil {
  83. return err
  84. }
  85. hashString := string(hash)
  86. userInfo := bson.M{
  87. "user": user,
  88. "password": hashString,
  89. "fullName": fullName,
  90. }
  91. _, err = s.usersCollection.InsertOne(context.Background(), userInfo)
  92. if err != nil {
  93. return err
  94. }
  95. err = s.addEmail(user, user, true)
  96. if err != nil {
  97. s.usersCollection.DeleteOne(context.Background(), bson.M{"user": user})
  98. return err
  99. }
  100. //TODO: Update postfix virtual map here
  101. return nil
  102. }
  103. func (s *Storage) AddEmail(user string, email string) error {
  104. return s.addEmail(user, email, false)
  105. }
  106. func (s *Storage) addEmail(user string, email string, upsert bool) error {
  107. result := struct {
  108. User string
  109. }{}
  110. err := s.usersCollection.FindOne(context.Background(), bson.M{"user": user}).Decode(&result)
  111. if err != nil {
  112. return err
  113. }
  114. _, err = s.emailsCollection.UpdateOne(context.Background(),
  115. bson.M{"user": user},
  116. bson.M{"$addToSet": bson.M{"email": email}},
  117. options.Update().SetUpsert(upsert))
  118. //TODO: Update postfix virtual map here
  119. return err
  120. }
  121. func (s *Storage) RemoveEmail(user string, email string) error {
  122. _, err := s.emailsCollection.UpdateOne(context.Background(),
  123. bson.M{"user": user},
  124. bson.M{"$pull": bson.M{"email": email}})
  125. //TODO: Update postfix virtual map here
  126. return err
  127. }
  128. func (s *Storage) CheckUser(user, password string) error {
  129. result := struct {
  130. User string
  131. Password string
  132. }{}
  133. err := s.usersCollection.FindOne(context.Background(), bson.M{"user": user}).Decode(&result)
  134. if err != nil {
  135. return errors.New("Invalid user or password")
  136. }
  137. if bcrypt.CompareHashAndPassword([]byte(password), []byte(result.Password)) != nil {
  138. return errors.New("Invalid user or password")
  139. }
  140. return nil
  141. }
  142. func (s *Storage) AddToken(user, token string) error {
  143. return nil
  144. }
  145. func (s *Storage) CheckToken(user, token string) error {
  146. return nil
  147. }
  148. func (s *Storage) SaveMail(user string, m *common.Mail) error {
  149. return nil
  150. }
  151. func (s *Storage) RemoveMail(user string, m *common.Mail) error {
  152. return nil
  153. }
  154. func (s *Storage) MailList(user string) ([]*common.MailHeader, error) {
  155. return nil, nil
  156. }
  157. func (s *Storage) GetMail(user string, header *common.MailHeader) (m *common.Mail, err error) {
  158. return nil, nil
  159. }
  160. func (s *Storage) GetAttachment(user string, attachmentId string) (filePath string, err error) {
  161. return "", nil
  162. }
  163. func (s *Storage) GetUsers() (users []string, err error) {
  164. return nil, nil
  165. }
  166. func (s *Storage) GetEmails(user []string) (emails []string, err error) {
  167. return nil, nil
  168. }
  169. func (s *Storage) GetAllEmails() (emails []string, err error) {
  170. return nil, nil
  171. }