public ActionResult SaveSecUserInfo(SecUserInfo SecUsrInfo) { RBACUser rUser = new RBACUser(Session["UserName"].ToString()); if (!rUser.HasPermission("SecUserInfo_Insert")) { return(Json("X", JsonRequestBehavior.AllowGet)); } string eCode = ""; using (var transaction = new TransactionScope()) { try { var UserInfo = _secUserInfoService.All().ToList().FirstOrDefault(x => x.UserName == SecUsrInfo.UserName); if (UserInfo == null) { SecUserInfo SecUinf = new SecUserInfo(); SecUinf.UserName = SecUsrInfo.UserName; SecUinf.Password = SHA1.Encode(SecUsrInfo.Password); SecUinf.Email = SecUsrInfo.Email; SecUinf.CreateDate = System.DateTime.Now; _secUserInfoService.Add(SecUinf); _secUserInfoService.Save(); //For user branch table by Farhad var userId = _secUserInfoService.All().ToList().Where(x => x.UserName == SecUsrInfo.UserName).Select(s => s.UserID).FirstOrDefault(); UserBranch UB = new UserBranch(); UB.Userid = userId.ToString(); UB.BranchCode = "01"; _userBranchService.Add(UB); _userBranchService.Save(); eCode = "1"; } else { eCode = "2"; } transaction.Complete(); return(Json(eCode, JsonRequestBehavior.AllowGet)); } catch (Exception) { transaction.Dispose(); return(Json("0", JsonRequestBehavior.AllowGet)); } } }
public ActionResult Register(Employee model, string[] EmployeesCustom, string[] roles, string returnUrl) { var isExist = _employeeService.All().FirstOrDefault(x => x.Email == model.Email.ToLower()); if (isExist == null) { try { model.UserName = model.Email; ModelState.Clear(); UpdateModel(model); } catch { } if (ModelState.IsValid) { if (model.Password == model.ConfirmPassword) { try { Employee employee = new Employee(); employee.UserName = model.UserName; employee.Email = model.Email; //employee.BranchCode = model.BranchCode; employee.IsActive = false; WebSecurity.CreateUserAndAccount(model.Email.ToLower(), model.Password); Roles.AddUserToRoles(employee.Email, roles); _employeeService.Add(employee); _employeeService.Save(); int id = employee.Id;//_employeeService.All().LastOrDefault().Id; List <string> userbranch = EmployeesCustom.ToList(); string UID = Convert.ToString(id); //List<UserBranch> userList = new List<UserBranch>(); foreach (var BranchCode in userbranch) { UserBranch userbranchs = new UserBranch(); userbranchs.Userid = UID; userbranchs.BranchCode = BranchCode; //userList.Add(userbranchs); _userbranchService.Add(userbranchs); _userbranchService.Save(); } // model.Branchs = userList; //_employeeService.Add(model); // _userbranchService.SaveChanges(); //Roles.AddUserToRole(model.Email, ""); ViewBag.Roles = roles; ViewBag.BranchCode = new SelectList(_branchService.All().ToList(), "BranchCode", "BranchName"); ViewBag.Id = new SelectList(_employeeService.All().ToList(), "Id", "UserName"); return(RedirectToAction("Register")); } catch (MembershipCreateUserException e) { ModelState.AddModelError("", ErrorCodeToString(e.StatusCode)); } } else { ModelState.AddModelError("error", "Password doesn't match. Please recheck password"); } } } else { ViewBag.IsAlreadyRegistered = "This User Name is already registered."; } List <string> allRole = new List <string>(); foreach (string role in System.Web.Security.Roles.GetAllRoles().ToList()) { allRole.Add(role); } ViewBag.Roles = allRole; ViewBag.BranchCode = new SelectList(_branchService.All().ToList(), "BranchCode", "BranchName"); ViewBag.Id = new SelectList(_employeeService.All().ToList(), "Id", "UserName"); return(View(model)); }