예제 #1
0
        public async Task <bool> UserActivation(UserActivationDto activationDto)
        {
            using (var _activateTrans = _userProfileDbContext.Database.BeginTransaction())
            {
                var activate = await _userProfileDbContext.UserActivationModel.FirstOrDefaultAsync(a => a.ActivationCode == activationDto.ActivationCode &&
                                                                                                   a.UserModelId == activationDto.UserId);

                if (activate != null)
                {
                    //check if code has expired
                    var checkTime = activate.ExpiresAt >= (DateTime.UtcNow) ? true : false;
                    if (checkTime)
                    {
                        //delete the code from the database
                        _userProfileDbContext.Remove(activate);
                        await _userProfileDbContext.SaveChangesAsync();

                        //update user table with statuses
                        var user = await _userProfileDbContext.UserModel.FirstOrDefaultAsync(u => u.Id == activationDto.UserId);

                        if (user != null)
                        {
                            user.IsActivated = true;
                            user.IsActive    = true;

                            _userProfileDbContext.Entry(user).State = EntityState.Modified;
                            await _userProfileDbContext.SaveChangesAsync();
                        }

                        //commit to database
                        _activateTrans.Commit();

                        return(true);
                    }
                    return(false);
                }
            }
            return(false);
        }