Exemplo n.º 1
0
        public override async Task <IdentityResult> UpdateAsync(DomainUser user, CancellationToken cancellationToken = default)
        {
            string baseErrMsg = "DomainUserStore.UpdateAsync failed with {Code}: {Description}";

            try {
                _dbContext.Set <DomainUser>().Update(user);
                await _dbContext.SaveChangesAsync(cancellationToken);
            } catch (DbUpdateException ex) {
                var err = ErrorDescriber.DbUpdateException(ex);
                _logger.LogError(baseErrMsg, err.Code, err.Description);
                return(IdentityResult.Failed(err));
            }
            return(IdentityResult.Success);
        }
Exemplo n.º 2
0
        public override async Task ReplaceClaimAsync(DomainUser user, Claim claim, Claim newClaim, CancellationToken cancellationToken = default)
        {
            string baseErrMsg = "DomainUserStore.ReplaceClaimAsync failed with {Code}: {Description}";
            var    userClaim  = new IdentityUserClaim <int> {
                UserId = user.Id, ClaimType = claim.Type, ClaimValue = claim.Value
            };

            try {
                var existingClaim = await _dbContext.Set <IdentityUserClaim <int> >()
                                    .FirstOrDefaultAsync(uc => uc.UserId == user.Id, cancellationToken);

                _dbContext.Set <IdentityUserClaim <int> >().Remove(existingClaim);
                _dbContext.Set <IdentityUserClaim <int> >().Add(userClaim);
                await _dbContext.SaveChangesAsync(cancellationToken);
            } catch (DbUpdateException ex) {
                var err = ErrorDescriber.DbUpdateException(ex);
                _logger.LogError(baseErrMsg, err.Code, err.Description);
                throw new ApplicationException(err.Description, ex);
            }
        }
Exemplo n.º 3
0
        public override async Task RemoveLoginAsync(DomainUser user, string loginProvider, string providerKey, CancellationToken cancellationToken = default)
        {
            string baseErrMsg = "DomainUserStore.RemoveLoginAsync failed with {Code}: {Description}";

            try {
                var existingLogin = await _dbContext.Set <IdentityUserLogin <int> >()
                                    .FirstOrDefaultAsync(ul => ul.UserId == user.Id &&
                                                         ul.LoginProvider == loginProvider &&
                                                         ul.ProviderKey == providerKey, cancellationToken);

                if (existingLogin != default)
                {
                    _dbContext.Set <IdentityUserLogin <int> >().Remove(existingLogin);
                    await _dbContext.SaveChangesAsync(cancellationToken);
                }
            } catch (DbUpdateException ex) {
                var err = ErrorDescriber.DbUpdateException(ex);
                _logger.LogError(baseErrMsg, err.Code, err.Description);
                throw new ApplicationException(err.Description, ex);
            }
        }
Exemplo n.º 4
0
        public override async Task AddClaimsAsync(DomainUser user, IEnumerable <Claim> claims, CancellationToken cancellationToken = default)
        {
            string baseErrMsg = "DomainUserStore.AddClaimsAsync failed with {Code}: {Description}";
            var    userClaims = claims.Select(c => new IdentityUserClaim <int> {
                UserId = user.Id, ClaimType = c.Type, ClaimValue = c.Value
            });

            try {
                var existingClaims = await _dbContext.Set <IdentityUserClaim <int> >()
                                     .Where(uc => uc.UserId == user.Id)
                                     .Select(uc => new IdentityUserClaim <int> {
                    UserId = uc.UserId, ClaimType = uc.ClaimType, ClaimValue = uc.ClaimValue
                })
                                     .ToListAsync(cancellationToken);

                var claimsToAdd = userClaims.Except(existingClaims);
                _dbContext.Set <IdentityUserClaim <int> >().AddRange(claimsToAdd);
                await _dbContext.SaveChangesAsync(cancellationToken);
            } catch (DbUpdateException ex) {
                var err = ErrorDescriber.DbUpdateException(ex);
                _logger.LogError(baseErrMsg, err.Code, err.Description);
                throw new ApplicationException(err.Description, ex);
            }
        }
