Exemple #1
0
        public async Task AddDefaultRolesAsync_In_Same_Uow()
        {
            await CreateRandomDefaultRoleAsync();

            using (var uow = _unitOfWorkManager.Begin())
            {
                var user = CreateRandomUser();

                (await _identityUserManager.CreateAsync(user)).CheckErrors();

                user.Roles.Count.ShouldBe(0);

                await _identityUserManager.AddDefaultRolesAsync(user);

                user.Roles.Count.ShouldBeGreaterThan(0);

                foreach (var roleId in user.Roles.Select(r => r.RoleId))
                {
                    var role = await _identityRoleRepository.GetAsync(roleId);

                    role.IsDefault.ShouldBe(true);
                }

                await uow.CompleteAsync();
            }
        }
Exemple #2
0
        public virtual async Task SeedAsync(
            string adminUserPassword,
            IEnumerable <string> adminRolePermissions = null,
            Guid?tenantId = null)
        {
            const string adminUserName = "******";
            const string adminRoleName = "admin";

            //"admin" user
            var adminUser = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize(adminUserName));

            if (adminUser != null)
            {
                return;
            }

            adminUser = new IdentityUser(_guidGenerator.Create(), adminUserName, "*****@*****.**", tenantId);
            CheckIdentityErrors(await _userManager.CreateAsync(adminUser, adminUserPassword));

            //"admin" role
            var adminRole = await _roleRepository.FindByNormalizedNameAsync(_lookupNormalizer.Normalize(adminRoleName));

            if (adminRole == null)
            {
                adminRole = new IdentityRole(_guidGenerator.Create(), adminRoleName, tenantId);
                CheckIdentityErrors(await _roleManager.CreateAsync(adminRole));

                if (adminRolePermissions != null)
                {
                    await AddRolePermissionsAsync(adminRole, adminRolePermissions);
                }
            }

            CheckIdentityErrors(await _userManager.AddToRoleAsync(adminUser, adminRoleName));
        }
Exemple #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();

            //"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);
        }
        public async Task <IdentityUserDto> CreateAsync(IdentityUserCreateDto input)
        {
            var user = new IdentityUser(GuidGenerator.Create(), input.UserName, CurrentTenant.Id);

            CheckIdentityErrors(await _userManager.CreateAsync(user, input.Password));
            await UpdateUserByInput(user, input);

            await CurrentUnitOfWork.SaveChangesAsync();

            return(ObjectMapper.Map <IdentityUser, IdentityUserDto>(user));
        }
Exemple #5
0
        public virtual async Task <IdentityUserDto> CreateAsync(IdentityUserCreateDto input)
        {
            var user = new IdentityUser(GuidGenerator.Create(), input.UserName, input.Email, CurrentTenant.Id);

            (await _userManager.CreateAsync(user, input.Password).ConfigureAwait(false)).CheckErrors();
            await UpdateUserByInput(user, input).ConfigureAwait(false);

            await CurrentUnitOfWork.SaveChangesAsync().ConfigureAwait(false);

            return(ObjectMapper.Map <IdentityUser, IdentityUserDto>(user));
        }
Exemple #6
0
        public async Task <IdentityUserDto> CreateAsync(IdentityUserCreateDto input)
        {
            var user = new IdentityUser(GuidGenerator.Create(), input.UserName, input.Email, CurrentTenant.Id);

            (await _userManager.CreateAsync(user, input.Password)).CheckErrors();
            await UpdateUserByInput(user, input);

            await CurrentUnitOfWork.SaveChangesAsync();

            //Send activation email
            if (input.SendActivationEmail)
            {
                user.SetNewEmailConfirmationCode();
                await _userEmailer.SendEmailActivationLinkAsync(user, input.Password);
            }

            return(ObjectMapper.Map <IdentityUser, IdentityUserDto>(user));
        }
Exemple #7
0
        public virtual async Task <IdentityDataSeedResult> SeedAsync(
            string adminUserPassword,
            Guid?tenantId = null)
        {
            var result = new IdentityDataSeedResult();

            const string adminUserName = "******";
            const string adminRoleName = "admin";

            //"admin" user
            var adminUser = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize(adminUserName));

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

            adminUser      = new IdentityUser(_guidGenerator.Create(), adminUserName, "*****@*****.**", tenantId);
            adminUser.Name = adminUserName;
            CheckIdentityErrors(await _userManager.CreateAsync(adminUser, adminUserPassword));
            result.CreatedAdminUser = true;

            //"admin" role
            var adminRole = await _roleRepository.FindByNormalizedNameAsync(_lookupNormalizer.Normalize(adminRoleName));

            if (adminRole == null)
            {
                adminRole = new IdentityRole(_guidGenerator.Create(), adminRoleName, tenantId);

                adminRole.IsStatic = true;
                adminRole.IsPublic = true;

                CheckIdentityErrors(await _roleManager.CreateAsync(adminRole));
                result.CreatedAdminRole = true;
            }

            CheckIdentityErrors(await _userManager.AddToRoleAsync(adminUser, adminRoleName));

            return(result);
        }