@@ -1217,8 +1217,10 @@ type oauthLoginParams struct {
1217
1217
// to the Groups provided.
1218
1218
UsingGroups bool
1219
1219
CreateMissingGroups bool
1220
- Groups map [uuid.UUID ][]string
1221
- GroupFilter * regexp.Regexp
1220
+ // These are the group names from the IDP. Internally, they will map to
1221
+ // some organization groups.
1222
+ Groups []string
1223
+ GroupFilter * regexp.Regexp
1222
1224
// Is UsingRoles is true, then the user will be assigned
1223
1225
// the roles provided.
1224
1226
UsingRoles bool
@@ -1301,7 +1303,6 @@ func (api *API) oauthLogin(r *http.Request, params *oauthLoginParams) ([]*http.C
1301
1303
link database.UserLink
1302
1304
err error
1303
1305
)
1304
-
1305
1306
user = params .User
1306
1307
link = params .Link
1307
1308
@@ -1457,23 +1458,50 @@ func (api *API) oauthLogin(r *http.Request, params *oauthLoginParams) ([]*http.C
1457
1458
}
1458
1459
1459
1460
// Ensure groups are correct.
1461
+ // This places all groups into the default organization.
1462
+ // To go multi-org, we need to add a mapping feature here to know which
1463
+ // groups go to which orgs.
1460
1464
if params .UsingGroups {
1461
1465
filtered := params .Groups
1462
1466
if params .GroupFilter != nil {
1463
- // For each org, filter the groups.
1464
- for orgID ,groups := range filtered {
1465
- filteredList := make ([]string ,0 ,len (groups ))
1466
- for _ ,group := range groups {
1467
- if params .GroupFilter .MatchString (group ) {
1468
- filteredList = append (filteredList ,group )
1469
- }
1467
+ filtered = make ([]string ,0 ,len (params .Groups ))
1468
+ for _ ,group := range params .Groups {
1469
+ if params .GroupFilter .MatchString (group ) {
1470
+ filtered = append (filtered ,group )
1470
1471
}
1471
- filtered [orgID ]= filteredList
1472
1472
}
1473
1473
}
1474
1474
1475
+ //nolint:gocritic // No user present in the context.
1476
+ defaultOrganization ,err := tx .GetDefaultOrganization (dbauthz .AsSystemRestricted (ctx ))
1477
+ if err != nil {
1478
+ // If there is no default org, then we can't assign groups.
1479
+ // By default, we assume all groups belong to the default org.
1480
+ return xerrors .Errorf ("get default organization: %w" ,err )
1481
+ }
1482
+
1483
+ //nolint:gocritic // No user present in the context.
1484
+ memberships ,err := tx .GetOrganizationMembershipsByUserID (dbauthz .AsSystemRestricted (ctx ),user .ID )
1485
+ if err != nil {
1486
+ return xerrors .Errorf ("get organization memberships: %w" ,err )
1487
+ }
1488
+
1489
+ inDefault := false
1490
+ for _ ,membership := range memberships {
1491
+ if membership .OrganizationID == defaultOrganization .ID {
1492
+ inDefault = true
1493
+ break
1494
+ }
1495
+ }
1496
+
1497
+ if ! inDefault {
1498
+ return xerrors .Errorf ("user %s is not a member of the default organization, cannot assign to groups in the org" ,user .ID )
1499
+ }
1500
+
1475
1501
//nolint:gocritic
1476
- err := api .Options .SetUserGroups (dbauthz .AsSystemRestricted (ctx ),logger ,tx ,user .ID ,filtered ,params .CreateMissingGroups )
1502
+ err = api .Options .SetUserGroups (dbauthz .AsSystemRestricted (ctx ),logger ,tx ,user .ID ,map [uuid.UUID ][]string {
1503
+ defaultOrganization .ID :filtered ,
1504
+ },params .CreateMissingGroups )
1477
1505
if err != nil {
1478
1506
return xerrors .Errorf ("set user groups: %w" ,err )
1479
1507
}