Exemplo n.º 1
0
        public void NavigationCanReadWrite()
        {
            IMenuDefinition[] menus;
            ISubMenuDefinition[] subMenus;
            var navigationService = new StubNavigationService();
            IServiceBus serviceBus;

            InitializeSecurityContext(navigationService, out menus, out subMenus, out serviceBus);

            var projectManagerService = (StubProjectManagerService)serviceBus.Get<IProjectManagerService>();

            var root = XDocument.Load(Path.Combine(TestContext.DeploymentDirectory, "RolesAccessTestsData.xml")).Root;

            foreach (var testcase in root.Elements("TestCase"))
            {
                var menuCode = testcase.Attribute("Menu").Value;
                var subMenuCode = testcase.Attribute("SubMenu").Value;
                var userRoles = testcase.Element("User").Elements("Role").Select(r => r.Value).ToArray();
                var userProjectRoles = testcase.Element("Project").Elements("Role").Select(r => r.Value).ToArray();
                var expectedIsEnabled = bool.Parse(testcase.Element("IsEnabled").Value);
                var expectedCanRead = bool.Parse(testcase.Element("CanRead").Value);
                var expectedCanWrite = bool.Parse(testcase.Element("CanWrite").Value);
                var hasRealizedLockedScenario = bool.Parse(testcase.Element("Project").Element("HasRealizedLockedScenario").Value);

                var subMenuDefinition = subMenus.First(smd => smd.Code == subMenuCode && smd.ParentCode == menuCode);

                var modelUser = new KProcess.Ksmed.Models.User
                {
                    Username = "******",
                };
                modelUser.RoleCodes.AddRange(userRoles);

                Security.SecurityContext.CurrentUser = new Security.User(modelUser);

                navigationService.IsProjectSecurityContext = subMenuDefinition.IsSecurityProjectContext;
                navigationService.ProjectRoles = userProjectRoles;

                projectManagerService.HasRealizedLockedScenario = hasRealizedLockedScenario;

                bool hasTryShowBeenCalled = false;

                Assert.AreEqual(expectedIsEnabled, subMenuDefinition.IsEnabledDelegate());

                navigationService.TryShowCallback = () =>
                {
                    hasTryShowBeenCalled = true;

                    Assert.AreEqual(expectedCanRead, navigationService.CanRead, string.Format("Expected CanRead faux : \r\n{0}", testcase.ToString()));
                    Assert.AreEqual(expectedCanWrite, navigationService.CanWrite, string.Format("Expected CanWrite faux : \r\n{0}", testcase.ToString()));
                };

                subMenuDefinition.Action(serviceBus);

                Assert.IsTrue(hasTryShowBeenCalled);

            }
        }
Exemplo n.º 2
0
        public override async Task OnActionExecutingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
        {
            if (actionContext.Request.GetDependencyScope().GetService(typeof(ISecurityContext)) is ISecurityContext securityContext)
            {
                IPrincipal     principal = actionContext.RequestContext.Principal;
                ClaimsIdentity identity  = principal?.Identity as ClaimsIdentity;
                string         username  = identity?.Name;
                if (string.IsNullOrEmpty(username))
                {
                    return;
                }

                var authenticationService = actionContext.Request.GetDependencyScope().GetService(typeof(IAuthenticationService)) as IAuthenticationService;
                var traceManager          = actionContext.Request.GetDependencyScope().GetService(typeof(ITraceManager)) as ITraceManager;

                KProcess.Ksmed.Models.User user = await authenticationService.GetUser(username);

                securityContext.CurrentUser = new SecurityUser(user);
            }
        }