2424using Microting . TimePlanningBase . Infrastructure . Data . Entities ;
2525using Rebus . Bus ;
2626using Microsoft . Extensions . Logging ;
27+ using Microting . EformAngularFrontendBase . Infrastructure . Data ;
28+ using Microting . EformAngularFrontendBase . Infrastructure . Data . Entities . Permissions ;
2729using Microting . eFormApi . BasePn . Infrastructure . Database . Entities ;
2830
2931namespace BackendConfiguration . Pn . Infrastructure . Helpers ;
@@ -259,6 +261,7 @@ public static async Task<OperationResult> UpdateDeviceUser(DeviceUserModel devic
259261 UserManager < EformUser > userManager ,
260262 BackendConfigurationPnDbContext backendConfigurationPnDbContext ,
261263 TimePlanningPnDbContext timePlanningDbContext ,
264+ BaseDbContext baseDbContext ,
262265 ILogger logger ,
263266 ItemsPlanningPnDbContext itemsPlanningPnDbContext )
264267 {
@@ -361,6 +364,32 @@ public static async Task<OperationResult> UpdateDeviceUser(DeviceUserModel devic
361364 user . Locale = language . LanguageCode ;
362365 var result = await userManager . UpdateAsync ( user ) ;
363366 }
367+ else
368+ {
369+ if ( deviceUserModel . TimeRegistrationEnabled != null && ( ( bool ) deviceUserModel . TimeRegistrationEnabled || deviceUserModel . ArchiveEnabled ||
370+ deviceUserModel . WebAccessEnabled ) )
371+ {
372+ user = new EformUser
373+ {
374+ Email = deviceUserModel . WorkerEmail ,
375+ UserName = deviceUserModel . WorkerEmail ,
376+ FirstName = deviceUserModel . UserFirstName . Trim ( ) ,
377+ LastName = deviceUserModel . UserLastName . Trim ( ) ,
378+ Locale = deviceUserModel . LanguageCode ,
379+ EmailConfirmed = true ,
380+ TwoFactorEnabled = false ,
381+ IsGoogleAuthenticatorEnabled = false ,
382+ TimeZone = "Europe/Copenhagen" ,
383+ Formats = "de-DE"
384+ } ;
385+
386+ var result = await userManager . CreateAsync ( user , "replace_me_with_a_proper_password_2024!" ) . ConfigureAwait ( false ) ;
387+ if ( result . Succeeded )
388+ {
389+ await userManager . AddToRoleAsync ( user , EformRole . User ) ;
390+ }
391+ }
392+ }
364393
365394 if ( isUpdated )
366395 {
@@ -459,7 +488,66 @@ await core.EntityItemUpdate(entityItem.Id, entityItem.Name, entityItem.Descripti
459488 await planningCaseSite . Update ( itemsPlanningPnDbContext ) . ConfigureAwait ( false ) ;
460489 }
461490
462- //var siteId = await sdkDbContext.Sites.Where(x => x.MicrotingUid == siteDto.SiteId).Select(x => x.Id).FirstAsync();
491+ var securityGroupUserWebAccess = await baseDbContext . SecurityGroupUsers
492+ . Include ( x => x . SecurityGroup )
493+ . Where ( x => x . EformUserId == user ! . Id )
494+ . Where ( x => x . SecurityGroup . Name == "eForm users" )
495+ . Where ( x => x . WorkflowState != Constants . WorkflowStates . Removed )
496+ . FirstOrDefaultAsync ( ) . ConfigureAwait ( false ) ;
497+ if ( deviceUserModel . WebAccessEnabled == false && securityGroupUserWebAccess != null )
498+ {
499+ var forDelete = await baseDbContext . SecurityGroupUsers . FirstAsync ( x => x . Id == securityGroupUserWebAccess . Id ) ;
500+ baseDbContext . SecurityGroupUsers . RemoveRange ( forDelete ) ;
501+ await baseDbContext . SaveChangesAsync ( ) . ConfigureAwait ( false ) ;
502+
503+ }
504+ if ( deviceUserModel . WebAccessEnabled && securityGroupUserWebAccess == null )
505+ {
506+ var newSecurityGroupUser = new SecurityGroupUser
507+ {
508+ EformUserId = user ! . Id ,
509+ SecurityGroupId = baseDbContext . SecurityGroups
510+ . Where ( x => x . Name == "eForm users" )
511+ . Select ( x => x . Id )
512+ . First ( )
513+ } ;
514+ baseDbContext . SecurityGroupUsers . Add ( newSecurityGroupUser ) ;
515+ await baseDbContext . SaveChangesAsync ( ) . ConfigureAwait ( false ) ;
516+ }
517+
518+ var securityGroupUserArchive = await baseDbContext . SecurityGroupUsers
519+ . Include ( x => x . SecurityGroup )
520+ . Where ( x => x . EformUserId == user ! . Id )
521+ . Where ( x => x . SecurityGroup . Name == "Kun arkiv" )
522+ . Where ( x => x . WorkflowState != Constants . WorkflowStates . Removed )
523+ . FirstOrDefaultAsync ( ) . ConfigureAwait ( false ) ;
524+ if ( deviceUserModel . ArchiveEnabled == false && securityGroupUserArchive != null )
525+ {
526+ var forDelete = await baseDbContext . SecurityGroupUsers . FirstAsync ( x => x . Id == securityGroupUserArchive . Id ) ;
527+ baseDbContext . SecurityGroupUsers . RemoveRange ( forDelete ) ;
528+ await baseDbContext . SaveChangesAsync ( ) . ConfigureAwait ( false ) ;
529+
530+ }
531+ if ( deviceUserModel . ArchiveEnabled && securityGroupUserArchive == null )
532+ {
533+ var newSecurityGroupUser = new SecurityGroupUser
534+ {
535+ EformUserId = user ! . Id ,
536+ SecurityGroupId = baseDbContext . SecurityGroups
537+ . Where ( x => x . Name == "Kun arkiv" )
538+ . Select ( x => x . Id )
539+ . First ( )
540+ } ;
541+ baseDbContext . SecurityGroupUsers . Add ( newSecurityGroupUser ) ;
542+ await baseDbContext . SaveChangesAsync ( ) . ConfigureAwait ( false ) ;
543+ }
544+
545+ var securityGroupUserTime = await baseDbContext . SecurityGroupUsers
546+ . Include ( x => x . SecurityGroup )
547+ . Where ( x => x . EformUserId == user ! . Id )
548+ . Where ( x => x . SecurityGroup . Name == "Kun tid" )
549+ . Where ( x => x . WorkflowState != Constants . WorkflowStates . Removed )
550+ . FirstOrDefaultAsync ( ) . ConfigureAwait ( false ) ;
463551 if ( deviceUserModel . TimeRegistrationEnabled == false && timePlanningDbContext . AssignedSites . Any ( x => x . SiteId == siteDto . SiteId && x . WorkflowState != Constants . WorkflowStates . Removed ) )
464552 {
465553 var assignmentForDeletes = await timePlanningDbContext . AssignedSites . Where ( x =>
@@ -469,11 +557,33 @@ await core.EntityItemUpdate(entityItem.Id, entityItem.Name, entityItem.Descripti
469557 {
470558 await assignmentForDelete . Delete ( timePlanningDbContext ) . ConfigureAwait ( false ) ;
471559 }
560+
561+ if ( securityGroupUserTime != null )
562+ {
563+ var forDelete = await baseDbContext . SecurityGroupUsers . FirstAsync ( x => x . Id == securityGroupUserTime . Id ) ;
564+ baseDbContext . SecurityGroupUsers . RemoveRange ( forDelete ) ;
565+ await baseDbContext . SaveChangesAsync ( ) . ConfigureAwait ( false ) ;
566+ }
472567 }
473568 else
474569 {
475570 if ( deviceUserModel . TimeRegistrationEnabled == true )
476571 {
572+
573+
574+ if ( securityGroupUserTime == null )
575+ {
576+ var newSecurityGroupUser = new SecurityGroupUser
577+ {
578+ EformUserId = user ! . Id ,
579+ SecurityGroupId = baseDbContext . SecurityGroups
580+ . Where ( x => x . Name == "Kun tid" )
581+ . Select ( x => x . Id )
582+ . First ( )
583+ } ;
584+ baseDbContext . SecurityGroupUsers . Add ( newSecurityGroupUser ) ;
585+ await baseDbContext . SaveChangesAsync ( ) . ConfigureAwait ( false ) ;
586+ }
477587 var assignments = await timePlanningDbContext . AssignedSites . Where ( x =>
478588 x . SiteId == siteDto . SiteId && x . WorkflowState != Constants . WorkflowStates . Removed ) . ToListAsync ( ) . ConfigureAwait ( false ) ;
479589
0 commit comments