@@ -15,7 +15,9 @@ import {
15
15
OTHER_SETTINGS_USERDEFINED_LLM ,
16
16
} from "./defaults" ;
17
17
18
+ import { checkRequiredSSO } from "@cocalc/util/auth-check-required-sso" ;
18
19
import { DEFAULT_LOCALE } from "@cocalc/util/consts/locale" ;
20
+ import { Strategy } from "@cocalc/util/types/sso" ;
19
21
20
22
Table ( {
21
23
name : "accounts" ,
@@ -399,6 +401,7 @@ Table({
399
401
// obviously min_balance can't be set!
400
402
} ,
401
403
async check_hook ( db , obj , account_id , _project_id , cb ) {
404
+ // db is of type PostgreSQL defined in @cocalc /database/postgres/types
402
405
if ( obj [ "name" ] != null ) {
403
406
// NOTE: there is no way to unset/remove a username after one is set...
404
407
try {
@@ -415,6 +418,7 @@ Table({
415
418
return ;
416
419
}
417
420
}
421
+
418
422
// Hook to truncate some text fields to at most 254 characters, to avoid
419
423
// further trouble down the line.
420
424
for ( const field of [ "first_name" , "last_name" , "email_address" ] ) {
@@ -427,10 +431,38 @@ Table({
427
431
}
428
432
}
429
433
}
430
- // check, if account is exclusively controlled by SSO and its update_on_login config is true
431
- const { email_address} = obj
432
- if ( email_address != null ) {
433
- // TODO
434
+
435
+ // if account is exclusively controlled by SSO, you're maybe prohibited from changing account details
436
+ const current_email_address =
437
+ await db . get_email_address_for_account_id ( account_id ) ;
438
+ console . log ( { current_email_address } ) ;
439
+ if ( typeof current_email_address === "string" ) {
440
+ const strategies : Strategy [ ] = await db . getStrategiesSSO ( ) ;
441
+ const strategy = checkRequiredSSO ( {
442
+ strategies,
443
+ email : current_email_address ,
444
+ } ) ;
445
+ console . log ( { strategy } ) ;
446
+ console . log ( obj ) ;
447
+ // we got a required exclusive SSO for the given account_id
448
+ if ( strategy != null ) {
449
+ // if user tries to change email_address
450
+ if ( typeof obj . email_address === "string" ) {
451
+ cb ( `You are not allowed to change your email address.` ) ;
452
+ return ;
453
+ }
454
+ // ... or tries to change first or last name, but strategy has update_on_login set
455
+ if (
456
+ strategy . updateOnLogin &&
457
+ ( typeof obj . first_name === "string" ||
458
+ obj . last_name === "string" )
459
+ ) {
460
+ cb (
461
+ `You are not allowed to change your first or last name. You have to change it at your single-sign-on provider: ${ strategy . display } .` ,
462
+ ) ;
463
+ return ;
464
+ }
465
+ }
434
466
}
435
467
cb ( ) ;
436
468
} ,
0 commit comments