/// <summary> /// Create a new UserObjectRight /// </summary> /// <param name="userId">Id of the user to create right for</param> /// <param name="objectType">The type of entity to create a right for</param> /// <exception cref="NotImplementedException">NotImplementedException if ObjectType is unhandled</exception> /// <returns>UserObjectRightCreateViewModel</returns> public ActionResult Create(int userId, ObjectTypes objectType) { using (var context = dataContextFactory.CreateByUser()) { UserObjectRight userObjectRight = NewUserObjectRight(objectType); SelectList objectList = null; Model.Right right = null; switch (objectType) { case ObjectTypes.Vendor: objectList = (from v in context.Vendors select v).ToSelectList(x => x.ObjectId, x => x.Name); right = (from x in context.Rights where x.RightId == Model.VendorAdmin.Id select x).FirstOrDefault(); break; case ObjectTypes.Customer: objectList = (from c in context.Customers select c).ToSelectList(x => x.ObjectId, x => x.Name); right = (from x in context.Rights where x.RightId == Model.EditEntityMembers.Id select x).FirstOrDefault(); break; case ObjectTypes.License: objectList = (from l in context.Licenses select new { Id = l.ObjectId, Name = l.Sku.SkuCode }).ToSelectList(x => x.Id, x => x.Name); right = (from x in context.Rights where x.RightId == Model.EditLicenseInfo.Id select x).FirstOrDefault(); break; default: throw new NotImplementedException("ObjectType not known"); } userObjectRight.User = (from x in context.Users where x.UserId == userId select x).FirstOrDefault(); userObjectRight.UserId = userId; userObjectRight.Right = right; userObjectRight.RightId = right.RightId; var viewModel = new UserObjectRightCreateViewModel(userObjectRight, objectType, objectList); viewModel.UseLocalReferrerAsRedirectUrl(Request); return(View(viewModel)); } }
public ActionResult Create(UserObjectRightCreateViewModel viewModel) { if (ModelState.IsValid) { using (var context = dataContextFactory.CreateByUser()) { var userObjectRight = viewModel.ToEntity(null); if (userObjectRight is UserVendorRight) { context.UserVendorRights.Add(userObjectRight as UserVendorRight); } else if (userObjectRight is UserCustomerRight) { context.UserCustomerRights.Add(userObjectRight as UserCustomerRight); } else if (userObjectRight is UserLicenseRight) { context.UserLicenseRights.Add(userObjectRight as UserLicenseRight); } context.SaveChanges(); Flash.Success(String.Format("Successfully granted {0} rights to {1}.", viewModel.ObjectType, viewModel.Email)); } if (!string.IsNullOrEmpty(viewModel.RedirectUrl)) { return(Redirect(viewModel.RedirectUrl)); } else { return(RedirectToAction("Edit", "Account", new { id = viewModel.UserId })); } } return(Create(viewModel.UserId, viewModel.ObjectType)); }