예제 #1
0
        public async Task <ActionResult <CronjobDto> > CreateCronjob(
            CronjobCreateRequest request,
            CancellationToken cancellationToken
            )
        {
            var cronjob = _mapper.Map <Cronjob>(request);

            if (!await _db.Projects.AnyAsync(e => e.Id == request.ProjectId, cancellationToken))
            {
                return(BadRequest(new ProblemDetails {
                    Detail = "No such project"
                }));
            }

            var project = await _db.Projects.FindAsync(request.ProjectId);

            var result = await _authorizationService.AuthorizeAsync(User, project, AuthorizationPolicies.RequireProjectManagerPolicy);

            if (!result.Succeeded)
            {
                return(Forbid());
            }

            await using var tx = await _db.Database.BeginTransactionAsync(cancellationToken);

            await _db.AddAsync(cronjob, cancellationToken);

            await _db.SaveChangesAsync(cancellationToken);

            await _cronjobRegistrationService.Add(cronjob);

            await tx.CommitAsync(cancellationToken);

            return(_mapper.Map <CronjobDto>(cronjob));
        }
예제 #2
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));
        }
예제 #3
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());
        }