示例#1
0
        public override void CreateRole(string roleName)
        {
            Role newRole = new Role()
            {
                Name = roleName
            };
            BelarusAttractionsContext db = new BelarusAttractionsContext();

            db.Roles.Add(newRole);
            db.SaveChanges();
        }
示例#2
0
 public ActionResult Registration(RegisterModel model)
 {
     if (ModelState.IsValid)
     {
         User user = null;
         using (BelarusAttractionsContext db = new BelarusAttractionsContext())
         {
             user = unit.Users.GetAll().FirstOrDefault(u => u.Email == model.Email);
         }
         if (user == null)
         {
             // создаем нового пользователя
             using (BelarusAttractionsContext db = new BelarusAttractionsContext())
             {
                 unit.Users.Create(new User
                 {
                     Email           = model.Email,
                     Password        = model.Password,
                     FirstName       = model.FirstName,
                     LastName        = model.LastName,
                     ConfirmPassword = model.ConfirmPassword,
                     Gender          = model.Gender,
                     Skype           = model.Skype,
                     Telephone       = model.Telephone,
                     RoleId          = 3
                 });
                 unit.Save();
                 //db.SaveChanges();
                 user = unit.Users.GetAll().Where(u => u.Email == model.Email && u.Password == model.Password).FirstOrDefault();
             }
             // если пользователь удачно добавлен в бд
             if (user != null)
             {
                 FormsAuthentication.SetAuthCookie(model.Email, true);
                 return(RedirectToAction("Index", "Home"));
             }
         }
         else
         {
             ModelState.AddModelError("Email", "Пользователь с таким логином уже существует");
         }
     }
     return(View(model));
 }
示例#3
0
 public override string[] GetRolesForUser(string username)
 {
     string[] role = new string[] { };
     using (BelarusAttractionsContext db = new BelarusAttractionsContext())
     {
         // Получаем пользователя
         User user = db.Users.FirstOrDefault(u => u.Email == username);
         if (user != null)
         {
             // получаем роль
             Role userRole = db.Roles.Find(user.RoleId);
             if (userRole != null)
             {
                 role = new string[] { userRole.Name }
             }
             ;
         }
     }
     return(role);;
 }
示例#4
0
        public override bool IsUserInRole(string username, string roleName)
        {
            bool outputResult = false;

            // Находим пользователя
            using (BelarusAttractionsContext db = new BelarusAttractionsContext())
            {
                // Получаем пользователя
                User user = db.Users.FirstOrDefault(u => u.Email == username);
                if (user != null)
                {
                    // получаем роль
                    Role userRole = db.Roles.Find(user.RoleId);
                    //сравниваем
                    if (userRole != null && userRole.Name == roleName)
                    {
                        outputResult = true;
                    }
                }
            }
            return(outputResult);
        }
示例#5
0
        public ActionResult Login(LoginModel model)
        {
            if (ModelState.IsValid)
            {
                // поиск пользователя в бд
                User user = null;
                using (BelarusAttractionsContext db = new BelarusAttractionsContext())
                {
                    user = unit.Users.GetAll().FirstOrDefault(u => u.Email == model.Email && u.Password == model.Password);
                }
                if (user != null)
                {
                    FormsAuthentication.SetAuthCookie(model.Email, true);
                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    ModelState.AddModelError("Password", "Пользователя с таким логином и паролем нет");
                }
            }

            return(View(model));
        }
示例#6
0
 public CityRepository(BelarusAttractionsContext context)
 {
     this._applicationDbContext = context;
 }