Newer
Older
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
168
169
170
171
172
173
174
175
176
177
178
179
package rbac
import (
"code.fbi.h-da.de/danet/gosdn/controller/interfaces/rbac"
"code.fbi.h-da.de/danet/gosdn/controller/nucleus/database"
"code.fbi.h-da.de/danet/gosdn/controller/nucleus/errors"
"code.fbi.h-da.de/danet/gosdn/controller/store"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// DatabaseUserStore is used to store users in database
type DatabaseUserStore struct {
userStoreName string
}
// Add adds an User.
func (s *DatabaseUserStore) Add(userToAdd rbac.User) error {
client, ctx, cancel := database.GetMongoConnection()
defer cancel()
defer client.Disconnect(ctx)
_, err := client.Database(database.DatabaseName).
Collection(s.userStoreName).
InsertOne(ctx, userToAdd)
if err != nil {
if mongo.IsDuplicateKeyError(err) {
return nil
}
return errors.ErrCouldNotCreate{StoreName: s.userStoreName}
}
return nil
}
// Delete deletes an User.
func (s *DatabaseUserStore) Delete(userToDelete rbac.User) error {
client, ctx, cancel := database.GetMongoConnection()
defer cancel()
defer client.Disconnect(ctx)
_, err := client.Database(database.DatabaseName).
Collection(s.userStoreName).
DeleteOne(ctx, bson.D{primitive.E{Key: "_id", Value: userToDelete.ID().String()}})
if err != nil {
return errors.ErrCouldNotFind{StoreName: s.userStoreName}
}
return nil
}
// Get takes a User's UUID or name and returns the User. If the requested
// User does not exist an error is returned.
func (s *DatabaseUserStore) Get(query store.Query) (rbac.LoadedUser, error) {
var loadedUser rbac.LoadedUser
if query.ID != uuid.Nil {
loadedUser, err := s.getByID(query.ID)
if err != nil {
return loadedUser, errors.ErrCouldNotFind{StoreName: s.userStoreName}
}
return loadedUser, nil
}
loadedUser, err := s.getByName(query.Name)
if err != nil {
return loadedUser, errors.ErrCouldNotFind{StoreName: s.userStoreName}
}
return loadedUser, nil
}
func (s *DatabaseUserStore) getByID(idOfUser uuid.UUID) (rbac.LoadedUser, error) {
var loadedUser rbac.LoadedUser
client, ctx, cancel := database.GetMongoConnection()
defer cancel()
defer client.Disconnect(ctx)
db := client.Database(database.DatabaseName)
collection := db.Collection(s.userStoreName)
result := collection.FindOne(ctx, bson.D{primitive.E{Key: "_id", Value: idOfUser.String()}})
if result == nil {
return loadedUser, errors.ErrCouldNotFind{StoreName: s.userStoreName}
}
err := result.Decode(&loadedUser)
if err != nil {
log.Printf("Failed marshalling %v", err)
return loadedUser, errors.ErrCouldNotFind{StoreName: s.userStoreName}
}
return loadedUser, nil
}
func (s *DatabaseUserStore) getByName(nameOfUser string) (rbac.LoadedUser, error) {
var loadedUser rbac.LoadedUser
client, ctx, cancel := database.GetMongoConnection()
defer cancel()
defer client.Disconnect(ctx)
db := client.Database(database.DatabaseName)
collection := db.Collection(s.userStoreName)
result := collection.FindOne(ctx, bson.D{primitive.E{Key: "username", Value: nameOfUser}})
if result == nil {
return loadedUser, errors.ErrCouldNotFind{StoreName: s.userStoreName}
}
err := result.Decode(&loadedUser)
if err != nil {
log.Printf("Failed marshalling %v", err)
return loadedUser, errors.ErrCouldNotFind{StoreName: s.userStoreName}
}
return loadedUser, nil
}
// GetAll returns all Users
func (s *DatabaseUserStore) GetAll() ([]rbac.LoadedUser, error) {
var loadedUsers []rbac.LoadedUser
client, ctx, cancel := database.GetMongoConnection()
defer cancel()
defer client.Disconnect(ctx)
db := client.Database(database.DatabaseName)
collection := db.Collection(s.userStoreName)
cursor, err := collection.Find(ctx, bson.D{})
if err != nil {
return nil, err
}
defer cursor.Close(ctx)
err = cursor.All(ctx, &loadedUsers)
if err != nil {
log.Printf("Failed marshalling %v", err)
return nil, errors.ErrCouldNotMarshall{StoreName: s.userStoreName}
}
return loadedUsers, nil
}
// Update updates the User.
func (s *DatabaseUserStore) Update(userToUpdate rbac.User) error {
var updatedLoadedUser rbac.LoadedUser
client, ctx, cancel := database.GetMongoConnection()
defer cancel()
defer client.Disconnect(ctx)
update := bson.D{primitive.E{Key: "$set", Value: userToUpdate}}
upsert := false
after := options.After
opt := options.FindOneAndUpdateOptions{
Upsert: &upsert,
ReturnDocument: &after,
}
err := client.Database(database.DatabaseName).
Collection(s.userStoreName).
FindOneAndUpdate(
ctx, bson.M{"_id": userToUpdate.ID().String()}, update, &opt).
Decode(&updatedLoadedUser)
if err != nil {
log.Printf("Could not update User: %v", err)
return errors.ErrCouldNotUpdate{StoreName: s.userStoreName}
}
return nil
}