상속: BaseView
예제 #1
0
        public void Create(RoleView view)
        {
            CreateRole(view);
            CreateRolePrivileges(view);

            UnitOfWork.Commit();
        }
예제 #2
0
        public void RoleView_CreatesEmpty()
        {
            JsTree actual = new RoleView().Permissions;

            Assert.Empty(actual.SelectedIds);
            Assert.Empty(actual.Nodes);
        }
예제 #3
0
        public Boolean CanEdit(RoleView view)
        {
            Boolean isValid = ModelState.IsValid;
            isValid &= IsUniqueRole(view);

            return isValid;
        }
예제 #4
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);
            }
        }
예제 #5
0
        public void RoleView_CreatesEmpty()
        {
            JsTree actual = new RoleView().PrivilegesTree;

            Assert.Empty(actual.SelectedIds);
            Assert.Empty(actual.Nodes);
        }
예제 #6
0
        public virtual void SeedPermissions(RoleView view)
        {
            JsTreeNode root = new JsTreeNode(Titles.All);
            view.Permissions.Nodes.Add(root);

            IEnumerable<Permission> permissions = GetAllPermissions();
            foreach (IGrouping<String, Permission> area in permissions.GroupBy(permission => permission.Area))
            {
                JsTreeNode areaNode = new JsTreeNode(area.Key);
                foreach (IGrouping<String, Permission> controller in area.GroupBy(permission => permission.Controller).OrderBy(permission => permission.Key))
                {
                    JsTreeNode controllerNode = new JsTreeNode(controller.Key);
                    foreach (Permission permission in controller)
                        controllerNode.Nodes.Add(new JsTreeNode(permission.Id, permission.Action));

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

                if (areaNode.Title != null)
                    root.Nodes.Add(areaNode);
            }
        }
예제 #7
0
        public void Create(RoleView view)
        {
            Role role = UnitOfWork.To<Role>(view);
            foreach (Int32 permissionId in view.Permissions.SelectedIds)
                role.Permissions.Add(new RolePermission { RoleId = role.Id, PermissionId = permissionId });

            UnitOfWork.Insert(role);
            UnitOfWork.Commit();
        }
        public RolesControllerTests()
        {
            validator = Substitute.For<IRoleValidator>();
            service = Substitute.For<IRoleService>();
            role = new RoleView();

            controller = Substitute.ForPartsOf<RolesController>(validator, service);
            controller.ControllerContext = new ControllerContext();
            controller.ControllerContext.RouteData = new RouteData();
        }
예제 #9
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.UniqueTitle);

            return isUnique;
        }
예제 #10
0
        public void SeedPermissions_ThirdDepth()
        {
            RoleView view = new RoleView();
            service.SeedPermissions(view);

            IEnumerator<JsTreeNode> expected = CreatePermissions().Nodes.SelectMany(node => node.Nodes).SelectMany(node => node.Nodes).GetEnumerator();
            IEnumerator<JsTreeNode> actual = view.Permissions.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);
            }
        }
예제 #11
0
        public void SeedPrivilegesTree_SecondDepth()
        {
            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).GetEnumerator();
            IEnumerator<JsTreeNode> actual = role.PrivilegesTree.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);
            }
        }
예제 #12
0
        private void EditPermissions(Role role, RoleView view)
        {
            List<Int32> permissions = view.Permissions.SelectedIds.ToList();
            RolePermission[] rolePermissions = UnitOfWork.Select<RolePermission>()
                .Where(rolePermission => rolePermission.RoleId == role.Id).ToArray();

            foreach (RolePermission rolePermission in rolePermissions)
                if (!permissions.Remove(rolePermission.PermissionId))
                    UnitOfWork.Delete(rolePermission);

            foreach (Int32 permissionId in permissions)
                UnitOfWork.Insert(new RolePermission
                {
                    RoleId = role.Id,
                    PermissionId = permissionId
                });
        }
예제 #13
0
        private void Edit(Role role, RoleView view)
        {
            role.Title = view.Title;

            UnitOfWork.Update(role);
        }
예제 #14
0
        public void Edit(RoleView view)
        {
            Role role = UnitOfWork.Get<Role>(view.Id);
            EditPermissions(role, view);
            Edit(role, view);

            UnitOfWork.Commit();

            Authorization.Refresh();
        }
예제 #15
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));
        }
예제 #16
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));
        }
예제 #17
0
 private void CreateRolePrivileges(RoleView role)
 {
     foreach (String privilegeId in role.PrivilegesTree.SelectedIds)
         UnitOfWork.Insert(new RolePrivilege
         {
             RoleId = role.Id,
             PrivilegeId = privilegeId
         });
 }
예제 #18
0
        private void EditRole(RoleView view)
        {
            Role role = UnitOfWork.To<Role>(view);

            UnitOfWork.Update(role);
        }
예제 #19
0
        private void CreateRole(RoleView view)
        {
            Role role = UnitOfWork.To<Role>(view);

            UnitOfWork.Insert(role);
        }
예제 #20
0
        public void SeedPermissions_LeafsWithId()
        {
            RoleView view = new RoleView();
            service.SeedPermissions(view);

            IEnumerable<JsTreeNode> nodes = view.Permissions.Nodes;
            IEnumerable<JsTreeNode> leafs = GetAllLeafNodes(nodes);

            Assert.Empty(leafs.Where(leaf => leaf.Id == null));
        }
예제 #21
0
        public void Edit(RoleView view)
        {
            DeleteRolePrivileges(view.Id);
            CreateRolePrivileges(view);
            EditRole(view);

            UnitOfWork.Commit();

            Authorization.Provider.Refresh();
        }
예제 #22
0
        public void SeedPermissions_BranchesWithoutId()
        {
            RoleView view = new RoleView();
            service.SeedPermissions(view);

            IEnumerable<JsTreeNode> nodes = view.Permissions.Nodes;
            IEnumerable<JsTreeNode> branches = GetAllBranchNodes(nodes);

            Assert.Empty(branches.Where(branch => branch.Id != null));
        }
예제 #23
0
        private void EditRolePermissions(Role role, RoleView view)
        {
            List<Int32> selectedPermissions = view.Permissions.SelectedIds.ToList();

            foreach (RolePermission rolePermission in role.Permissions.ToArray())
                if (!selectedPermissions.Remove(rolePermission.PermissionId))
                    UnitOfWork.Delete(rolePermission);

            foreach (Int32 permissionId in selectedPermissions)
                UnitOfWork.Insert(new RolePermission { RoleId = role.Id, PermissionId = permissionId });
        }