public ActionResult Create(OrganizationModel organizationmodel) { if (ModelState.IsValid) { //Creation of the Organization db.OrganizationInfo.Add(organizationmodel); db.SaveChanges(); //Changing of user role UserModel usermodel = db.Users.FirstOrDefault(u => u.Email == User.Identity.Name); //Old UserRole UserRole UR = usermodel.UserRoles.FirstOrDefault(u => u.UserId == usermodel.UserId); db.UserRoles.Remove(UR); db.SaveChanges(); //New UserRole UserRole NUR = new UserRole(); NUR.UserId = usermodel.UserId; Role R = new Role(); foreach (var role in db.Roles.ToList()) { if (role.RoleName == "Organization") { R = role; break; } } UR.RoleId = R.RoleId; UR.Role = R; db.UserRoles.Add(UR); usermodel.OrganizationId = organizationmodel.OrganizationId; db.SaveChanges(); //Redirect back to home return RedirectToAction("Index", "Home", null); } return View(organizationmodel); }
public void CreateOrganization() { UserModel user = new UserModel(); user.Email = "*****@*****.**"; user.Password = "******"; using (var db = new DataContext()) { try { if (db.Users.FirstOrDefault(u => u.Email == user.Email) == null) { var crypto = new SimpleCrypto.PBKDF2(); var encryptPass = crypto.Compute(user.Password); var u = new UserModel(); u.Email = user.Email; u.Password = encryptPass; u.PasswordSalt = crypto.Salt; db.Users.Add(u); db.SaveChanges(); UserRole UR = new UserRole(); UR.UserId = u.UserId; Role R = new Role(); foreach (var role in db.Roles.ToList()) { if (role.RoleName == "Organization") { R = role; break; } } UR.RoleId = R.RoleId; UR.Role = R; db.UserRoles.Add(UR); db.SaveChanges(); } } catch (Exception ex) { } } }
public ActionResult CreateUser(UserModel user) { if (ModelState.IsValid) { using (var db = new DataContext()) { //By default all registration of any user will start off as a normal user till they decide to request for a operator role //in manage a site/resort or both. if (db.Users.FirstOrDefault(u => u.Email == user.Email) == null) { var crypto = new SimpleCrypto.PBKDF2(); var encryptPass = crypto.Compute(user.Password); var u = new UserModel(); u.Email = user.Email; u.Password = encryptPass; u.PasswordSalt = crypto.Salt; db.Users.Add(u); db.SaveChanges(); UserRole UR = new UserRole(); UR.UserId = u.UserId; //Get from User input upon creating a new User //u.UserRoles = user.UserRoles; //Role R = new Role(); //foreach (var role in db.Roles.ToList()) //{ // if (role.RoleName == u.UserRoles.ToString()) // { // R = role; // break; // } //} Role R = new Role(); foreach (var role in db.Roles.ToList()) { if (role.RoleName == "Organization") { R = role; break; } } UR.RoleId = R.RoleId; UR.Role = R; db.UserRoles.Add(UR); db.SaveChanges(); OrganizationModel info = new OrganizationModel(); db.OrganizationInfo.Add(info); db.SaveChanges(); u.OrganizationId = info.OrganizationId; db.SaveChanges(); return Login(user); } } } else { ModelState.AddModelError("", "Data input is incorrect!"); } return View(); }
public void AddUserRole(UserRole userRole) { var roleEntry = UserRoles.SingleOrDefault(r => r.UserId == userRole.UserId); if (roleEntry != null) { UserRoles.Remove(roleEntry); SaveChanges(); } UserRoles.Add(userRole); SaveChanges(); }