@@ -578,42 +578,69 @@ func (a *Auth) CreateAdminAccount(authenticationType string, appID string, orgID
578
578
}
579
579
580
580
// create account
581
- var accountAuthType * model.AccountAuthType
582
581
var newAccount * model.Account
583
582
var params map [string ]interface {}
583
+
584
584
transaction := func (context storage.TransactionContext ) error {
585
- //1. check if the user exists
586
- account , err := a .storage .FindAccount ( context , appOrg .ID , authType .ID , identifier )
585
+ //find the account for the org and the user identity
586
+ foundedAccount , err := a .storage .FindAccountByOrgAndIdentifier ( nil , appOrg .Organization . ID , authType .ID , identifier , appOrg . ID )
587
587
if err != nil {
588
588
return errors .WrapErrorAction (logutils .ActionFind , model .TypeAccount , nil , err )
589
589
}
590
- if account != nil {
591
- return errors .ErrorData (logutils .StatusFound , model .TypeAccount , & logutils.FieldArgs {"app_org_id" : appOrg .ID , "auth_type" : authType .Code , "identifier" : identifier })
590
+
591
+ //check if the account exists for this app
592
+ if foundedAccount != nil && foundedAccount .HasApp (appID ) {
593
+ return errors .Newf ("there is already account for %s in %s application" , identifier , appID )
592
594
}
593
595
594
- //2. account does not exist, so apply sign up
595
- profile .DateCreated = time .Now ().UTC ()
596
- if authType .IsExternal {
597
- externalUser := model.ExternalSystemUser {Identifier : identifier }
598
- accountAuthType , err = a .applySignUpAdminExternal (context , * authType , * appOrg , externalUser , profile , privacy , username , permissions , roleIDs , groupIDs , scopes , creatorPermissions , clientVersion , l )
599
- if err != nil {
600
- return errors .WrapErrorAction (logutils .ActionRegister , "admin user" , & logutils.FieldArgs {"auth_type" : authType .Code , "identifier" : identifier }, err )
601
- }
602
- } else {
603
- authImpl , err := a .getAuthTypeImpl (* authType )
596
+ //determine operation - "org-sign-up" or "app-sign-up"
597
+ operation , err := a .determineOperation (foundedAccount , appOrg .ID , l )
598
+ if err != nil {
599
+ return errors .WrapErrorAction (logutils .ActionFind , model .TypeAccount , nil , err )
600
+ }
601
+
602
+ //apply operation
603
+ switch operation {
604
+ case "app-sign-up" :
605
+ // account exists in the organization but not for the application
606
+
607
+ udatedAccount , err := a .appSignUp (context , * foundedAccount , * appOrg , permissions , roleIDs , groupIDs , clientVersion , creatorPermissions , l )
604
608
if err != nil {
605
- return errors .WrapErrorAction (logutils . ActionLoadCache , typeExternalAuthType , nil , err )
609
+ return errors .WrapErrorAction ("app sign up" , "" , nil , err )
606
610
}
607
611
608
- profile .Email = identifier
609
- params , accountAuthType , err = a .applySignUpAdmin (context , authImpl , account , * authType , * appOrg , identifier , "" , profile , privacy , username , permissions , roleIDs , groupIDs , scopes , creatorPermissions , clientVersion , l )
610
- if err != nil {
611
- return errors .WrapErrorAction (logutils .ActionRegister , "admin user" , & logutils.FieldArgs {"auth_type" : authType .Code , "identifier" : identifier }, err )
612
+ newAccount = udatedAccount
613
+ return nil
614
+ case "org-sign-up" :
615
+ // account does not exist in the organization
616
+
617
+ var accountAuthType * model.AccountAuthType
618
+
619
+ profile .DateCreated = time .Now ().UTC ()
620
+ if authType .IsExternal {
621
+ externalUser := model.ExternalSystemUser {Identifier : identifier }
622
+ accountAuthType , err = a .applySignUpAdminExternal (context , * authType , * appOrg , externalUser , profile , privacy , username , permissions , roleIDs , groupIDs , scopes , creatorPermissions , clientVersion , l )
623
+ if err != nil {
624
+ return errors .WrapErrorAction (logutils .ActionRegister , "admin user" , & logutils.FieldArgs {"auth_type" : authType .Code , "identifier" : identifier }, err )
625
+ }
626
+ } else {
627
+ authImpl , err := a .getAuthTypeImpl (* authType )
628
+ if err != nil {
629
+ return errors .WrapErrorAction (logutils .ActionLoadCache , typeExternalAuthType , nil , err )
630
+ }
631
+
632
+ profile .Email = identifier
633
+ params , accountAuthType , err = a .applySignUpAdmin (context , authImpl , * authType , * appOrg , identifier , "" , profile , privacy , username , permissions , roleIDs , groupIDs , scopes , creatorPermissions , clientVersion , l )
634
+ if err != nil {
635
+ return errors .WrapErrorAction (logutils .ActionRegister , "admin user" , & logutils.FieldArgs {"auth_type" : authType .Code , "identifier" : identifier }, err )
636
+ }
612
637
}
638
+
639
+ newAccount = & accountAuthType .Account
640
+ return nil
613
641
}
614
642
615
- newAccount = & accountAuthType .Account
616
- return nil
643
+ return errors .Newf ("not supported operation - create account via admin API" )
617
644
}
618
645
619
646
err = a .storage .PerformTransaction (transaction )
@@ -630,7 +657,10 @@ func (a *Auth) UpdateAdminAccount(authenticationType string, appID string, orgID
630
657
//TODO: when elevating existing accounts to application level admin, need to enforce any authentication policies set up for the app org
631
658
// when demoting from application level admin to standard user, may want to inform user of applicable authentication policy changes
632
659
633
- if authenticationType != AuthTypeOidc && authenticationType != AuthTypeEmail && ! strings .HasSuffix (authenticationType , "_oidc" ) {
660
+ if authenticationType != AuthTypeOidc &&
661
+ authenticationType != AuthTypeEmail &&
662
+ ! strings .HasSuffix (authenticationType , "_oidc" ) &&
663
+ authenticationType != AuthTypeTwilioPhone { //Once we have tenants accounts this means that phone is also valid
634
664
return nil , nil , errors .ErrorData (logutils .StatusInvalid , "auth type" , nil )
635
665
}
636
666
@@ -1779,7 +1809,7 @@ func (a *Auth) InitializeSystemAccount(context storage.TransactionContext, authT
1779
1809
privacy := model.Privacy {Public : false }
1780
1810
permissions := []string {allSystemPermission }
1781
1811
1782
- _ , accountAuthType , err := a .applySignUpAdmin (context , authImpl , nil , authType , appOrg , email , password , profile , privacy , "" , permissions , nil , nil , nil , permissions , & clientVersion , l )
1812
+ _ , accountAuthType , err := a .applySignUpAdmin (context , authImpl , authType , appOrg , email , password , profile , privacy , "" , permissions , nil , nil , nil , permissions , & clientVersion , l )
1783
1813
if err != nil {
1784
1814
return "" , errors .WrapErrorAction (logutils .ActionRegister , "initial system user" , & logutils.FieldArgs {"email" : email }, err )
1785
1815
}
0 commit comments