Exemplo n.º 5
0
 protected override async Task <IdentityUserToken <int> > FindTokenAsync(DomainUser user, string loginProvider, string name, CancellationToken cancellationToken)
 {
     return(await _dbContext.Set <IdentityUserToken <int> >().FirstOrDefaultAsync(
                ut => ut.UserId == user.Id && ut.LoginProvider == loginProvider && ut.Name == name, cancellationToken));
 }
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            builder.HasSequence <int>("seqAspNetUsers");
            builder.HasSequence <int>("seqAspNetRoles");
            builder.HasSequence <int>("seqAspNetUserClaims");
            builder.HasSequence <int>("seqAspNetRoleClaims");


            builder.Entity <IdentityUserClaim <int> >(e =>
            {
                e.Property(p => p.Id)
                .HasDefaultValueSql("NEXT VALUE FOR seqAspNetUserClaims");
            });

            builder.Entity <IdentityRole <int> >(e =>
            {
                e.Property(p => p.Id)
                .HasDefaultValueSql("NEXT VALUE FOR seqAspNetRoles");
            });

            builder.Entity <IdentityRoleClaim <int> >(e =>
            {
                e.Property(p => p.Id)
                .HasDefaultValueSql("NEXT VALUE FOR seqAspNetRoleClaims");
            });


            builder.Entity <SearchableDomainUser>(e =>
            {
                e.HasNoKey();
                e.ToView("SearchableDomainUser");
            });


            builder.Entity <DomainOrganization>(e =>
            {
                e.ToTable("AspNetOrganizations")
                .HasKey(p => p.Name);
                e.Property(p => p.Name)
                .IsUnicode(false)
                .HasMaxLength(128);
#if DBCONTEXT_HASDATA
                e.HasData(new DomainOrganization[] {
                    new DomainOrganization {
                        Name = "McDougall's"
                    },
                    new DomainOrganization {
                        Name = "Burger Squire"
                    },
                    new DomainOrganization {
                        Name = "Windy's"
                    }
                });
#endif
            });
            builder.Entity <DomainApplication>(e =>
            {
                e.ToTable("AspNetApplications")
                .HasKey(p => p.Name);
                e.Property(p => p.Name)
                .IsUnicode(false)
                .HasMaxLength(128);
#if DBCONTEXT_HASDATA
                e.HasData(new DomainApplication[] {
                    new DomainApplication {
                        Name = "DataGenie"
                    },
                    new DomainApplication {
                        Name = "InfoMaster"
                    },
                });
#endif
            });

            builder.Entity <DomainOrganizationApplication>(e =>
            {
                e.ToTable("AspNetOrganizationApplications")
                .HasKey(p => new { p.Organization, p.Application });

                e.Property(p => p.Organization)
                .IsUnicode(false)
                .HasMaxLength(128);

                e.Property(p => p.Application)
                .IsUnicode(false)
                .HasMaxLength(128);

#if DBCONTEXT_HASDATA
                e.HasData(new DomainOrganizationApplication[] {
                    new DomainOrganizationApplication {
                        Organization = "McDougall's",
                        Application  = "DataGenie"
                    },
                    new DomainOrganizationApplication {
                        Organization = "Burger Squire",
                        Application  = "DataGenie"
                    },
                    new DomainOrganizationApplication {
                        Organization = "Burger Squire",
                        Application  = "InfoMaster"
                    },
                    new DomainOrganizationApplication {
                        Organization = "Windy's",
                        Application  = "InfoMaster"
                    }
                });
#endif
            });


            builder.Entity <DomainApplicationClaim>(e =>
            {
                e.ToTable("AspNetApplicationClaims")
                .HasKey(p => new { p.Application, p.ClaimTypePrefix, p.ClaimValue });
                e.Property(p => p.Application)
                .IsUnicode(false)
                .HasMaxLength(128);
                e.Property(p => p.ClaimTypePrefix)
                .IsUnicode(false)
                .HasMaxLength(128);
                e.Property(p => p.ClaimValue)
                .IsUnicode(false)
                .HasMaxLength(128);

#if DBCONTEXT_HASDATA
                e.HasData(new DomainApplicationClaim[] {
                    new DomainApplicationClaim {
                        Application     = "DataGenie",
                        ClaimTypePrefix = "role:",
                        ClaimValue      = "admin",
                        OrgAdminable    = true
                    },
                    new DomainApplicationClaim {
                        Application     = "DataGenie",
                        ClaimTypePrefix = "role:",
                        ClaimValue      = "user",
                        OrgAdminable    = true
                    },
                    new DomainApplicationClaim {
                        Application     = "InfoMaster",
                        ClaimTypePrefix = "role:",
                        ClaimValue      = "admin",
                        OrgAdminable    = true
                    },
                    new DomainApplicationClaim {
                        Application     = "InfoMaster",
                        ClaimTypePrefix = "role:",
                        ClaimValue      = "readonly",
                        OrgAdminable    = true
                    },
                    new DomainApplicationClaim {
                        Application     = "InfoMaster",
                        ClaimTypePrefix = "role:",
                        ClaimValue      = "auditor",
                        OrgAdminable    = false
                    }
                });
#endif
            });


            builder.Entity <DomainUser>(e =>
            {
                e.Property(p => p.Organization)
                .IsUnicode(false)
                .HasMaxLength(128);

                e.Property(p => p.Id)
                .HasDefaultValueSql("NEXT VALUE FOR seqAspNetUsers");

#if DBCONTEXT_HASDATA
                var users =
                    new DomainUser[] {
                    new DomainUser {
                        Id                = -1,
                        UserName          = "******",
                        Email             = "*****@*****.**",
                        EmailConfirmed    = true,
                        PhoneNumber       = "000-111-2222",
                        Organization      = "McDougall's",
                        OrganizationAdmin = true
                    },
                    new DomainUser {
                        Id                   = -2,
                        UserName             = "******",
                        Email                = "*****@*****.**",
                        EmailConfirmed       = true,
                        PhoneNumber          = "111-222-3333",
                        PhoneNumberConfirmed = true,
                        Organization         = "Burger Squire",
                        OrganizationAdmin    = true
                    },
                    new DomainUser {
                        Id                = -3,
                        UserName          = "******",
                        Email             = "*****@*****.**",
                        EmailConfirmed    = true,
                        PhoneNumber       = "222-333-4444",
                        Organization      = "Windy's",
                        OrganizationAdmin = true
                    },
                    new DomainUser {
                        Id             = -4,
                        UserName       = "******",
                        Email          = "*****@*****.**",
                        EmailConfirmed = true,
                        PhoneNumber    = "000-111-2223",
                        Organization   = "McDougall's"
                    },
                    new DomainUser {
                        Id                   = -5,
                        UserName             = "******",
                        Email                = "*****@*****.**",
                        EmailConfirmed       = true,
                        PhoneNumber          = "111-222-3334",
                        PhoneNumberConfirmed = true,
                        Organization         = "Burger Squire"
                    },
                    new DomainUser {
                        Id             = -6,
                        UserName       = "******",
                        Email          = "*****@*****.**",
                        EmailConfirmed = true,
                        PhoneNumber    = "222-333-4445",
                        Organization   = "Windy's"
                    },
                    new DomainUser {
                        Id             = -7,
                        UserName       = "******",
                        Email          = "*****@*****.**",
                        EmailConfirmed = true,
                        PhoneNumber    = "000-111-2224",
                        Organization   = "McDougall's"
                    },
                    new DomainUser {
                        Id                   = -8,
                        UserName             = "******",
                        Email                = "*****@*****.**",
                        EmailConfirmed       = true,
                        PhoneNumber          = "111-222-3335",
                        PhoneNumberConfirmed = true,
                        Organization         = "Burger Squire"
                    },
                    new DomainUser {
                        Id             = -9,
                        UserName       = "******",
                        Email          = "*****@*****.**",
                        EmailConfirmed = true,
                        PhoneNumber    = "222-333-4446",
                        Organization   = "Windy's",
                        LockoutBegin   = new DateTime(2020, 1, 1),
                        LockoutEnd     = new DateTime(2030, 12, 13)
                    },
                    new DomainUser {
                        Id             = -10,
                        UserName       = "******",
                        Email          = "*****@*****.**",
                        EmailConfirmed = true,
                        PhoneNumber    = "222-333-4446",
                        Organization   = "Windy's"
                    },
                    new DomainUser {
                        Id             = -11,
                        UserName       = "******",
                        Email          = "*****@*****.**",
                        EmailConfirmed = true,
                        PhoneNumber    = "999-888-7777",
                        Organization   = "Burger Squire",
                        SuperAdmin     = true
                    }
                };
                foreach (var user in users)
                {
                    user.PasswordHash       = HashPassword("test");
                    user.SecurityStamp      = Guid.NewGuid().ToString();
                    user.ConcurrencyStamp   = Guid.NewGuid().ToString();
                    user.NormalizedEmail    = user.Email.ToUpper();
                    user.NormalizedUserName = user.UserName.ToUpper();
                }

                e.HasData(users);
#endif
            });


            builder.Entity <DomainUserHistory>(e =>
            {
                e.ToTable("AspNetUsersHistory")
                .HasKey(k => new { k.Id, k.DateReplaced });
            });


