Skip to content

Commit

Permalink
Fix for creating an org that auto-adds users based on email domain, f…
Browse files Browse the repository at this point in the history
  • Loading branch information
danenania committed Apr 5, 2024
1 parent 12656c4 commit caff7c5
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/server/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func MigrationsUp() error {

// Uncomment below (and update migration version) to reset migration state to a specific version after a failure
// if os.Getenv("GOENV") == "development" {
// migrateVersion := 2024013000
// migrateVersion := 2024040400
// if err := m.Force(migrateVersion); err != nil {
// return fmt.Errorf("error forcing migration version: %v", err)
// }
Expand Down
13 changes: 9 additions & 4 deletions app/server/db/org_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,23 @@ func AddOrgDomainUsers(orgId, domain string, tx *sql.Tx) error {
}

if len(usersForDomain) > 0 {
memberRoleId, err := GetOrgMemberRoleId()

if err != nil {
return fmt.Errorf("error getting org member role id: %v", err)
}

// create org users for each user
var valueStrings []string
var valueArgs []interface{}
for i, user := range usersForDomain {
num := i * 2
valueStrings = append(valueStrings, fmt.Sprintf("($%d, $%d)", num+1, num+2))
valueArgs = append(valueArgs, orgId, user.Id)
num := i * 3
valueStrings = append(valueStrings, fmt.Sprintf("($%d, $%d, $%d)", num+1, num+2, num+3))
valueArgs = append(valueArgs, orgId, user.Id, memberRoleId)
}

// Join all value strings and execute a single query
stmt := fmt.Sprintf("INSERT INTO orgs_users (org_id, user_id) VALUES %s", strings.Join(valueStrings, ","))
stmt := fmt.Sprintf("INSERT INTO orgs_users (org_id, user_id, org_role_id) VALUES %s ON CONFLICT ON CONSTRAINT org_user_unique DO NOTHING", strings.Join(valueStrings, ","))
_, err = tx.Exec(stmt, valueArgs...)

if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions app/server/db/rbac_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,14 @@ func GetOrgOwnerRoleId() (string, error) {

return roleId, nil
}

func GetOrgMemberRoleId() (string, error) {
var roleId string
err := Conn.Get(&roleId, "SELECT id FROM org_roles WHERE name = 'member'")

if err != nil {
return "", fmt.Errorf("error getting member role id: %v", err)
}

return roleId, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE orgs_users DROP CONSTRAINT org_user_unique;
12 changes: 12 additions & 0 deletions app/server/migrations/2024040400_add_orgs_users_unique.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- clean up any duplicates added mistakenly earlier
WITH ranked_duplicates AS (
SELECT id,
ROW_NUMBER() OVER (PARTITION BY org_id, user_id ORDER BY created_at) AS rn
FROM orgs_users
)
DELETE FROM orgs_users
WHERE id IN (
SELECT id FROM ranked_duplicates WHERE rn > 1
);

ALTER TABLE orgs_users ADD CONSTRAINT org_user_unique UNIQUE (org_id, user_id);
1 change: 1 addition & 0 deletions releases/server/versions/0.8.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix for creating an org that auto-adds users based on email domain (https://github.com/plandex-ai/plandex/issues/24)

0 comments on commit caff7c5

Please sign in to comment.