This repository has been archived by the owner on May 5, 2023. It is now read-only.
/
mongo_user_repo.go
167 lines (131 loc) · 4.03 KB
/
mongo_user_repo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package repo
import (
"context"
"log"
"time"
"go.mongodb.org/mongo-driver/bson"
"github.com/google/uuid"
"github.com/maximthomas/blazewall/user-service/models"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
type mongoRepoUser struct {
models.User `bson:",inline"`
Password string `json:"password,omitempty"`
}
type UserRepositoryMongoDB struct {
client *mongo.Client
db string
collection string
}
/*
GetUser(realm, userID string) (User, error)
CreateUser(user User) (User, error)
UpdateUser(user User) (User, error)
DeleteUser(realm, userID string) error
SetPassword(realm, userID, password string) error
ValidatePassword(realm, userID, password string) (bool, error)
*/
func (ur *UserRepositoryMongoDB) GetUser(realm, userID string) (models.User, error) {
var user models.User
collection := ur.getCollection()
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
filter := bson.M{"id": userID, "realm": realm}
var repoUser mongoRepoUser
err := collection.FindOne(ctx, filter).Decode(&repoUser)
if err != nil {
return user, err
}
return repoUser.User, nil
}
func (ur *UserRepositoryMongoDB) CreateUser(user models.User) (models.User, error) {
if user.ID == "" {
user.ID = uuid.New().String()
}
repoUser := mongoRepoUser{
User: user,
Password: "",
}
collection := ur.getCollection()
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
_, err := collection.InsertOne(ctx, &repoUser)
if err != nil {
return user, err
}
return user, nil
}
func (ur *UserRepositoryMongoDB) UpdateUser(user models.User) (models.User, error) {
collection := ur.getCollection()
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
filter := bson.M{"id": user.ID, "realm": user.Realm}
var updatedUser mongoRepoUser
err := collection.FindOneAndUpdate(ctx, filter, bson.M{"$set": user}).Decode(&updatedUser)
if err != nil {
return user, err
}
return user, nil
}
func (ur *UserRepositoryMongoDB) DeleteUser(realm, userID string) error {
collection := ur.getCollection()
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
filter := bson.M{"id": userID, "realm": realm}
err := collection.FindOneAndDelete(ctx, filter).Err()
if err != nil {
return err
}
return nil
}
func (ur *UserRepositoryMongoDB) SetPassword(realm, userID, password string) error {
collection := ur.getCollection()
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
filter := bson.M{"id": userID, "realm": realm}
var updatedUser mongoRepoUser
err := collection.FindOneAndUpdate(ctx, filter, bson.M{"$set": bson.M{"password": password}}).Decode(&updatedUser)
if err != nil {
return err
}
return nil
}
func (ur *UserRepositoryMongoDB) ValidatePassword(realm, userID, password string) (bool, error) {
collection := ur.getCollection()
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
filter := bson.M{"id": userID, "realm": realm}
var repoUser mongoRepoUser
err := collection.FindOne(ctx, filter).Decode(&repoUser)
var valid bool
if err != nil {
return valid, err
}
valid = repoUser.Password == password
return valid, nil
}
func (ur *UserRepositoryMongoDB) getCollection() *mongo.Collection {
return ur.client.Database(ur.db).Collection(ur.collection)
}
func NewUserRepositoryMongoDB(uri, db, collection string) UserRepositoryMongoDB {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
log.Printf("connecting to mongo, uri: %v", uri)
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
if err != nil {
panic(err)
}
ctx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = client.Ping(ctx, readpref.Primary())
if err != nil {
panic(err)
}
return UserRepositoryMongoDB{
client: client,
db: db,
collection: collection,
}
}