public ActionResult Edit([Bind(Include = "Id,UserId,TenantId,Firstname,LastName,Username,Email,PasswordHash,SecurityStamp,MobileNumber,Gender,Birthdate,Created")] EditUserViewModel userviewmodel) { try { using (var ctx = new SaasDbContext()) { // Get existing user from db var user = ctx.Users.Where(u => u.Id == userviewmodel.UserId).FirstOrDefault(); user.Firstname = userviewmodel.Firstname; user.Lastname = userviewmodel.Lastname; user.Email = userviewmodel.Email; user.UserName = userviewmodel.Email; user.Gender = userviewmodel.Gender; user.MobileNumber = userviewmodel.MobileNumber; //Get the user role from db var userRoleold = ctx.UserRoles.Where(ui => ui.UserId == userviewmodel.UserId).FirstOrDefault(); //Remove user role from db ctx.UserRoles.Remove(userRoleold); ctx.SaveChanges(); //Create a new user role var modelRole = new LidiaUserRole() { UserId = userviewmodel.UserId, RoleId = userviewmodel.Id, TenantId = userviewmodel.TenantId }; if (userviewmodel.UserId == CurrentUser.Id || User.IsInRole("SystemAdministrator") || User.IsInRole("TenantAdministrator")) { if (ModelState.IsValid) { //Update the user ctx.Entry(user).State = EntityState.Modified; ctx.UserRoles.Add(modelRole); ctx.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.TenantId = new SelectList(ctx.Tenants.ToList(), "TenantId", "Name"); } else { return(View("Authorize")); } } } catch (Exception ex) { LogService.Info("Applications not found", ex.Message, ex.InnerException); } return(View(userviewmodel)); }
public ActionResult DeleteConfirmed(int id) { try { using (var ctx = new SaasDbContext()) { //Get application from db Application application = ctx.Applications.Find(id); if (application.TenantId == CurrentTenant.TenantId || User.IsInRole("SystemAdministrator")) { if (ModelState.IsValid) { // Remove application from db ctx.Applications.Remove(application); 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 Create([Bind(Include = "Id,TenantId,Code,Domain,Name,Culture,TimeZone,Updated,Created,Status")] Application application) { try { using (var ctx = new SaasDbContext()) { if (application.TenantId == CurrentTenant.TenantId || User.IsInRole("SystemAdministrator")) { if (ModelState.IsValid) { // Add application to db ctx.Applications.Add(application); ctx.SaveChanges(); return(RedirectToAction("Index")); } } else { return(View("Authorize")); } } } catch (Exception ex) { LogService.Info("Application do not create", ex.Message, ex.InnerException); } return(View(application)); }
public ActionResult DeleteConfirmed(int id) { try { using (var ctx = new SaasDbContext()) { //Get tenant from db Tenant tenant = ctx.Tenants.Find(id); ctx.Tenants.Remove(tenant); ctx.SaveChanges(); } // Create the breadcrumb var breadcrumb = new List <BreadcrumbItemViewModel>(); breadcrumb.Add(new BreadcrumbItemViewModel() { Text = "Tenants", Link = "/Tenants" }); breadcrumb.Add(new BreadcrumbItemViewModel() { Text = "Delete Confirm" }); ViewBag.Breadcrumb = breadcrumb; } catch (Exception ex) { LogService.Info("Tenants not found", ex.Message, ex.InnerException); } return(RedirectToAction("Index")); }
public ActionResult Edit([Bind(Include = "TenantId,Code,Name,Description,Culture,TimeZone,Created,Status")] EditTenantViewModel tenantViewModel) { try { using (var ctx = new SaasDbContext()) { // Get existing tenant from db var tenant = ctx.Tenants.Where(t => t.TenantId == tenantViewModel.TenantId).FirstOrDefault(); tenant.Code = tenantViewModel.Code; tenant.Name = tenantViewModel.Name; tenant.Description = tenantViewModel.Description; tenant.Culture = tenantViewModel.Culture; tenant.TimeZone = tenantViewModel.TimeZone; tenant.Status = tenantViewModel.Status; if (ModelState.IsValid) { ctx.Entry(tenant).State = EntityState.Modified; ctx.SaveChanges(); return(RedirectToAction("Index")); } } } catch (Exception ex) { LogService.Info("Tenants not found", ex.Message, ex.InnerException); } return(View(tenantViewModel)); }
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 Edit([Bind(Include = "Id,TenantId,Code,Domain,Name,Culture,TimeZone,Updated,Created,Status")] EditApplicationViewModel applicationViewModel) { try { using (var ctx = new SaasDbContext()) { if (applicationViewModel.TenantId == CurrentTenant.TenantId || User.IsInRole("SystemAdministrator")) { //Get existing application from db var application = ctx.Applications.Where(a => a.Id == applicationViewModel.Id).FirstOrDefault(); application.Name = applicationViewModel.Name; application.Code = applicationViewModel.Code; application.Domain = applicationViewModel.Domain; application.Culture = applicationViewModel.Culture; application.TimeZone = applicationViewModel.TimeZone; application.Status = applicationViewModel.Status; if (ModelState.IsValid) { // Update application ctx.Entry(application).State = EntityState.Modified; ctx.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.TenantId = new SelectList(ctx.Tenants.ToList(), "TenantId", "Name"); } else { return(View("Authorize")); } } } catch (Exception ex) { LogService.Info("Applications not found", ex.Message, ex.InnerException); } return(View(applicationViewModel)); }
public ActionResult Create([Bind(Include = "Code,Name,Description,Culture,TimeZone,Created,Status")] Tenant tenant) { try { using (var ctx = new SaasDbContext()) { if (ModelState.IsValid) { ctx.Tenants.Add(tenant); ctx.SaveChanges(); return(RedirectToAction("Index")); } } } catch (Exception ex) { LogService.Info("Tenants not found", ex.Message, ex.InnerException); } return(View(tenant)); }
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)); }