Beispiel #1
0
        public async Task <IdentityDataSeedResult> SeedAsync(string adminEmail, string adminPassword, Guid?tenantId)
        {
            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;

                var adminRole = await RoleRepository.FindByNormalizedNameAsync(
                    LookupNormalizer.NormalizeName(ScoringRoleNames.Admin)
                    );

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

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

                (await UserManager.AddToRoleAsync(adminUser, ScoringRoleNames.Admin)).CheckErrors();
                var multiTenancySides = CurrentTenant.GetMultiTenancySide();
                await GrantedAllPermissionsToAdminUser(adminUser, multiTenancySides);

                return(result);
            }
        }
Beispiel #2
0
    public virtual async Task <IdentityDataSeedResult> SeedAsync(
        string adminEmail,
        string adminPassword,
        Guid?tenantId = null)
    {
        Check.NotNullOrWhiteSpace(adminEmail, nameof(adminEmail));
        Check.NotNullOrWhiteSpace(adminPassword, nameof(adminPassword));

        using (CurrentTenant.Change(tenantId))
        {
            await IdentityOptions.SetAsync();

            var result = new IdentityDataSeedResult();
            //"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, validatePassword: false)).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);
        }
    }