public UserViewModel SaveUser(UserViewModel model) { exceptionService.Execute((m) => { if (!InternetConnectionIsAvailable) { model.AddModelError(x => x.Email, "Message.NoInternetConnection", "Internet connection is not available, please try again later."); } if (model.HasError) { return; } var duplicateUserName = userRepository.Table.Any(x => x.UserId != model.UserId && x.Username == model.Username); if (duplicateUserName) { model.AddModelError(x => x.Username, "Message.Duplicate", string.Format("{0} already exists, please use another.", model.GetDisplayName(x => x.Username))); } if (model.HasError) { return; } if (!model.Validate()) { return; } var entity = new User(); if (model.UserId > 0) { entity = userRepository.GetById(model.UserId); userRepository.Update(entity); var employee = employeeRepository.GetById(model.EmployeeId); if (employee != null) { employee.UserId = model.UserId; employeeRepository.Update(employee); } var savedUser = employeeRepository.GetById(model.EmployeeId).User; if (savedUser != null) { var savedRoles = savedUser.UserRoles; if (savedRoles != null) { userRoleRepository.DeleteRange(savedRoles); } } if (employee != null && model.Roles != null && model.Roles.Any() && savedUser != null) { var roles = model.Roles.Select(x => x.Value); foreach (var r in roles) { userRoleRepository.Insert(new UserRole { UserId = employee.UserId.Value, RoleId = r, RoleGrantedFrom = DateTime.Now }); } } } else { var encServ = new EncryptionService(); var generatedPassword = encServ.RandomString(5); model.IsActive = true; model.Salt = encServ.CreateSaltKey(10); model.Password = encServ.CreatePasswordHash(generatedPassword, model.Salt); entity = AutomapperConfig.Mapper.Map <User>(model); userRepository.Insert(entity); model.UserId = entity.UserId; var employee = employeeRepository.GetById(model.EmployeeId); if (employee != null) { employee.UserId = model.UserId; employeeRepository.Update(employee); } if (employee != null && model.Roles != null && model.Roles.Any()) { var roles = model.Roles.Select(x => x.Value); foreach (var r in roles) { userRoleRepository.Insert(new UserRole { UserId = employee.UserId.Value, RoleId = r, RoleGrantedFrom = DateTime.Now }); } } if (employee != null && employee.Email != null) { var template = notificationService.EmailTemplate(NTAEnum.eEmailTemplateType.UserCreated); var subject = template.Subject; var body = template.TemplateBody.Replace("{EmployeeName}", employee.Name) .Replace("{UserName}", model.Username) .Replace("{Password}", generatedPassword); var messageBody = body; ServiceModel sm = notificationService.SendEmail(employee.Email, null, null, subject, messageBody, null, true, true); } } if (model.UserId == 0) { model.Message = localizationService.GetLocalizedText("Message.RecordSavedSuccessfully", IMSAppConfig.Instance.CurrentLanguage, "Record saved succeeded."); } else { model.Message = localizationService.GetLocalizedText("Message.RecordUpdatedSuccessfully", IMSAppConfig.Instance.CurrentLanguage, "Record update succeeded."); } }, model); return(model); }