示例#1
0
        public async Task <GoblinIdentityEmailConfirmationModel> RequestConfirmEmailAsync(long id, CancellationToken cancellationToken = default)
        {
            var userEntity = await _userRepo.Get(x => x.Id == id).FirstOrDefaultAsync(cancellationToken).ConfigureAwait(true);

            if (userEntity == null)
            {
                throw new GoblinException(nameof(GoblinIdentityErrorCode.UserNotFound), GoblinIdentityErrorCode.UserNotFound);
            }

            if (userEntity.EmailConfirmedTime != null)
            {
                throw new GoblinException(nameof(GoblinIdentityErrorCode.EmailAlreadyConfirmed), GoblinIdentityErrorCode.EmailAlreadyConfirmed);
            }

            var resetConfirmEmailTokenModel = new GoblinIdentityEmailConfirmationModel
            {
                Id = id,
                EmailConfirmToken           = userEntity.EmailConfirmToken = StringHelper.Generate(6, false, false),
                EmailConfirmTokenExpireTime = userEntity.EmailConfirmTokenExpireTime = GoblinDateTimeHelper.SystemTimeNow.Add(SystemSetting.Current.EmailConfirmTokenLifetime)
            };

            _userRepo.Update(userEntity,
                             x => x.EmailConfirmToken,
                             x => x.EmailConfirmTokenExpireTime
                             );

            await GoblinUnitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(true);

            return(resetConfirmEmailTokenModel);
        }
示例#2
0
        public async Task <GoblinIdentityEmailConfirmationModel> UpdateIdentityAsync(long id,
                                                                                     GoblinIdentityUpdateIdentityModel model,
                                                                                     CancellationToken cancellationToken = default)
        {
            model.NewEmail = model.NewEmail?.Trim().ToLowerInvariant();

            model.NewUserName = model.NewUserName?.Trim().ToLowerInvariant();

            var userEntity = await _userRepo.Get(x => x.Id == id)
                             .FirstOrDefaultAsync(cancellationToken)
                             .ConfigureAwait(true);

            if (userEntity == null)
            {
                throw new GoblinException(nameof(GoblinIdentityErrorCode.UserNotFound), GoblinIdentityErrorCode.UserNotFound);
            }

            var currentPasswordHashWithOldSalt = PasswordHelper.HashPassword(model.CurrentPassword, userEntity.PasswordLastUpdatedTime);

            if (currentPasswordHashWithOldSalt != userEntity.PasswordHash)
            {
                throw new GoblinException(nameof(GoblinIdentityErrorCode.WrongPassword), GoblinIdentityErrorCode.WrongPassword);
            }

            var emailConfirmationModel = new GoblinIdentityEmailConfirmationModel
            {
                Id = userEntity.Id
            };

            var changedProperties = new List <string>();

            // Update Password
            if (!string.IsNullOrWhiteSpace(model.NewPassword))
            {
                var newPasswordHashWithOldSalt = PasswordHelper.HashPassword(model.NewPassword, userEntity.PasswordLastUpdatedTime);

                // If user have changed password, then update password and related information
                if (newPasswordHashWithOldSalt != userEntity.PasswordHash)
                {
                    userEntity.PasswordLastUpdatedTime            =
                        userEntity.RevokeTokenGeneratedBeforeTime = GoblinDateTimeHelper.SystemTimeNow;
                    changedProperties.Add(nameof(userEntity.PasswordLastUpdatedTime));
                    changedProperties.Add(nameof(userEntity.RevokeTokenGeneratedBeforeTime));

                    userEntity.PasswordHash =
                        PasswordHelper.HashPassword(model.NewPassword, userEntity.PasswordLastUpdatedTime);
                    changedProperties.Add(nameof(userEntity.PasswordHash));
                }
            }

            // Update Email
            if (!string.IsNullOrWhiteSpace(model.NewEmail) && model.NewEmail != userEntity.Email)
            {
                CheckUniqueEmail(model.NewEmail);

                userEntity.EmailConfirmToken = StringHelper.Generate(6, false, false);
                changedProperties.Add(nameof(userEntity.EmailConfirmToken));

                userEntity.EmailConfirmTokenExpireTime = GoblinDateTimeHelper.SystemTimeNow.Add(SystemSetting.Current.EmailConfirmTokenLifetime);
                changedProperties.Add(nameof(userEntity.EmailConfirmTokenExpireTime));


                // Email Confirmation Token

                emailConfirmationModel.EmailConfirmToken = userEntity.EmailConfirmToken;

                emailConfirmationModel.EmailConfirmTokenExpireTime = userEntity.EmailConfirmTokenExpireTime;
            }

            // Update UserName
            if (!string.IsNullOrWhiteSpace(model.NewUserName))
            {
                CheckUniqueUserName(model.NewUserName);

                userEntity.UserName = model.NewUserName;
                changedProperties.Add(nameof(userEntity.UserName));
            }

            if (!changedProperties.Any())
            {
                return(emailConfirmationModel);
            }

            _userRepo.Update(userEntity, changedProperties.ToArray());

            await GoblinUnitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(true);

            return(emailConfirmationModel);
        }
示例#3
0
        public async Task <GoblinIdentityEmailConfirmationModel> RegisterAsync(GoblinIdentityRegisterModel model,
                                                                               CancellationToken cancellationToken = default)
        {
            model.Email = model.Email?.Trim().ToLowerInvariant();

            model.UserName = model.UserName?.Trim().ToLowerInvariant();

            CheckUniqueEmail(model.Email);

            CheckUniqueUserName(model.UserName);

            using var transaction =
                      await GoblinUnitOfWork.BeginTransactionAsync(cancellationToken).ConfigureAwait(true);

            var userEntity = model.MapTo <UserEntity>();

            userEntity.PasswordLastUpdatedTime            =
                userEntity.RevokeTokenGeneratedBeforeTime = GoblinDateTimeHelper.SystemTimeNow;

            userEntity.PasswordHash =
                PasswordHelper.HashPassword(model.Password, userEntity.PasswordLastUpdatedTime);

            userEntity.EmailConfirmToken = StringHelper.Generate(6, false, false);

            userEntity.EmailConfirmTokenExpireTime =
                GoblinDateTimeHelper.SystemTimeNow.Add(SystemSetting.Current.EmailConfirmTokenLifetime);

            _userRepo.Add(userEntity);

            await GoblinUnitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(true);

            // User Roles

            if (model.Roles?.Any() == true)
            {
                model.Roles = model.Roles.Select(x => x.Trim()).ToList();

                var roleEntities = await _roleRepo.Get(x => model.Roles.Contains(x.Name)).ToListAsync(cancellationToken)
                                   .ConfigureAwait(true);

                foreach (var roleEntity in roleEntities)
                {
                    _userRoleRepo.Add(new UserRoleEntity
                    {
                        UserId = userEntity.Id,
                        RoleId = roleEntity.Id
                    });
                }

                await GoblinUnitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(true);
            }

            transaction.Commit();

            // Email Confirmation Code

            var emailConfirmationModel = new GoblinIdentityEmailConfirmationModel
            {
                Id = userEntity.Id,
                EmailConfirmToken           = userEntity.EmailConfirmToken,
                EmailConfirmTokenExpireTime = userEntity.EmailConfirmTokenExpireTime
            };

            return(emailConfirmationModel);
        }