public void TestRoles()
        {
            RolesModel model = this.controller.GetRoles();

            Assert.IsNull(model.DefaultRoleId, "No default role should have been returned.");
            Assert.AreEqual(2, model.Roles.Count, "Expected to find two roles.");
            Assert.AreEqual(32, model.AllSecurables.Count, "Expected to find 32 securables.");

            RolesModel.RoleItem roleModel = model.Roles.Single(y => y.Id == this.roleUserId);

            // Make sure the controller returns the role WITHOUT any securables assigned to it in the model
            Assert.IsNotNull(roleModel, "Expected the JSON response to contain the RoleItem model.");

            Assert.AreEqual(this.roleUserId, roleModel.Id, "Returned model does not match the role id requested.");
            Assert.AreEqual(0, roleModel.AssignedSecurables.Count, "Role shouldn't have any securables assigned to it yet.");

            this.mockRepo.AssociateSecurableToRole("user", SecurableNames.TEMPLATE_EDIT);
            this.mockRepo.AssociateSecurableToRole("user", SecurableNames.TEMPLATE_LIST);

            model     = this.controller.GetRoles();
            roleModel = model.Roles.Single(y => y.Id == this.roleUserId);

            Assert.AreEqual(2, roleModel.AssignedSecurables.Count, "Expected two securables to be returned for the role.");

            // Remove a securable from the role and make sure the controller only returns one securable for the role
            this.mockRepo.RemoveSecurableFromRole("user", SecurableNames.TEMPLATE_EDIT);

            model     = this.controller.GetRoles();
            roleModel = model.Roles.Single(y => y.Id == this.roleUserId);

            Assert.AreEqual(1, roleModel.AssignedSecurables.Count, "Expected one securable to be returned for the role.");
        }
Пример #2
0
        public RolesModel.RoleItem AddRole(string roleName)
        {
            Role newRole = new Role()
            {
                Name = roleName
            };

            this.tdb.Roles.AddObject(newRole);

            this.tdb.SaveChanges();

            RolesModel.RoleItem model = new RolesModel.RoleItem()
            {
                Id   = newRole.Id,
                Name = newRole.Name
            };

            return(model);
        }
Пример #3
0
        public RolesModel GetRoles()
        {
            RolesModel model = new RolesModel();

            // Populate the model's list of Roles
            foreach (Role cRole in this.tdb.Roles)
            {
                // Create the model's role item
                RolesModel.RoleItem newRoleItem = new RolesModel.RoleItem()
                {
                    Id   = cRole.Id,
                    Name = cRole.Name
                };

                // Add each assigned securable of the role to the model's role item
                foreach (RoleAppSecurable cRoleAppSecurable in cRole.AppSecurables)
                {
                    newRoleItem.AssignedSecurables.Add(
                        new RolesModel.SecurableItem()
                    {
                        Id          = cRoleAppSecurable.AppSecurable.Id,
                        Key         = cRoleAppSecurable.AppSecurable.Name,
                        Name        = cRoleAppSecurable.AppSecurable.DisplayName,
                        Description = cRoleAppSecurable.AppSecurable.Description
                    });
                }

                newRoleItem.Organizations = (from r in this.tdb.Roles
                                             from o in this.tdb.Organizations
                                             where r.Id == cRole.Id
                                             select new RolesModel.Organization
                {
                    Id = o.Id,
                    Name = o.Name,
                    Restricted = r.Restrictions.Count(y => y.OrganizationId == o.Id) > 0
                })
                                            .Distinct()
                                            .OrderBy(y => y.Name)
                                            .ToList();

                // Add the model's role item to the model
                model.Roles.Add(newRoleItem);
            }

            foreach (AppSecurable cAppSecurable in this.tdb.AppSecurables)
            {
                model.AllSecurables.Add(
                    new RolesModel.SecurableItem()
                {
                    Id          = cAppSecurable.Id,
                    Key         = cAppSecurable.Name,
                    Name        = cAppSecurable.DisplayName,
                    Description = cAppSecurable.Description
                });
            }

            Role defaultRole = this.tdb.Roles.FirstOrDefault(y => y.IsDefault);

            model.DefaultRoleId = defaultRole != null ? (int?)defaultRole.Id : null;

            return(model);
        }