Esempio n. 1
0
        public virtual void SeedPrivilegesTree(RoleView view)
        {
            JsTreeNode rootNode = new JsTreeNode(Titles.All);
            view.PrivilegesTree.Nodes.Add(rootNode);

            IEnumerable<Privilege> privileges = GetAllPrivileges();
            foreach (IGrouping<String, Privilege> area in privileges.GroupBy(privilege => privilege.Area))
            {
                JsTreeNode areaNode = new JsTreeNode(area.Key);
                foreach (IGrouping<String, Privilege> controller in area.GroupBy(privilege => privilege.Controller).OrderBy(privilege => privilege.Key))
                {
                    JsTreeNode controllerNode = new JsTreeNode(controller.Key);
                    foreach (IGrouping<String, Privilege> action in controller.GroupBy(privilege => privilege.Action).OrderBy(privilege => privilege.Key))
                        controllerNode.Nodes.Add(new JsTreeNode(action.First().Id, action.Key));

                    if (areaNode.Title == null)
                        rootNode.Nodes.Add(controllerNode);
                    else
                        areaNode.Nodes.Add(controllerNode);
                }

                if (areaNode.Title != null)
                    rootNode.Nodes.Add(areaNode);
            }
        }
Esempio n. 2
0
        public Boolean CanEdit(RoleView view)
        {
            Boolean isValid = ModelState.IsValid;
            isValid &= IsUniqueRole(view);

            return isValid;
        }
Esempio n. 3
0
        public void Create(RoleView view)
        {
            CreateRole(view);
            CreateRolePrivileges(view);

            UnitOfWork.Commit();
        }
Esempio n. 4
0
        public void RoleView_CreatesEmpty()
        {
            JsTree actual = new RoleView().PrivilegesTree;

            Assert.Empty(actual.SelectedIds);
            Assert.Empty(actual.Nodes);
        }
Esempio n. 5
0
        public void Edit(RoleView view)
        {
            DeleteRolePrivileges(view.Id);
            CreateRolePrivileges(view);
            EditRole(view);

            UnitOfWork.Commit();

            Authorization.Provider.Refresh();
        }
Esempio n. 6
0
        public RolesControllerTests()
        {
            validator = Substitute.For<IRoleValidator>();
            service = Substitute.For<IRoleService>();

            role = ObjectFactory.CreateRoleView();

            controller = Substitute.ForPartsOf<RolesController>(validator, service);
            controller.ControllerContext = new ControllerContext();
            controller.ControllerContext.RouteData = new RouteData();
        }
Esempio n. 7
0
        private Boolean IsUniqueRole(RoleView view)
        {
            Boolean isUnique = !UnitOfWork
                .Select<Role>()
                .Any(role =>
                    role.Id != view.Id &&
                    role.Title.ToLower() == view.Title.ToLower());

            if (!isUnique)
                ModelState.AddModelError<RoleView>(role => role.Title, Validations.TitleIsAlreadyUsed);

            return isUnique;
        }
Esempio n. 8
0
        public void SeedPrivilegesTree_ThirdDepth()
        {
            IEnumerable<Privilege> privileges = CreateRoleWithPrivileges().RolePrivileges.Select(rolePriv => rolePriv.Privilege);
            context.Set<Privilege>().AddRange(privileges);
            context.SaveChanges();

            RoleView role = new RoleView();
            service.SeedPrivilegesTree(role);

            IEnumerator<JsTreeNode> expected = CreateRoleView().PrivilegesTree.Nodes.SelectMany(node => node.Nodes).SelectMany(node => node.Nodes).GetEnumerator();
            IEnumerator<JsTreeNode> actual = role.PrivilegesTree.Nodes.SelectMany(node => node.Nodes).SelectMany(node => node.Nodes).GetEnumerator();

            while (expected.MoveNext() | actual.MoveNext())
            {
                Assert.Equal(expected.Current.Id, actual.Current.Id);
                Assert.Equal(expected.Current.Title, actual.Current.Title);
                Assert.Equal(expected.Current.Nodes.Count, actual.Current.Nodes.Count);
            }
        }
Esempio n. 9
0
        public void SeedPrivilegesTree_LeafsWithId()
        {
            IEnumerable<Privilege> privileges = CreateRoleWithPrivileges().RolePrivileges.Select(rolePriv => rolePriv.Privilege);
            context.Set<Privilege>().AddRange(privileges);
            context.SaveChanges();

            RoleView role = new RoleView();
            service.SeedPrivilegesTree(role);

            IEnumerable<JsTreeNode> nodes = role.PrivilegesTree.Nodes;
            IEnumerable<JsTreeNode> leafs = GetAllLeafNodes(nodes);

            Assert.Empty(leafs.Where(leaf => leaf.Id == null));
        }
Esempio n. 10
0
        public void SeedPrivilegesTree_BranchesWithoutId()
        {
            IEnumerable<Privilege> privileges = CreateRoleWithPrivileges().RolePrivileges.Select(rolePriv => rolePriv.Privilege);
            context.Set<Privilege>().AddRange(privileges);
            context.SaveChanges();

            RoleView role = new RoleView();
            service.SeedPrivilegesTree(role);

            IEnumerable<JsTreeNode> nodes = role.PrivilegesTree.Nodes;
            IEnumerable<JsTreeNode> branches = GetAllBranchNodes(nodes);

            Assert.Empty(branches.Where(branch => branch.Id != null));
        }
Esempio n. 11
0
        private void EditRole(RoleView view)
        {
            Role role = UnitOfWork.To<Role>(view);

            UnitOfWork.Update(role);
        }
Esempio n. 12
0
 private void CreateRolePrivileges(RoleView role)
 {
     foreach (String privilegeId in role.PrivilegesTree.SelectedIds)
         UnitOfWork.Insert(new RolePrivilege
         {
             RoleId = role.Id,
             PrivilegeId = privilegeId
         });
 }
Esempio n. 13
0
        private void CreateRole(RoleView view)
        {
            Role role = UnitOfWork.To<Role>(view);

            UnitOfWork.Insert(role);
        }