diff --git a/src/Controllers/HomeController.cs b/src/Controllers/HomeController.cs index 43751e5b..c1728512 100644 --- a/src/Controllers/HomeController.cs +++ b/src/Controllers/HomeController.cs @@ -31,6 +31,7 @@ public class HomeController : Controller private readonly IUserProfiles _userProfileService; private readonly IAuthentication _authenticationService; private readonly IApplicationRepository _applicationRepository; + private readonly IParties _partiesService; private readonly IClaims _claimsService; private readonly ILocalApp _localApp; private readonly TestDataService _testDataService; @@ -41,6 +42,7 @@ public HomeController( IUserProfiles userProfileService, IAuthentication authenticationService, IApplicationRepository applicationRepository, + IParties partiesService, IClaims claimsService, ILocalApp localApp, TestDataService testDataService) @@ -50,6 +52,7 @@ public HomeController( _userProfileService = userProfileService; _authenticationService = authenticationService; _applicationRepository = applicationRepository; + _partiesService = partiesService; _claimsService = claimsService; _localApp = localApp; _testDataService = testDataService; @@ -144,7 +147,7 @@ public async Task LogInTestUser(StartAppModel startAppModel) int authenticationLevel = Convert.ToInt32(startAppModel.AuthenticationLevel); string token = await _authenticationService.GenerateTokenForProfile(profile, authenticationLevel); - CreateJwtCookieAndAppendToResponse(token); + CreateJwtCookieAndAppendToResponse(token, startAppModel.PartyId); } if (startAppModel.AppPathSelection?.Equals("accessmanagement") == true) @@ -157,33 +160,26 @@ public async Task LogInTestUser(StartAppModel startAppModel) // Ensure that the documentstorage in LocalTestingStorageBasePath is updated with the most recent app data await _applicationRepository.Update(app); - if(_localPlatformSettings.LocalAppMode == "http") + if (_localPlatformSettings.LocalAppMode == "http") { // Instantiate a prefill if a file attachment exists. var prefill = Request.Form.Files.FirstOrDefault(); if (prefill != null) { - var instance = new Instance{ + var instance = new Instance + { AppId = app.Id, Org = app.Org, - InstanceOwner = new(), - DataValues = new(), + InstanceOwner = new() + { + PartyId = startAppModel.PartyId.ToString(), + }, + DataValues = new() + { + { "PrefillFilename", prefill.FileName } + }, }; - var owner = prefill.FileName.Split(".")[0]; - if (owner.Length == 9) - { - instance.InstanceOwner.OrganisationNumber = owner; - } - else if (owner.Length == 12) - { - instance.InstanceOwner.PersonNumber = owner; - } - else - { - throw new Exception($"instance owner must be specified as part of the prefill filename. 9 digigts for OrganisationNumber, 12 for PersonNumber (eg 897069631.xml, not {prefill.FileName})"); - } - var xmlDataId = app.DataTypes.First(dt => dt.AppLogic is not null).Id; using var reader = new StreamReader(prefill.OpenReadStream()); @@ -303,18 +299,41 @@ public ActionResult FrontendVersion(FrontendVersion frontendVersion) private async Task> GetTestUsersForList() { var data = await _testDataService.GetTestData(); - List userItems = new List(); + var userItems = new List(); foreach (UserProfile profile in data.Profile.User.Values) { var properProfile = await _userProfileService.GetUser(profile.UserId); - SelectListItem item = new SelectListItem() + + var group = new SelectListGroup() { - Value = properProfile.UserId.ToString(), - Text = properProfile.Party.Person.Name + Name = properProfile.Party.Person.Name, }; + var userParties = await _partiesService.GetParties(properProfile.UserId); - userItems.Add(item); + if (userParties.Count == 1) + { + // Don't add singe party users to a group + var party = userParties.First(); + userItems.Add(new() + { + Value = properProfile.UserId + "." + party.PartyId, + Text = party.Name, + }); + } + else + { + // When a user represents multiple parties, add it to a group, so that it stands out visually + foreach (var party in userParties) + { + userItems.Add(new() + { + Value = properProfile.UserId + "." + party.PartyId, + Text = $"{party.Name} ({party.PartyTypeName})", + Group = group, + }); + } + } } return userItems; @@ -403,8 +422,11 @@ private static SelectListItem GetSelectItem(Application app, string path) /// Creates a session cookie meant to be used to hold the generated JSON Web Token and appends it to the response. /// /// The cookie value. - private void CreateJwtCookieAndAppendToResponse(string cookieValue) + private void CreateJwtCookieAndAppendToResponse(string identityCookie, int altinnPartyId) { + ICookieManager cookieManager = new ChunkingCookieManager(); + + // Add cookie proving the users identity CookieBuilder cookieBuilder = new RequestPathBaseCookieBuilder { Name = "AltinnStudioRuntime", @@ -415,15 +437,30 @@ private void CreateJwtCookieAndAppendToResponse(string cookieValue) Domain = _generalSettings.Hostname, Expiration = new TimeSpan(0, 1337, 0) }; - CookieOptions cookieOptions = cookieBuilder.Build(HttpContext); - - ICookieManager cookieManager = new ChunkingCookieManager(); cookieManager.AppendResponseCookie( HttpContext, cookieBuilder.Name, - cookieValue, + identityCookie, cookieOptions); + + // Add cookie about users prefered party (for creating new instances) + CookieBuilder partyCookieBuilder = new RequestPathBaseCookieBuilder + { + Name = "AltinnPartyId", + SameSite = SameSiteMode.Lax, + HttpOnly = false, + SecurePolicy = CookieSecurePolicy.None, + IsEssential = true, + Domain = _generalSettings.Hostname, + Expiration = new TimeSpan(0, 1337, 0) + }; + CookieOptions partyCookieOptions = cookieBuilder.Build(HttpContext); + cookieManager.AppendResponseCookie( + HttpContext, + partyCookieBuilder.Name, + altinnPartyId.ToString(), + partyCookieOptions); } } } diff --git a/src/Models/StartAppModel.cs b/src/Models/StartAppModel.cs index 2f40c3c9..cf16c2b0 100644 --- a/src/Models/StartAppModel.cs +++ b/src/Models/StartAppModel.cs @@ -55,9 +55,19 @@ public class StartAppModel public HttpRequestException HttpException { get; set; } /// - /// Selected userId + /// Selected User and party separated by "." /// - public int UserId { get; set; } + public string UserSelect { get; set; } + + /// + /// The userId part of + /// + public int UserId => int.TryParse(UserSelect?.Split(".").First(), out int result) ? result : 0; + + /// + /// The partyId part of + /// + public int PartyId => int.TryParse(UserSelect?.Split(".").Last(), out int result) ? result : 0; /// /// Path for the selected app diff --git a/src/Views/Home/Index.cshtml b/src/Views/Home/Index.cshtml index f10b5a9c..27c63280 100644 --- a/src/Views/Home/Index.cshtml +++ b/src/Views/Home/Index.cshtml @@ -71,7 +71,7 @@ @Html.AntiForgeryToken();
- @Html.DropDownListFor(model => model.UserId, Model.TestUsers, new { Class = "form-control" }) + @Html.DropDownListFor(model => model.UserSelect, Model.TestUsers, new { Class = "form-control" })
@if(!Model.AppModeIsHttp) { @@ -87,7 +87,7 @@ @if(Model.AppModeIsHttp) {
- +
}