Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated teams layout to include members & removed members page #256

Merged
merged 14 commits into from
Feb 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions ServerCore/Helpers/TeamHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@
using System.Threading.Tasks;
using ServerCore.DataModel;

using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using ServerCore.ModelBases;

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using ServerCore.DataModel;
using ServerCore.Helpers;
using ServerCore.ModelBases;

namespace ServerCore.Helpers
{
/// <summary>
Expand Down Expand Up @@ -39,5 +55,67 @@ public static async Task DeleteTeamAsync(PuzzleServerContext context, Team team)

await context.SaveChangesAsync();
}

/// <summary>
/// Adds a user to a team after performing a number of checks to make sure that the change is valid
/// </summary>
/// <param name="context">The context to update</param>
/// <param name="Event">The event that the team is for</param>
/// <param name="EventRole">The event role of the user that is making this change</param>
/// <param name="teamId">The id of the team the player should be added to</param>
/// <param name="userId">The user that should be added to the team</param>
/// <returns>
/// A tuple where the first element is a boolean that indicates whether the player was successfully
/// added to the team and the second element is a message to display that explains the error in the
/// case where the user was not successfully added to the team
/// </returns>
public static async Task<Tuple<bool, string>> AddMemberAsync(PuzzleServerContext context, Event Event, EventRole EventRole, int teamId, int userId)
{
Team team = await context.Teams.FirstOrDefaultAsync(m => m.ID == teamId);
if (team == null)
{
return new Tuple<bool, string>(false, $"Could not find team with ID '{teamId}'. Check to make sure the team hasn't been removed.");
}

var currentTeamMembers = await context.TeamMembers.Where(members => members.Team.ID == team.ID).ToListAsync();
if (currentTeamMembers.Count >= Event.MaxTeamSize && EventRole != EventRole.admin)
{
return new Tuple<bool, string>(false, $"The team '{team.Name}' is full.");
}

PuzzleUser user = await context.PuzzleUsers.FirstOrDefaultAsync(m => m.ID == userId);
if (user == null)
{
return new Tuple<bool, string>(false, $"Could not find user with ID '{userId}'. Check to make sure the user hasn't been removed.");
}

if (user.EmployeeAlias == null && currentTeamMembers.Where((m) => m.Member.EmployeeAlias == null).Count() >= Event.MaxExternalsPerTeam)
{
return new Tuple<bool, string>(false, $"The team '{team.Name}' is already at its maximum count of non-employee players, and '{user.Email}' has no registered alias.");
}

if (await (from teamMember in context.TeamMembers
where teamMember.Member == user &&
teamMember.Team.Event == Event
select teamMember).AnyAsync())
{
return new Tuple<bool, string>(false, $"'{user.Email}' is already on a team in this event.");
}

TeamMembers Member = new TeamMembers();
Member.Team = team;
Member.Member = user;

// Remove any applications the user might have started for this event
var allApplications = from app in context.TeamApplications
where app.Player == user &&
app.Team.Event == Event
select app;
context.TeamApplications.RemoveRange(allApplications);

context.TeamMembers.Add(Member);
await context.SaveChangesAsync();
return new Tuple<bool, string>(true, "");
}
}
}
2 changes: 1 addition & 1 deletion ServerCore/Pages/Puzzles/SubmitFeedback.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace ServerCore.Pages.Puzzles
{
// [Authorize(Policy = "PlayerIsOnTeam, PlayerCanSeePuzzle")] // TODO: These auth checks not working currently.
// [Authorize(Policy = "PlayerIsOnTeam, PlayerCanSeePuzzle")] // TODO: These auth checks not working currently.
public class SubmitFeedbackModel : EventSpecificPageModel
{
public SubmitFeedbackModel(PuzzleServerContext serverContext, UserManager<IdentityUser> userManager) : base(serverContext, userManager)
Expand Down
10 changes: 10 additions & 0 deletions ServerCore/Pages/Responses/Create.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ public async Task<IActionResult> OnPostAsync(int puzzleId)

PuzzleResponse.PuzzleID = puzzleId;

// Ensure that the response text is unique across all responses for this puzzle.
foreach (Response r in _context.Responses)
{
if (r.SubmittedText.Equals(PuzzleResponse.SubmittedText))
{
ModelState.AddModelError("PuzzleResponse.SubmittedText", "Submission text is not unique");
return await OnGetAsync(puzzleId);
}
}

_context.Responses.Add(PuzzleResponse);
await _context.SaveChangesAsync();

Expand Down
22 changes: 21 additions & 1 deletion ServerCore/Pages/Responses/CreateBulk.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,26 @@ public async Task<IActionResult> OnPostAsync(int puzzleId)
break;
}

// Ensure that the submission text is unique for this puzzle.
bool isSubmittedTextUnique(string text)
{
foreach (Response r in _context.Responses)
{
if (r.SubmittedText.Equals(ServerCore.DataModel.Response.FormatSubmission(text)))
{
ModelState.AddModelError("SubmittedText", "Submission text is not unique");
return false;
}
}

return true;
};

if (isSubmittedTextUnique(submittedText) == false)
{
break;
}

if (responseText == null)
{
ModelState.AddModelError("SubmittedText", "Unmatched Submission without Response");
Expand All @@ -102,7 +122,7 @@ public async Task<IActionResult> OnPostAsync(int puzzleId)

if (!ModelState.IsValid)
{
return Page();
return await OnGetAsync(puzzleId);
}

await _context.SaveChangesAsync();
Expand Down
2 changes: 1 addition & 1 deletion ServerCore/Pages/Teams/AddMember.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<h2>@Html.DisplayFor(model => model.Team.Name): Add member</h2>

<div>
<a asp-page="./Members" asp-route-teamId="@Model.Team.ID">Cancel</a>
<a asp-page="./Details" asp-route-teamId="@Model.Team.ID">Cancel</a>
</div>

<h4>Select a user to add to this team:</h4>
Expand Down
114 changes: 12 additions & 102 deletions ServerCore/Pages/Teams/AddMember.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using ServerCore.DataModel;
using ServerCore.Helpers;
using ServerCore.ModelBases;

namespace ServerCore.Pages.Teams
{
// TODO: Uncomment when auth can read teamId from the route
//[Authorize("IsEventAdminOrPlayerOnTeam")]
[Authorize("IsEventAdmin")]
public class AddMemberModel : EventSpecificPageModel
{
public Team Team { get; set; }
Expand All @@ -34,114 +34,24 @@ public async Task<IActionResult> OnGetAsync(int teamId)
return NotFound("Could not find team with ID '" + teamId + "'. Check to make sure you're accessing this page in the context of a team.");
}

if (EventRole == EventRole.play)
{
// Get all users that want to be on this team
Users = await (from application in _context.TeamApplications
where application.Team == Team &&
!((from teamMember in _context.TeamMembers
where teamMember.Member == application.Player &&
teamMember.Team.Event == Event
select teamMember).Any())
select new Tuple<PuzzleUser, int>(application.Player, application.ID)).ToListAsync();
}
else
{
Debug.Assert(EventRole == EventRole.admin);

// Admins can add anyone
Users = await (from user in _context.PuzzleUsers
where !((from teamMember in _context.TeamMembers
where teamMember.Team.Event == Event
where teamMember.Member == user
select teamMember).Any())
select new Tuple<PuzzleUser, int>(user, -1)).ToListAsync();
}
Users = await (from user in _context.PuzzleUsers
where !((from teamMember in _context.TeamMembers
where teamMember.Team.Event == Event
where teamMember.Member == user
select teamMember).Any())
select new Tuple<PuzzleUser, int>(user, -1)).ToListAsync();

return Page();
}

public async Task<IActionResult> OnGetAddMemberAsync(int teamId, int userId, int applicationId)
{
if (applicationId == -1)
{
if (EventRole != EventRole.admin)
{
return Forbid();
}
}

if (EventRole == EventRole.play && !Event.IsTeamMembershipChangeActive)
Tuple<bool, string> result = TeamHelper.AddMemberAsync(_context, Event, EventRole, teamId, userId).Result;
if (result.Item1)
{
return NotFound("Team membership change is not currently active.");
return RedirectToPage("./Details", new { teamId = teamId });
}

Team team = await _context.Teams.FirstOrDefaultAsync(m => m.ID == teamId);
if (team == null)
{
return NotFound($"Could not find team with ID '{teamId}'. Check to make sure the team hasn't been removed.");
}

var currentTeamMembers = await _context.TeamMembers.Where(members => members.Team.ID == team.ID).ToListAsync();
if (currentTeamMembers.Count >= Event.MaxTeamSize && EventRole != EventRole.admin)
{
return NotFound($"The team '{team.Name}' is full.");
}

PuzzleUser user = await _context.PuzzleUsers.FirstOrDefaultAsync(m => m.ID == userId);
if (user == null)
{
return NotFound($"Could not find user with ID '{userId}'. Check to make sure the user hasn't been removed.");
}

if (user.EmployeeAlias == null && currentTeamMembers.Where((m) => m.Member.EmployeeAlias == null).Count() >= Event.MaxExternalsPerTeam)
{
return NotFound($"The team '{team.Name}' is already at its maximum count of non-employee players, and '{user.Email}' has no registered alias.");
}

if (await (from teamMember in _context.TeamMembers
where teamMember.Member == user &&
teamMember.Team.Event == Event
select teamMember).AnyAsync())
{
return NotFound($"'{user.Email}' is already on a team in this event.");
}

TeamMembers Member = new TeamMembers();
Member.Team = team;
Member.Member = user;

if (applicationId != -1)
{
TeamApplication application = await (from app in _context.TeamApplications
where app.ID == applicationId
select app).FirstOrDefaultAsync();
if (application == null)
{
return NotFound("Could not find application");
}

if (application.Player.ID != userId)
{
return NotFound("Mismatched player and application");
}

if (application.Team != team)
{
return Forbid();
}
}

// Remove any applications the user might have started for this event
var allApplications = from app in _context.TeamApplications
where app.Player == user &&
app.Team.Event == Event
select app;
_context.TeamApplications.RemoveRange(allApplications);

_context.TeamMembers.Add(Member);
await _context.SaveChangesAsync();
return RedirectToPage("./Members", new { teamId = teamId });
return NotFound(result.Item2);
}
}
}
Loading