Skip to content
Snippets Groups Projects
databaseUserStore.go 5.68 KiB
Newer Older
  • Learn to ignore specific revisions
  • package rbac
    
    import (
    
    	"context"
    
    	"code.fbi.h-da.de/danet/gosdn/controller/customerrs"
    
    	"code.fbi.h-da.de/danet/gosdn/controller/interfaces/rbac"
    	"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"
    
    	"go.mongodb.org/mongo-driver/mongo/writeconcern"
    
    const storeName = "user.json"
    
    
    // DatabaseUserStore is used to store users in database.
    
    type DatabaseUserStore struct {
    
    	collection *mongo.Collection
    
    func NewDatabaseUserStore(db *mongo.Database) *DatabaseUserStore {
    	collection := db.Collection(storeName)
    
    	return &DatabaseUserStore{
    		collection: collection,
    
    // Add adds an User.
    func (s *DatabaseUserStore) Add(ctx context.Context, userToAdd rbac.User) (err error) {
    	_, err = s.collection.InsertOne(ctx, userToAdd)
    
    	if err != nil {
    		if mongo.IsDuplicateKeyError(err) {
    			return nil
    		}
    
    
    		return customerrs.CouldNotCreateError{Identifier: userToAdd.ID(), Type: userToAdd, Err: err}
    
    	}
    
    	return nil
    }
    
    // Delete deletes an User.
    
    func (s *DatabaseUserStore) Delete(ctx context.Context, userToDelete rbac.User) (err error) {
    	_, err = s.collection.DeleteOne(ctx, bson.D{primitive.E{Key: "_id", Value: userToDelete.ID().String()}})
    
    		return customerrs.CouldNotDeleteError{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(ctx context.Context, query store.Query) (rbac.LoadedUser, error) {
    
    	var loadedUser rbac.LoadedUser
    
    	if query.ID != uuid.Nil {
    
    		loadedUser, err := s.getByID(ctx, query.ID)
    
    	loadedUser, err := s.getByName(ctx, query.Name)
    
    func (s *DatabaseUserStore) getByID(ctx context.Context, idOfUser uuid.UUID) (loadedUser rbac.LoadedUser, err error) {
    	result := s.collection.FindOne(ctx, bson.D{primitive.E{Key: "_id", Value: idOfUser.String()}})
    
    	if result == nil {
    
    		return loadedUser, customerrs.CouldNotFindError{ID: idOfUser}
    
    	if err != nil {
    		log.Printf("Failed marshalling %v", err)
    
    		return loadedUser, customerrs.CouldNotMarshallError{Identifier: idOfUser, Type: loadedUser, Err: err}
    
    func (s *DatabaseUserStore) getByName(ctx context.Context, nameOfUser string) (loadedUser rbac.LoadedUser, err error) {
    	result := s.collection.FindOne(ctx, bson.D{primitive.E{Key: "username", Value: nameOfUser}})
    
    	if result == nil {
    
    		return loadedUser, customerrs.CouldNotFindError{Name: nameOfUser}
    
    	if err != nil {
    		log.Printf("Failed marshalling %v", err)
    
    		return loadedUser, customerrs.CouldNotMarshallError{Identifier: nameOfUser, Type: loadedUser, Err: err}
    
    func (s *DatabaseUserStore) GetAll(ctx context.Context) (loadedUsers []rbac.LoadedUser, err error) {
    	cursor, err := s.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, customerrs.CouldNotMarshallError{Type: loadedUsers, Err: err}
    
    	}
    	return loadedUsers, nil
    }
    
    // Update updates the User.
    
    func (s *DatabaseUserStore) Update(ctx context.Context, userToUpdate rbac.User) (err error) {
    
    	var updatedLoadedUser rbac.LoadedUser
    
    
    	wc := writeconcern.Majority()
    	txnOptions := options.Transaction().SetWriteConcern(wc)
    	// Starts a session on the client
    
    	session, err := s.collection.Database().Client().StartSession()
    
    	if err != nil {
    		return err
    
    	// Defers ending the session after the transaction is committed or ended
    
    	defer session.EndSession(ctx)
    
    	// 3.2.1 If yes -> Update entity in callback
    	callback := func(sessCtx mongo.SessionContext) (interface{}, error) {
    		// 2. Fetch exisiting Entity
    		existingUser, err := s.getByID(ctx, userToUpdate.ID())
    		if err != nil {
    			return nil, err
    		}
    
    		// 3. Check if Entity.Metadata.ResourceVersion == UpdatedEntity.Metadata.ResourceVersion
    		if userToUpdate.GetMetadata().ResourceVersion != existingUser.Metadata.ResourceVersion {
    			// 3.1.1 End transaction
    			// 3.1.2 If no -> return error
    
    			return nil, fmt.Errorf(
    				"resource version %d of provided user %s is older or newer than %d in the store",
    				userToUpdate.GetMetadata().ResourceVersion,
    				userToUpdate.ID().String(), existingUser.Metadata.ResourceVersion,
    			)
    		}
    
    
    		// Important: You must pass sessCtx as the Context parameter to the operations for them to be executed in the
    		// transaction.
    
    		u, _ := userToUpdate.(*User)
    		u.Metadata.ResourceVersion = u.Metadata.ResourceVersion + 1
    
    		update := bson.D{primitive.E{Key: "$set", Value: u}}
    
    		upsert := false
    		after := options.After
    		opt := options.FindOneAndUpdateOptions{
    			Upsert:         &upsert,
    			ReturnDocument: &after,
    		}
    
    
    		err = s.collection.FindOneAndUpdate(
    			ctx, bson.M{"_id": userToUpdate.ID().String()}, update, &opt).
    
    			Decode(&updatedLoadedUser)
    		if err != nil {
    			log.Printf("Could not update User: %v", err)
    
    			return nil, customerrs.CouldNotUpdateError{Identifier: userToUpdate.ID(), Type: userToUpdate, Err: err}
    		}
    
    		// 3.2.2 End transaction
    		return "", nil
    	}
    
    
    	_, err = session.WithTransaction(ctx, callback, txnOptions)
    
    	if err != nil {
    		return err