datumforge / datum

Datum's central server
https://datum.net
Apache License 2.0
53 stars 7 forks source link

feat: update session on org delete, update default org ID #1057

Closed golanglemonade closed 1 week ago

golanglemonade commented 2 weeks ago

Summary

  1. When an organization is deleted, we need to issue a new pair of oauth tokens and session for the user if the organization is the what they are currently authenticated into (this will always be true when using the UI, a little different when using the CLI or API directly). This PR attempts to solve the first case of using access tokens + sessions.

  2. We also need to update the defaultOrgID in the user settings for future login attempts if the org that was deleted was the defaultOrgID.

  3. Finally, we need to ensure personal orgs cannot be deleted except when the user itself is deleted.

Details

New Tokens

When an org is deleted, a new set of access tokens and refresh tokens are issue and set in the cookie:

Create Org:

(⎈ |default:default)➜  datum git:(feat-delete-org-session) go run cmd/cli/main.go org create -n funkytown4
...

Switch to Org:

(⎈ |default:default)➜  datum git:(feat-delete-org-session) go run cmd/cli/main.go switch -t 01J16WHRTYP2XMHNBC0BERVN32
Successfully switched to organization: 01J16WHRTYP2XMHNBC0BERVN32!
auth tokens successfully stored in keychain

Delete org:

(⎈ |default:default)➜  datum git:(feat-delete-org-session) go run cmd/cli/main.go org delete -i 01J16WHRTYP2XMHNBC0BERVN32
{
  "deleteOrganization": {
    "deletedID": "01J16WHRTYP2XMHNBC0BERVN32"
  }
}

New token's will now get saved and subsequent requests will continue to work:

(⎈ |default:default)➜  datum git:(feat-delete-org-session) go run cmd/cli/main.go org get                                 
  ID                          NAME             DESCRIPTION                            PERSONALORG  CHILDREN  MEMBERS  
  01J169Y91PJEXBDGCCNTEFBHDM  Organic Katydid  Personal Organization - Matt Anderson  true         0         1      

Default Org Update

Deleted default org should update to another org: Before deleting org:

(⎈ |default:default)➜  datum git:(feat-delete-org-session) go run cmd/cli/main.go user get -z json  |jq -r '.users.edges[0].node.setting.defaultOrg.name'
funkytown4

Delete Org:

(⎈ |default:default)➜  datum git:(feat-delete-org-session) go run cmd/cli/main.go org delete -i 01J16WMYMMM988WTYVTCDCPHEM                               
{
  "deleteOrganization": {
    "deletedID": "01J16WMYMMM988WTYVTCDCPHEM"
  }
}

After deleting org, the default org is updated:

(⎈ |default:default)➜  datum git:(feat-delete-org-session) go run cmd/cli/main.go user get -z json  |jq -r '.users.edges[0].node.setting.defaultOrg.name'
Organic Katydid

Personal Org Deletion

Attempting to delete a personal org results in an error:

(⎈ |default:default)➜  datum git:(feat-delete-org-session) go run cmd/cli/main.go org delete -i 01J169Y91PJEXBDGCCNTEFBHDM
Error: {"networkErrors":null,"graphqlErrors":[{"message":"invalid input: cannot delete personal organizations","path":["deleteOrganization"]}]}
sonarcloud[bot] commented 1 week ago

Quality Gate Passed Quality Gate passed

Issues
1 New issue
0 Accepted issues

Measures
0 Security Hotspots
25.3% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarCloud