public IActionResult Index(int kullaniciId)
 {
     _entityListModel = new EntityListModel
     {
         kullanicilar = _uyedetayService.GetAll(),
         dersler      = _dersService.GetAll(),
         roller       = _rolService.GetAll(),
         sinavlar     = _sinavService.GetAll(),
         temalar      = _temaService.GetAll(),
         yetkiler     = _yetkiService.GetAll(),
         AdminID      = kullaniciId
     };
     return(View(_entityListModel));
 }
 public IActionResult SinavListesi()
 {
     _entityListModel = new EntityListModel
     {
         sinavlar     = _sinavService.GetAll(),
         kullanicilar = _uyedetayService.GetAll()
     };
     if (TempData["Sonuc"] != null)
     {
         ViewBag.Message = TempData["Sonuc"].ToString();
     }
     if (TempData["Exception"] != null)
     {
         ViewBag.Exceptions = TempData["Exception"].ToString();
     }
     return(View(_entityListModel));
 }
        public async Task <IActionResult> Index(string kullaniciAdi, string sifre)
        {
            try
            {
                if (kullaniciAdi == null || sifre == null)
                {
                    throw new RequiredFieldsException("Kullanıcı Adı / Şifre boş bırakılamaz!");
                }

                //Bilgiler asp.users tablosunda varmı
                var loggedUser = await _userManager.FindByNameAsync(kullaniciAdi);

                if (loggedUser != null)//var
                {
                    var loginResult = await _signInManager.PasswordSignInAsync(kullaniciAdi, sifre, false, false);

                    if (loginResult.Succeeded)//bilgiler dogrumu
                    {
                        int userStatus = _uyedetayService.GetAll().Where(k => k.kullanici_adi == kullaniciAdi && k.kullanici_sifre == sifre).Single().sil_id;
                        if (userStatus == 1)
                        {
                            if (await _userManager.IsInRoleAsync(loggedUser, "Admin"))//Admin paneli
                            {
                                return(RedirectToAction("Index", "dashboard", new { area = "administrator", kullaniciId = loggedUser.uyeDetayId }));
                            }
                            else //Kullanıcı Sayfası
                            {
                                return(RedirectToAction("Index", "Home", new { area = "Study" }));
                            }
                        }
                        else if (userStatus == 0)
                        {
                            throw new Exception("Hesabınız pasif durumda. Admin ile iletişime geçin!");
                        }
                    }
                    else
                    {
                        throw new Exception("Kullanıcı bilgileri hatalı");
                    }
                }
                else //yok
                {
                    //Bilgiler uyedetayTablosunda varmı
                    var user = _uyedetayService.GetAll().Where(k => k.kullanici_adi == kullaniciAdi && k.kullanici_sifre == sifre).FirstOrDefault();
                    if (user != null)//var
                    {
                        Uye  uye        = _uyeService.GetById(user.uye_id);
                        User createUser = new User
                        {
                            uyeId          = uye.id,
                            uyeDetayId     = user.id,
                            uyeAdi         = uye.uye_ad,
                            uyeSoyadi      = uye.uye_soyad,
                            kullaniciAdi   = user.kullanici_adi,
                            kullaniciSifre = user.kullanici_sifre,
                            kullaniciMail  = user.kullanici_mail,
                            UserName       = user.kullanici_adi,
                            PasswordHash   = user.kullanici_sifre,
                            Email          = user.kullanici_mail,
                            rolId          = user.rol_id
                        };
                        IdentityResult identityResult = await _userManager.CreateAsync(createUser, createUser.kullaniciSifre);

                        if (!identityResult.Succeeded)//failed
                        {
                            ViewBag.IdentityResult = identityResult;
                            throw new Exception("Hesabınız bulundu fakat web versiyonu için hesap bilgileri güncellenmeli! (Code:21) (Yardım)");
                        }
                        else if (identityResult.Succeeded)//success
                        {
                            var newLoginResult = await _signInManager.PasswordSignInAsync(createUser, createUser.kullaniciSifre, false, false);

                            if (newLoginResult.Succeeded)
                            {
                                ViewBag.Warning = "Hesabınız senkronize edildi fakat giriş yapabilmek için onaylanmasını bekleyin(Code:06)";
                                if (createUser.kullaniciAdi.ToLower() == "admin")
                                {
                                    var setupUser = await _userManager.FindByNameAsync(createUser.kullaniciAdi);

                                    var rolResult = await _userManager.AddToRoleAsync(setupUser, "Admin");

                                    if (rolResult.Succeeded)
                                    {
                                        ViewBag.Warning = "Admin hesap aktifleştirme başarılı";
                                    }
                                }
                            }
                            else
                            {
                                ViewBag.Exceptions = newLoginResult;
                                throw new Exception("Hata");
                            }
                        }
                    }
                    else //veri uyedetayTablosunda yoksa
                    {
                        throw new Exception("Kullanıcı bulunamadı! Bilgilerinizi kontrol edin veya Yeni kullanıcı oluşturun");
                    }
                }
            }
            catch (Exception ex)
            {
                ViewBag.Exceptions = ex.Message;
            }
            return(View());
        }
        public async Task <IActionResult> Index(string ad, string soyad, string kullaniciAdi, string sifre, string email)
        {
            try
            {
                if (ad == null || soyad == null)
                {
                    throw new RequiredFieldsException("Ad/Soyad boş bırakılamaz!");
                }
                else if (kullaniciAdi == null)
                {
                    throw new RequiredFieldsException("Kullanıcı adı boş bırakılamaz!");
                }
                else if (sifre == null)
                {
                    throw new RequiredFieldsException("Şifre boş bırakılamaz!");
                }

                if (await _userManager.FindByNameAsync(kullaniciAdi) != null || await _userManager.FindByEmailAsync(email) != null) //veri asp.users tablosunda var mı
                {
                    throw new Exception("Bu kullanıcı zaten mevcut");                                                               //var
                }
                else//yok
                {
                    var kullanici = _uyedetayService.GetAll().Where(k => k.kullanici_adi == kullaniciAdi || k.kullanici_mail == email).FirstOrDefault(); //veri uyedetay tablosunda var mı
                    if (kullanici != null)                                                                                                               //var
                    {
                        throw new Exception("Bu kullanıcı bilgileri masaüstü uygulamasında zaten mevcut. Bu kullanıcı ile Giriş yapmayı deneyin yada farklı bilgiler ile kayıt olun!");
                    }
                    else //yok
                    {
                        using (StudyCheckContext context = new StudyCheckContext())
                        {
                            using (var transaction = context.Database.BeginTransaction())//transaction'a başla ve tabloları kilitle
                            {
                                try
                                {
                                    Uye uyemiz = new Uye
                                    {
                                        uye_ad    = ad,
                                        uye_soyad = soyad
                                    };
                                    Uye createdUye = context.Uyeler.Add(uyemiz).Entity;
                                    context.SaveChanges();
                                    Uyedetay kullanicimiz = new Uyedetay
                                    {
                                        uye_id            = createdUye.id,
                                        kullanici_adi     = kullaniciAdi,
                                        kullanici_sifre   = sifre,
                                        kullanici_mail    = email,
                                        guncelleyen_id    = 1,
                                        guncelleme_tarihi = DateTime.Now,
                                        kayit_tarihi      = DateTime.Now,
                                        rol_id            = 1,
                                        sil_id            = 1,
                                        tema_id           = 1
                                    };
                                    var createdUyedetay = context.UyeDetay.Add(kullanicimiz).Entity;
                                    context.SaveChanges();
                                    User createdLogin = new User
                                    {
                                        kullaniciAdi   = kullaniciAdi,
                                        kullaniciSifre = sifre,
                                        kullaniciMail  = email,
                                        uyeAdi         = ad,
                                        uyeSoyadi      = soyad,
                                        rolId          = 1,
                                        uyeId          = createdUye.id,
                                        uyeDetayId     = createdUyedetay.id,
                                        UserName       = kullaniciAdi,
                                        Email          = email,
                                        PasswordHash   = sifre
                                    };
                                    IdentityResult createResult = await _userManager.CreateAsync(createdLogin, sifre);

                                    if (createResult.Succeeded)
                                    {
                                        ViewBag.IdentityResult = "Kullanıcı başarıyla oluşturuldu fakat giriş yapabilmek için onaylanmasını bekleyin(Code:06)";
                                    }
                                    else
                                    {
                                        ViewBag.IdentityErrors = createResult;
                                        throw new Exception("Hata! İşlem iptal edildi");
                                    }
                                }
                                catch (Exception ex)
                                {
                                    transaction.Rollback();//değişiklikleri geri al
                                    throw new Exception(ex.Message);
                                }
                                transaction.Commit();//tablo kilitlerini aç
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ViewBag.Exceptions = ex.Message; //red
            }
            return(View());
        }
Exemple #5
0
        public async Task <IActionResult> KullaniciDuzenle(string uyeDetayId, string uyeId, string kayitTarihi, string kullaniciAdi, string sifre, string mail, int durum, int rol, string uyeAd, string uyeSoyad)
        {
            try
            {
                if (uyeAd == null || uyeSoyad == null)
                {
                    throw new RequiredFieldsException("Üye bilgileri boş bırakılamaz!");
                }
                else if (kullaniciAdi == null || sifre == null || mail == null)
                {
                    throw new RequiredFieldsException("Kullanıcı bilgileri boş bırakılamaz!");
                }
                var kullanici = _uyedetayService.GetAll().Where(k => k.kullanici_adi == kullaniciAdi || k.kullanici_mail == mail).ToList();
                if (kullanici.Count > 1)
                {
                    throw new Exception("Bu kullanıcı adı / mail zaten mevcut!");
                }
                else
                {
                    using (StudyCheckContext context = new StudyCheckContext())
                    {
                        using (var transaction = context.Database.BeginTransaction())
                        {
                            try
                            {
                                var oldUser     = _uyedetayService.GetById(Convert.ToInt32(uyeDetayId));
                                var updatedUser = await _userManager.FindByNameAsync(oldUser.kullanici_adi);

                                if (oldUser.kullanici_sifre != sifre)
                                {
                                    IdentityResult passResult = await _userManager.ChangePasswordAsync(updatedUser, oldUser.kullanici_sifre, sifre);

                                    if (!passResult.Succeeded)
                                    {
                                        throw new Exception(passResult.Errors.First().Description);
                                    }
                                }
                                updatedUser.UserName           = kullaniciAdi;
                                updatedUser.NormalizedUserName = kullaniciAdi.ToUpper();
                                updatedUser.Email           = mail;
                                updatedUser.NormalizedEmail = mail.ToUpper();
                                updatedUser.uyeAdi          = uyeAd;
                                updatedUser.uyeSoyadi       = uyeSoyad;
                                updatedUser.kullaniciAdi    = kullaniciAdi;
                                updatedUser.kullaniciSifre  = sifre;
                                updatedUser.kullaniciMail   = mail;
                                updatedUser.rolId           = rol;
                                IdentityResult result = await _userManager.UpdateAsync(updatedUser);

                                if (!result.Succeeded)
                                {
                                    throw new Exception(result.Errors.First().Description);
                                }
                                else
                                {
                                    Uye updatedUye = new Uye
                                    {
                                        id        = Convert.ToInt32(uyeId),
                                        uye_ad    = uyeAd,
                                        uye_soyad = uyeSoyad
                                    };
                                    context.Uyeler.Update(updatedUye);
                                    context.SaveChanges();
                                    var identityUser = await _userManager.GetUserAsync(HttpContext.User);

                                    Uyedetay updatedKullanici = new Uyedetay
                                    {
                                        id                = Convert.ToInt32(uyeDetayId),
                                        kullanici_adi     = kullaniciAdi,
                                        kullanici_sifre   = sifre,
                                        kullanici_mail    = mail,
                                        kayit_tarihi      = Convert.ToDateTime(kayitTarihi),
                                        guncelleyen_id    = identityUser.uyeDetayId,
                                        guncelleme_tarihi = DateTime.Now,
                                        uye_id            = Convert.ToInt32(uyeId),
                                        rol_id            = rol,
                                        tema_id           = 1,
                                        sil_id            = durum
                                    };
                                    context.UyeDetay.Update(updatedKullanici);
                                    context.SaveChanges();
                                }
                            }
                            catch (Exception transException)
                            {
                                transaction.Rollback();
                                throw new Exception(transException.Message);
                            }
                            TempData["Sonuc"] = "Kullanıcı güncellendi";
                            transaction.Commit();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                TempData["Exception"] = ex.Message;
            }
            return(RedirectToAction("KullaniciListesi"));
        }