Merge pull request #899 from abpframework/add-AppUser

Add AppUser to the MVC startup template. Fixes #809.
pull/900/head
Halil İbrahim Kalkan 7 years ago committed by GitHub
commit e6f5ccbb48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2,8 +2,8 @@
{
public static class MyProjectNameConsts
{
public const string DefaultDbTablePrefix = "App";
public const string DbTablePrefix = "App";
public const string DefaultDbSchema = null;
public const string DbSchema = null;
}
}

@ -1,5 +1,4 @@
using MyCompanyName.MyProjectName.Localization.MyProjectName;
using MyCompanyName.MyProjectName.Settings;
using Volo.Abp.Auditing;
using Volo.Abp.AuditLogging;
using Volo.Abp.BackgroundJobs;
@ -8,7 +7,6 @@ using Volo.Abp.Localization;
using Volo.Abp.Localization.Resources.AbpValidation;
using Volo.Abp.Modularity;
using Volo.Abp.PermissionManagement.Identity;
using Volo.Abp.Settings;
using Volo.Abp.VirtualFileSystem;
namespace MyCompanyName.MyProjectName

@ -0,0 +1,53 @@
using System;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.Users;
namespace MyCompanyName.MyProjectName.Users
{
/* This entity shares the same table/collection ("AbpUsers" by default) with the
* IdentityUser entity of the Identity module.
*
* - You can define your custom properties into this class.
* - You never create or delete this entity, becase it is Identity module's job.
* - You can query users from database with this entity.
* - You can update values of your custom properties.
*/
public class AppUser : FullAuditedAggregateRoot<Guid>, IUser
{
#region Base properties
/* These properties are shared with the IdentityUser entity of the Identity module.
* Do not change these properties through this class. Instead, use Identity module
* services (like IdentityUserManager) to change them.
* So, this properties are designed as read only!
*/
public virtual Guid? TenantId { get; private set; }
public virtual string UserName { get; private set; }
public virtual string Name { get; private set; }
public virtual string Surname { get; private set; }
public virtual string Email { get; private set; }
public virtual bool EmailConfirmed { get; private set; }
public virtual string PhoneNumber { get; private set; }
public virtual bool PhoneNumberConfirmed { get; private set; }
#endregion
/* Add your own properties here. Example:
*
* public virtual string MyProperty { get; set; }
*/
private AppUser()
{
}
}
}

@ -2,6 +2,7 @@
using Volo.Abp.AuditLogging.EntityFrameworkCore;
using Volo.Abp.BackgroundJobs.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.Identity;
using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.PermissionManagement.EntityFrameworkCore;
using Volo.Abp.SettingManagement.EntityFrameworkCore;
@ -20,12 +21,23 @@ namespace MyCompanyName.MyProjectName.EntityFrameworkCore
{
base.OnModelCreating(builder);
/* Include modules to your migration db context */
builder.ConfigurePermissionManagement();
builder.ConfigureSettingManagement();
builder.ConfigureBackgroundJobs();
builder.ConfigureAuditLogging();
builder.ConfigureIdentity();
/* Configure customizations for entities from the modules included */
builder.Entity<IdentityUser>(b =>
{
b.ConfigureCustomUserProperties();
});
/* Configure your own tables/entities inside the ConfigureMyProjectName method */
builder.ConfigureMyProjectName();
}
}

@ -1,12 +1,17 @@
using Microsoft.EntityFrameworkCore;
using MyCompanyName.MyProjectName.Users;
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.Modeling;
using Volo.Abp.Users.EntityFrameworkCore;
namespace MyCompanyName.MyProjectName.EntityFrameworkCore
{
[ConnectionStringName("Default")]
public class MyProjectNameDbContext : AbpDbContext<MyProjectNameDbContext>
{
public DbSet<AppUser> Users { get; set; }
public MyProjectNameDbContext(DbContextOptions<MyProjectNameDbContext> options)
: base(options)
{
@ -17,6 +22,23 @@ namespace MyCompanyName.MyProjectName.EntityFrameworkCore
{
base.OnModelCreating(builder);
/* Configure the shared tables (with included modules) here */
builder.Entity<AppUser>(b =>
{
b.ToTable("AbpUsers"); //Sharing the same table "AbpUsers" with the IdentityUser
b.ConfigureFullAudited();
b.ConfigureExtraProperties();
b.ConfigureConcurrencyStamp();
b.ConfigureAbpUser();
//Moved customization to a method so we can share it with the MyProjectNameMigrationsDbContext class
b.ConfigureCustomUserProperties();
});
/* Configure your own tables/entities inside the ConfigureMyProjectName method */
builder.ConfigureMyProjectName();
}
}

@ -1,29 +0,0 @@
using System.IO;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
namespace MyCompanyName.MyProjectName.EntityFrameworkCore
{
public class MyProjectNameDbContextFactory : IDesignTimeDbContextFactory<MyProjectNameDbContext>
{
public MyProjectNameDbContext CreateDbContext(string[] args)
{
var configuration = BuildConfiguration();
var builder = new DbContextOptionsBuilder<MyProjectNameDbContext>()
.UseSqlServer(configuration.GetConnectionString("Default"));
return new MyProjectNameDbContext(builder.Options);
}
private static IConfigurationRoot BuildConfiguration()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), "../MyCompanyName.MyProjectName.Web/"))
.AddJsonFile("appsettings.json", optional: false);
return builder.Build();
}
}
}

