public async Task <Group> AddRoleToGroup(string groupName, Guid roleId)
        {
            var group = await _groupStore.Get(groupName);

            var role = await _roleStore.Get(roleId);

            if (group.Roles.Any(r => r.Id == roleId) ||
                role.Groups.Any(g => string.Equals(g, groupName, StringComparison.OrdinalIgnoreCase)))
            {
                throw new AlreadyExistsException <Role>($"Role {role.Name} already exists for group {group.Name}. Please provide a new role id.");
            }

            return(await _groupStore.AddRoleToGroup(group, role));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> GetByFilter([FromQuery] RoleFilter filter)
        {
            var validationResult = new RoleFilterValidator().Validate(filter);

            validationResult.AddToModelState(this.ModelState, null);

            if (!validationResult.IsValid)
            {
                return(ValidationError());
            }

            var page = await RoleStore.Get(filter);

            if (page.IsEmpty)
            {
                return(NotFound <Role>(filter, page.Total));
            }

            return(Collection <Role>(filter, page.Total, page.Items));
        }
        public async Task <Group> AddRoleToGroup(string groupName, Guid roleId)
        {
            var group = await _groupStore.Get(groupName);

            var role = await _roleStore.Get(roleId);

            if (group.Roles.All(r => r.Id != roleId))
            {
                group.Roles.Add(role);
            }

            if (role.Groups.All(g => !string.Equals(g, groupName, StringComparison.OrdinalIgnoreCase)))
            {
                role.Groups.Add(groupName);
            }

            await _roleStore.Update(role);

            await _groupStore.Update(group);

            return(group);
        }
Exemplo n.º 4
0
        public void DeleteRoleFromGroup_GroupExists_Success()
        {
            const string group1Name = "Group1Name";

            SetupGroup(group1Name, "Custom");
            var roleId   = SetupRole("Role1Name");
            var response = SetupGroupRoleMapping(group1Name, roleId.ToString());

            Assert.Equal(HttpStatusCode.Created, response.StatusCode);

            // delete the mapping
            response = Browser.Delete($"/groups/{group1Name}/roles", with =>
            {
                with.HttpRequest();
                with.Header("Accept", "application/json");
                with.FormValue("Id", roleId.ToString());
            }).Result;

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);

            response = Browser.Get($"/groups/{group1Name}/roles", with =>
            {
                with.HttpRequest();
                with.Header("Accept", "application/json");
            }).Result;

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);

            var responseEntity = response.Body.DeserializeJson <GroupRoleApiModel>();
            var roleList       = responseEntity.Roles.ToList();

            Assert.Equal(0, roleList.Count);

            // ensure the deletion is reflected in the role model
            var role      = _roleStore.Get(roleId).Result;
            var groupList = role.Groups;

            Assert.Equal(0, groupList.Count);
        }
Exemplo n.º 5
0
 /// <summary>
 /// Gets a role by Id.
 /// </summary>
 public async Task <Role> GetRole(Guid roleId)
 {
     return(await _roleStore.Get(roleId));
 }