refactored
This commit is contained in:
161
dbhandler/mongo/servers_authorization.go
Normal file
161
dbhandler/mongo/servers_authorization.go
Normal file
@@ -0,0 +1,161 @@
|
||||
package mongo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.acooldomain.co/server-manager/backend/dbhandler"
|
||||
"git.acooldomain.co/server-manager/backend/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 `bson:"username"`
|
||||
ServerId string `bson:"server_id"`
|
||||
Permissions models.Permission `bson:"permissions"`
|
||||
}
|
||||
|
||||
type ServersAuthorizationDbHandler struct {
|
||||
dbhandler.ServersAuthorizationDbHandler
|
||||
collection *mongo.Collection
|
||||
}
|
||||
|
||||
func (self *ServersAuthorizationDbHandler) RemoveUser(ctx context.Context, username string) error {
|
||||
_, err := self.collection.DeleteMany(
|
||||
ctx,
|
||||
bson.M{
|
||||
"username": username,
|
||||
},
|
||||
)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (self *ServersAuthorizationDbHandler) RemoveServer(ctx context.Context, serverId string) error {
|
||||
_, err := self.collection.DeleteMany(
|
||||
ctx,
|
||||
bson.M{
|
||||
"server_id": serverId,
|
||||
},
|
||||
)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (self *ServersAuthorizationDbHandler) AddPermissions(ctx context.Context, username string, serverId string, permissions models.Permission) error {
|
||||
var serverPermissions ServerPermissions
|
||||
err := self.collection.FindOne(
|
||||
ctx,
|
||||
bson.M{
|
||||
"username": username,
|
||||
"server_id": serverId,
|
||||
},
|
||||
).Decode(&serverPermissions)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
newPermissions := serverPermissions.Permissions | permissions
|
||||
|
||||
_, err = self.collection.UpdateOne(
|
||||
ctx,
|
||||
bson.M{
|
||||
"username": username,
|
||||
"server_id": serverId,
|
||||
},
|
||||
bson.M{"$set": bson.M{
|
||||
"permissions": newPermissions,
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (self *ServersAuthorizationDbHandler) RemovePermissions(ctx context.Context, username string, serverId string, permissions models.Permission) error {
|
||||
var serverPermissions ServerPermissions
|
||||
err := self.collection.FindOne(
|
||||
ctx,
|
||||
bson.M{
|
||||
"username": username,
|
||||
"server_id": serverId,
|
||||
},
|
||||
).Decode(&serverPermissions)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
newPermissions := serverPermissions.Permissions | permissions ^ permissions
|
||||
|
||||
_, err = self.collection.UpdateOne(
|
||||
ctx,
|
||||
bson.M{
|
||||
"username": username,
|
||||
"server_id": serverId,
|
||||
},
|
||||
bson.M{"$set": bson.M{
|
||||
"permissions": newPermissions,
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (self *ServersAuthorizationDbHandler) SetPermissions(ctx context.Context, username string, serverId string, permissions models.Permission) error {
|
||||
_, err := self.collection.UpdateOne(
|
||||
ctx,
|
||||
bson.M{
|
||||
"username": username,
|
||||
"server_id": serverId,
|
||||
},
|
||||
bson.M{"$set": bson.M{
|
||||
"permissions": permissions,
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (self *ServersAuthorizationDbHandler) GetPermissions(ctx context.Context, username string, serverId string) (models.Permission, error) {
|
||||
var serverPermissions ServerPermissions
|
||||
err := self.collection.FindOne(
|
||||
ctx,
|
||||
bson.M{
|
||||
"username": username,
|
||||
"server_id": serverId,
|
||||
},
|
||||
).Decode(&serverPermissions)
|
||||
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return serverPermissions.Permissions, nil
|
||||
}
|
||||
|
||||
func NewServersAuthorizationHandler(config models.MongoDBConfig) (*ServersAuthorizationDbHandler, error) {
|
||||
clientOptions := options.Client().ApplyURI(config.Url).SetAuth(options.Credential{
|
||||
Username: config.Username,
|
||||
Password: config.Password,
|
||||
})
|
||||
|
||||
ctx, cancel := context.WithTimeoutCause(context.Background(), 30*time.Second, fmt.Errorf("Timeout"))
|
||||
defer cancel()
|
||||
|
||||
client, err := mongo.Connect(ctx, clientOptions)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &ServersAuthorizationDbHandler{
|
||||
collection: client.Database(config.Database).Collection(config.Collection),
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user