protected override async Task <InputModel> CreateInputModelAsync(RegisterProviderContext context) { return(new InputModel { Affiliations = await context.ListAffiliationsAsync(false), Categories = await context.ListCategoriesAsync(false), }); }
protected override Task RenderOutputAsync(RegisterProviderContext context, RegisterProviderOutput <OutputModel> output) { output.WithTitle("Batch import result") .AppendDataTable( elements: output.Model, tableClass: "text-center table-hover", theadClass: "thead-light"); return(Task.CompletedTask); }
protected override async Task <OutputModel> ExecuteAsync(RegisterProviderContext context, InputModel model) { var studentStore = context.GetRequiredService <IStudentStore>(); var @class = model.Classes.Single(a => a.Id == model.ClassId); var category = model.Categories[model.CategoryId]; int classId = model.ClassId, catId = model.CategoryId; int cid = context.Contest.Id, affId = @class.AffiliationId; var students = await studentStore.ListStudentsAsync(@class); var targets = students.ToLookup( keySelector: s => new { s.Id, s.Name }, elementSelector: s => s.UserId.HasValue ? new { UserId = s.UserId !.Value, UserName = s.UserName ! } : null);
protected override async Task <StatusMessageModel> ExecuteAsync(RegisterProviderContext context, InputModel model) { // This time, current users haven't got any team registered. var t = await context.CreateTeamAsync( users : model.UserIds.Select(i => new FakeRegisterUser(i)), team : new Entities.Team { ContestId = context.Contest.Id, TeamName = model.TeamName, RegisterTime = DateTimeOffset.Now, Status = DefaultStatus, AffiliationId = model.AffiliationId, CategoryId = context.Contest.Settings.RegisterCategory ![FancyName],
protected override Task ValidateAsync(RegisterProviderContext context, InputModel model, ModelStateDictionary modelState) { if (!model.Categories.ContainsKey(model.CategoryId)) { modelState.AddModelError("tcrp::nocat", "Category not found."); } if (model.Classes.Count(a => a.Id == model.ClassId) != 1) { modelState.AddModelError("tcrp::noaff", "Student group not found."); } return(Task.CompletedTask); }
protected override async Task <InputModel> CreateInputModelAsync(RegisterProviderContext context) { var studentStore = context.GetRequiredService <IStudentStore>(); var userId = int.Parse(context.User.GetUserId() !); IEnumerable <int> tenantId = context.User.IsInRole("Administrator") ? context.GetRequiredService <IAffiliationStore>().GetQueryable().Select(a => a.Id) : context.User.FindAll("tenant_admin").Select(a => int.Parse(a.Value)); return(new InputModel { Classes = await studentStore.ListClassesAsync(tenantId, c => c.UserId == null || c.UserId == userId), Categories = await context.ListCategoriesAsync(false), }); }
protected override Task RenderInputAsync(RegisterProviderContext context, RegisterProviderOutput <InputModel> output) { output.WithTitle("Batch team register") .AppendValidationSummary() .AppendSelect( @for: __model => __model.AffiliationId, items: output.Model.Affiliations.Values.Select(a => new SelectListItem(a.Name, $"{a.Id}"))) .AppendSelect( @for: __model => __model.CategoryId, items: output.Model.Categories.Values.Select(c => new SelectListItem(c.Name, $"{c.Id}"))) .AppendTextArea( @for: __model => __model.TeamNames, comment: "队伍名称,每个一行,区分大小写和空格,提交后会绑定新用户并重置密码。"); return(Task.CompletedTask); }
protected override async Task <StatusMessageModel> ExecuteAsync(RegisterProviderContext context, EmptyModel model) { // This time, current user haven't got any team registered. var teamName = context.User.GetNickName() !; var user = new FakeRegisterUser(int.Parse(context.User.GetUserId() !)); var t = await context.CreateTeamAsync( users : new[] { user }, team : new Entities.Team { ContestId = context.Contest.Id, TeamName = teamName, RegisterTime = DateTimeOffset.Now, Status = DefaultStatus, AffiliationId = -1, // (none), should've existed CategoryId = context.Contest.Settings.RegisterCategory ![FancyName],
protected override async Task <StatusMessageModel> ExecuteAsync(RegisterProviderContext context, EmptyModel model) { var ratingClaim = context.User.FindFirst(RatingClaimsProvider.RatingClaimsName); var rating = int.Parse(ratingClaim?.Value ?? "1500"); var category = RatingAtLeast > rating || RatingAtMost <= rating ? -4 : -3; // -4 => Observers, -3 => Participants var team = new Entities.Team { AffiliationId = -1, CategoryId = category, ContestId = context.Contest.Id, Status = 1, TeamName = context.User.GetUserName() !, }; var users = new[]
public static bool TryGetStudent( this RegisterProviderContext context, out int affiliationId, out string?studentId, out string?affiliationName, out string?studentName) { affiliationId = int.MinValue; studentId = context.User.FindFirst("student")?.Value; affiliationName = context.User.FindFirst("affiliation")?.Value; studentName = context.User.FindFirst("given_name")?.Value; return(studentId != null && affiliationName != null && studentName != null && int.TryParse(context.User.FindFirst("tenant")?.Value, out affiliationId)); }
protected override Task ValidateAsync(RegisterProviderContext context, InputModel model, ModelStateDictionary modelState) { if (!model.Categories.ContainsKey(model.CategoryId)) { modelState.AddModelError("bbtnrp::nocat", "Category not found."); } if (!model.Affiliations.ContainsKey(model.AffiliationId)) { modelState.AddModelError("bbtnrp::noaff", "Affiliation not found."); } if (string.IsNullOrWhiteSpace(model.TeamNames)) { modelState.AddModelError("bbtnrp::notn", "No team name specified."); } return(Task.CompletedTask); }
protected override async Task <StatusMessageModel> ExecuteAsync(RegisterProviderContext context, EmptyModel model) { if (!context.TryGetStudent(out int affId, out var studId, out _, out var studName)) { throw new NotImplementedException("Unknown error occurred."); } // This time, current user haven't got any team registered. var teamName = studId + studName; var user = new FakeRegisterUser(int.Parse(context.User.GetUserId() !)); var t = await context.CreateTeamAsync( users : new[] { user }, team : new Entities.Team { ContestId = context.Contest.Id, TeamName = teamName, RegisterTime = DateTimeOffset.Now, Status = 1, AffiliationId = affId, CategoryId = context.Contest.Settings.RegisterCategory ![FancyName],
async Task <object> IRegisterProvider.CreateInputModelAsync(RegisterProviderContext context) => await CreateInputModelAsync(context);
/// <inheritdoc cref="IRegisterProvider.IsAvailableAsync(RegisterProviderContext)" /> protected virtual Task <bool> IsAvailableAsync(RegisterProviderContext context) => IsAvailable(context) ? _OK : _Fail;
/// <inheritdoc cref="IRegisterProvider.IsAvailableAsync(RegisterProviderContext)" /> protected virtual bool IsAvailable(RegisterProviderContext context) => true;
/// <inheritdoc cref="IRegisterProvider.RenderOutputAsync(RegisterProviderContext, RegisterProviderOutput)" /> protected abstract Task RenderOutputAsync(RegisterProviderContext context, RegisterProviderOutput <TOutputModel> output);
/// <inheritdoc cref="IRegisterProvider.ExecuteAsync(RegisterProviderContext, object)" /> protected abstract Task <TOutputModel> ExecuteAsync(RegisterProviderContext context, TInputModel model);
/// <inheritdoc cref="IRegisterProvider.ValidateAsync(RegisterProviderContext, object, ModelStateDictionary)" /> protected abstract Task ValidateAsync(RegisterProviderContext context, TInputModel model, ModelStateDictionary modelState);
/// <inheritdoc cref="IRegisterProvider.CreateInputModelAsync(RegisterProviderContext)" /> protected abstract Task <TInputModel> CreateInputModelAsync(RegisterProviderContext context);
protected override bool IsAvailable(RegisterProviderContext context) => base.IsAvailable(context) && context.TryGetStudent(out _, out _, out _, out _);
/// <inheritdoc /> protected override sealed Task RenderOutputAsync(RegisterProviderContext context, RegisterProviderOutput <StatusMessageModel> output) => throw new System.NotSupportedException();
Task IRegisterProvider.RenderOutputAsync(RegisterProviderContext context, RegisterProviderOutput output) => RenderOutputAsync(context, (RegisterProviderOutput <TOutputModel>)output);
Task IRegisterProvider.ValidateAsync(RegisterProviderContext context, object model, ModelStateDictionary modelState) => ValidateAsync(context, (TInputModel)model, modelState);
protected override Task <EmptyModel> CreateInputModelAsync(RegisterProviderContext context) => EmptyModel.CompletedTask;
async Task <object> IRegisterProvider.ExecuteAsync(RegisterProviderContext context, object model) => await ExecuteAsync(context, (TInputModel)model);
protected override async Task <OutputModel> ExecuteAsync(RegisterProviderContext context, InputModel model) { var rng = CreatePasswordGenerator(); var result = new List <TeamAccount>(); var teamNames = model.TeamNames.Split('\n'); int affId = model.AffiliationId, catId = model.CategoryId; var existingTeams = await context.ListTeamsAsync(c => c.Status == 1 && c.AffiliationId == affId && c.CategoryId == catId); var list = existingTeams.ToLookup(a => a.TeamName); var userManager = context.UserManager; foreach (var item2 in teamNames) { var item = item2.Trim(); if (list.Contains(item)) { var e = list[item]; foreach (var team in e) { string pwd = rng(); var user = await EnsureTeamWithPassword(team, pwd); result.Add(new TeamAccount { Id = team.TeamId, TeamName = team.TeamName, UserName = user.UserName, Password = pwd, }); } } else { var team = new Team { AffiliationId = affId, CategoryId = catId, ContestId = context.Contest.Id, Status = 1, TeamName = item, }; await context.CreateTeamAsync(team, null); string pwd = rng(); var user = await EnsureTeamWithPassword(team, pwd); result.Add(new TeamAccount { Id = team.TeamId, TeamName = team.TeamName, UserName = user.UserName, Password = pwd, }); } } return(new OutputModel(result)); async Task <IUser> EnsureTeamWithPassword(Team team, string password) { string username = UserNameForTeamId(team.TeamId); var user = await userManager.FindByNameAsync(username); if (user != null) { if (user.HasPassword()) { var token = await userManager.GeneratePasswordResetTokenAsync(user); await userManager.ResetPasswordAsync(user, token, password); } else { await userManager.AddPasswordAsync(user, password); } if (await userManager.IsLockedOutAsync(user)) { await userManager.SetLockoutEndDateAsync(user, null); } } else { user = userManager.CreateEmpty(username); user.Email = $"{username}@contest.acm.xylab.fun"; await userManager.CreateAsync(user, password); await userManager.AddToRoleAsync(user, "TemporaryTeamAccount"); } await context.AttachMemberAsync(team, user, true); return(user); } }
Task <bool> IRegisterProvider.IsAvailableAsync(RegisterProviderContext context) => IsAvailableAsync(context);
protected override Task <InputModel> CreateInputModelAsync(RegisterProviderContext context) => Task.FromResult(new InputModel());
/// <inheritdoc /> protected override bool IsAvailable(RegisterProviderContext context) => !context.User.IsInRole("TemporaryTeamAccount") && (context.Contest.Settings.RegisterCategory?.ContainsKey(FancyName) ?? false);
protected override bool IsAvailable(RegisterProviderContext context) => CcsDefaults.SupportsRating && base.IsAvailable(context) && (OutOfCompetition || (int.TryParse(context.User.FindFirst(RatingClaimsProvider.RatingClaimsName)?.Value ?? "1500", out int rating) && !(RatingAtLeast > rating || RatingAtMost <= rating)));