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 override void Save() { using (_context) { _context.SaveChanges(); } }
public void Create() { new DefaultEditionCreator(_context).Create(); new DefaultLanguagesCreator(_context).Create(); new HostRoleAndUserCreator(_context).Create(); new DefaultSettingsCreator(_context).Create(); _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(); }
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 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 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(); } // Student and Professor roles var studTenantId = _tenantId + 1; var profTenantId = _tenantId + 2; var studentRole = _context.Roles.IgnoreQueryFilters().FirstOrDefault(r => r.TenantId == studTenantId && r.Name == StaticRoleNames.Tenants.Student); if (studentRole == null) { studentRole = _context.Roles.Add(new Role(studTenantId, StaticRoleNames.Tenants.Student, StaticRoleNames.Tenants.Student) { IsStatic = true }).Entity; _context.SaveChanges(); } var professorRole = _context.Roles.IgnoreQueryFilters().FirstOrDefault(r => r.TenantId == profTenantId && r.Name == StaticRoleNames.Tenants.Professor); if (professorRole == null) { professorRole = _context.Roles.Add(new Role(profTenantId, StaticRoleNames.Tenants.Professor, StaticRoleNames.Tenants.Professor) { 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 eMMAAuthorizationProvider()) .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(); } ///////////////////////////////////////// /// // Student and prof user var studUser = _context.Users.IgnoreQueryFilters().FirstOrDefault(u => u.TenantId == studTenantId && u.UserName == "stud1"); if (studUser == null) { studUser = User.CreateTenantAdminUser(studTenantId, "*****@*****.**"); studUser.UserName = "******"; studUser.Name = "First"; studUser.Surname = "Secondsur"; studUser.Password = new PasswordHasher <User>(new OptionsWrapper <PasswordHasherOptions>(new PasswordHasherOptions())).HashPassword(studUser, "123qwe"); studUser.IsEmailConfirmed = true; studUser.IsActive = true; _context.Users.Add(studUser); _context.SaveChanges(); // Assign Admin role to admin user _context.UserRoles.Add(new UserRole(studTenantId, studUser.Id, studentRole.Id)); _context.SaveChanges(); } var profUser = _context.Users.IgnoreQueryFilters().FirstOrDefault(u => u.TenantId == profTenantId && u.UserName == "prof1"); if (profUser == null) { profUser = User.CreateTenantAdminUser(profTenantId, "*****@*****.**"); profUser.UserName = "******"; profUser.Name = "Firstprof"; profUser.Surname = "Secondprof"; profUser.Password = new PasswordHasher <User>(new OptionsWrapper <PasswordHasherOptions>(new PasswordHasherOptions())).HashPassword(profUser, "123qwe"); profUser.IsEmailConfirmed = true; profUser.IsActive = true; _context.Users.Add(profUser); _context.SaveChanges(); _context.UserRoles.Add(new UserRole(profTenantId, profUser.Id, professorRole.Id)); _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 eMMAAuthorizationProvider()) .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(); } }
private void CreateUniSubject() { //.NET object var subjectNET = new UniSubject() { Id = new Guid(), Title = "Introducere in .NET", Credits = 4 }; var subject = _context.UniSubjects.FirstOrDefault(r => r.Title == subjectNET.Title); if (subject == null) { _context.UniSubjects.Add(subjectNET); } //ML object var subjectML = new UniSubject() { Id = new Guid(), Title = "Invatare Automata", Credits = 6, Description = "Invata automat." }; var subject2 = _context.UniSubjects.FirstOrDefault(r => r.Title == subjectML.Title); if (subject2 == null) { _context.UniSubjects.Add(subjectML); } //Python object var subjectPython = new UniSubject() { Id = new Guid(), Title = "Python", Credits = 4, Description = "Cum sa pescuiesti pesti" }; var subject3 = _context.UniSubjects.FirstOrDefault(r => r.Title == subjectPython.Title); if (subject3 == null) { _context.UniSubjects.Add(subjectPython); } //Inteligenta artificala object var subjectIA = new UniSubject() { Id = new Guid(), Title = "Inteligenta artificiala", Credits = 6, Description = "Invata si mai automat decat automat" }; var subject4 = _context.UniSubjects.FirstOrDefault(r => r.Title == subjectIA.Title); if (subject4 == null) { _context.UniSubjects.Add(subjectIA); } _context.SaveChanges(); }