Exemple #1
0
 /// <summary>
 /// Supply conversion from <see cref="string"/> to <seealso cref="Base64Id"/> otherwise use default implementation.
 /// </summary>
 /// <param name="context"></param>
 /// <param name="culture"></param>
 /// <param name="value"></param>
 public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
 {
     if (value is string)
     {
         return(Base64Id.Parse((string)value));
     }
     return(base.ConvertFrom(context, culture, value));
 }
Exemple #2
0
        /// <inheritdoc />
        public async Task <ILockLease> AcquireLock(string name, TimeSpan?timeout = null, CancellationToken cancellationToken = default)
        {
            await _signal.WaitAsync();

            var leaseId = new Base64Id(Guid.NewGuid()).ToString();

            _logger.LogInformation("Item with lease id {0} acquired the lock.", leaseId);
            return(new LockLease(leaseId, name, this));
        }
Exemple #3
0
        public async Task <IActionResult> Track([FromRoute] Base64Id trackingCode)
        {
            var campaignId = trackingCode.Id;
            var campaign   = await CampaignService.GetCampaignById(campaignId);

            if (campaign is null)
            {
                return(NotFound());
            }
            await CampaignService.UpdateCampaignVisit(campaignId);

            return(Redirect(campaign.ActionUrl));
        }
Exemple #4
0
        public async Task GetFileTest()
        {
            var folder   = new Base64Id(Guid.NewGuid());
            var filename = $"{new Base64Id(Guid.NewGuid())}.txt";
            await _FileService.SaveAsync($"getfiles/{folder}/{filename}", Encoding.UTF8.GetBytes($"This is the contents of the file. {DateTime.UtcNow:D}"));

            var data = await _FileService.GetAsync($"getfiles/{folder}/{filename}");

            var contents = Encoding.UTF8.GetString(data);

            Assert.StartsWith("This is the contents of the file", contents);
            await _FileService.DeleteAsync($"getfiles");
        }
Exemple #5
0
        public async Task UploadFile()
        {
            var folder   = new Base64Id(Guid.NewGuid());
            var filename = $"{new Base64Id(Guid.NewGuid())}.txt";
            var contents = Encoding.UTF8.GetBytes($"This is the contents of the file. {DateTime.UtcNow:D}");
            await _FileService.SaveAsync($"uploads/{folder}/{filename}", contents);

            var properties = await _FileService.GetPropertiesAsync($"uploads/{folder}/{filename}");

            Assert.Equal("text/plain", properties.ContentType);
            Assert.Equal(contents.Length, properties.Length);
            await _FileService.SaveAsync($"uploads/{folder}/{filename}", Encoding.UTF8.GetBytes("Updated contents"));

            await _FileService.DeleteAsync($"uploads");
        }
Exemple #6
0
        public async Task <IActionResult> ValidatePassword([FromBody] ValidatePasswordRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(new ValidationProblemDetails(ModelState)));
            }
            User user = null;

            if (!string.IsNullOrWhiteSpace(request.Token) && Base64Id.TryParse(request.Token, out var userId))
            {
                user = await _userManager.FindByIdAsync(userId.Id.ToString());
            }
            var userAvailable     = user != null;
            var userNameAvailable = !string.IsNullOrWhiteSpace(request.UserName);
            var availableRules    = GetAvailableRules(userAvailable, userNameAvailable).ToDictionary(rule => rule.Key, rule => new PasswordRuleInfo {
                Code        = rule.Key,
                IsValid     = true,
                Description = rule.Value.Description,
                Requirement = rule.Value.Hint
            });

            foreach (var validator in _userManager.PasswordValidators)
            {
                var userInstance = user ?? (userNameAvailable ? new User {
                    UserName = request.UserName
                } : new User());
                var result       = await validator.ValidateAsync(_userManager, userInstance, request.Password ?? string.Empty);

                if (!result.Succeeded)
                {
                    foreach (var error in result.Errors)
                    {
                        if (availableRules.ContainsKey(error.Code))
                        {
                            availableRules[error.Code].IsValid = false;
                        }
                    }
                }
            }
            return(Ok(new CredentialsValidationInfo {
                PasswordRules = availableRules.Values.ToList()
            }));
        }
