private void CreateRolesAndUsers() { //Admin role var adminRole = _context.Roles.FirstOrDefault(r => r.TenantId == _tenantId && r.Name == StaticRoleNames.Tenants.Admin); if (adminRole == null) { adminRole = new Role(_tenantId, StaticRoleNames.Tenants.Admin, StaticRoleNames.Tenants.Admin) { IsStatic = true }; adminRole.SetNormalizedName(); _context.Roles.Add(adminRole); _context.SaveChanges(); //Grant all permissions to admin role var permissions = PermissionFinder .GetAllPermissions(new TestProjectAuthorizationProvider()) .Where(p => p.MultiTenancySides.HasFlag(MultiTenancySides.Tenant)) .ToList(); foreach (var permission in permissions) { _context.Permissions.Add( new RolePermissionSetting { TenantId = _tenantId, Name = permission.Name, IsGranted = true, RoleId = adminRole.Id }); } _context.SaveChanges(); } //admin user var adminUser = _context.Users.FirstOrDefault(u => u.TenantId == _tenantId && u.UserName == User.AdminUserName); if (adminUser == null) { adminUser = User.CreateTenantAdminUser(_tenantId, "*****@*****.**", User.DefaultPassword); adminUser.IsEmailConfirmed = true; adminUser.IsActive = true; _context.Users.Add(adminUser); _context.SaveChanges(); //Assign Admin role to admin user _context.UserRoles.Add(new UserRole(_tenantId, adminUser.Id, adminRole.Id)); _context.SaveChanges(); } }
private void CreateEditions() { var defaultEdition = _context.Editions.IgnoreQueryFilters().FirstOrDefault(e => e.Name == EditionManager.DefaultEditionName); if (defaultEdition == null) { defaultEdition = new Edition { Name = EditionManager.DefaultEditionName, DisplayName = EditionManager.DefaultEditionName }; _context.Editions.Add(defaultEdition); _context.SaveChanges(); /* Add desired features to the standard edition, if wanted... */ } }
public void InitContext() { var builder = new DbContextOptionsBuilder <TestProjectDbContext>().UseInMemoryDatabase(); var context = new TestProjectDbContext(builder.Options); IEnumerable <Identifier> identifiers = Enumerable.Range(1, 10).Select(i => new Identifier { Id = Guid.NewGuid(), Type = IdentifiersType.Email, Value = $"value{i}" }); var person = Enumerable.Range(1, 10).Select(i => new Person { Id = Guid.NewGuid(), FirstName = $"FirstName{i}", LastName = $"LastName{i}", Identifiers = identifiers.ToList() }); context.Person.AddRange(person); int changed = context.SaveChanges(); _context = context; }
public void Create() { new DefaultEditionCreator(_context).Create(); new DefaultLanguagesCreator(_context).Create(); new HostRoleAndUserCreator(_context).Create(); new DefaultSettingsCreator(_context).Create(); _context.SaveChanges(); }
private void AddSettingIfNotExists(string name, string value, int?tenantId = null) { if (_context.Settings.IgnoreQueryFilters().Any(s => s.Name == name && s.TenantId == tenantId && s.UserId == null)) { return; } _context.Settings.Add(new Setting(tenantId, null, name, value)); _context.SaveChanges(); }
private void AddLanguageIfNotExists(ApplicationLanguage language) { if (_context.Languages.IgnoreQueryFilters().Any(l => l.TenantId == language.TenantId && l.Name == language.Name)) { return; } _context.Languages.Add(language); _context.SaveChanges(); }
public void Build() { var vova = new Person("Vova"); _context.People.Add(vova); _context.SaveChanges(); _context.Tasks.AddRange( new Task("Follow the white rabbit", "Follow the while rabbit in order to know that this is virtual reality"), new Task("Clean the room", "") ); }
private void CreateUserAndRoles() { //Default tenant var defaultTenant = _context.Tenants.FirstOrDefault(t => t.TenancyName == Tenant.DefaultTenantName); if (defaultTenant == null) { _context.Tenants.Add(new Tenant { TenancyName = Tenant.DefaultTenantName, Name = Tenant.DefaultTenantName }); _context.SaveChanges(); } }
private void CreateDefaultTenant() { // Default tenant var defaultTenant = _context.Tenants.IgnoreQueryFilters().FirstOrDefault(t => t.TenancyName == AbpTenantBase.DefaultTenantName); if (defaultTenant == null) { defaultTenant = new Tenant(AbpTenantBase.DefaultTenantName, AbpTenantBase.DefaultTenantName); var defaultEdition = _context.Editions.IgnoreQueryFilters().FirstOrDefault(e => e.Name == EditionManager.DefaultEditionName); if (defaultEdition != null) { defaultTenant.EditionId = defaultEdition.Id; } _context.Tenants.Add(defaultTenant); _context.SaveChanges(); } }
private void CreateHostRoleAndUsers() { // Admin role for host var adminRoleForHost = _context.Roles.IgnoreQueryFilters().FirstOrDefault(r => r.TenantId == null && r.Name == StaticRoleNames.Host.Admin); if (adminRoleForHost == null) { adminRoleForHost = _context.Roles.Add(new Role(null, StaticRoleNames.Host.Admin, StaticRoleNames.Host.Admin) { IsStatic = true, IsDefault = true }).Entity; _context.SaveChanges(); } // Grant all permissions to admin role for host var grantedPermissions = _context.Permissions.IgnoreQueryFilters() .OfType <RolePermissionSetting>() .Where(p => p.TenantId == null && p.RoleId == adminRoleForHost.Id) .Select(p => p.Name) .ToList(); var permissions = PermissionFinder .GetAllPermissions(new TestProjectAuthorizationProvider()) .Where(p => p.MultiTenancySides.HasFlag(MultiTenancySides.Host) && !grantedPermissions.Contains(p.Name)) .ToList(); if (permissions.Any()) { _context.Permissions.AddRange( permissions.Select(permission => new RolePermissionSetting { TenantId = null, Name = permission.Name, IsGranted = true, RoleId = adminRoleForHost.Id }) ); _context.SaveChanges(); } // Admin user for host var adminUserForHost = _context.Users.IgnoreQueryFilters().FirstOrDefault(u => u.TenantId == null && u.UserName == AbpUserBase.AdminUserName); if (adminUserForHost == null) { var user = new User { TenantId = null, UserName = AbpUserBase.AdminUserName, Name = "admin", Surname = "admin", EmailAddress = "*****@*****.**", IsEmailConfirmed = true, IsActive = true }; user.Password = new PasswordHasher <User>(new OptionsWrapper <PasswordHasherOptions>(new PasswordHasherOptions())).HashPassword(user, "123qwe"); user.SetNormalizedNames(); adminUserForHost = _context.Users.Add(user).Entity; _context.SaveChanges(); // Assign Admin role to admin user _context.UserRoles.Add(new UserRole(null, adminUserForHost.Id, adminRoleForHost.Id)); _context.SaveChanges(); _context.SaveChanges(); } }
public async Task Seed() { if (!_contextClass.Employees.Any()) { await _contextClass.Employees.AddRangeAsync(new List <Employee> { new Employee { FirstName = "John", Email = "*****@*****.**", Surname = "Alia", ClientCode = "42111", CustomerCode = "3212" }, new Employee { FirstName = "Lionel", Email = "*****@*****.**", Surname = "Messi", ClientCode = "123112", CustomerCode = "3332" }, new Employee { FirstName = "Neymar", Email = "*****@*****.**", Surname = "Jr", ClientCode = "231211", CustomerCode = "5466" }, new Employee { FirstName = "Isco", Email = "*****@*****.**", Surname = "Alcander", ClientCode = "322303", CustomerCode = "1321" }, }); } _contextClass.SaveChanges(); if (!_contextClass.Banks.Any()) { await _contextClass.Banks.AddRangeAsync(new List <Bank> { new Bank { Name = "Zeen", BranchCode = 4331, Reference = "Zeen Bank" }, new Bank { Name = "Alfalah", BranchCode = 5331, Reference = "Alfalah Bank" }, new Bank { Name = "Sindh", BranchCode = 2331, Reference = "Sindh Bank" }, new Bank { Name = "Yelo", BranchCode = 1331, Reference = "Yelo Bank" } }); } _contextClass.SaveChanges(); if (!_contextClass.Accounts.Any()) { await _contextClass.Accounts.AddRangeAsync(new List <Account> { new Account { AccountNumber = "01021231", BankId = 1, EmployeeId = 1, AccountType = 0, BranchCode = 4801 }, new Account { AccountNumber = "01021232", BankId = 2, EmployeeId = 2, AccountType = 0, BranchCode = 2111 }, new Account { AccountNumber = "01021233", BankId = 3, EmployeeId = 3, AccountType = 0, BranchCode = 042 }, new Account { AccountNumber = "01021234", BankId = 4, EmployeeId = 4, AccountType = 0, BranchCode = 2117 } }); } _contextClass.SaveChanges(); }
private void CreateRolesAndUsers() { // Admin role var adminRole = _context.Roles.IgnoreQueryFilters().FirstOrDefault(r => r.TenantId == _tenantId && r.Name == StaticRoleNames.Tenants.Admin); if (adminRole == null) { adminRole = _context.Roles.Add(new Role(_tenantId, StaticRoleNames.Tenants.Admin, StaticRoleNames.Tenants.Admin) { IsStatic = true }).Entity; _context.SaveChanges(); } // Grant all permissions to admin role var grantedPermissions = _context.Permissions.IgnoreQueryFilters() .OfType <RolePermissionSetting>() .Where(p => p.TenantId == _tenantId && p.RoleId == adminRole.Id) .Select(p => p.Name) .ToList(); var permissions = PermissionFinder .GetAllPermissions(new TestProjectAuthorizationProvider()) .Where(p => p.MultiTenancySides.HasFlag(MultiTenancySides.Tenant) && !grantedPermissions.Contains(p.Name)) .ToList(); if (permissions.Any()) { _context.Permissions.AddRange( permissions.Select(permission => new RolePermissionSetting { TenantId = _tenantId, Name = permission.Name, IsGranted = true, RoleId = adminRole.Id }) ); _context.SaveChanges(); } // Admin user var adminUser = _context.Users.IgnoreQueryFilters().FirstOrDefault(u => u.TenantId == _tenantId && u.UserName == AbpUserBase.AdminUserName); if (adminUser == null) { adminUser = User.CreateTenantAdminUser(_tenantId, "*****@*****.**"); adminUser.Password = new PasswordHasher <User>(new OptionsWrapper <PasswordHasherOptions>(new PasswordHasherOptions())).HashPassword(adminUser, "123qwe"); adminUser.IsEmailConfirmed = true; adminUser.IsActive = true; _context.Users.Add(adminUser); _context.SaveChanges(); // Assign Admin role to admin user _context.UserRoles.Add(new UserRole(_tenantId, adminUser.Id, adminRole.Id)); _context.SaveChanges(); } }
public int SaveChanges() { return(Cntext.SaveChanges()); }
public bool Commit() { return(_context.SaveChanges() > 0); }
private void CreateHostRoleAndUsers() { //Admin role for host var adminRoleForHost = _context.Roles.FirstOrDefault(r => r.TenantId == null && r.Name == StaticRoleNames.Host.Admin); if (adminRoleForHost == null) { adminRoleForHost = new Role { Name = StaticRoleNames.Host.Admin, DisplayName = StaticRoleNames.Host.Admin, IsStatic = true }; adminRoleForHost.SetNormalizedName(); _context.Roles.Add(adminRoleForHost); _context.SaveChanges(); //Grant all tenant permissions var permissions = PermissionFinder .GetAllPermissions(new TestProjectAuthorizationProvider()) .Where(p => p.MultiTenancySides.HasFlag(MultiTenancySides.Host)) .ToList(); foreach (var permission in permissions) { _context.Permissions.Add( new RolePermissionSetting { Name = permission.Name, IsGranted = true, RoleId = adminRoleForHost.Id }); } _context.SaveChanges(); } //Admin user for tenancy host var adminUserForHost = _context.Users.FirstOrDefault(u => u.TenantId == null && u.UserName == User.AdminUserName); if (adminUserForHost == null) { adminUserForHost = _context.Users.Add( new User { UserName = User.AdminUserName, Name = "System", Surname = "Administrator", EmailAddress = "*****@*****.**", IsEmailConfirmed = true, Password = new PasswordHasher().HashPassword(User.DefaultPassword) }); adminUserForHost.SetNormalizedNames(); _context.SaveChanges(); _context.UserRoles.Add(new UserRole(null, adminUserForHost.Id, adminRoleForHost.Id)); _context.SaveChanges(); } }