11
pkg/usrmgr/coredata/migrations/20250225T201106Z.sql
Normal file
11
pkg/usrmgr/coredata/migrations/20250225T201106Z.sql
Normal file
@@ -0,0 +1,11 @@
|
||||
CREATE TABLE usrmgr_user_organizations (
|
||||
user_id TEXT REFERENCES usrmgr_users(id) NOT NULL,
|
||||
organization_id TEXT REFERENCES organizations(id) NOT NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
||||
PRIMARY KEY (user_id, organization_id)
|
||||
);
|
||||
|
||||
INSERT INTO usrmgr_user_organizations (user_id, organization_id, created_at)
|
||||
SELECT id, organization_id, NOW()
|
||||
FROM usrmgr_users
|
||||
WHERE organization_id IS NOT NULL;
|
||||
1
pkg/usrmgr/coredata/migrations/20250225T211652Z.sql
Normal file
1
pkg/usrmgr/coredata/migrations/20250225T211652Z.sql
Normal file
@@ -0,0 +1 @@
|
||||
ALTER TABLE usrmgr_users ADD COLUMN fullname TEXT;
|
||||
1
pkg/usrmgr/coredata/migrations/20250225T222000Z.sql
Normal file
1
pkg/usrmgr/coredata/migrations/20250225T222000Z.sql
Normal file
@@ -0,0 +1 @@
|
||||
ALTER TABLE usrmgr_users ALTER COLUMN fullname SET NOT NULL;
|
||||
@@ -29,6 +29,7 @@ type (
|
||||
ID gid.GID
|
||||
EmailAddress string
|
||||
HashedPassword []byte
|
||||
FullName string
|
||||
OrganizationID gid.GID
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
@@ -44,6 +45,7 @@ func (u *User) scan(r pgx.Row) error {
|
||||
&u.ID,
|
||||
&u.EmailAddress,
|
||||
&u.HashedPassword,
|
||||
&u.FullName,
|
||||
&u.OrganizationID,
|
||||
&u.CreatedAt,
|
||||
&u.UpdatedAt,
|
||||
@@ -60,6 +62,7 @@ SELECT
|
||||
id,
|
||||
email_address,
|
||||
hashed_password,
|
||||
fullname,
|
||||
organization_id,
|
||||
created_at,
|
||||
updated_at
|
||||
@@ -94,6 +97,7 @@ SELECT
|
||||
id,
|
||||
email_address,
|
||||
hashed_password,
|
||||
fullname,
|
||||
organization_id,
|
||||
created_at,
|
||||
updated_at
|
||||
@@ -124,11 +128,12 @@ func (u *User) Insert(
|
||||
) error {
|
||||
q := `
|
||||
INSERT INTO
|
||||
usrmgr_users (id, email_address, hashed_password, organization_id, created_at, updated_at)
|
||||
usrmgr_users (id, email_address, hashed_password, fullname, organization_id, created_at, updated_at)
|
||||
VALUES (
|
||||
@user_id,
|
||||
@email_address,
|
||||
@hashed_password,
|
||||
@fullname,
|
||||
@organization_id,
|
||||
@created_at,
|
||||
@updated_at
|
||||
@@ -139,6 +144,7 @@ VALUES (
|
||||
"user_id": u.ID,
|
||||
"email_address": u.EmailAddress,
|
||||
"hashed_password": u.HashedPassword,
|
||||
"fullname": u.FullName,
|
||||
"organization_id": "AZSfP_xAcAC5IAAAAAAltA",
|
||||
"created_at": u.CreatedAt,
|
||||
"updated_at": u.UpdatedAt,
|
||||
|
||||
@@ -308,46 +308,6 @@ func (s Service) GetUserBySession(
|
||||
return s.GetUserByID(ctx, session.UserID)
|
||||
}
|
||||
|
||||
// SetUserOrganization sets the organization for a user
|
||||
func (s Service) SetUserOrganization(
|
||||
ctx context.Context,
|
||||
userID gid.GID,
|
||||
organizationID gid.GID,
|
||||
) error {
|
||||
return s.pg.WithTx(
|
||||
ctx,
|
||||
func(tx pg.Conn) error {
|
||||
user := &coredata.User{}
|
||||
if err := user.LoadByID(ctx, tx, userID); err != nil {
|
||||
return fmt.Errorf("user not found: %w", err)
|
||||
}
|
||||
|
||||
// Update the organization ID
|
||||
user.OrganizationID = organizationID
|
||||
user.UpdatedAt = time.Now()
|
||||
|
||||
// Update the user in the database
|
||||
q := `
|
||||
UPDATE usrmgr_users
|
||||
SET organization_id = @organization_id, updated_at = @updated_at
|
||||
WHERE id = @user_id
|
||||
`
|
||||
args := pgx.NamedArgs{
|
||||
"user_id": user.ID,
|
||||
"organization_id": user.OrganizationID,
|
||||
"updated_at": user.UpdatedAt,
|
||||
}
|
||||
|
||||
_, err := tx.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot update user organization: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// GetUserOrganization gets the organization ID for a user
|
||||
func (s Service) GetUserOrganization(
|
||||
ctx context.Context,
|
||||
@@ -360,3 +320,101 @@ func (s Service) GetUserOrganization(
|
||||
|
||||
return user.OrganizationID, nil
|
||||
}
|
||||
|
||||
// GetUserOrganizations gets all organizations for a user
|
||||
func (s Service) GetUserOrganizations(
|
||||
ctx context.Context,
|
||||
userID gid.GID,
|
||||
) ([]gid.GID, error) {
|
||||
q := `
|
||||
SELECT
|
||||
organization_id
|
||||
FROM
|
||||
usrmgr_user_organizations
|
||||
WHERE
|
||||
user_id = @user_id;
|
||||
`
|
||||
|
||||
args := pgx.NamedArgs{"user_id": userID}
|
||||
|
||||
var organizationIDs []gid.GID
|
||||
|
||||
err := s.pg.WithTx(
|
||||
ctx,
|
||||
func(tx pg.Conn) error {
|
||||
rows, err := tx.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to query user organizations: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var organizationID gid.GID
|
||||
if err := rows.Scan(&organizationID); err != nil {
|
||||
return fmt.Errorf("failed to scan organization ID: %w", err)
|
||||
}
|
||||
organizationIDs = append(organizationIDs, organizationID)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return fmt.Errorf("error iterating over rows: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return organizationIDs, nil
|
||||
}
|
||||
|
||||
// AddUserToOrganization adds a user to an organization
|
||||
func (s Service) AddUserToOrganization(
|
||||
ctx context.Context,
|
||||
userID gid.GID,
|
||||
organizationID gid.GID,
|
||||
) error {
|
||||
q := `
|
||||
INSERT INTO
|
||||
usrmgr_user_organizations (user_id, organization_id, created_at)
|
||||
VALUES
|
||||
(@user_id, @organization_id, NOW())
|
||||
ON CONFLICT (user_id, organization_id) DO NOTHING;
|
||||
`
|
||||
|
||||
args := pgx.NamedArgs{
|
||||
"user_id": userID,
|
||||
"organization_id": organizationID,
|
||||
}
|
||||
|
||||
return s.pg.WithTx(
|
||||
ctx,
|
||||
func(tx pg.Conn) error {
|
||||
_, err := tx.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to add user to organization: %w", err)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// GetUserIDFromContext gets the user ID from the context
|
||||
func (s Service) GetUserIDFromContext(ctx context.Context) (gid.GID, error) {
|
||||
// Get the session ID from the context
|
||||
sessionID, ok := ctx.Value("session_id").(gid.GID)
|
||||
if !ok {
|
||||
return gid.GID{}, fmt.Errorf("no session ID in context")
|
||||
}
|
||||
|
||||
// Get the session
|
||||
session, err := s.GetSession(ctx, sessionID)
|
||||
if err != nil {
|
||||
return gid.GID{}, fmt.Errorf("failed to get session: %w", err)
|
||||
}
|
||||
|
||||
return session.UserID, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user