Ejemplo n.º 1
0
        public async Task HandleEventAsync(CreateEventData eventData)
        {
            using (CurrentTenant.Change(eventData.Id, eventData.Name))
            {
                const string tenantAdminRoleName = "admin";
                var          tenantAdminRoleId   = Guid.Empty;;

                if (!await IdentityRoleManager.RoleExistsAsync(tenantAdminRoleName))
                {
                    tenantAdminRoleId = GuidGenerator.Create();
                    var tenantAdminRole = new IdentityRole(tenantAdminRoleId, tenantAdminRoleName, eventData.Id)
                    {
                        IsStatic = true,
                        IsPublic = true
                    };
                    (await IdentityRoleManager.CreateAsync(tenantAdminRole)).CheckErrors();
                }
                else
                {
                    var tenantAdminRole = await IdentityRoleManager.FindByNameAsync(tenantAdminRoleName);

                    tenantAdminRoleId = tenantAdminRole.Id;
                }

                var tenantAdminUser = await IdentityUserManager.FindByNameAsync(eventData.AdminEmailAddress);

                if (tenantAdminUser == null)
                {
                    tenantAdminUser = new IdentityUser(eventData.AdminUserId, eventData.AdminEmailAddress,
                                                       eventData.AdminEmailAddress, eventData.Id);

                    tenantAdminUser.AddRole(tenantAdminRoleId);

                    // 创建租户管理用户
                    (await IdentityUserManager.CreateAsync(tenantAdminUser)).CheckErrors();
                    (await IdentityUserManager.AddPasswordAsync(tenantAdminUser, eventData.AdminPassword)).CheckErrors();
                }
                //var identitySeedResult = await IdentityDataSeeder
                //   .SeedAsync(eventData.AdminEmailAddress, eventData.AdminPassword, eventData.Id);
                //if (!identitySeedResult.CreatedAdminUser)
                //{
                //    Logger.LogWarning("Tenant {0} admin user {1} not created!", eventData.Name, eventData.AdminEmailAddress);
                //}
                //if (!identitySeedResult.CreatedAdminRole)
                //{
                //    Logger.LogWarning("Tenant {0} admin role not created!", eventData.Name);
                //}
            }
        }
Ejemplo n.º 2
0
 public virtual Task RemoveRoleFromOrganizationUnitAsync(IdentityRole role, OrganizationUnit organizationUnit)
 {
     organizationUnit.RemoveRole(role.Id);
     return(OrganizationUnitRepository.UpdateAsync(organizationUnit));
 }
Ejemplo n.º 3
0
        public virtual async Task <IdentityDataSeedResult> SeedAsync(
            string adminEmail,
            string adminPassword,
            Guid?tenantId = null)
        {
            Check.NotNullOrWhiteSpace(adminEmail, nameof(adminEmail));
            Check.NotNullOrWhiteSpace(adminPassword, nameof(adminPassword));

            var result = new IdentityDataSeedResult();

            using (CurrentTenant.Change(tenantId))
            {
                //"admin" user
                const string adminUserName = "******";
                var          adminUser     = await UserRepository.FindByNormalizedUserNameAsync(
                    LookupNormalizer.NormalizeName(adminUserName)
                    );

                if (adminUser != null)
                {
                    return(result);
                }

                adminUser = new IdentityUser(
                    GuidGenerator.Create(),
                    adminUserName,
                    adminEmail,
                    tenantId
                    )
                {
                    Name = adminUserName
                };

                (await UserManager.CreateAsync(adminUser, adminPassword)).CheckErrors();
                result.CreatedAdminUser = true;

                //"admin" role
                const string adminRoleName = "admin";
                var          adminRole     = await RoleRepository.FindByNormalizedNameAsync(LookupNormalizer.NormalizeName(adminRoleName));

                if (adminRole == null)
                {
                    adminRole = new IdentityRole(
                        GuidGenerator.Create(),
                        adminRoleName,
                        tenantId
                        )
                    {
                        IsStatic = true,
                        IsPublic = true
                    };

                    (await RoleManager.CreateAsync(adminRole)).CheckErrors();
                    result.CreatedAdminRole = true;
                }

                (await UserManager.AddToRoleAsync(adminUser, adminRoleName)).CheckErrors();

                return(result);
            }
        }
Ejemplo n.º 4
0
 public virtual Task RemoveRoleFromOrganizationUnitAsync(IdentityRole role, OrganizationUnit organizationUnit)
 {
     organizationUnit.RemoveRole(role.Id);
     return(Task.FromResult(0));
 }