From fe643c8bbda65d210a0eeaa0f0fe4c2db4fc7211 Mon Sep 17 00:00:00 2001 From: Anbraten Date: Tue, 24 May 2022 13:30:05 +0200 Subject: [PATCH] fix: correct postgres check if user exits (#8) --- adapters/postgres_adapter.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/adapters/postgres_adapter.go b/adapters/postgres_adapter.go index ed68889..b0f7bdd 100644 --- a/adapters/postgres_adapter.go +++ b/adapters/postgres_adapter.go @@ -34,12 +34,23 @@ func (adapter postgresAdapter) DeleteDatabase(ctx context.Context, database stri } func (adapter postgresAdapter) HasDatabaseUserWithAccess(ctx context.Context, database string, username string) (bool, error) { + var count int + query := fmt.Sprintf("SELECT COUNT(*) FROM pg_roles WHERE rolname='%s';", username) + err := adapter.db.QueryRow(ctx, query).Scan(&count) + if err != nil { + return false, err + } + if count == 0 { + return false, nil + } + var hasPrivilege bool - query := fmt.Sprintf("SELECT has_database_privilege('%s', '%s', 'CONNECT');", username, database) - err := adapter.db.QueryRow(ctx, query).Scan(&hasPrivilege) + query = fmt.Sprintf("SELECT has_database_privilege('%s', '%s', 'CONNECT');", username, database) + err = adapter.db.QueryRow(ctx, query).Scan(&hasPrivilege) if err != nil { return false, err } + return hasPrivilege, nil }