public ActionResult AddUserToGroup(int[] to, int id) { var lstUser = _userRepository.GetAll().ToList(); //lấy những user đã được thêm vào nhóm var lstUserAdded = lstUser.Where(g => g.Active && !string.IsNullOrEmpty(g.GroupUserID) && g.GroupUserID.Split(',').Select(Int32.Parse).Contains(id)).ToList(); //xóa hết user khỏi nhóm foreach (var userAdded in lstUserAdded) { var arrUser = userAdded.GroupUserID.Split(',').Select(Int32.Parse).Where(val => val != id).ToArray(); userAdded.GroupUserID = string.Join(",", arrUser); } try { _userRepository.Edit(lstUserAdded); } catch (Exception) { return(Json(new { IsSuccess = false, Messenger = string.Format("Có lỗi xảy ra") }, JsonRequestBehavior.AllowGet)); } //thêm lại danh sách user vào nhóm if (to != null) { var lstUserNewAdd = lstUser.Where(g => to.Contains(g.ID)).ToList(); foreach (var user in lstUserNewAdd) { var arrUser = new List <int>(); if (!string.IsNullOrEmpty(user.GroupUserID)) { arrUser = user.GroupUserID.Split(',').Select(Int32.Parse).ToList(); } arrUser.Add(id); user.GroupUserID = string.Join(",", arrUser); } try { _userRepository.Edit(lstUserNewAdd); } catch (Exception) { return(Json(new { IsSuccess = false, Messenger = string.Format("Có lỗi xảy ra") }, JsonRequestBehavior.AllowGet)); } } return(Json(new { IsSuccess = true, Messenger = string.Format("Thêm người dùng vào nhóm thành công") }, JsonRequestBehavior.AllowGet)); }
public ActionResult Login(User obj) { if (string.IsNullOrEmpty(obj.UserName) || string.IsNullOrWhiteSpace(obj.Password)) { Session["Messenger"] = new Notified { Value = EnumNotifield.Error, Messenger = "Bạn chưa nhập tên đăng nhập hoặc mật khẩu nhập vào không đúng!" }; return(View("/Areas/Admin/Views/Account/Login.cshtml")); } var user = _userRepository.GetAll().FirstOrDefault(u => u.Password == HelperEncryptor.Md5Hash(obj.Password) && u.UserName == obj.UserName); if (user != null) { user.TimeLogin = DateTime.Now; user.IPLogin = obj.IPLogin; _userRepository.Edit(user); if (user.Active == false) { Session["Messenger"] = new Notified { Value = EnumNotifield.Error, Messenger = "Tài khoản này chưa được kích hoạt!" }; return(View("/Areas/Admin/Views/Account/Login.cshtml")); } var serializeModel = new CustomPrincipalSerializeModel { ID = user.ID, Username = user.UserName, FullName = user.FullName, Email = user.Email, GroupUser = user.GroupUserID, UserType = user.UserType, isAdmin = user.isAdmin, }; var serializer = new JavaScriptSerializer(); var userData = serializer.Serialize(serializeModel); var authTicket = new FormsAuthenticationTicket( 1, obj.UserName, DateTime.Now, DateTime.Now.AddHours(1), false, userData); var encTicket = FormsAuthentication.Encrypt(authTicket); var faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket); faCookie.Name = "ADMIN_COOKIES"; Response.Cookies.Add(faCookie); } else { Session["Messenger"] = new Notified { Value = EnumNotifield.Error, Messenger = "Bạn chưa nhập tên đăng nhập hoặc mật khẩu nhập vào không đúng!" }; return(View("/Areas/Admin/Views/Account/Login.cshtml")); } var preUrl = (Uri)Session["returnUrl"]; if (preUrl != null) { Session["returnUrl"] = null; return(Redirect(preUrl.ToString())); } return(RedirectToAction("Index", "Home", new { area = "Admin" })); }