Exemplo n.º 1
0
        public async Task <IActionResult> Register([FromBody] AfricanFarmersCommodities.Models.UserDetails userDetails)
        {
            if (userDetails.password != userDetails.repassword)
            {
                ModelState.AddModelError(string.Empty, "Password don't match");
                return(BadRequest(new{ Error = "Passwords don't match", IsRegistered = false }));
            }
            if (string.IsNullOrEmpty(userDetails.mobileNumber))
            {
                ModelState.AddModelError(string.Empty, "Mobile Number Required");
                return(BadRequest(new { Error = "Mobile Number Required", IsRegistered = false }));
            }
            var newUser = new User
            {
                Username     = userDetails.emailAddress,
                Email        = userDetails.emailAddress,
                MobileNumber = userDetails.mobileNumber,
                FirstName    = userDetails.firstName,
                LastName     = userDetails.lastName,
                CreateTime   = DateTime.Now,
                IsActive     = false,
                IsLockedOut  = false
            };

            UserInteractionResults userCreationResult = await _userService.CreateAsync(newUser, userDetails.password);

            if (userCreationResult != UserInteractionResults.Succeeded)
            {
                ModelState.AddModelError(userCreationResult.ToString(), userCreationResult.ToString());
                return(Ok(new { IsRegistered = false, ErrorMessage = userCreationResult.ToString() }));
            }

            return(Ok(new { IsRegistered = true, IsAdministrator = false, Message = UserInteractionResults.Succeeded.ToString() }));
        }
Exemplo n.º 2
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() }));
        }
Exemplo n.º 3
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" }));
        }
Exemplo n.º 4
0
        private async Task <IActionResult> CreateAuthoriseUsingLoginCredentials(AfricanFarmersCommodities.Models.UserDetails userDetails)
        {
            var user = await _userService.FindByEmailAsync(userDetails.emailAddress);

            if (user == null)
            {
                ModelState.AddModelError(string.Empty, "Invalid login");

                return(BadRequest(new { IsLoggedIn = false, ErrorMessage = "Invalid login" }));
            }

            UserInteractionResults result = await _userService.PasswordSignInAsync(user, userDetails.password, isPersistent : userDetails.keepLoggedIn, lockoutOnFailure : false);

            if (result != UserInteractionResults.Succeeded)
            {
                ModelState.AddModelError(string.Empty, "Invalid login");
                return(BadRequest(new { IsLoggedIn = false, ErrorMessage = "Invalid Login" }));
            }
            else if (result == UserInteractionResults.Succeeded)
            {
                var tmpUser = await _userService.FindByNameAsync(user.Username);

                var userRoles = await _roleService.FindByUserNameAsync(tmpUser.Username);

                var authToken = await _userService.AddUserRolesClaimAsync(tmpUser.Username, userRoles);

                tmpUser.Token = authToken;
                _unitOfWork.SaveChanges();

                var isAdministrator = await _userService.IsUserInRoleAsync(user.Username.ToLower(), "administrator");

                return(Ok(new { AuthToken = authToken, IsLoggedIn = true, IsAdministrator = isAdministrator }));
            }

            return(Ok(new { IsLoggedIn = false, IsAdministrator = false, Message = "Failed to Login!", Result = result.ToString() }));
        }