mirror of
https://github.com/netbirdio/netbird.git
synced 2026-10-25 13:59:08 +01:00
support for GetAllowedUsers in sqlite
Signed-off-by: Dmitri Dolguikh <dmitri.external@netbird.io>
This commit is contained in:
@@ -6,14 +6,10 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/netbirdio/netbird/management/server/types"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGetAllowedUsers(t *testing.T) {
|
func TestGetAllowedUsers(t *testing.T) {
|
||||||
if engine == string(types.SqliteStoreEngine) {
|
|
||||||
t.Skip()
|
|
||||||
}
|
|
||||||
ctx := context.TODO()
|
ctx := context.TODO()
|
||||||
|
|
||||||
execQuery(t, ctx,
|
execQuery(t, ctx,
|
||||||
|
|||||||
@@ -118,7 +118,3 @@ func CollectRowsForSqlite[T any](rows *sql.Rows) ([]T, error) {
|
|||||||
|
|
||||||
return toret, nil
|
return toret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SqliteStoreConn) GetAllowedUsers(ctx context.Context, accountId string) (map[string]struct{}, map[string][]string, error) {
|
|
||||||
return nil, nil, nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -0,0 +1,84 @@
|
|||||||
|
package networkmap_sqlite
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"encoding/json"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
GetAllowedUserIdsQuery = `
|
||||||
|
select id, auto_groups
|
||||||
|
from users
|
||||||
|
where account_id=? and not blocked and not is_service_user
|
||||||
|
`
|
||||||
|
|
||||||
|
GetAllGroupIdQuery = `
|
||||||
|
select id from groups
|
||||||
|
where account_id=? and name='All'
|
||||||
|
`
|
||||||
|
)
|
||||||
|
|
||||||
|
func (sc *SqliteStoreConn) GetAllowedUsers(ctx context.Context, accountId string) (map[string]struct{}, map[string][]string, error) {
|
||||||
|
rows, err := sc.Conn.QueryContext(ctx, GetAllowedUserIdsQuery, accountId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
users, err := CollectRowsForSqlite[user](rows)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err = sc.Conn.QueryContext(ctx, GetAllGroupIdQuery, accountId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
allGroupIds, err := collectAllGroupIds(rows)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
userIdIdx := make(map[string]struct{})
|
||||||
|
groupIdToUserIds := make(map[string][]string)
|
||||||
|
for _, user := range users {
|
||||||
|
autogroups := make([]string, 0)
|
||||||
|
if err := json.Unmarshal(user.AutoGroups, &autogroups); err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
userIdIdx[user.ID] = struct{}{}
|
||||||
|
for _, groupId := range autogroups {
|
||||||
|
groupIdToUserIds[groupId] = append(groupIdToUserIds[groupId], user.ID)
|
||||||
|
}
|
||||||
|
for _, allgid := range allGroupIds {
|
||||||
|
groupIdToUserIds[allgid] = append(groupIdToUserIds[allgid], user.ID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return userIdIdx, groupIdToUserIds, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func collectAllGroupIds(rows *sql.Rows) ([]string, error) {
|
||||||
|
defer rows.Close()
|
||||||
|
var toret []string
|
||||||
|
|
||||||
|
for rows.Next() {
|
||||||
|
var id string
|
||||||
|
err := rows.Scan(&id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
toret = append(toret, id)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return toret, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type user struct {
|
||||||
|
ID string
|
||||||
|
AutoGroups []byte
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user