Exemplo n.º 1
0
        public async Task <ActionResult> QuétTask()
        {
            var phanquyen = await _phanQuyenRepository.All.ToListAsync();

            var users = _accountRepository.GetCustomerViewModel();

            foreach (var item in users)
            {
                item.TenLoaiKhachHang = _phanQuyenRepository.All.Include(x => x.Quyen)
                                        .FirstOrDefault(x => x.MaTaiKhoan == item.Id)
                                        ?.Quyen.TenQuyen;
            }
            users = users.Where(x => x.TenLoaiKhachHang.ToLower().Contains("Khách".ToLower())).ToList();
            // maquyen
            foreach (var item in phanquyen)
            {
                var kh      = users.FirstOrDefault(x => x.Id == item.MaTaiKhoan);
                var quyenkh = _quyenRepository.Get(item.MaQuyen);
                if (kh != null)
                {
                    var mucdiemtieptheochuasosanh = await _quyenRepository.All.Where(x => (kh.Point >= x.Diem && x.Diem >= 1)).ToListAsync();

                    var mucdiemtieptheo = mucdiemtieptheochuasosanh.OrderBy(quyen => Math.Abs(kh.Point - quyen.Diem)).First();
                    if (!mucdiemtieptheo.TenQuyen.Equals(quyenkh.TenQuyen))
                    {
                        _phanQuyenRepository.Delete(item);
                        item.MaQuyen    = mucdiemtieptheo.MaQuyen;
                        item.MaTaiKhoan = kh.Id;
                        await _phanQuyenRepository.AddAsync(item);
                    }
                }
            }
            await _phanQuyenRepository.SaveAsync(RequestContext);

            string thongBao = "Đã nâng cấp tài khoản khách hàng thành công";

            return(RedirectToAction("Index", "Customer", new { thongbao = thongBao.ToUpper() }));
        }
Exemplo n.º 2
0
        //[Route("AddUser/{thongTinDangKy}")]
        public async Task <IActionResult> AddUser([FromBody] UserItem item)
        {
            string message;

            var checkRegister = _accountRepository.All.Count(u => u.Email == item.email);

            if (checkRegister == 0)
            {
                //string hashpass;
                //using (Md5 = MD5.Create())
                //{
                //    Encrypt = new EnCryptography();
                //    hashpass = Encrypt.GetMd5Hash(Md5, item.password);
                //}
                //string salt = EncryptionHelper.GetSalt();
                //Password = EncryptionHelper.GetHash(item.password + salt),
                var avatars = new string[] { "avatar1.png", "avatar2.png", "avatar3.png", "avatar4.png" };
                var index   = int.Parse(item.avatar);
                if (index < 0 || index > avatars.Count() - 1)
                {
                    index = 0;
                }

                var      avatarName = avatars[index];
                Customer customer   = new Customer()
                {
                    Id        = Guid.NewGuid().ToString(),
                    Email     = item.email,
                    Password  = StringHelper.stringToSHA512(item.password),
                    FullName  = item.fullName,
                    CreateAt  = DateTime.Now,
                    Avatar    = avatarName,
                    TinhTrang = "Chưa kích hoạt"
                };
                _accountRepository.Add(customer);
                await _accountRepository.SaveAsync();

                var quyen = _quyenRepository.All.FirstOrDefault(x => x.TenQuyen.Contains("Khách hàng"));
                if (quyen != null)
                {
                    var       maquyen   = quyen.MaQuyen;
                    PhanQuyen phanQuyen = new PhanQuyen()
                    {
                        MaQuyen    = maquyen,
                        MaTaiKhoan = customer.Id
                    };
                    await _phanQuyenRepository.AddAsync(phanQuyen);

                    await _phanQuyenRepository.SaveAsync();

                    HttpContext.Session.SetInt32(SessionIdQuyen, maquyen);
                    HttpContext.Session.SetString(SessionName, customer.Email);
                    HttpContext.Session.SetString(SessionId, customer.Id);
                    try
                    {
                        Random random = new Random();
                        var    code   = new string(Enumerable.Repeat(Chars, 9)
                                                   .Select(s => s[random.Next(s.Length)]).ToArray());
                        var taikhoan = await _accountRepository.All.FirstOrDefaultAsync(x => x.Email.Contains(item.email));

                        XacMinh xm = new XacMinh
                        {
                            Code    = code,
                            Id_User = taikhoan.Id,
                            Timer   = DateTime.Now
                        };
                        await _xacMinhRepository.AddAsync(xm);

                        await _xacMinhRepository.SaveAsync();

                        string kichhoat = "Để kích hoạt tài khoản, vui lòng nhấn vào link phía dưới: \n";
                        var    local    = HttpContext.Request.Host;
                        kichhoat += "https://" + local + "/TaiKhoan/Activate?key=" + taikhoan.Id + "&code=" + code;
                        var client = new SmtpClient
                        {
                            Host                  = "smtp.gmail.com",
                            Port                  = 587,
                            EnableSsl             = true,
                            UseDefaultCredentials = false,
                            Credentials           = new NetworkCredential("*****@*****.**", "irts mecn xoyz pngm")
                        };

                        using (var messagee = new MailMessage("*****@*****.**", taikhoan.Email)
                        {
                            Subject = "Email kích hoạt tài khoản",
                            Body = kichhoat,
                            Priority = MailPriority.High,
                            BodyEncoding = Encoding.UTF8
                        })
                        {
                            await client.SendMailAsync(messagee);
                        }
                        message = "Đăng ký thành công. Vui lòng kích hoạt email để mua sản phẩm";
                    }
                    catch (Exception)
                    {
                        message = "Hệ thống đang gặp lỗi";
                    }
                }
                else
                {
                    message = "Hệ thống đang gặp lỗi";
                }
            }
            else
            {
                message = "Tài khoản này đã được đăng ký";
            }

            return(Json(message));
        }
