public static bool UpdateUserRoles(MasterUserSession masterUserSession, long userid, List <MasterRoleType> roletypes, out Exception exception) { bool result = false; exception = null; try { var context = new ContextMaster(); List <MasterUserRole> userroles = context.UserRoles.Where(item => item.UserId == userid).ToList();//context.UserRoles.Where(item=>item.UserId==((long)Convert.ToInt16(roletypes.Any()))).ToList(); foreach (var item in userroles) { //context.UserRoles.Remove(item); context.Entry(item).State = System.Data.Entity.EntityState.Deleted; context.SaveChanges(); } context.Dispose(); context = new ContextMaster(); foreach (var item in roletypes) { MasterUserRole userrole = new MasterUserRole(); userrole.UserId = userid; userrole.RoleId = Convert.ToInt16(item); context.UserRoles.Add(userrole); context.SaveChanges(); } context.Dispose(); result = true; } catch (Exception ex) { exception = ex; } return(result); }
public static bool UpdateTenantSubscription(MasterUserSession MasterUserSession, TenantSubscription tenantsubscription, out TenantSubscription outtenantsubscription, out Exception exception) { var result = false; outtenantsubscription = null; exception = null; try { using (var context = new ContextMaster()) { var temp = new TenantSubscription(); temp = context.TenantSubscriptions.FirstOrDefault(t => t.Id == tenantsubscription.Id); temp.IsDemo = tenantsubscription.IsDemo; temp.IsActive = tenantsubscription.IsActive; temp.DateTimeStart = tenantsubscription.DateTimeStart; temp.DateTimeExpires = tenantsubscription.DateTimeExpires; temp.NumberOfFormsAllowed = tenantsubscription.NumberOfFormsAllowed; temp.NumberOfUsersAllowed = tenantsubscription.NumberOfUsersAllowed; temp.NumberOfPagesAllowed = tenantsubscription.NumberOfPagesAllowed; temp.NumberOfTemplatesAllowed = tenantsubscription.NumberOfTemplatesAllowed; temp.NumberOfFormsUsed = tenantsubscription.NumberOfFormsUsed; temp.NumberOfPagesUsed = tenantsubscription.NumberOfPagesUsed; temp.NumberOfUsersUsed = tenantsubscription.NumberOfUsersUsed; temp.NumberOfTemplatesUsed = tenantsubscription.NumberOfTemplatesUsed; temp.AllowScanning = tenantsubscription.AllowScanning; temp.AllowBranding = tenantsubscription.AllowBranding; temp.AllowTemplateWorkflows = tenantsubscription.AllowTemplateWorkflows; context.TenantSubscriptions.Attach(temp); context.Entry(temp).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); outtenantsubscription = temp; } result = true; } catch (Exception e) { exception = e; } return(result); }
public static bool UpdateUser(MasterUserSession masterUserSession, MasterUser masteruser, out MasterUser outmasteruser, out Exception exception) { bool result = false; outmasteruser = null; exception = null; try { using (var context = new ContextMaster()) { MasterUser user = new MasterUser(); user = context.Users.Where(x => x.Id == masteruser.Id).FirstOrDefault(); user.Address1 = masteruser.Address1; user.Address2 = masteruser.Address2; user.City = masteruser.City; user.Country = masteruser.Country; user.Email = masteruser.Email; user.NameFamily = masteruser.NameFamily; user.NameGiven = masteruser.NameGiven; user.PasswordHash = masteruser.PasswordHash; user.PasswordSalt = masteruser.PasswordSalt; user.PhoneMobile = masteruser.PhoneMobile; user.PhoneWork = masteruser.PhoneWork; user.ActiveDirectoryId = masteruser.ActiveDirectoryId; // user.UserName = masteruser.UserName; user.AuthenticationType = masteruser.AuthenticationType; user.ZipOrPostCode = masteruser.ZipOrPostCode; context.Users.Attach(user); context.Entry(user).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); context.Dispose(); outmasteruser = user; result = true; } } catch (Exception ex) { exception = ex; } return(result); }
public static bool CreateTenantSubscriptionAndDeactivateExistingTenantSubscription(MasterUserSession MasterUserSession, long existingtenantsubscriptionid, Subscription subscriptionMaster, TenantSubscription tenantsubscriptionMaster, out Exception exception) { var result = false; exception = null; //System.Data.Common.DbTransaction Tran = null; //ContextMaster context = new ContextMaster(); //context.ObjectContext.Connection.Open(); //Tran = context.ObjectContext.Connection.BeginTransaction(); DbContextTransaction masterTrans = null; DbContextTransaction tenantTrans = null; using (var contextMaster = new ContextMaster()) { try { using (masterTrans = contextMaster.Database.BeginTransaction()) { try { var existingSubscriptions = contextMaster.TenantSubscriptions.Where(x => (x.TenantId == existingtenantsubscriptionid) && (x.IsActive == true)).ToList(); foreach (var existingSubnscription in existingSubscriptions) { existingSubnscription.IsActive = false; contextMaster.SaveChanges(); } contextMaster.TenantSubscriptions.Add(tenantsubscriptionMaster); contextMaster.SaveChanges(); tenantsubscriptionMaster.MasterTenantSubscriptionId = tenantsubscriptionMaster.Id; contextMaster.Entry(tenantsubscriptionMaster).State = EntityState.Modified; contextMaster.SaveChanges(); var tenant = contextMaster.Tenants.SingleOrDefault(x => x.Id == tenantsubscriptionMaster.TenantId); if (tenant == null) { throw (new Exception("Unable to Find Tenant in the Master DB.")); } using (var contextTenant = new ContextTenant(tenant.DatabaseConnectionString)) { try { using (tenantTrans = contextTenant.Database.BeginTransaction()) { var existingtenantsubscriptionstenant = contextTenant.TenantSubscriptions.Where(x => (x.IsActive == true)).ToList(); foreach (var existingSubscriptionTenant in existingtenantsubscriptionstenant) { existingSubscriptionTenant.IsActive = false; contextTenant.SaveChanges(); } var subscriptionTenant = contextTenant.Subscriptions.SingleOrDefault(s => s.MasterSubscriptionId == subscriptionMaster.Id); if (subscriptionTenant == null) { subscriptionTenant = subscriptionMaster.Clone(); subscriptionTenant.Id = 0; subscriptionTenant.MasterSubscriptionId = subscriptionMaster.Id; subscriptionTenant.SubscriptionType = EntityMasterTenantType.Tenant; contextTenant.Subscriptions.Add(subscriptionTenant); contextTenant.SaveChanges(); } else { //subscriptionTenant = subscription.Clone(); //subscriptionTenant.MasterTenantId = subscription.Id;//Yeh Maam Nay kerwaya hai. //contextTenant.Entry(subscriptionTenant).State = EntityState.Modified; //contextTenant.SaveChanges(); } var count = contextTenant.Tenants.Count(); if (count != 1) { throw (new Exception($"The number of tenant entries found in the Tenant database was {count} instead of 1.")); } var tenantTenant = contextTenant.Tenants.Single(); var tenantsubscriptionTenant = tenantsubscriptionMaster; var masterTenantSubscriptionId = contextMaster.TenantSubscriptions.AsNoTracking().Where(x => x.IsActive == true).FirstOrDefault().Id; tenantsubscriptionTenant.MasterTenantSubscriptionId = tenantsubscriptionMaster.Id; tenantsubscriptionTenant.Id = 0; tenantsubscriptionTenant.SubscriptionId = subscriptionTenant.Id; tenantsubscriptionTenant.Tenant = null; tenantsubscriptionTenant.TenantId = tenantTenant.Id; tenantsubscriptionTenant.TenantSubscriptionType = EntityMasterTenantType.Tenant; contextTenant.TenantSubscriptions.Add(tenantsubscriptionTenant); contextTenant.SaveChanges(); try { tenantTrans.Commit(); masterTrans.Commit(); } catch { tenantTrans.Rollback(); masterTrans.Rollback(); } result = true; } } catch (Exception ex) { exception = ex; throw; } } } catch (Exception ex) { exception = ex; throw; //Tran.Rollback(); //Tran.Dispose(); } } } catch (Exception ex) { exception = ex; masterTrans.Rollback(); tenantTrans.Rollback(); masterTrans.Dispose(); tenantTrans.Dispose(); } } return(result); }
public static bool CreateTenant(MasterUserSession tenantUserSession, Tenant tenant, out Tenant tenantMaster, out Exception exception) { var result = false; Tenant tenantMasterTemp = null; exception = null; tenantMaster = null; try { using (var context = new ContextMaster()) { using (var transaction = context.Database.BeginTransaction()) { try { var count = context.Tenants.Count(t => t.Domain.ToLower() == tenant.Domain.ToLower()); if (count == 1) { throw (new Exception("The provided domain already exists in the system.")); } else if (count > 1) { throw (new Exception("The provided domain exists multiple times in the system.")); } tenant.DatabaseConnectionString = AzureDBConnectionStringBuilder(tenant.DatabaseConnectionString); tenantMasterTemp = context.Tenants.Add(tenant); context.SaveChanges(); tenantMasterTemp.MasterTenantId = tenantMasterTemp.Id; context.Entry(tenantMasterTemp).State = EntityState.Modified; context.SaveChanges(); if (tenantMasterTemp != null) { var tenantTenant = tenantMasterTemp.Clone(); tenantTenant.Id = 0; tenantTenant.MasterTenantId = tenant.Id; tenantTenant.TenantType = EntityMasterTenantType.Tenant; tenantTenant.AuthenticationType = AuthenticationType.None; ContextTenant.Initialize(tenantTenant.DatabaseConnectionString); // Commented out by Raheel to find a better way to automate the process. //using (var contexttenant = new ContextTenant (tenantTenant.DatabaseConnectionString)) //{ // bool seederresult = DataLayer.Seeders.SeederTenant.Seed (contexttenant, tenantTenant, out exception); // if (seederresult) // { // transaction.Commit (); // tenantMaster = tenantMasterTemp; // } // else // { // transaction.Rollback (); // } //} } } catch (Exception ex) { exception = ex; transaction.Rollback(); tenantMaster = tenant; } } } result = true; } catch (Exception e) { exception = e; tenantMaster = tenant; } return(result); }