Ejemplo n.º 1
0
        public IActionResult Register(CreateUpdateUserDto model)
        {
            model.Password = model.Password.GetMd5String();

            model.CreateTime = DateTime.Now;
            var user = _mapper.Map <User>(model);

            _userService.AddUser(user);
            return(Ok("success"));
        }
Ejemplo n.º 2
0
        public async Task <ApiResult <UserDto> > UpdateUser(Guid id, [FromQuery] CreateUpdateUserDto input, CancellationToken cancellationToken)
        {
            var result = await _userService.UpdateAsync(id, input, cancellationToken);

            if (result == null)
            {
                return(new BadRequestResult());
            }

            return(result);
        }
Ejemplo n.º 3
0
        public async Task <IResponseDTO> CreateUser(CreateUpdateUserDto options, int userId)
        {
            try
            {
                // Generate user password
                var password = GeneratePassword();
                var appUser  = _mapper.Map <ApplicationUser>(options);
                appUser.CreatedBy      = userId;
                appUser.CreatedOn      = DateTime.Now;
                appUser.UserName       = appUser.Email;
                appUser.ChangePassword = true;
                appUser.Status         = UserStatusEnum.Active.ToString();
                appUser.UserRoles      = new List <ApplicationUserRole>
                {
                    new ApplicationUserRole
                    {
                        RoleId = options.RoleId
                    }
                };

                // Create the user
                IdentityResult result = await _userManager.CreateAsync(appUser, password);

                if (!result.Succeeded)
                {
                    _response.IsPassed = false;
                    _response.Errors   = result.Errors.Select(x => x.Description).ToList();
                    return(_response);
                }

                // send email to the user to change his password
                var verifyEmailToken = await _userManager.GeneratePasswordResetTokenAsync(appUser);

                verifyEmailToken = WebUtility.UrlEncode(verifyEmailToken);
                await _emailService.AfterRegistiration(appUser.Email, verifyEmailToken);

                _response.IsPassed = true;
                _response.Message  = "Email was sent to the user email so he can set his password.";
            }
            catch (Exception ex)
            {
                _response.Data     = null;
                _response.IsPassed = false;
                _response.Errors.Add($"Error: {ex.Message}");
            }

            return(_response);
        }
Ejemplo n.º 4
0
        public async Task <UserDto> UpdateAsync(Guid id, CreateUpdateUserDto input, CancellationToken cancellationToken)
        {
            var user = await _userRepository.GetByIdAsync(id, cancellationToken);

            if (user != null)
            {
                user.Name                 = input.Name;
                user.Surname              = input.Surname;
                user.BirthDate            = input.BirthDate;
                user.Address              = input.Address;
                user.LastModificationTime = DateTime.Now;
                await _userRepository.UpdateAsync(user, cancellationToken);

                return(_mapper.Map <User, UserDto>(user));
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 5
0
        public async Task <UserDto> CreateAsync(CreateUpdateUserDto input, CancellationToken cancellationToken)
        {
            var user = new User
            {
                Name      = input.Name,
                Surname   = input.Surname,
                BirthDate = input.BirthDate,
                Address   = input.Address
            };

            var chkTCKN = await _userManager.IsUserTCKNCodeExist(user.TCKN);

            if (!chkTCKN)
            {
                user.LastModificationTime = DateTime.Now;
                user.CreationTime         = DateTime.Now;
                await _userRepository.AddAsync(user, cancellationToken);

                return(_mapper.Map <User, UserDto>(user));
            }

            return(null);
        }
Ejemplo n.º 6
0
        public async Task <IResponseDTO> UpdateUser(int id, CreateUpdateUserDto options, int userId)
        {
            try
            {
                var appUser = _userManager.Users
                              .Include(x => x.UserRoles)
                              .FirstOrDefault(x => x.Id == id);

                if (appUser == null)
                {
                    _response.Errors.Add("User id is not exist.");
                    _response.IsPassed = false;
                    return(_response);
                }
                else if (appUser.Status == UserStatusEnum.Locked.ToString())
                {
                    _response.Errors.Add("Your Account is locked. Please contact your administration");
                    _response.IsPassed = false;
                    return(_response);
                }
                else if (appUser.Status == UserStatusEnum.NotActive.ToString())
                {
                    _response.Errors.Add("Your Account is disabled. Please contact your administration");
                    _response.IsPassed = false;
                    return(_response);
                }

                // Update user info
                appUser.FirstName = options.FirstName;
                appUser.LastName  = options.LastName;
                appUser.Address   = options.Address;
                appUser.UpdatedBy = userId;
                appUser.UpdatedOn = DateTime.Now;

                // Update DB
                IdentityResult result = await _userManager.UpdateAsync(appUser);

                if (!result.Succeeded)
                {
                    _response.IsPassed = false;
                    _response.Errors   = result.Errors.Select(x => x.Description).ToList();
                    return(_response);
                }

                // Update the role
                var oldRoleId = appUser.UserRoles.FirstOrDefault().RoleId;
                if (options.RoleId != oldRoleId)
                {
                    var oldUserRole = _appDbContext.UserRoles.FirstOrDefault(x => x.Id == appUser.UserRoles.FirstOrDefault().Id);
                    var newUserRole = new ApplicationUserRole
                    {
                        User   = appUser,
                        UserId = id,
                        RoleId = options.RoleId
                    };

                    _appDbContext.UserRoles.Remove(oldUserRole);
                    await _appDbContext.UserRoles.AddAsync(newUserRole);

                    // save to the database
                    var save = await _appDbContext.SaveChangesAsync();

                    if (save == 0)
                    {
                        _response.IsPassed = false;
                        _response.Errors.Add("Database did not save the object");
                        return(_response);
                    }
                }

                _response.IsPassed = true;
                _response.Message  = "User is updated successfully";
            }
            catch (Exception ex)
            {
                _response.Data     = null;
                _response.IsPassed = false;
                _response.Errors.Add($"Error: {ex.Message}");
            }

            return(_response);
        }
Ejemplo n.º 7
0
        public async Task <IResponseDTO> UpdateUser([FromRoute] int id, [FromBody] CreateUpdateUserDto options)
        {
            _response = await _accountService.UpdateUser(id, options, LoggedInUserId);

            return(_response);
        }