Skip to content
Snippets Groups Projects
databaseUserStore.go 5.78 KiB
Newer Older
  • Learn to ignore specific revisions
  • 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) (err error) {
    
    	client, ctx, cancel := database.GetMongoConnection()
    	defer cancel()
    
    	defer func() {
    		if ferr := client.Disconnect(ctx); ferr != nil {
    			fErrString := ferr.Error()
    			err = fmt.Errorf("InternalError=%w DeferError=%+s", err, fErrString)
    		}
    	}()
    
    	_, err = client.Database(database.DatabaseName).
    
    		Collection(s.userStoreName).
    		InsertOne(ctx, userToAdd)
    	if err != nil {
    		if mongo.IsDuplicateKeyError(err) {
    			return nil
    		}
    
    
    		return errors.ErrCouldNotCreate{Identifier: userToAdd.ID(), Type: userToAdd, Err: err}
    
    	}
    
    	return nil
    }
    
    // Delete deletes an User.
    
    func (s *DatabaseUserStore) Delete(userToDelete rbac.User) (err error) {
    
    	client, ctx, cancel := database.GetMongoConnection()
    	defer cancel()
    
    	defer func() {
    		if ferr := client.Disconnect(ctx); ferr != nil {
    			fErrString := ferr.Error()
    			err = fmt.Errorf("InternalError=%w DeferError=%+s", err, fErrString)
    		}
    	}()
    
    	_, 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.ErrCouldNotDelete{Identifier: userToDelete.ID(), Type: userToDelete, Err: err}
    
    	}
    
    	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, nil
    	}
    
    	loadedUser, err := s.getByName(query.Name)
    	if err != nil {
    
    func (s *DatabaseUserStore) getByID(idOfUser uuid.UUID) (loadedUser rbac.LoadedUser, err error) {
    
    	client, ctx, cancel := database.GetMongoConnection()
    	defer cancel()
    
    	defer func() {
    		if ferr := client.Disconnect(ctx); ferr != nil {
    			fErrString := ferr.Error()
    			err = fmt.Errorf("InternalError=%w DeferError=%+s", err, fErrString)
    		}
    	}()
    
    
    	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{ID: idOfUser}
    
    	if err != nil {
    		log.Printf("Failed marshalling %v", err)
    
    		return loadedUser, errors.ErrCouldNotMarshall{Identifier: idOfUser, Type: loadedUser, Err: err}
    
    func (s *DatabaseUserStore) getByName(nameOfUser string) (loadedUser rbac.LoadedUser, err error) {
    
    	client, ctx, cancel := database.GetMongoConnection()
    	defer cancel()
    
    	defer func() {
    		if ferr := client.Disconnect(ctx); ferr != nil {
    			fErrString := ferr.Error()
    			err = fmt.Errorf("InternalError=%w DeferError=%+s", err, fErrString)
    		}
    	}()
    
    
    	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{Name: nameOfUser}
    
    	if err != nil {
    		log.Printf("Failed marshalling %v", err)
    
    		return loadedUser, errors.ErrCouldNotMarshall{Identifier: nameOfUser, Type: loadedUser, Err: err}
    
    // GetAll returns all Users.
    func (s *DatabaseUserStore) GetAll() (loadedUsers []rbac.LoadedUser, err error) {
    
    	client, ctx, cancel := database.GetMongoConnection()
    	defer cancel()
    
    	defer func() {
    		if ferr := client.Disconnect(ctx); ferr != nil {
    			fErrString := ferr.Error()
    			err = fmt.Errorf("InternalError=%w DeferError=%+s", err, fErrString)
    		}
    	}()
    
    
    	db := client.Database(database.DatabaseName)
    	collection := db.Collection(s.userStoreName)
    
    	cursor, err := collection.Find(ctx, bson.D{})
    	if err != nil {
    		return nil, err
    	}
    
    	defer func() {
    		if ferr := cursor.Close(ctx); ferr != nil {
    			fErrString := ferr.Error()
    			err = fmt.Errorf("InternalError=%w DeferError=%+s", err, fErrString)
    		}
    	}()
    
    
    	err = cursor.All(ctx, &loadedUsers)
    	if err != nil {
    		log.Printf("Failed marshalling %v", err)
    
    
    		return nil, errors.ErrCouldNotMarshall{Type: loadedUsers, Err: err}
    
    	}
    	return loadedUsers, nil
    }
    
    // Update updates the User.
    
    func (s *DatabaseUserStore) Update(userToUpdate rbac.User) (err error) {
    
    	var updatedLoadedUser rbac.LoadedUser
    
    	client, ctx, cancel := database.GetMongoConnection()
    	defer cancel()
    
    	defer func() {
    		if ferr := client.Disconnect(ctx); ferr != nil {
    			fErrString := ferr.Error()
    			err = fmt.Errorf("InternalError=%w DeferError=%+s", err, fErrString)
    		}
    	}()
    
    
    	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{Identifier: userToUpdate.ID(), Type: userToUpdate, Err: err}