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();

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

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

            adminUser = new IdentityUser(
                GuidGenerator.Create(),
                Guid.Empty,
                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);
        }
Пример #2
0
        public async Task GetListByNormalizedRoleNameAsync()
        {
            var users = await UserRepository.GetListByNormalizedRoleNameAsync(LookupNormalizer.NormalizeName("supporter"));

            users.Count.ShouldBe(2);
            users.ShouldContain(u => u.UserName == "john.nash");
            users.ShouldContain(u => u.UserName == "neo");
        }
Пример #3
0
        public async Task GetRoleNamesAsync()
        {
            var john = await UserRepository.FindByNormalizedUserNameAsync(LookupNormalizer.NormalizeName("john.nash"));

            var roles = await UserRepository.GetRoleNamesAsync(john.Id);

            roles.Count.ShouldBe(3);
            roles.ShouldContain("moderator");
            roles.ShouldContain("supporter");
            roles.ShouldContain("manager");
        }
Пример #4
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);
            }
        }
        public async Task SendPasswordResetCodeAsync(string userName)
        {
            if (string.IsNullOrWhiteSpace(userName))
            {
                throw new ArgumentException("Invalid userName");
            }

            var userE = await UserManager.FindByNameAsync(LookupNormalizer.NormalizeName(userName)) ?? throw new EntityNotFoundException();

            _ = await UserManager.UpdateSecurityStampAsync(userE);

            var token = await UserManager.GeneratePasswordResetTotpTokenAsync(userE);

            await EmailSender.SendAsync(userE.Email, "Password reset code", token);
        }
Пример #6
0
        public async Task Should_Eager_Load_User_Collections()
        {
            var john = await UserRepository.FindByNormalizedUserNameAsync(LookupNormalizer.NormalizeName("john.nash"));

            john.Roles.ShouldNotBeNull();
            john.Roles.Any().ShouldBeTrue();

            john.Logins.ShouldNotBeNull();
            john.Logins.Any().ShouldBeTrue();

            john.Claims.ShouldNotBeNull();
            john.Claims.Any().ShouldBeTrue();

            john.Tokens.ShouldNotBeNull();
            john.Tokens.Any().ShouldBeTrue();

            john.OrganizationUnits.ShouldNotBeNull();
            john.OrganizationUnits.Any().ShouldBeTrue();
        }
        public async Task ResetPasswordAsync(string userName, string resetCode, string password)
        {
            if (string.IsNullOrWhiteSpace(userName) ||
                string.IsNullOrWhiteSpace(resetCode) ||
                string.IsNullOrWhiteSpace(password))
            {
                throw new ArgumentException();
            }

            var userE = await UserManager.FindByNameAsync(LookupNormalizer.NormalizeName(userName)) ?? throw new EntityNotFoundException();

            if (!(await UserManager.VerifyPasswordResetTotpTokenAsync(userE, resetCode)))
            {
                throw new BusinessException();
            }

            await UserManager.RemovePasswordAsync(userE);

            await UserManager.AddPasswordAsync(userE, password);

            await UserManager.UpdateSecurityStampAsync(userE);
        }
Пример #8
0
 public async Task FindByNormalizedUserNameAsync()
 {
     (await UserRepository.FindByNormalizedUserNameAsync(LookupNormalizer.NormalizeName("john.nash"))).ShouldNotBeNull();
     (await UserRepository.FindByNormalizedUserNameAsync(LookupNormalizer.NormalizeName("undefined-user"))).ShouldBeNull();
 }
Пример #9
0
        public virtual async Task <IdentityDataSeedResult> SeedAsync(
            string adminPhoneNumber,
            string adminPassword,
            Guid?tenantId = null)
        {
            Check.NotNullOrWhiteSpace(adminPhoneNumber, nameof(adminPhoneNumber));
            Check.NotNullOrWhiteSpace(adminPassword, nameof(adminPassword));

            var result = new IdentityDataSeedResult();

            //"超管账号" 用户
            const string adminName     = "超管账号";
            string       adminUserName = adminPhoneNumber; // 将用户名设置为手机号 userName -> phoneNumber(默认)
            var          adminUser     = await UserRepository.FindByNormalizedUserNameAsync(
                LookupNormalizer.NormalizeName(adminUserName)
                );

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

            adminUser = new IdentityUser(
                // GuidGenerator.Create (),
                new Guid("716d0fe6-3101-057b-ba30-39f53e403930"),
                adminUserName,
                adminPhoneNumber,
                tenantId
                )
            {
                Name = adminName
            };

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

            //"超级管理员" 角色
            const string adminRoleName = "超级管理员";
            var          adminRole     = await RoleRepository.FindByNormalizedNameAsync(LookupNormalizer.NormalizeName(adminRoleName));

            if (adminRole == null)
            {
                adminRole = new IdentityRole(
                    // GuidGenerator.Create (),
                    new Guid("63eab816-3619-a6d7-c82b-39f53e403b4a"),
                    adminRoleName,
                    tenantId
                    )
                {
                    IsStatic = true,
                    IsPublic = true
                };

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

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

            return(result);
        }
        public async Task <AppUserDto> GetByUsername(string username)
        {
            var userE = await IdentityUserRepository.FindByNormalizedUserNameAsync(LookupNormalizer.NormalizeName(username));

            return(ObjectMapper.Map <Volo.Abp.Identity.IdentityUser, AppUserDto>(userE));
        }
 public async Task <bool> IsUsernameAvailable(string username)
 {
     return((await IdentityUserRepository.FindByNormalizedUserNameAsync(LookupNormalizer.NormalizeName(username))) == null);
 }