private static void CreateUser(string username, string email) { var scope = ObjectScopeProvider1.GetNewObjectScope(); List<User> users = (from c in scope.GetOqlQuery<User>().ExecuteEnumerable() where c.Username.ToLower().Trim().Equals(username.Trim().ToLower()) select c).ToList(); if (users.Count == 0) { scope.Transaction.Begin(); var user = new User { Email = email, Failcount = 0, IsheAdmin = true, IsheDonationReceiver = true, Username = username.Trim().ToLower() }; scope.Add(user); scope.Transaction.Commit(); } else { foreach (var user in users) { scope.Transaction.Begin(); user.IsheDonationReceiver = true; user.IsheAdmin = true; scope.Add(user); scope.Transaction.Commit(); } } }
public ActionResult AddUser(RegisterModel model) { if (User.Identity.IsAuthenticated) { var scope = ObjectScopeProvider1.GetNewObjectScope(); if (CheckAdminauthorization(scope, User.Identity.Name)) { if (ModelState.IsValid) { int count = (from c in scope.GetOqlQuery<User>().ExecuteEnumerable() where c.Username.ToLower().Equals(model.UserName.ToLower()) select c).Count(); if (count == 0) { // Attempt to register the user MembershipCreateStatus createStatus; Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus); if (createStatus == MembershipCreateStatus.Success) { scope.Transaction.Begin(); var user = new User { Failcount = 0, IsheAdmin = model.Admin, IsheDonationReceiver = model.DonationReceiver, Lasttriedtime = DateTime.Now, Username = model.UserName, Email = model.Email }; scope.Add(user); scope.Transaction.Commit(); //FormsAuthentication.SetAuthCookie(model.UserName, true /* createPersistentCookie */); ViewData["Status"] = "User added successfully."; return View("Status"); //return RedirectToAction("Index", "Home"); } ModelState.AddModelError("", ErrorCodeToString(createStatus)); } ModelState.AddModelError("Username", "Username already exists, try another name."); } // If we got this far, something failed, redisplay form return View(model); } ViewData["Status"] = "You are not authorized to do this operation"; return View("Status"); } ViewData["Status"] = "Your session has been expired, please login again and try."; return View("Status"); }