public async Task <ActionResult> UpdateCronjob(Guid id, CronjobUpdateRequest request, CancellationToken cancellationToken)
        {
            var cronjob = await _db.Cronjobs.SingleOrDefaultAsync(
                e => e.Id == id, cancellationToken : cancellationToken
                );

            if (cronjob is null)
            {
                return(NotFound());
            }
            // OPTIMIZE: dont load everything before authorization
            // var result = await _authorizationService.AuthorizeAsync(User, cronjob, AuthorizationPolicies.RequireProjectManagerPolicy);
            // if (!result.Succeeded)
            // {
            //     return Forbid();
            // }

            // remove registered cronjobs that belongs to existing cronjob record
            // then update its details
            await _cronjobRegistrationService.Remove(cronjob);

            // TODO: figure out how to ignore null members when mapping
            cronjob.Enabled = request.Enabled ?? cronjob.Enabled;
            cronjob.Title   = request.Title ?? cronjob.Title;
            cronjob.Cron    = _mapper.Map <CronExpression>(request.Cron) ?? cronjob.Cron;

            if (cronjob.Enabled)
            {
                await _cronjobRegistrationService.Add(cronjob);
            }

            await _db.SaveChangesAsync(cancellationToken);

            return(NoContent());
        }
Example #2
0
        public async Task <IActionResult> CreateTemplate(ConfigTemplateCreateDto dto, CancellationToken cancellationToken = default)
        {
            var templateExists = await _dbContext.ConfigTemplates.AnyAsync(e => e.Key == dto.Key, cancellationToken);

            if (templateExists)
            {
                return(BadRequest(new ProblemDetails {
                    Detail = $"A template with key '{dto.Key}' already exists"
                }));
            }

            var template = _mapper.Map <ConfigTemplate>(dto);
            await _dbContext.AddAsync(template, cancellationToken);

            await _dbContext.SaveChangesAsync(cancellationToken);

            return(Ok());
        }
Example #3
0
        private async Task CreateDefaultRoles(CancellationToken cancellationToken)
        {
            _logger.LogInformation("Creating default roles");
            if (await _dbContext.Roles.AnyAsync(cancellationToken))
            {
                _logger.LogDebug("Roles found in database, skipping seed");
                return;
            }
            await _dbContext.Roles.AddRangeAsync(IdentityDefaults.DefaultRoles, cancellationToken);

            await _dbContext.SaveChangesAsync(cancellationToken);
        }
Example #4
0
        public async Task <IActionResult> UpdateUserRoles(Guid userId, UserRoleUpdateRequest request, CancellationToken cancellationToken)
        {
            var user = await _dbContext.Users
                       .Include(e => e.Roles)
                       .FirstOrDefaultAsync(e => e.Id == userId, cancellationToken: cancellationToken);

            if (user == null)
            {
                return(NotFound(new ProblemDetails {
                    Detail = "No such user"
                }));
            }

            var otherAdminsPresent = await _dbContext.Users
                                     .Where(e => e.Id != user.Id)
                                     .AnyAsync(e => e.Roles.Any(r => r.Name == "admin"), cancellationToken);

            if (!request.RoleNames.Contains("admin") && !otherAdminsPresent)
            {
                return(BadRequest(new ProblemDetails {
                    Detail = "There must be at least one user with admin role"
                }));
            }

            user.Roles.Clear();
            var requestedRoles = await _dbContext.Roles.Where(r => request.RoleNames.Contains(r.Name))
                                 .ToListAsync(cancellationToken: cancellationToken);

            foreach (var role in requestedRoles)
            {
                user.Roles.Add(role);
            }

            await _dbContext.SaveChangesAsync(cancellationToken);

            return(NoContent());
        }
Example #5
0
        public async Task <ClaimsPrincipal> ImportUserAsync(ClaimsPrincipal principal, AuthenticationScheme authenticationScheme)
        {
            var authScheme = authenticationScheme.DisplayName ?? authenticationScheme.Name;

            var user = await _dbContext.Set <User>().FirstOrDefaultAsync(e => e.IdProvider == authScheme &&
                                                                         e.IdProviderSub ==
                                                                         principal.FindFirstValue(ClaimTypes.NameIdentifier));

            if (user != null)
            {
                _logger.LogInformation("Updating details of {UserName}", user.Name);
                user.Name  = principal.FindFirstValue(ClaimTypes.Name);
                user.Email = principal.FindFirstValue(ClaimTypes.Email);
            }
            else
            {
                user = new User
                {
                    Name          = principal.FindFirstValue(ClaimTypes.Name),
                    Email         = principal.FindFirstValue(ClaimTypes.Email),
                    IdProvider    = authScheme,
                    IdProviderSub = principal.FindFirstValue(ClaimTypes.NameIdentifier),
                };

                var isFirstUser = !await _dbContext.Users.AnyAsync();

                if (isFirstUser)
                {
                    _logger.LogInformation("{User} is the first registered user, promoting to admin role", user);
                    var adminRole = await _dbContext.Roles.FirstOrDefaultAsync(r => r.Name == "admin");

                    user.Roles.Add(adminRole);
                }

                await _dbContext.AddAsync(user);
            }

            await _dbContext.SaveChangesAsync();

            var localIdentity = new ClaimsIdentity(principal.Identity !.AuthenticationType);

            localIdentity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()));
            localIdentity.AddClaim(new Claim(ClaimTypes.Name, user.Name));
            return(new ClaimsPrincipal(localIdentity));
        }
Example #6
0
        public async Task <ActionResult> UpdateExecutionStatus(
            Guid id,
            ExecutionStatusCreateDto update,
            CancellationToken cancellationToken
            )
        {
            var existing = await _db.Executions.FirstOrDefaultAsync(
                e => e.Id == id, cancellationToken
                );

            if (existing is null)
            {
                return(NotFound());
            }

            var status = _mapper.Map <ExecutionStatus>(update);

            existing.UpdateStatus(status);
            await _db.SaveChangesAsync(cancellationToken);

            return(NoContent());
        }