public async Task <ActionResult <UserVM> > LoginProvider(string provider, LoginProviderVMR userProvider) { if (string.IsNullOrWhiteSpace(provider)) { return(Unauthorized()); } if (provider.ToUpper() == Providers.GOOGLE) { if (await _googleProvider.Auth(userProvider.Email, userProvider.Token)) { var user = await _context.Users.Where(u => u.Email.Value == userProvider.Email).FirstOrDefaultAsync(); if (user is null) { user = new User() { FirstName = userProvider.FirstName, LastName = userProvider.LastName, Email = new EmailVO(userProvider.Email), }; await _context.Users.AddAsync(user); await _context.SaveChangesAsync(); } var token = TokenService.GenerateToken(user); return(user.To_UserVM(token)); } } return(Unauthorized()); }
public async Task <IActionResult> PutProject(long id, ProjectVM projectVM) { // verificar validações em memória if (id != projectVM.Id) { return(BadRequest()); } if (projectVM.UserInfo.IdUser != User.Id() || projectVM.UserInfo.TypePermission != TypesOfPermissions.Owner) { return(Unauthorized()); } // verificar em banco var project = await _context.Projects.Where(p => p.Id == id).Include(p => p.Users).FirstOrDefaultAsync(); if (project is null) { return(NotFound()); } var isOwner = project.Users.Where(u => u.IdUser == User.Id()).Where(u => u.IsOwner()).Any(); if (!isOwner) { return(Unauthorized()); } project.Name = projectVM.Name; project.Description = projectVM.Description; project.StartDate = projectVM.StartDate; project.ExpectedEndDate = projectVM.ExpectedEndDate; project.IsPrivate = projectVM.IsPrivate; _context.Entry(project).State = EntityState.Modified; await _context.SaveChangesAsync(); return(NoContent()); }
public async Task <ActionResult <Goal> > PostGoal(Goal goal) { var projectUser = await _context.ProjectUser.Where(p => p.IdUser == User.Id() && p.IdProject == goal.IdProject).FirstOrDefaultAsync(); if (projectUser is null) { return(NotFound(new { Erro = "Projeto não localizado para esse usuário" })); } if (!projectUser.HasAccess()) { return(Unauthorized()); } _context.Timelines.Add(goal); await _context.SaveChangesAsync(); return(CreatedAtAction("PostGoal", new { id = goal.Id }, goal)); }
public async Task IntegrationPoliticals() { using var client = new HttpClient(); var jsonStream = await client.GetStreamAsync("https://portal.oragon.inf.br/api/political"); var usersPoliticals = await _context.Users.Where(u => u.IsPolitical).Select(u => u.IdOragon).ToArrayAsync(); var politicalList = (await JsonSerializer.DeserializeAsync <PoliticalVM[]>(jsonStream, options: new JsonSerializerOptions { PropertyNameCaseInsensitive = true } )).Where(p => !usersPoliticals.Contains(p.Id)); var userList = politicalList .Select(p => new User { Username = p.Name.Replace(" ", "").ToLower(), FirstName = p.Name.Split(" ").FirstOrDefault(), LastName = p.Name.Split(" ").LastOrDefault(), Email = new EmailVO($"{p.Name.Replace(" ", "").ToLower()}@emailtest.com"), Password = p.Name.Replace(" ", "").ToLower(), IsPolitical = true, IdOragon = p.Id, }) .Select(u => { u.Projects = MockProjects().ToList().Select(proj => new ProjectUser(TypesOfPermissions.Owner) { Project = proj, User = u }).ToList(); return(u); }); var teste = userList.ToList(); await _context.AddRangeAsync(userList); await _context.SaveChangesAsync(); }