예제 #1
0
        async public Task <TransactionResult> AddEmploye(string userId, EmployeFormViewModel model)
        {
            try
            {
                var user = new User
                {
                    FullName             = model.FullName,
                    UserName             = model.Email,
                    PhoneNumber          = model.CellPhone,
                    Email                = model.Email,
                    EmailConfirmed       = true,
                    SlugUrl              = GetUniqueSlugUrl(model.FullName),
                    TwoFactorEnabled     = true,
                    PhoneNumberConfirmed = true,
                    LockoutEnabled       = bool.Parse(model.IsActive),
                    CreatedDate          = DateTime.Now
                };

                var existsPhoneNumberOrEmail = _userManager.Users.Any(i => i.PhoneNumber == model.CellPhone || i.Email == model.Email);

                if (existsPhoneNumberOrEmail)
                {
                    return(new TransactionResult()
                    {
                        Message = "Email veya Cep Telefonu başka bir kullanıcı tarafından kullanılmaktadır.", Type = TransactionType.Warning
                    });
                }
                else
                {
                    string password = Utils.GeneratePassword(3, 3, 3);

                    var result = await _userManager.CreateAsync(user, password);

                    if (result.Succeeded)
                    {
                        user = await _userManager.FindByIdAsync(user.Id);

                        await _userManager.SetLockoutEnabledAsync(user, bool.Parse(model.IsActive));

                        await _userManager.AddToRoleAsync(user, "Admin");

                        _notificationService.AddNotification(user.Id, NotificationTypes.AdminInfoMail, param: password);

                        //_notificationService.SendAdminUserInfo(user.Id, user.Email, user.Email, password);
                    }
                }

                return(new TransactionResult()
                {
                    Type = TransactionType.Success
                });
            }
            catch (Exception ex)
            {
                return(new TransactionResult(message: ex.Message, type: TransactionType.Error));
            }
        }
예제 #2
0
        async public Task <TransactionResult> UpdateEmploye(EmployeFormViewModel model)
        {
            try
            {
                User user = _userManager.FindByIdAsync(model.Id).Result;

                user.FullName    = model.FullName;
                user.UserName    = model.Email;
                user.PhoneNumber = model.CellPhone;
                user.Email       = model.Email;
                var isActive = bool.Parse(model.IsActive);
                user.LockoutEnabled = isActive;
                if (isActive)
                {
                    user.LockoutEnd = DateTimeOffset.MaxValue;
                }
                else
                {
                    user.LockoutEnd = null;
                }

                var existsPhoneNumberOrEmail = _userManager.Users.Any(i => (i.PhoneNumber == model.CellPhone || i.Email == model.Email) && i.Id != model.Id);

                if (existsPhoneNumberOrEmail)
                {
                    return(new TransactionResult()
                    {
                        Message = "Email veya Cep Telefonu başka bir kullanıcı tarafından kullanılmaktadır.", Type = TransactionType.Warning
                    });
                }
                else
                {
                    var result = await _userManager.UpdateAsync(user);

                    if (result.Succeeded)
                    {
                        _notificationService.AddNotification(user.Id, Common.Enums.NotificationTypes.UpdateAdmin);
                    }
                }

                return(new TransactionResult()
                {
                    Type = TransactionType.Success
                });
            }
            catch (Exception ex)
            {
                return(new TransactionResult(message: ex.Message, type: TransactionType.Error));
            }
        }