public void Create(RoleView view) { CreateRole(view); CreateRolePrivileges(view); UnitOfWork.Commit(); }
public void RoleView_CreatesEmpty() { JsTree actual = new RoleView().Permissions; Assert.Empty(actual.SelectedIds); Assert.Empty(actual.Nodes); }
public Boolean CanEdit(RoleView view) { Boolean isValid = ModelState.IsValid; isValid &= IsUniqueRole(view); return isValid; }
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); } }
public void RoleView_CreatesEmpty() { JsTree actual = new RoleView().PrivilegesTree; Assert.Empty(actual.SelectedIds); Assert.Empty(actual.Nodes); }
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); } }
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(); }
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; }
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); } }
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); } }
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 }); }
private void Edit(Role role, RoleView view) { role.Title = view.Title; UnitOfWork.Update(role); }
public void Edit(RoleView view) { Role role = UnitOfWork.Get<Role>(view.Id); EditPermissions(role, view); Edit(role, view); UnitOfWork.Commit(); Authorization.Refresh(); }
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)); }
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)); }
private void CreateRolePrivileges(RoleView role) { foreach (String privilegeId in role.PrivilegesTree.SelectedIds) UnitOfWork.Insert(new RolePrivilege { RoleId = role.Id, PrivilegeId = privilegeId }); }
private void EditRole(RoleView view) { Role role = UnitOfWork.To<Role>(view); UnitOfWork.Update(role); }
private void CreateRole(RoleView view) { Role role = UnitOfWork.To<Role>(view); UnitOfWork.Insert(role); }
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)); }
public void Edit(RoleView view) { DeleteRolePrivileges(view.Id); CreateRolePrivileges(view); EditRole(view); UnitOfWork.Commit(); Authorization.Provider.Refresh(); }
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)); }
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 }); }