Exemplo n.º 3
0
        public async Task <ActionResult> Create(UserViewModel model)
        {
            if (ModelState.IsValid && Validate(model))
            {
                try
                {
                    model.Id = Guid.NewGuid().ToString();
                    User user = new User();
                    PropertyCopy.Copy(model, user);
                    user.Password = Security.EncryptPassword(model.Password);
                    _userRepository.Add(user);
                    if (model.Roles != null)
                    {
                        var addUserRole = model.Roles.Select(p => new UserRole()
                        {
                            Id     = Guid.NewGuid().ToString(),
                            RoleId = p,
                            UserId = model.Id
                        }).ToList();
                        _userRoleRepository.Add(addUserRole);
                    }
                    _userRepository.Save(RequestContext);
                    //add customer
                    Customer customer = new Customer()
                    {
                        Id        = Guid.NewGuid().ToString(),
                        FullName  = model.FullName,
                        Email     = model.UserName + "@gmail.com",
                        Password  = StringHelper.stringToSHA512(model.Password),
                        CreateBy  = DateTime.Now.ToShortDateString(),
                        TinhTrang = "Không khoá",
                        Avatar    = "avatar1.png"
                    };
                    await _accountRepository.AddAsync(customer);

                    await _accountRepository.SaveAsync(RequestContext);

                    var quyen = _quyenRepository.All.FirstOrDefault(x => x.TenQuyen.Contains("Nhân viên"));

                    if (quyen != null)
                    {
                        var       maquyen   = quyen.MaQuyen;
                        PhanQuyen phanQuyen = new PhanQuyen()
                        {
                            MaQuyen    = maquyen,
                            MaTaiKhoan = customer.Id
                        };
                        await _phanQuyenRepository.AddAsync(phanQuyen);

                        await _phanQuyenRepository.SaveAsync();
                    }

                    _logger.LogInformation("Create User {0} - ID: {1}", user.UserName, user.Id);
                    return(RedirectToAction("Index"));
                }
                catch (Exception e)
                {
                    _logger.LogError(e, "Update user {0} failed", model.UserName);
                    return(View());

                    throw;
                }
            }
            return(View());
        }