public ActionResult Create(CreateNewUserModel newUser)
        {
            User user = new User();
            if (ModelState.IsValid)
            {

                user.UserName = newUser.UserName;
                user.UserPassword = newUser.UserPassword;
                user.UserRoles = newUser.UserRoles;
                db.Users.Add(user);
                db.SaveChanges();
                return RedirectToAction("ManageUsers");
            }

            return View(user);
        }
 public static void SetCurrentUser(HttpSessionStateBase session, User user)
 {
     session[User] = user;
 }
        public async Task<ActionResult> Signin(AdminLoginViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                User admin = new User();
                List <User> listAdmin = DataHelper.GetAllUsers();

                List<User> newAdmin = DataHelper.GetUsersByKeyword(model.Username);
                
                if (newAdmin.Count == 0)
                {
                    
                    newAdmin = listAdmin.FindAll(m => m.UserPassword.Equals(model.Password));
                    if (newAdmin.Count == 0)
                    {
                        ModelState.AddModelError("", "Username and Password is incorrect!");
                       
                        return View(model);
                    }else
                    {
                        ModelState.AddModelError("", "Username is incorrect!");
                        return View(model);
                    }
                }
                else
                {
                    //newAdmin = listAdmin.FindAll(m => m.UserPassword.Equals(model.Password));
                    if (newAdmin[0].UserPassword.Trim().Equals(model.Password.Trim()))
                    {

                        if (newAdmin[0].UserRoles == 2 || newAdmin[0].UserRoles==3)
                        {
                            if (newAdmin[0].AccountStatus == false)
                            {
                                ModelState.AddModelError("", "Admin is blocked!");
                                return View(model);
                            }
                            else
                            {
                                Session["UserName"] = model.Username;

                                admin.UserName = model.Username;
                                admin.UserPassword = model.Password;

                                UserAdminHelpers.SetCurrentUser(Session, admin);
                                return View("AdminView", admin);
                            }
                            
                        }
                        else
                        {
                            ModelState.AddModelError("", "Account is not an admin");
                            return View(model);
                        }
                        
                    }
                    else
                    {
                        ModelState.AddModelError("", "Password is incorrect!");
                       
                        return View(model);
                    }
                }

            }
            else
            {
                return RedirectToAction("AdminView", "Home");
            }
           
        }
        public async Task<ActionResult> Signup(SignUpViewModel model)
        {
            if (ModelState.IsValid)
            {
                User admin = new User();
                List<User> listAdmin = DataHelper.GetAllUsers();

                List<User> newAdmin = DataHelper.GetUsersByKeyword(model.UserName);
                if (newAdmin.Count == 0)
                {
                    User newAdmin1 = new User();
                    newAdmin1.UserName = model.UserName;
                    newAdmin1.UserPassword = model.Password;
                    DataHelper.AddAdmin(newAdmin1);
                    
                    ModelState.AddModelError("", "Sucessful!");
                }
                else
                {
                    ModelState.AddModelError("", "Username is existed");
                }
                //var user = new ApplicationUser { UserName = model.Username, Email = model.Email };
                //var result = await UserManager.CreateAsync(user, model.Password);
                //if (result.Succeeded)
                //{
                //    await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
                    
                //    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                //    // Send an email with this link
                //    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                //    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                //    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                //    return RedirectToAction("Index", "Home");
                //}
                //AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
 public static void AddAdmin(User u)
 {
     db.Users.Add(u);
     db.SaveChanges();
 }
 public static void SetCurrentAdmin(HttpSessionStateBase session, User admin)
 {
     session[Admin] = admin;
 }
 public ActionResult ExternalLoginConfirmation(GoogleAccountModel model)
 {
     User user = UserHelpers.GetCurrentUser(Session);
     if (user != null) {
         TempData["errorTittle"] = "Bad request";
         TempData["errorMessage"] = "You are already signed in the system";
         return RedirectToAction("Index", "Home");
     }
     if (ModelState.IsValid)
     {
         User newUser = UserDatabaseHelper.Instance.GetUserByUserName(model.UserName);
         if (newUser!= null)
         {
             ModelState.AddModelError("", "UserName is already exist. Please choose another.");
             TempData["errorTitle"] = null;
             TempData["errorMessage"] = null;
             return View("ConfirmRegisterGoogle", model);
         }
         newUser = new User();
         newUser.UserEmail = model.Email;
         newUser.UserName = model.UserName;
         newUser.UserPassword = model.Password;
         newUser.UserDOB = model.UserDOB;
         newUser.Place = model.Place;
         newUser.UserFirstName = model.UserFirstName;
         newUser.DataJoin = DateTime.Today;
         if (model.UserLastName != null && model.UserLastName != "")
         {
             newUser.UserLastName = model.UserLastName;
         }
         newUser.AccountStatus = EventZoneConstants.ActiveUser; //set Active account
         newUser.Avartar = 10032;
         newUser.UserRoles = EventZoneConstants.User; //set UserRole
         // insert user to Database
         db.Users.Add(newUser);
         db.SaveChanges();
         UserHelpers.SetCurrentUser(Session, newUser);
         //Send email confirm
         MailHelpers.Instance.SendMailWelcome(newUser.UserEmail, newUser.UserFirstName, newUser.UserLastName);
         TempData["errorTitle"] = "Sucessfull SignUp";
         TempData["errorMessage"] = "Thank you for signing up in EventZone! We sent you a welcome message! Hope you have more fun and comfortable by joining with us";
         return RedirectToAction("Index", "Home");
     }
     TempData["errorTitle"] = "Invald input";
     TempData["errorMessage"] = "Invalid input! Please try again";
     // If we got this far, something failed, redisplay form
     return RedirectToAction("ExternalLoginConfirmation", "Account",model);
 }
        public async Task<ActionResult> SignUpPost(SignUpViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new User();
                var listUser = new List<User>();
                listUser = db.Users.ToList();
                var newUser = listUser.FindAll(a => a.UserName.Equals(model.UserName));
                if (newUser.Count != 0)
                {
                    //ModelState.AddModelError("", "UserName is already exist. Please choose another.");
                    return Json(new
                    {
                        state = 0,
                        message = "UserName is already exist. Please choose another."
                    });
                }
                newUser = listUser.FindAll(a => a.UserEmail.Equals(model.Email));
                if (newUser.Count != 0)
                {
                    //ModelState.AddModelError("", "Email is already registered. Please choose another.");
                    return Json(new
                    {
                        state = 0,
                        message = "Email is already registered. Please choose another."
                    });
                }
                UserDatabaseHelper.Instance.AddNewUser(model, user);

                UserHelpers.SetCurrentUser(Session, user);

                //Create Channel
                UserDatabaseHelper.Instance.CreateUserChannel(user);
                //Send email confirm
                MailHelpers.Instance.SendMailWelcome(user.UserEmail,user.UserFirstName,user.UserLastName);
                //return RedirectToAction("RegisterSuccess", "Account");
                return Json(new
                {
                    state = 1,
                    message = "Registered Successfully"
                });
            }

            // If we got this far, something failed, redisplay form
            return Json(new
            {
                state = 0,
                message = "Something Wrong"
            });
        }
        public ActionResult AddNewUserPost(UserCreatedByAdmin model)
        {
            User admin = UserHelpers.GetCurrentAdmin(Session);
            if (admin == null)
            {
                return Json(new
                {
                    state = 0,
                    error = "Require signin!",
                    message = "You are not signed in..."
                });
            }
            else if (admin.AccountStatus == EventZoneConstants.LockedUser)
            {
                return Json(new
                {
                    state = 0,
                    error = "Locked account",
                    message = "Your account is locked. You cant use this feature!"
                });
            }
            else if (admin.UserRoles != EventZoneConstants.RootAdmin && admin.UserRoles != EventZoneConstants.Admin)
            {
                return Json(new
                {
                    state = 0,
                    error = "Permission denied",
                    message = "This feature not avaiable for you!"
                });
            }
            if (admin.AccountStatus != EventZoneConstants.LockedUser)
            {

                if (ModelState.IsValid) {
                    User newUser = UserDatabaseHelper.Instance.GetUserByUserName(model.UserName);
                    if (newUser!=null)
                    {
                        //ModelState.AddModelError("", "UserName is already exist. Please choose another.");
                        return Json(new
                        {
                            state = 0,
                            message = "UserName is already exist. Please choose another."
                        });
                    }
                    newUser = UserDatabaseHelper.Instance.GetUserByEmail(model.Email);
                    if (newUser!=null)
                    {
                        //ModelState.AddModelError("", "Email is already registered. Please choose another.");
                        return Json(new
                        {
                            state = 0,
                            message = "Email is already registered. Please choose another."
                        });
                    }
                    User user = new User
                    {
                        UserEmail = model.Email,
                        UserName = model.UserName,
                        UserPassword = model.Password,
                        UserDOB = model.UserDOB,
                        UserFirstName = model.UserFirstName,
                        DataJoin = DateTime.Today,
                        AccountStatus = EventZoneConstants.ActiveUser, //set Active account
                        Avartar = 10032,
                        UserRoles = EventZoneConstants.User //set UserRole
                    };
                    if (AdminDataHelpers.Instance.AddUser(user)) {
                        UserDatabaseHelper.Instance.CreateUserChannel(user);
                        return Json(new
                        {
                            state=1,
                            userID= user.UserID
                        });
                    }
                }

            }
            return Json(new
            {
                state = 0,
                error = "Erorr",
                message = "Something wrong! Please try again!"
            });
        }