public static Person PersonFromClaimsIdentity(IAuthenticationManager authenticationManager)
        {
            var claimsPrincipal = authenticationManager.User;

            if (claimsPrincipal?.Identity == null || !claimsPrincipal.Identity.IsAuthenticated || claimsPrincipal.Identity.AuthenticationType != DefaultAuthenticationTypes.ApplicationCookie)
            {
                return(Person.GetAnonymousSitkaUser());
            }

            try
            {
                // This parsing out of depends on the write of data into ClaimTypes.Name
                var personID = int.Parse(claimsPrincipal.Identity.Name);
                var person   = HttpRequestStorage.DatabaseEntities.People.GetPerson(personID);
                Check.Require(person.IsActive, $"Account for {person.Email} is not active.");
                return(person);
            }
            catch (Exception ex)
            {
                IdentitySignOut(authenticationManager);
                throw new SitkaDisplayErrorException("Something went wrong with your session or credentials. Please try logging in again. If this does not resolve the issue, please contact support.", ex);
            }
        }
        public void ProjectProposedTest()
        {
            var editPerformanceMeasureFeature = new ProjectEditAsAdminFeature();
            var viewPerformanceMeasureFeature = new ProjectsInProposalStageViewListFeature();

            // Test organizations we'll use for membership checks
            var testOrganizationForProject  = TestFramework.TestOrganization.Create();
            var testOrganizationJustForUser = TestFramework.TestOrganization.Create();

            Person userAnonymous = Person.GetAnonymousSitkaUser();

            var userNormal = TestFramework.TestPerson.Create();

            userNormal.RoleID = Role.Normal.RoleID;

            var userAdmin = TestFramework.TestPerson.Create();

            userAdmin.RoleID = Role.Admin.RoleID;

            var userSitkaAdmin = TestFramework.TestPerson.Create();

            userSitkaAdmin.RoleID = Role.SitkaAdmin.RoleID;

            // Deferred Project
            // ----------------
            //{
            //    // Deferred Project
            //    var deferredProject = TestFramework.TestProject.Create();
            //    deferredProject.ProjectStageID = ProjectStage.Deferred.ProjectStageID;

            //    // - View PMs -
            //    TestExpectedUserPermission(userAnonymous, viewPerformanceMeasureFeature, false);
            //    TestExpectedUserPermission(userNormal, viewPerformanceMeasureFeature, true);
            //    TestExpectedUserPermission(userAdmin, viewPerformanceMeasureFeature, true);
            //    TestExpectedUserPermission(userSitkaAdmin, viewPerformanceMeasureFeature, true);

            //    // - Edit PMs -
            //    TestExpectedUserPermission(userAnonymous, deferredProject, editPerformanceMeasureFeature, false);
            //    TestExpectedUserPermission(userNormal, deferredProject, editPerformanceMeasureFeature, false);
            //    TestExpectedUserPermission(userAdmin, deferredProject, editPerformanceMeasureFeature, true);
            //    TestExpectedUserPermission(userSitkaAdmin, deferredProject, editPerformanceMeasureFeature, true);

            //    TestExpectedUserPermissionWithUserInLeadImplementingOrg(userNormal, deferredProject, editPerformanceMeasureFeature, testOrganizationForProject, true);
            //    TestExpectedUserPermissionWithUserAsPrimaryContactForImplementingOrg(userNormal,
            //        deferredProject,
            //        editPerformanceMeasureFeature,
            //        testOrganizationJustForUser,
            //        testOrganizationForProject,
            //        true);

            //    TestExpectedUserPermission(userAdmin, deferredProject, editPerformanceMeasureFeature, true);
            //    TestExpectedUserPermission(userSitkaAdmin, deferredProject, editPerformanceMeasureFeature, true);
            //}

            // Planning/Design Project
            // -----------------------
            {
                // Planning / Design Project
                var planningDesignProject = TestFramework.TestProject.Create();
                planningDesignProject.ProjectStageID = ProjectStage.Planned.ProjectStageID;

                // - View PMs -

                TestExpectedUserPermission(userAnonymous, viewPerformanceMeasureFeature, true);
                TestExpectedUserPermission(userNormal, viewPerformanceMeasureFeature, true);
                TestExpectedUserPermission(userAdmin, viewPerformanceMeasureFeature, true);
                TestExpectedUserPermission(userSitkaAdmin, viewPerformanceMeasureFeature, true);

                // - Edit PMs -
                TestExpectedUserPermission(userAnonymous, planningDesignProject, editPerformanceMeasureFeature, false);
                TestExpectedUserPermission(userNormal, planningDesignProject, editPerformanceMeasureFeature, false);
                TestExpectedUserPermission(userAdmin, planningDesignProject, editPerformanceMeasureFeature, true);
                TestExpectedUserPermission(userSitkaAdmin, planningDesignProject, editPerformanceMeasureFeature, true);

                TestExpectedUserPermissionWithUserInLeadImplementingOrg(userNormal, planningDesignProject, editPerformanceMeasureFeature, testOrganizationForProject, true);
                TestExpectedUserPermissionWithUserAsPrimaryContactForImplementingOrg(userNormal,
                                                                                     planningDesignProject,
                                                                                     editPerformanceMeasureFeature,
                                                                                     testOrganizationJustForUser,
                                                                                     testOrganizationForProject,
                                                                                     true);
                TestExpectedUserPermission(userAdmin, planningDesignProject, editPerformanceMeasureFeature, true);
                TestExpectedUserPermission(userSitkaAdmin, planningDesignProject, editPerformanceMeasureFeature, true);
            }

            // Implementation Project
            // -----------------------
            {
                // Implementation Project
                var implementationProject = TestFramework.TestProject.Create();
                implementationProject.ProjectStageID = ProjectStage.Implementation.ProjectStageID;

                // - View PMs -
                TestExpectedUserPermission(userAnonymous, viewPerformanceMeasureFeature, false);
                TestExpectedUserPermission(userNormal, viewPerformanceMeasureFeature, true);
                TestExpectedUserPermission(userAdmin, viewPerformanceMeasureFeature, true);
                TestExpectedUserPermission(userSitkaAdmin, viewPerformanceMeasureFeature, true);

                // - Edit PMs -
                TestExpectedUserPermission(userAnonymous, implementationProject, editPerformanceMeasureFeature, false);
                TestExpectedUserPermission(userNormal, implementationProject, editPerformanceMeasureFeature, false);
                TestExpectedUserPermission(userAdmin, implementationProject, editPerformanceMeasureFeature, true);
                TestExpectedUserPermission(userSitkaAdmin, implementationProject, editPerformanceMeasureFeature, true);

                TestExpectedUserPermissionWithUserInLeadImplementingOrg(userNormal, implementationProject, editPerformanceMeasureFeature, testOrganizationForProject, false);
                TestExpectedUserPermissionWithUserAsPrimaryContactForImplementingOrg(userNormal,
                                                                                     implementationProject,
                                                                                     editPerformanceMeasureFeature,
                                                                                     testOrganizationJustForUser,
                                                                                     testOrganizationForProject,
                                                                                     false);
                TestExpectedUserPermission(userAdmin, implementationProject, editPerformanceMeasureFeature, true);
                TestExpectedUserPermission(userSitkaAdmin, implementationProject, editPerformanceMeasureFeature, true);
            }

            //// Post-Implementation Project
            //// -----------------------
            //{
            //    // Post-Implementation Project
            //    var postImplementationProject = TestFramework.TestProject.Create();
            //    postImplementationProject.ProjectStageID = ProjectStage.PostImplementation.ProjectStageID;

            //    // - View PMs -
            //    TestExpectedUserPermission(userAnonymous, viewPerformanceMeasureFeature, false);
            //    TestExpectedUserPermission(userNormal, viewPerformanceMeasureFeature, true);
            //    TestExpectedUserPermission(userAdmin, viewPerformanceMeasureFeature, true);
            //    TestExpectedUserPermission(userSitkaAdmin, viewPerformanceMeasureFeature, true);

            //    // - Edit PMs -
            //    TestExpectedUserPermission(userAnonymous, postImplementationProject, editPerformanceMeasureFeature, false);
            //    TestExpectedUserPermission(userNormal, postImplementationProject, editPerformanceMeasureFeature, false);
            //    TestExpectedUserPermission(userAdmin, postImplementationProject, editPerformanceMeasureFeature, true);
            //    TestExpectedUserPermission(userSitkaAdmin, postImplementationProject, editPerformanceMeasureFeature, true);

            //    TestExpectedUserPermissionWithUserInLeadImplementingOrg(userNormal, postImplementationProject, editPerformanceMeasureFeature, testOrganizationForProject, false);
            //    TestExpectedUserPermissionWithUserAsPrimaryContactForImplementingOrg(userNormal,
            //        postImplementationProject,
            //        editPerformanceMeasureFeature,
            //        testOrganizationJustForUser,
            //        testOrganizationForProject,
            //        false);
            //    TestExpectedUserPermission(userAdmin, postImplementationProject, editPerformanceMeasureFeature, true);
            //    TestExpectedUserPermission(userSitkaAdmin, postImplementationProject, editPerformanceMeasureFeature, true);
            //}

            // Completed Project
            // -----------------------
            {
                // Completed Project
                var completedProject = TestFramework.TestProject.Create();
                completedProject.ProjectStageID = ProjectStage.Completed.ProjectStageID;

                // - View PMs -
                TestExpectedUserPermission(userAnonymous, viewPerformanceMeasureFeature, false);
                TestExpectedUserPermission(userNormal, viewPerformanceMeasureFeature, true);
                TestExpectedUserPermission(userAdmin, viewPerformanceMeasureFeature, true);
                TestExpectedUserPermission(userSitkaAdmin, viewPerformanceMeasureFeature, true);

                // - Edit PMs -
                TestExpectedUserPermission(userAnonymous, completedProject, editPerformanceMeasureFeature, false);
                TestExpectedUserPermission(userNormal, completedProject, editPerformanceMeasureFeature, false);
                TestExpectedUserPermission(userAdmin, completedProject, editPerformanceMeasureFeature, true);
                TestExpectedUserPermission(userSitkaAdmin, completedProject, editPerformanceMeasureFeature, true);

                TestExpectedUserPermissionWithUserInLeadImplementingOrg(userNormal, completedProject, editPerformanceMeasureFeature, testOrganizationForProject, false);
                TestExpectedUserPermissionWithUserAsPrimaryContactForImplementingOrg(userNormal,
                                                                                     completedProject,
                                                                                     editPerformanceMeasureFeature,
                                                                                     testOrganizationJustForUser,
                                                                                     testOrganizationForProject,
                                                                                     false);
                TestExpectedUserPermission(userAdmin, completedProject, editPerformanceMeasureFeature, true);
                TestExpectedUserPermission(userSitkaAdmin, completedProject, editPerformanceMeasureFeature, true);
            }

            // Terminated Project
            // -----------------------
            {
                // Terminated Project
                var terminatedProject = TestFramework.TestProject.Create();
                terminatedProject.ProjectStageID = ProjectStage.Cancelled.ProjectStageID;

                // - View PMs -
                TestExpectedUserPermission(userAnonymous, viewPerformanceMeasureFeature, false);
                TestExpectedUserPermission(userNormal, viewPerformanceMeasureFeature, true);
                TestExpectedUserPermission(userAdmin, viewPerformanceMeasureFeature, true);
                TestExpectedUserPermission(userSitkaAdmin, viewPerformanceMeasureFeature, true);

                // - Edit PMs -
                TestExpectedUserPermission(userAnonymous, terminatedProject, editPerformanceMeasureFeature, false);
                TestExpectedUserPermission(userNormal, terminatedProject, editPerformanceMeasureFeature, false);
                TestExpectedUserPermission(userAdmin, terminatedProject, editPerformanceMeasureFeature, true);
                TestExpectedUserPermission(userSitkaAdmin, terminatedProject, editPerformanceMeasureFeature, true);

                TestExpectedUserPermissionWithUserInLeadImplementingOrg(userNormal, terminatedProject, editPerformanceMeasureFeature, testOrganizationForProject, false);
                TestExpectedUserPermissionWithUserAsPrimaryContactForImplementingOrg(userNormal,
                                                                                     terminatedProject,
                                                                                     editPerformanceMeasureFeature,
                                                                                     testOrganizationJustForUser,
                                                                                     testOrganizationForProject,
                                                                                     false);
                TestExpectedUserPermission(userAdmin, terminatedProject, editPerformanceMeasureFeature, true);
                TestExpectedUserPermission(userSitkaAdmin, terminatedProject, editPerformanceMeasureFeature, true);
            }
        }
 public static void IdentitySignOut(IAuthenticationManager authenticationManager)
 {
     authenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie, DefaultAuthenticationTypes.ExternalCookie);
     HttpContext.Current.Request.Cookies.Remove(AuthenticationApplicationCookieName);
     HttpRequestStorage.Person = Person.GetAnonymousSitkaUser();
 }