Exemple #7
0
        public async Task GetDirectoryListTest()
        {
            var folder = new Base64Id(Guid.NewGuid());
            await _FileService.SaveAsync($"listing/{folder}/{new Base64Id(Guid.NewGuid())}.txt", Encoding.UTF8.GetBytes($"This is the contents of the file. {DateTime.UtcNow:D}"));

            await _FileService.SaveAsync($"listing/{folder}/{new Base64Id(Guid.NewGuid())}.txt", Encoding.UTF8.GetBytes($"This is the contents of the file. {DateTime.UtcNow:D}"));

            await _FileService.SaveAsync($"listing/{folder}/{new Base64Id(Guid.NewGuid())}.txt", Encoding.UTF8.GetBytes($"This is the contents of the file. {DateTime.UtcNow:D}"));

            await _FileService.SaveAsync($"listing/{folder}/{new Base64Id(Guid.NewGuid())}.txt", Encoding.UTF8.GetBytes($"This is the contents of the file. {DateTime.UtcNow:D}"));

            var list = await _FileService.SearchAsync($"listing/{folder}/");

            var list2 = await _FileService.SearchAsync($"listing/{folder}");

            var list3 = await _FileService.SearchAsync($"listing");

            await _FileService.DeleteAsync($"listing");

            Assert.Equal(4, list.Count());
        }
Exemple #8
0
        public async Task DeleteFileTest()
        {
            var folder   = new Base64Id(Guid.NewGuid());
            var filename = $"{new Base64Id(Guid.NewGuid())}.txt";
            await _FileService.SaveAsync($"deletefiles/{folder}/{filename}", Encoding.UTF8.GetBytes($"This is the contents of the file. {DateTime.UtcNow:D}"));

            await _FileService.SaveAsync($"deletefiles/{folder}/2_{filename}", Encoding.UTF8.GetBytes($"This is the contents of the file. {DateTime.UtcNow:D}"));

            await _FileService.SaveAsync($"deletefiles/{folder}/3_{filename}", Encoding.UTF8.GetBytes($"This is the contents of the file. {DateTime.UtcNow:D}"));

            var ok = await _FileService.DeleteAsync($"deletefiles/{folder}/{filename}");

            Assert.True(ok);
            var ok2 = await _FileService.DeleteAsync($"deletefiles/{folder}/", isDirectory : true);

            Assert.True(ok2);
            var list = await _FileService.SearchAsync($"deletefiles/{folder}");

            await _FileService.DeleteAsync($"deletefiles");

            Assert.Empty(list);
        }
Exemple #9
0
        /// <inheritdoc/>
        public async Task <ILockLease> Renew(string name, string leaseId, CancellationToken cancellationToken = default)
        {
            var  base64Id = Base64Id.Parse(leaseId);
            bool success;

            try {
                var affecterRows = await _dbContext.Database.ExecuteSqlRawAsync(_queryDescriptor.RenewLease, new List <object> {
                    base64Id.Id
                }, cancellationToken);

                success = affecterRows > 0;
            } catch (SqlException) {
                await Cleanup();

                success = false;
            }
            if (!success)
            {
                throw new LockManagerException($"Unable to renew lease {name} for leaseid {leaseId}.");
            }
            return(new LockLease(base64Id, name, this));
        }
Exemple #10
0
 public async Task <IActionResult> GetCampaignAttachment([FromRoute] Base64Id fileGuid, [FromRoute] string format) => await GetFile("campaigns", fileGuid, format);
 /// <inheritdoc/>
 public async Task ReleaseLock(ILockLease @lock)
 {
     var query = @"DELETE FROM [work].[Lock] WHERE ([Name] = {0} AND [Id] < {1}) OR [ExpirationDate] < GetDate();";
     await _dbContext.Database.ExecuteSqlRawAsync(query, @lock.Name, Base64Id.Parse(@lock.LeaseId).Id);
 }
Exemple #12
0
 /// <inheritdoc/>
 public async Task ReleaseLock(ILockLease @lock) => await _dbContext.Database.ExecuteSqlRawAsync(_queryDescriptor.ReleaseLock, @lock.Name, Base64Id.Parse(@lock.LeaseId).Id);