public async Task <ActionResult> Register(RegisterViewModel model) { // Add debug log LogService.Debug($"Getting the registration page (HTTP_POST). SessionId:{SessionId}"); if (ModelState.IsValid) { // Create the user object var user = new LidiaUser { UserName = model.Email, Email = model.Email }; // Create the user at the user store var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); // Add debug log LogService.Debug($"User registration complete, now redirecting to home page. SessionId:{SessionId}"); return(RedirectToAction("Index", "Home")); } AddErrors(result); } // Add debug log LogService.Debug($"User registration failed, re-opening the registration page. SessionId:{SessionId}"); // If we got this far, something failed, redisplay form return(View(model)); }
public ActionResult DeleteConfirmed(int id) { //Create the user var user = new LidiaUser(); try { using (var ctx = new SaasDbContext()) { //Get the user from db user = ctx.Users.Where(u => u.Id == id).FirstOrDefault(); //Get user's tenant from db var tenantId = user.Roles.Where(tı => tı.UserId == id).FirstOrDefault().TenantId; if (User.IsInRole("SystemAdministrator")) { // Delete user from db ctx.Users.Remove(user); ctx.SaveChanges(); return(RedirectToAction("Index")); } else if (User.IsInRole("TenantAdministrator") && CurrentTenant.TenantId == tenantId) { // Delete user from db ctx.Users.Remove(user); ctx.SaveChanges(); return(RedirectToAction("Index")); } else { return(View("Authorize")); } } } catch (Exception ex) { LogService.Info("Applications not found", ex.Message, ex.InnerException); } return(RedirectToAction("Index")); }
public ActionResult Details(int id) { //Create the model var model = new UserViewModel(); //Create the user var user = new LidiaUser(); try { using (var ctx = new SaasDbContext()) { // Get the user from the database user = ctx.Users.Include("Roles").Where(u => u.Id == id).FirstOrDefault(); //Get user's tenant from db var tenantId = user.Roles.Where(tı => tı.UserId == id).FirstOrDefault().TenantId; if (User.IsInRole("SystemAdministrator")) { //Add user to model model.User = user; } else if (User.IsInRole("TenantAdministrator") && CurrentTenant.TenantId == tenantId) { //Add user to model model.User = user; } else if (id == CurrentUser.Id) { //Add user to model model.User = user; } else { return(View("Authorize")); } } // Create the breadcrumb var breadcrumb = new List <BreadcrumbItemViewModel>(); // Set breadcrumb item breadcrumb.Add(new BreadcrumbItemViewModel() { Text = "Users", Link = "/Users" }); breadcrumb.Add(new BreadcrumbItemViewModel() { Text = user.Firstname + " " + user.Lastname, Link = "/Users/" + user.Id + "/Details" }); breadcrumb.Add(new BreadcrumbItemViewModel() { Text = "Details" }); //set breadcrumb item to Viewbag ViewBag.Breadcrumb = breadcrumb; } catch (Exception ex) { LogService.Info("User not found", ex.Message, ex.InnerException); } return(View(model)); }
public ActionResult Delete(int id) { //Create the model var model = new UserViewModel(); //Create the user var user = new LidiaUser(); try { using (var ctx = new SaasDbContext()) { // Get the users from the database user = ctx.Users.Include("Roles").Where(u => u.Id == id).FirstOrDefault(); //Get tenant id from db var tenantid = ctx.UserRoles.Where(t => t.UserId == id).FirstOrDefault().TenantId; if (User.IsInRole("SystemAdministrator")) { //Add user to model model.User = user; } else if (User.IsInRole("TenantAdministrator") && CurrentTenant.TenantId == tenantid) { //Add user to model model.User = user; } else { return(View("Authorize")); } } // Create the breadcrumb var breadcrumb = new List <BreadcrumbItemViewModel>(); breadcrumb.Add(new BreadcrumbItemViewModel() { Text = "Users", Link = "/Users" }); breadcrumb.Add(new BreadcrumbItemViewModel() { Text = user.Firstname + " " + user.Lastname, Link = "/Users/" + user.Id + "/Details" }); breadcrumb.Add(new BreadcrumbItemViewModel() { Text = "Delete Confirm" }); ViewBag.Breadcrumb = breadcrumb; if (model.User != null) { return(View(model)); } else { return(View("Authorize")); } } catch (Exception ex) { LogService.Info("User not found to delete"); return(RedirectToAction("Home/Index", ex.Message, ex.InnerException)); } }
public ActionResult Edit(int?id) { //Create the model var model = new EditUserViewModel(); // Create the user var user = new LidiaUser(); var userRole = new LidiaUserRole(); try { using (var ctx = new SaasDbContext()) { //Get user from db user = ctx.Users.Where(u => u.Id == id).Include("Roles").FirstOrDefault(); //Get user role from db userRole = ctx.UserRoles.Where(s => s.UserId == user.Id).FirstOrDefault(); if (User.IsInRole("SystemAdministrator")) { model.Firstname = user.Firstname; model.Lastname = user.Lastname; model.Email = user.Email; model.MobileNumber = user.MobileNumber; model.Gender = user.Gender; model.UserId = user.Id; model.Id = userRole.RoleId; model.TenantId = userRole.TenantId; model.PasswordHash = user.PasswordHash; model.Birthdate = user.Birthdate; model.SecurityStamp = user.SecurityStamp; //Add user to model ViewBag.Id = new SelectList(ctx.Roles.ToList(), "Id", "Name"); } else if (CurrentTenant.TenantId == userRole.TenantId && User.IsInRole("TenantAdministrator")) { model.Firstname = user.Firstname; model.Lastname = user.Lastname; model.Email = user.Email; model.MobileNumber = user.MobileNumber; model.Gender = user.Gender; model.UserId = user.Id; model.Id = userRole.RoleId; model.TenantId = userRole.TenantId; model.PasswordHash = user.PasswordHash; model.Birthdate = user.Birthdate; model.SecurityStamp = user.SecurityStamp; ViewBag.Id = new SelectList(ctx.Roles.Where(r => r.Id != 3).ToList(), "Id", "Name"); } else if (CurrentUser.Id == user.Id) { model.Firstname = user.Firstname; model.Lastname = user.Lastname; model.Email = user.Email; model.MobileNumber = user.MobileNumber; model.Gender = user.Gender; model.UserId = user.Id; model.Id = userRole.RoleId; model.TenantId = userRole.TenantId; model.PasswordHash = user.PasswordHash; model.Birthdate = user.Birthdate; model.SecurityStamp = user.SecurityStamp; ViewBag.Id = new SelectList(ctx.Roles.Where(r => r.Id == 1).ToList(), "Id", "Name"); } else { return(View("Authorize")); } } // Create the breadcrumb var breadcrumb = new List <BreadcrumbItemViewModel>(); breadcrumb.Add(new BreadcrumbItemViewModel() { Text = "Users", Link = "/Users" }); breadcrumb.Add(new BreadcrumbItemViewModel() { Text = user.Firstname + " " + user.Lastname, Link = "/Users/" + user.Id + "/Details" }); breadcrumb.Add(new BreadcrumbItemViewModel() { Text = "Edit" }); ViewBag.Breadcrumb = breadcrumb; // Add currentUserId to model ViewBag.CurrentUser = CurrentUser.Id; ViewBag.CurrentTenant = CurrentTenant.TenantId; if (model != null) { return(View(model)); } else { return(RedirectToAction("Home/Index")); } } catch (Exception ex) { LogService.Info("User not found to edit", ex.Message, ex.InnerException); return(RedirectToAction("Home/Index")); } }
public async Task <ActionResult> Create([Bind(Include = "RoleId,TenantId,Firstname,LastName,Username,Email,Password,MobileNumber,Gender,Created")] CreateUserViewModel user) { try { //Create the LidiaUser var newUser = new LidiaUser() { Firstname = user.Firstname, Lastname = user.Lastname, Email = user.Email, Gender = user.Gender, MobileNumber = user.MobileNumber, UserName = user.Email }; using (var ctx = new SaasDbContext()) { if (User.IsInRole("SystemAdministrator") || user.TenantId == CurrentTenant.TenantId) { var result = await UserManager.CreateAsync(newUser, user.Password); if (result.Succeeded) { //Add user role var userRole = new LidiaUserRole() { RoleId = user.RoleId, TenantId = user.TenantId, UserId = newUser.Id }; ctx.UserRoles.Add(userRole); ctx.SaveChanges(); return(RedirectToAction("/Index")); } } if (User.IsInRole("SystemAdministrator")) { ViewBag.TenantId = new SelectList(ctx.Tenants.ToList(), "TenantId", "Name"); ViewBag.Id = new SelectList(ctx.Roles.ToList(), "Id", "Name"); } else if (User.IsInRole("TenantAdministrator")) { ViewBag.TenantId = new SelectList(ctx.Tenants.Where(t => t.TenantId == CurrentTenant.TenantId).ToList(), "TenantId", "Name"); ViewBag.Id = new SelectList(ctx.Roles.Where(t => t.Id != 3).ToList(), "Id", "Name"); } else { return(View("Authorize")); } } } catch (Exception ex) { LogService.Info("Application do not create", ex.Message, ex.InnerException); } return(View(user)); }