132 lines
3.1 KiB
Go
132 lines
3.1 KiB
Go
package mongo_db_handlers
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.acooldomain.co/server-manager/backend-kubernetes-go/models"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
type ServerPermissions struct {
|
|
Username string `json:"username"`
|
|
ServerId string `json:"server_id"`
|
|
Permissions models.Permission `json:"permissions"`
|
|
}
|
|
|
|
type MongoDbAuthorizationHandler struct {
|
|
collection *mongo.Collection
|
|
}
|
|
|
|
func (self *MongoDbAuthorizationHandler) RemoveUser(username string) error {
|
|
_, err := self.collection.DeleteMany(
|
|
context.Background(),
|
|
bson.D{
|
|
{Key: "username", Value: username},
|
|
},
|
|
)
|
|
|
|
return err
|
|
}
|
|
|
|
func (self *MongoDbAuthorizationHandler) RemoveServer(server_id string) error {
|
|
_, err := self.collection.DeleteMany(
|
|
context.Background(),
|
|
bson.D{
|
|
{Key: "server_id", Value: server_id},
|
|
},
|
|
)
|
|
|
|
return err
|
|
}
|
|
|
|
func (self *MongoDbAuthorizationHandler) AddPermissions(username string, serverId string, permissions models.Permission) error {
|
|
var serverPermissions ServerPermissions
|
|
err := self.collection.FindOne(
|
|
context.Background(),
|
|
bson.D{
|
|
{Key: "username", Value: username},
|
|
{Key: "server_id", Value: serverId},
|
|
},
|
|
).Decode(&serverPermissions)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
newPermissions := serverPermissions.Permissions | permissions
|
|
|
|
_, err = self.collection.UpdateOne(
|
|
context.Background(),
|
|
bson.D{
|
|
{Key: "username", Value: username},
|
|
{Key: "server_id", Value: serverId},
|
|
},
|
|
&ServerPermissions{
|
|
Username: username,
|
|
ServerId: serverId,
|
|
Permissions: newPermissions,
|
|
},
|
|
)
|
|
|
|
return err
|
|
}
|
|
|
|
func (self *MongoDbAuthorizationHandler) RemovePermissions(username string, server_id string, permissions models.Permission) error {
|
|
var serverPermissions ServerPermissions
|
|
err := self.collection.FindOne(
|
|
context.Background(),
|
|
bson.D{
|
|
{Key: "username", Value: username},
|
|
{Key: "server_id", Value: serverId},
|
|
},
|
|
).Decode(&serverPermissions)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
newPermissions := serverPermissions.Permissions | permissions
|
|
|
|
_, err = self.collection.UpdateOne(
|
|
context.Background(),
|
|
bson.D{
|
|
{Key: "username", Value: username},
|
|
{Key: "server_id", Value: serverId},
|
|
},
|
|
&ServerPermissions{
|
|
Username: username,
|
|
ServerId: serverId,
|
|
Permissions: newPermissions,
|
|
},
|
|
)
|
|
|
|
return err
|
|
}
|
|
|
|
func (self *MongoDbAuthorizationHandler) SetPermissions(username string, server_id string, permissions models.Permission) error {
|
|
return nil
|
|
}
|
|
|
|
func (self *MongoDbAuthorizationHandler) GetPermissions(username string, server_id string) (models.Permission, error) {
|
|
return 0, nil
|
|
}
|
|
|
|
func NewMongoDbAuthorizationHandler(config models.MongoDBConfig) (*MongoDbAuthorizationHandler, error) {
|
|
clientOptions := options.Client().ApplyURI(config.Url).SetAuth(options.Credential{
|
|
Username: config.Username,
|
|
Password: config.Password,
|
|
})
|
|
|
|
client, err := mongo.Connect(context.TODO(), clientOptions)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &MongoDbAuthorizationHandler{
|
|
collection: client.Database(config.Database).Collection(config.Collection),
|
|
}, nil
|
|
}
|