Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix workspace check query #1398

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions pkg/auth/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package db

import (
"errors"
"fmt"
"github.com/jackc/pgtype"
"github.com/kaytu-io/kaytu-util/pkg/api"
"gorm.io/gorm"
Expand Down Expand Up @@ -264,8 +265,8 @@ func (db Database) GetUsersByEmail(email string) ([]User, error) {

func (db Database) GetUsersByWorkspace(ws string) ([]User, error) {
var users []User
query := `SELECT * FROM users WHERE app_metadata->'workspaceAccess' \? ?`
err := db.Orm.Raw(query, ws).Scan(&users).Error
query := fmt.Sprintf("SELECT * FROM users WHERE app_metadata->'workspaceAccess' ? '%s'", ws)
err := db.Orm.Raw(query).Scan(&users).Error
if err != nil {
return nil, err
}
Expand All @@ -274,21 +275,17 @@ func (db Database) GetUsersByWorkspace(ws string) ([]User, error) {

func (db Database) SearchUsers(ws string, email *string, emailVerified *bool) ([]User, error) {
var users []User
query := `SELECT * FROM users WHERE app_metadata->'workspaceAccess' \? ?`

params := []interface{}{ws}
query := fmt.Sprintf("SELECT * FROM users WHERE app_metadata->'workspaceAccess' ? '%s'", ws)

if email != nil {
query += ` AND email = ?`
params = append(params, *email)
query += fmt.Sprintf(" AND email = %s", *email)
}

if emailVerified != nil {
query += ` AND email_verified = ?`
params = append(params, *emailVerified)
query += fmt.Sprintf(" AND email_verified = %v", *emailVerified)
}

err := db.Orm.Raw(query, params...).Scan(&users).Error
err := db.Orm.Raw(query).Scan(&users).Error
if err != nil {
return nil, err
}
Expand Down