Example #1
0
        public async Task <InvokeResult> SetUserOrgAsync(AuthRequest authRequest, AppUser appUser)
        {
            // Synthesize the org and user from request and app user
            var org = new EntityHeader()
            {
                Id = authRequest.OrgId, Text = authRequest.OrgName
            };
            var user = new EntityHeader()
            {
                Id = appUser.Id, Text = $"{appUser.FirstName} {appUser.LastName}"
            };

            authRequest.OrgId   = org.Id;
            authRequest.OrgName = org.Text;

            // 1) Ensure user has access to the requested org.
            var orgs = await _orgManager.GetOrganizationsForUserAsync(appUser.Id, org, user);

            var switchToOrg = orgs.Where(o => o.OrgId == authRequest.OrgId).FirstOrDefault();

            if (switchToOrg == null)
            {
                _adminLogger.AddCustomEvent(Core.PlatformSupport.LogLevel.Error, "AuthTokenManager_SetOrg", UserAdminErrorCodes.AuthOrgNotAuthorized.Message,
                                            new KeyValuePair <string, string>("userid", appUser.Id),
                                            new KeyValuePair <string, string>("requestedOrgId", authRequest.OrgId));
                return(InvokeResult.FromErrors(UserAdminErrorCodes.AuthOrgNotAuthorized.ToErrorMessage()));
            }

            var oldOrgId   = EntityHeader.IsNullOrEmpty(appUser.CurrentOrganization) ? "none" : appUser.CurrentOrganization.Id;
            var oldOrgName = EntityHeader.IsNullOrEmpty(appUser.CurrentOrganization) ? "none" : appUser.CurrentOrganization.Text;

            // 2) Change the org on the user object
            appUser.CurrentOrganization = new EntityHeader()
            {
                Id   = authRequest.OrgId,
                Text = switchToOrg.OrganizationName,
            };

            appUser.IsOrgAdmin = switchToOrg.IsOrgAdmin;

            // 3) Add the roles to the user for the org.
            var orgRoles = await _orgManager.GetUsersRolesInOrgAsync(authRequest.OrgId, appUser.Id, appUser.CurrentOrganization, appUser.ToEntityHeader());

            appUser.CurrentOrganizationRoles = new List <EntityHeader>();
            foreach (var orgRole in orgRoles)
            {
                appUser.CurrentOrganizationRoles.Add(orgRole.ToEntityHeader());
            }

            // 4) Write the updated user back to storage.
            var updateResult = await _userManager.UpdateAsync(appUser);

            if (!updateResult.Successful)
            {
                var invokeResult = updateResult.ToInvokeResult();

                _adminLogger.LogInvokeResult("OrgHelper_SetUserOrgAsync", invokeResult,
                                             new KeyValuePair <string, string>("userId", appUser.Id),
                                             new KeyValuePair <string, string>("userName", appUser.UserName));

                return(invokeResult);
            }

            // 5) Write this change to logger.
            _adminLogger.AddCustomEvent(Core.PlatformSupport.LogLevel.Message, "AuthTokenManager_SetOrg", "UserSwitchedOrg",
                                        new KeyValuePair <string, string>("userId", appUser.Id),
                                        new KeyValuePair <string, string>("userName", appUser.UserName),
                                        new KeyValuePair <string, string>("oldOrgId", oldOrgId),
                                        new KeyValuePair <string, string>("oldOrgName", oldOrgName),
                                        new KeyValuePair <string, string>("newOrgId", appUser.CurrentOrganization.Id),
                                        new KeyValuePair <string, string>("newOrgName", appUser.CurrentOrganization.Text));

            // 6) Return success, no response data necessary, app user is by reference so it should already be updated.
            return(InvokeResult.Success);
        }
        public async Task <ListResponse <OrgUser> > GetOrgsForUserAsync()
        {
            var orgsForUser = await _orgManager.GetOrganizationsForUserAsync(UserEntityHeader.Id, OrgEntityHeader, UserEntityHeader);

            return(ListResponse <OrgUser> .Create(orgsForUser));
        }