Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

rbac: create users and assign role in single transaction - #47700

Merged
BolajiOlajide merged 8 commits into
mainfrom
bo/create-users-and-assign-role-in-single-transaction
Feb 17, 2023
Merged

rbac: create users and assign role in single transaction#47700
BolajiOlajide merged 8 commits into
mainfrom
bo/create-users-and-assign-role-in-single-transaction

Conversation

@BolajiOlajide

@BolajiOlajide BolajiOlajide commented Feb 15, 2023

Copy link
Copy Markdown
Contributor

This is a follow-up to this comment on #47406.

This ensures that we don't have zombie users (users without any role assigned to them) on a Sourcegraph instance.

Test plan

  • Manually tested
  • Updated unit tests

@github-actions

Copy link
Copy Markdown
Contributor

Problem: the label i-acknowledge-this-goes-into-the-release is absent.
👉 What to do: we're in the next Sourcegraph release code freeze period. If you are 100% sure your changes should get released or provide no risk to the release, add the label your PR with i-acknowledge-this-goes-into-the-release.

@sourcegraph-bot

sourcegraph-bot commented Feb 15, 2023

Copy link
Copy Markdown
Contributor

Codenotify: Notifying subscribers in CODENOTIFY files for diff 26d6843eee7a4c3a7dd9a1787051c5dfecf94757...852e4ebf207a5151e5574f6b8265850f39475709.

Notify File(s)
@eseliger internal/database/external_accounts.go
@unknwon enterprise/cmd/frontend/internal/auth/sourcegraphoperator/associate_test.go

@sourcegraph-bot

sourcegraph-bot commented Feb 15, 2023

Copy link
Copy Markdown
Contributor

Codenotify: Notifying subscribers in OWNERS files for diff 26d6843eee7a4c3a7dd9a1787051c5dfecf94757...852e4ebf207a5151e5574f6b8265850f39475709.

Notify File(s)
@mrnugget dev/sg/sg_db.go
@sourcegraph/dev-experience dev/sg/sg_db.go

@unknwon unknwon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@kopancek kopancek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks much neater and I like that it removed more lines than it added :)

BolajiOlajide added a commit that referenced this pull request Feb 17, 2023
## Context
We have several `Assign`-ment methods in the store for `user_roles` and
`role_permissions`, however most of the call site for these methods
don't use the `types.UserRole` or `types.RolePermission` returned.
While working on #47700, I noticed that some tests were failing because:

* The tests had a call to `db.Users().Create`, which creates a site
admin user because the `GlobalState` hasn't been initialized. Then
calling `db.Users().SetIsSiteAdmin` after that results in a `sql: no
rows in result set` error being returned because the [`ON CONFLICT DO
NOTHING`
clause](https://sourcegraph.com/github.com/sourcegraph/sourcegraph/-/blob/internal/database/user_roles.go?L111)
in the upsert statement doesn't return a result set if the user is
already assigned a role.

This PR adds an extra check so that we don't return an error when an
upsert is done. I also updated the signature to only return an error
since we never use the relationship result in the call site for Role /
Permission assignment.
@BolajiOlajide
BolajiOlajide force-pushed the bo/create-users-and-assign-role-in-single-transaction branch from 852e4eb to 7c0930d Compare February 17, 2023 12:13
@BolajiOlajide
BolajiOlajide merged commit c45b476 into main Feb 17, 2023
@BolajiOlajide
BolajiOlajide deleted the bo/create-users-and-assign-role-in-single-transaction branch February 17, 2023 14:07
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants