I really confused about your idea.
Why can one user assign many groups? (no different with role). This concept makes lots of redundant data.
I think one user shoudl only be able to assign just 1 group, 0-1 to Many.
For example: the application has 3 groups: admin, user, anonym.
- admin has all role,
- user have role 'home,usermanager, etc'
- anonym have role 'home'
You have to change the structure of the entity like this:
IdentityUser
public class IdentityUser<TKey, TLogin, TClaim, TGroup, TGroupRole> : IUser<TKey>
where TLogin : IdentityUserLogin<TKey>
where TClaim : IdentityUserClaim<TKey>
where TGroup : IdentityGroup<TKey, TGroupRole>
where TGroupRole : IdentityGroupRole<TKey>
{
public virtual string Email { get; set; }
public virtual bool EmailConfirmed { get; set; }
public virtual string PasswordHash { get; set; }
public virtual string SecurityStamp { get; set; }
public virtual string PhoneNumber { get; set; }
public virtual bool PhoneNumberConfirmed { get; set; }
public virtual bool TwoFactorEnabled { get; set; }
public virtual DateTime? LockoutEndDateUtc { get; set; }
public virtual bool LockoutEnabled { get; set; }
public virtual int AccessFailedCount { get; set; }
public virtual ICollection<TClaim> Claims { get; private set; }
public virtual ICollection<TLogin> Logins { get; private set; }
public virtual TGroup Group { get; set; }
public virtual TKey Id { get; set; }
public virtual string UserName { get; set; }
public IdentityUser()
{
this.Claims = new List<TClaim>();
this.Logins = new List<TLogin>();
}
}
IdentityGroup
public class IdentityGroup<TKey, TGroupRole> : IGroup<TKey>
where TGroupRole : IdentityGroupRole<TKey>
{
public IdentityGroup()
{
Roles = new List<TGroupRole>();
}
public TKey Id { get; set; }
public string Name { get; set; }
public virtual ICollection<TGroupRole> Roles { get; private set; }
}
GroupRole
public class IdentityGroupRole<TKey>
{
public virtual TKey RoleId { get; set; }
public virtual TKey GroupId { get; set; }
}
IdentityDbContext
public class IdentityDbContext<TUser, TRole, TKey, TUserLogin, TUserClaim, TGroup, TGroupRole> : DbContext
where TUser : IdentityUser<TKey, TUserLogin, TUserClaim, TGroup, TGroupRole>
where TRole : IdentityRole<TKey, TGroupRole>
where TUserLogin : IdentityUserLogin<TKey>
where TUserClaim : IdentityUserClaim<TKey>
where TGroup : IdentityGroup<TKey, TGroupRole>
where TGroupRole : IdentityGroupRole<TKey>
{
private IdentityConfiguration _config;
public virtual IDbSet<TUser> Users { get; set; }
public virtual IDbSet<TRole> Roles { get; set; }
public virtual IDbSet<TGroup> Groups { get; set; }
public bool RequireUniqueEmail { get; set; }
public IdentityDbContext()
: this("DefaultConnection", new IdentityConfiguration())
{
}
public IdentityDbContext(string nameOrConnectionString)
: this(nameOrConnectionString, new IdentityConfiguration())
{
}
public IdentityDbContext(string nameOrConnectionString, IdentityConfiguration config)
: base(nameOrConnectionString)
{
_config = config;
}
public IdentityDbContext(DbConnection existingConnection, DbCompiledModel model, bool contextOwnsConnection)
: base(existingConnection, model, contextOwnsConnection)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
if (modelBuilder == null)
{
throw new ArgumentNullException("modelBuilder");
}
var user = modelBuilder.Entity<TUser>().ToTable(_config.UserTableName, _config.Schema);
user.HasMany(u => u.Claims).WithRequired().HasForeignKey(uc => uc.UserId);
user.HasMany(u => u.Logins).WithRequired().HasForeignKey(ul => ul.UserId);
user.HasOptional(u => u.Group).WithMany().Map(m => m.MapKey("GroupId"));
user.Property(u => u.UserName)
.IsRequired()
.HasMaxLength(256)
.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("UserNameIndex") { IsUnique = true }));
// CONSIDER: u.Email is Required if set on options?
user.Property(u => u.Email).HasMaxLength(256);
modelBuilder.Entity<TGroupRole>().HasKey((TGroupRole r) => new
{
r.GroupId,
r.RoleId
}).ToTable(_config.GroupRoleTableName, _config.Schema);
modelBuilder.Entity<TUserLogin>().HasKey((TUserLogin l) => new
{
l.LoginProvider,
l.ProviderKey,
l.UserId
}).ToTable(_config.LoginTableName, _config.Schema);
modelBuilder.Entity<TUserClaim>().ToTable(_config.ClaimTableName, _config.Schema);
var role = modelBuilder.Entity<TRole>()
.ToTable(_config.RoleTableName, _config.Schema);
role.Property(r => r.Name)
.IsRequired()
.HasMaxLength(256)
.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("RoleNameIndex") { IsUnique = true }));
role.HasMany(r => r.Groups).WithRequired().HasForeignKey(ur => ur.RoleId).WillCascadeOnDelete();
var group = modelBuilder.Entity<TGroup>()
.ToTable(_config.GroupTableName, _config.Schema);
group.Property(r => r.Name)
.IsRequired()
.HasMaxLength(256)
.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("GroupNameIndex") { IsUnique = true }));
group.HasMany(r => r.Roles).WithRequired().HasForeignKey(ur => ur.GroupId).WillCascadeOnDelete();
//group.HasMany(g => g.Users).WithOptional().Map(m => m.MapKey("GroupId"));
}
protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, IDictionary<object, object> items)
{
if (entityEntry != null && entityEntry.State == EntityState.Added)
{
List<DbValidationError> list = new List<DbValidationError>();
TUser user = entityEntry.Entity as TUser;
if (user != null)
{
if (this.Users.Any((TUser u) => string.Equals(u.UserName, user.UserName)))
{
list.Add(new DbValidationError("User", string.Format(CultureInfo.CurrentCulture, IdentityResources.DuplicateUserName, new object[]
{
user.UserName
})));
}
if (this.RequireUniqueEmail && this.Users.Any((TUser u) => string.Equals(u.Email, user.Email)))
{
list.Add(new DbValidationError("User", string.Format(CultureInfo.CurrentCulture, IdentityResources.DuplicateEmail, new object[]
{
user.Email
})));
}
}
else
{
TRole role = entityEntry.Entity as TRole;
if (role != null && this.Roles.Any((TRole r) => string.Equals(r.Name, role.Name)))
{
list.Add(new DbValidationError("Role", string.Format(CultureInfo.CurrentCulture, IdentityResources.RoleAlreadyExists, new object[]
{
role.Name
})));
}
}
if (list.Any<DbValidationError>())
{
return new DbEntityValidationResult(entityEntry, list);
}
}
return base.ValidateEntity(entityEntry, items);
}
}
i just finish my library, full source you can download here
or you can add library via nuget here