Beispiel #1
0
        public async Task <IActionResult> CreateRole([FromBody] AfricanFarmersCommodities.Models.UserRole role)
        {
            try
            {
                var newRole = _roleService.GetAllRoles().Result.FirstOrDefault(p => p.RoleName.ToLower().Equals(role.role.ToLower().Trim()));

                if (newRole == null)
                {
                    UserInteractionResults result = await _roleService.CreateAsync(new AfricanFarmersCommodities.Domain.Role {
                        RoleName = role.role
                    });

                    if (result == UserInteractionResults.Succeeded)
                    {
                        return(Ok(new { Message = UserInteractionResults.Succeeded.ToString() }));
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(BadRequest(new { ErrorMessage = UserInteractionResults.Failed.ToString() }));
        }
Beispiel #2
0
        public async Task <IActionResult> RemoveUserFromRole([FromBody] AfricanFarmersCommodities.Models.UserRole userRole)
        {
            var user = await _userService.FindByEmailAsync(userRole.email);

            var userInRole = false;

            if (user != null)
            {
                userInRole = await _userService.IsUserInRoleAsync(user.Email.ToLower(), userRole.role.ToLower());
            }
            if (userInRole)
            {
                UserInteractionResults result = await _userService.RemoveFromRolesAsync(user, new string[] { userRole.role });

                return(Ok(new { Result = result.ToString() }));
            }
            return(BadRequest(new { Result = UserInteractionResults.Failed.ToString(), ErrorMessage = "Failed to remove user from Role" }));
        }
Beispiel #3
0
        public async Task <IActionResult> AddUserToRole([FromBody] AfricanFarmersCommodities.Models.UserRole userRole)
        {
            var user = await _userService.FindByEmailAsync(userRole.email);

            var isUserInrole = false;

            if (user != null)
            {
                isUserInrole = await _userService.IsUserInRoleAsync(user.Email.ToLower(), userRole.role.ToLower());
            }
            if (!isUserInrole)
            {
                UserInteractionResults result = await _userService.AddToRoleAsync(user, userRole.role);

                return(Json(new { Result = result.ToString() }));
            }
            return(Ok(new { Message = UserInteractionResults.Failed.ToString() }));
        }