#if DBCONTEXT_HASDATA
            builder.Entity <IdentityUserClaim <int> >(e =>
            {
                e.HasData(new IdentityUserClaim <int>[] {
                    new IdentityUserClaim <int> {
                        Id = -9902, UserId = -1, ClaimType = "role:DataGenie", ClaimValue = "admin"
                    },
                    new IdentityUserClaim <int> {
                        Id = -9904, UserId = -2, ClaimType = "role:DataGenie", ClaimValue = "admin"
                    },
                    new IdentityUserClaim <int> {
                        Id = -9905, UserId = -2, ClaimType = "role:InfoMaster", ClaimValue = "admin"
                    },
                    new IdentityUserClaim <int> {
                        Id = -9907, UserId = -3, ClaimType = "role:InfoMaster", ClaimValue = "admin"
                    },
                    new IdentityUserClaim <int> {
                        Id = -9908, UserId = -4, ClaimType = "role:DataGenie", ClaimValue = "user"
                    },
                    new IdentityUserClaim <int> {
                        Id = -9909, UserId = -5, ClaimType = "role:DataGenie", ClaimValue = "user"
                    },
                    new IdentityUserClaim <int> {
                        Id = -9910, UserId = -5, ClaimType = "role:InfoMaster", ClaimValue = "readonly"
                    },
                    new IdentityUserClaim <int> {
                        Id = -9911, UserId = -6, ClaimType = "role:InfoMaster", ClaimValue = "readonly"
                    },
                    new IdentityUserClaim <int> {
                        Id = -9912, UserId = -7, ClaimType = "role:DataGenie", ClaimValue = "user"
                    },
                    new IdentityUserClaim <int> {
                        Id = -9913, UserId = -8, ClaimType = "role:DataGenie", ClaimValue = "user"
                    },
                    new IdentityUserClaim <int> {
                        Id = -9914, UserId = -8, ClaimType = "role:InfoMaster", ClaimValue = "readonly"
                    },
                    new IdentityUserClaim <int> {
                        Id = -9915, UserId = -9, ClaimType = "role:InfoMaster", ClaimValue = "readonly"
                    },
                    new IdentityUserClaim <int> {
                        Id = -9916, UserId = -10, ClaimType = "role:InfoMaster", ClaimValue = "auditor"
                    },
                });
            });
#endif



            builder.Entity <ChildClaim>(e => {
                e.ToTable("AspNetChildClaims")
                .HasKey(p => new { p.ParentType, p.ParentValue, p.ChildType, p.ChildValue });
                e.Property(p => p.ParentType)
                .IsUnicode(false)
                .HasMaxLength(100);
                e.Property(p => p.ParentValue)
                .IsUnicode(false)
                .HasMaxLength(100);
                e.Property(p => p.ChildType)
                .IsUnicode(false)
                .HasMaxLength(100);
                e.Property(p => p.ChildValue)
                .IsUnicode(false)
                .HasMaxLength(100);
#if DBCONTEXT_HASDATA
#endif
            });
        }