Enforce allowed tenant in each request
Signed-off-by: gearnode <bryan@frimin.fr>
This commit is contained in:
1
pkg/coredata/migrations/20250312T094600Z.sql
Normal file
1
pkg/coredata/migrations/20250312T094600Z.sql
Normal file
@@ -0,0 +1 @@
|
||||
ALTER TABLE usrmgr_user_organizations RENAME TO users_organizations;
|
||||
@@ -34,6 +34,8 @@ type (
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
}
|
||||
|
||||
Organizations []*Organization
|
||||
)
|
||||
|
||||
func (o *Organization) LoadByID(
|
||||
|
||||
80
pkg/coredata/user_organization.go
Normal file
80
pkg/coredata/user_organization.go
Normal file
@@ -0,0 +1,80 @@
|
||||
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
package coredata
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/getprobo/probo/pkg/gid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"go.gearno.de/kit/pg"
|
||||
)
|
||||
|
||||
type (
|
||||
UserOrganization struct {
|
||||
UserID gid.GID `db:"user_id"`
|
||||
OrganizationID gid.GID `db:"organization_id"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
}
|
||||
|
||||
UserOrganizations []*UserOrganization
|
||||
)
|
||||
|
||||
func (uo UserOrganization) Insert(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
) error {
|
||||
q := `
|
||||
INSERT INTO users_organizations (user_id, organization_id, created_at)
|
||||
VALUES (@user_id, @organization_id, @created_at)
|
||||
`
|
||||
|
||||
_, err := conn.Exec(ctx, q, pgx.StrictNamedArgs{"user_id": uo.UserID, "organization_id": uo.OrganizationID, "created_at": uo.CreatedAt})
|
||||
return err
|
||||
}
|
||||
|
||||
func (uo UserOrganization) Delete(ctx context.Context, conn pg.Conn) error {
|
||||
q := `
|
||||
DELETE FROM users_organizations WHERE user_id = @user_id AND organization_id = @organization_id
|
||||
`
|
||||
|
||||
_, err := conn.Exec(ctx, q, pgx.StrictNamedArgs{"user_id": uo.UserID, "organization_id": uo.OrganizationID})
|
||||
return err
|
||||
}
|
||||
|
||||
func (uo *UserOrganizations) ForUserID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
userID gid.GID,
|
||||
) error {
|
||||
q := `
|
||||
SELECT user_id, organization_id, created_at FROM users_organizations WHERE user_id = @user_id
|
||||
`
|
||||
|
||||
rows, err := conn.Query(ctx, q, pgx.StrictNamedArgs{"user_id": userID})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
userOrganizations, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[UserOrganization])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*uo = userOrganizations
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user