@ -1,5 +1,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Volo.Abp;
using Volo.Abp.Users;
namespace MyCompanyName.MyProjectName.EntityFrameworkCore
{
@ -9,26 +11,20 @@ namespace MyCompanyName.MyProjectName.EntityFrameworkCore
{
Check.NotNull(builder, nameof(builder));
var tablePrefix = MyProjectNameConsts.DefaultDbTablePrefix;
var schema = MyProjectNameConsts.DefaultDbSchema;
/* Configure your own tables/entities inside here */
/* Configure all entities here. Example:
//builder.Entity<YourEntity>(b =>
//{
// b.ToTable(MyProjectNameConsts.DbTablePrefix + "YourEntities", MyProjectNameConsts.DbSchema);
builder.Entity<Question>(b =>
{
//Configure table & schema name
//b.ToTable(tablePrefix + "Questions", schema);
//Properties
//b.Property(q => q.Title).IsRequired().HasMaxLength(QuestionConsts.MaxTitleLength);
//Configure relations
//b.HasMany(question => question.Tags).WithOne().HasForeignKey(qt => qt.QuestionId);
// //...
//});
}
//Configure indexes
//b.HasIndex(q => q.CreationTime);
});
*/
public static void ConfigureCustomUserProperties<TUser>(this EntityTypeBuilder<TUser> b)
where TUser: class, IUser
{
//b.Property(nameof(AppUser.MyProperty))...
}
}
}

@ -1,4 +1,6 @@
using Volo.Abp.Data;
using MongoDB.Driver;
using MyCompanyName.MyProjectName.Users;
using Volo.Abp.Data;
using Volo.Abp.MongoDB;
namespace MyCompanyName.MyProjectName.MongoDb
@ -6,6 +8,16 @@ namespace MyCompanyName.MyProjectName.MongoDb
[ConnectionStringName("Default")]
public class MyProjectNameMongoDbContext : AbpMongoDbContext
{
public IMongoCollection<AppUser> Users => Collection<AppUser>();
protected override void CreateModel(IMongoModelBuilder modelBuilder)
{
base.CreateModel(modelBuilder);
modelBuilder.Entity<AppUser>(b =>
{
b.CollectionName = "AbpUsers"; //Sharing the same collection "AbpUsers" with the IdentityUser
});
}
}
}

@ -9,9 +9,9 @@
<ItemGroup>
<ProjectReference Include="..\..\src\MyCompanyName.MyProjectName.Application\MyCompanyName.MyProjectName.Application.csproj" />
<ProjectReference Include="..\..\src\MyCompanyName.MyProjectName.EntityFrameworkCore\MyCompanyName.MyProjectName.EntityFrameworkCore.csproj" />
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.TestBase\Volo.Abp.TestBase.csproj" />
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.Autofac\Volo.Abp.Autofac.csproj" />
<ProjectReference Include="..\..\src\MyCompanyName.MyProjectName.EntityFrameworkCore.DbMigrations\MyCompanyName.MyProjectName.EntityFrameworkCore.DbMigrations.csproj" />
</ItemGroup>
<ItemGroup>

@ -53,11 +53,11 @@ namespace MyCompanyName.MyProjectName
var connection = new SqliteConnection("Data Source=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<MyProjectNameDbContext>()
var options = new DbContextOptionsBuilder<MyProjectNameMigrationsDbContext>()
.UseSqlite(connection)
.Options;
using (var context = new MyProjectNameDbContext(options))
using (var context = new MyProjectNameMigrationsDbContext(options))
{
context.GetService<IRelationalDatabaseCreator>().CreateTables();
}

@ -1,26 +0,0 @@
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Volo.Abp.Identity;
using Xunit;
namespace MyCompanyName.MyProjectName.Samples
{
public class SampleTest : MyProjectNameApplicationTestBase
{
private readonly IIdentityUserAppService _userAppService;
public SampleTest()
{
_userAppService = ServiceProvider.GetRequiredService<IIdentityUserAppService>();
}
[Fact]
public async Task Initial_Data_Should_Contain_Admin_User()
{
var result = await _userAppService.GetListAsync(new GetIdentityUsersInput());
result.TotalCount.ShouldBeGreaterThan(0);
result.Items.ShouldContain(u => u.UserName == "admin");
}
}
}

@ -0,0 +1,59 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using MyCompanyName.MyProjectName.Users;
using Shouldly;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Identity;
using Volo.Abp.Uow;
using Xunit;
namespace MyCompanyName.MyProjectName.Samples
{
public class SampleTests : MyProjectNameApplicationTestBase
{
private readonly IIdentityUserAppService _userAppService;
private readonly IRepository<AppUser, Guid> _appUserRepository;
private readonly IUnitOfWorkManager _unitOfWorkManager;
public SampleTests()
{
_userAppService = ServiceProvider.GetRequiredService<IIdentityUserAppService>();
_appUserRepository = ServiceProvider.GetRequiredService<IRepository<AppUser, Guid>>();
_unitOfWorkManager = ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
}
[Fact]
public async Task Initial_Data_Should_Contain_Admin_User()
{
//Act
var result = await _userAppService.GetListAsync(new GetIdentityUsersInput());
//Assert
result.TotalCount.ShouldBeGreaterThan(0);
result.Items.ShouldContain(u => u.UserName == "admin");
}
[Fact]
public async Task Should_Query_AppUser()
{
/* Need to manually start Unit Of Work because
* FirstOrDefaultAsync should be executed while db connection / context is available.
*/
using (var uow = _unitOfWorkManager.Begin())
{
//Act
var adminUser = await _appUserRepository
.Where(u => u.UserName == "admin")
.FirstOrDefaultAsync();
//Assert
adminUser.ShouldNotBeNull();
await uow.CompleteAsync();
}
}
}
}
Loading…
Cancel
Save