Пример #1
0
        public ActionResult CreateUser(UserModels userModel)
        {
            User user = new User();

            if (Membership.GetUser(userModel.Username) == null)
            {
                String[] kq = userModel.MyOption;
                user.Username = userModel.Username;
                user.Password = Cryptography.CreateMD5Hash(userModel.Password);
                user.LastName = userModel.LastName;
                user.FirstName = userModel.FirstName;
                user.IsLocked = userModel.IsLocked;
                user.IsActived = userModel.IsActive;
                user.Email = userModel.Email;
                user.CreateDate = DateTime.Now;
                if (kq.Count() != 0)
                {
                    foreach (String s in kq)
                    {
                        foreach (Role r in db.Roles)
                        {
                            if (r.ID.ToString() == s)
                                user.Roles.Add(r);
                        }
                    }
                }
                db.Users.AddObject(user);

                //---------------
                foreach (Role r in user.Roles)
                {
                    if (r.ID == 2)
                    {
                        String[] ls = userModel.OptionSubject;
                        if (userModel.OptionSubject.Count() != 0)
                        {
                            foreach (String str in ls)
                            {
                                foreach (Subject s in db.Subjects)
                                {
                                    if (s.ID == str)
                                        user.Subjects.Add(s);
                                }
                            }
                        }

                    }

                }

                db.SaveChanges();
                string Message = "Đã tạo User có tên đăng nhập là: " + user.Username + " thành công";
                return RedirectToAction("Index", "Home", new { Message = Message });
            }
            else
            {
                string Message = "Username đã tồn tại. Vui lòng nhập lại!";
                return RedirectToAction("CreateUser", "Account", new { Message = Message }); //TODO: báo ra username đã tồn tại.
            }
        }
Пример #2
0
 /// <summary>
 /// Deprecated Method for adding a new object to the Users EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToUsers(User user)
 {
     base.AddObject("Users", user);
 }
Пример #3
0
 public ActionResult EditUser(User user)
 {
     db.Users.Attach(user);
     db.ObjectStateManager.ChangeObjectState(user, EntityState.Modified);
     db.SaveChanges();
     return View(user);
 }
Пример #4
0
 /// <summary>
 /// Create a new User object.
 /// </summary>
 /// <param name="username">Initial value of the Username property.</param>
 /// <param name="firstName">Initial value of the FirstName property.</param>
 /// <param name="lastName">Initial value of the LastName property.</param>
 /// <param name="email">Initial value of the Email property.</param>
 /// <param name="isLocked">Initial value of the IsLocked property.</param>
 /// <param name="createDate">Initial value of the CreateDate property.</param>
 public static User CreateUser(global::System.String username, global::System.String firstName, global::System.String lastName, global::System.String email, global::System.Boolean isLocked, global::System.DateTime createDate)
 {
     User user = new User();
     user.Username = username;
     user.FirstName = firstName;
     user.LastName = lastName;
     user.Email = email;
     user.IsLocked = isLocked;
     user.CreateDate = createDate;
     return user;
 }