Skip to content

Commit

Permalink
fix: correct postgres check if user exits (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
anbraten authored May 24, 2022
1 parent 4194459 commit fe643c8
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions adapters/postgres_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit fe643c8

Please sign in to comment.