Exemplo n.º 1
0
        public async Task <IActionResult> CreateRoyalePublisher([FromBody] CreateRoyalePublisherRequest request)
        {
            var currentUser = await _userManager.FindByNameAsync(User.Identity.Name);

            if (currentUser is null)
            {
                return(BadRequest());
            }

            IReadOnlyList <RoyaleYearQuarter> supportedQuarters = await _royaleService.GetYearQuarters();

            var selectedQuarter = supportedQuarters.Single(x => x.YearQuarter.Year == request.Year && x.YearQuarter.Quarter == request.Quarter);

            if (!selectedQuarter.OpenForPlay || selectedQuarter.Finished)
            {
                return(BadRequest());
            }

            var existingPublisher = await _royaleService.GetPublisher(selectedQuarter, currentUser);

            if (existingPublisher.HasValue)
            {
                return(BadRequest());
            }

            RoyalePublisher publisher = await _royaleService.CreatePublisher(selectedQuarter, currentUser, request.PublisherName);

            return(Ok(publisher.PublisherID));
        }
        public async Task <ActionResult> CurrentUser()
        {
            var currentUser = await _userManager.FindByNameAsync(User.Identity.Name);

            var roles = await _userManager.GetRolesAsync(currentUser);

            FantasyCriticUserViewModel vm = new FantasyCriticUserViewModel(currentUser, roles);

            return(Ok(vm));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Refresh([FromBody] TokenRefreshRequest request)
        {
            var principal    = _tokenService.GetPrincipalFromExpiredToken(request.Token);
            var emailAddress = principal.Identity.Name; //this is mapped to the Name claim by default

            var user = await _userManager.FindByNameAsync(emailAddress);

            if (user == null)
            {
                return(BadRequest());
            }

            var refreshTokens = await _userManager.GetRefreshTokens(user);

            if (!refreshTokens.Contains(request.RefreshToken))
            {
                return(BadRequest());
            }

            string issuedTimeString = principal.Claims.FirstOrDefault(x => x.Type == "nbf")?.Value;

            if (issuedTimeString == null)
            {
                return(BadRequest("Invalid JWT."));
            }

            Instant issuedTime = Instant.FromUnixTimeSeconds(Convert.ToInt64(issuedTimeString));

            if (issuedTime < user.LastChangedCredentials)
            {
                return(StatusCode(401));
            }

            var roles = await _userManager.GetRolesAsync(user);

            var claims          = user.GetUserClaims(roles);
            var newJwtToken     = _tokenService.GenerateAccessToken(claims);
            var newRefreshToken = _tokenService.GenerateRefreshToken();

            await _userManager.RemoveRefreshToken(user, request.RefreshToken);

            await _userManager.AddRefreshToken(user, newRefreshToken);

            var newJwtString = new JwtSecurityTokenHandler().WriteToken(newJwtToken);

            return(new ObjectResult(new
            {
                token = newJwtString,
                refreshToken = newRefreshToken,
                expiration = newJwtToken.ValidTo
            }));
        }
Exemplo n.º 4
0
        public override async Task OnConnectedAsync()
        {
            var currentUser = await _userManager.FindByNameAsync(Context.User.Identity.Name);

            var leagues = await _fantasyCriticService.GetLeaguesForUser(currentUser);

            List <LeagueYear> draftingLeagueYears = new List <LeagueYear>();

            foreach (var league in leagues)
            {
                foreach (var year in league.Years)
                {
                    var leagueYear = await _fantasyCriticService.GetLeagueYear(league.LeagueID, year);

                    if (leagueYear.HasValue && leagueYear.Value.PlayStatus.DraftIsActive)
                    {
                        draftingLeagueYears.Add(leagueYear.Value);
                    }
                }
            }

            foreach (var leagueYear in draftingLeagueYears)
            {
                await Groups.AddToGroupAsync(Context.ConnectionId, GetGroupName(leagueYear));
            }
        }
Exemplo n.º 5
0
        public async Task <IActionResult> CreateMasterGameRequest([FromBody] MasterGameRequestRequest request)
        {
            var currentUser = await _userManager.FindByNameAsync(User.Identity.Name);

            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            MasterGameRequest domainRequest = request.ToDomain(currentUser, _clock.GetCurrentInstant());

            await _interLeagueService.CreateMasterGameRequest(domainRequest);

            return(Ok());
        }
Exemplo n.º 6
0
        public async Task <IActionResult> CreateLeague([FromBody] CreateLeagueRequest request)
        {
            var currentUser = await _userManager.FindByNameAsync(User.Identity.Name);

            if (currentUser == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            if (!request.ValidForOldYears())
            {
                return(BadRequest());
            }

            var supportedYears = await _interLeagueService.GetSupportedYears();

            var selectedSupportedYear = supportedYears.SingleOrDefault(x => x.Year == request.InitialYear);

            if (selectedSupportedYear is null)
            {
                return(BadRequest());
            }

            var userIsBetaUser = await _userManager.IsInRoleAsync(currentUser, "BetaTester");

            bool yearIsOpen = selectedSupportedYear.OpenForCreation || (userIsBetaUser && selectedSupportedYear.OpenForBetaUsers);

            if (!yearIsOpen)
            {
                return(BadRequest());
            }

            EligibilityLevel eligibilityLevel = await _interLeagueService.GetEligibilityLevel(request.MaximumEligibilityLevel);

            LeagueCreationParameters domainRequest = request.ToDomain(currentUser, eligibilityLevel);
            var league = await _fantasyCriticService.CreateLeague(domainRequest);

            if (league.IsFailure)
            {
                return(BadRequest(league.Error));
            }

            return(Ok());
        }
        public async Task <IActionResult> CreateLeague([FromBody] CreateLeagueRequest request)
        {
            var currentUser = await _userManager.FindByNameAsync(User.Identity.Name);

            if (currentUser == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            EligibilityLevel eligibilityLevel = await _fantasyCriticService.GetEligibilityLevel(request.MaximumEligibilityLevel);

            LeagueCreationParameters domainRequest = request.ToDomain(currentUser, eligibilityLevel);
            await _fantasyCriticService.CreateLeague(domainRequest);

            return(Ok());
        }
Exemplo n.º 8
0
        public async Task <IActionResult> MyLeagues()
        {
            var currentUser = await _userManager.FindByNameAsync(User.Identity.Name);

            IReadOnlyList <League> myLeagues = await _fantasyCriticService.GetLeaguesForUser(currentUser);

            List <LeagueViewModel> viewModels = new List <LeagueViewModel>();

            foreach (var league in myLeagues)
            {
                bool isManager = (league.LeagueManager.UserID == currentUser.UserID);
                viewModels.Add(new LeagueViewModel(league, isManager));
            }

            return(Ok(viewModels));
        }