Esempio n. 1
0
 protected override async Task <InputModel> CreateInputModelAsync(RegisterProviderContext context)
 {
     return(new InputModel
     {
         Affiliations = await context.ListAffiliationsAsync(false),
         Categories = await context.ListCategoriesAsync(false),
     });
 }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        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);
Esempio n. 4
0
 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],
Esempio n. 5
0
        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);
        }
Esempio n. 6
0
        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),
            });
        }
Esempio n. 7
0
        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);
        }
Esempio n. 8
0
        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],
Esempio n. 9
0
        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[]
Esempio n. 10
0
        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));
        }
Esempio n. 11
0
        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);
        }
Esempio n. 12
0
        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],
Esempio n. 13
0
 async Task <object> IRegisterProvider.CreateInputModelAsync(RegisterProviderContext context)
 => await CreateInputModelAsync(context);
Esempio n. 14
0
 /// <inheritdoc cref="IRegisterProvider.IsAvailableAsync(RegisterProviderContext)" />
 protected virtual Task <bool> IsAvailableAsync(RegisterProviderContext context)
 => IsAvailable(context) ? _OK : _Fail;
Esempio n. 15
0
 /// <inheritdoc cref="IRegisterProvider.IsAvailableAsync(RegisterProviderContext)" />
 protected virtual bool IsAvailable(RegisterProviderContext context)
 => true;
Esempio n. 16
0
 /// <inheritdoc cref="IRegisterProvider.RenderOutputAsync(RegisterProviderContext, RegisterProviderOutput)" />
 protected abstract Task RenderOutputAsync(RegisterProviderContext context, RegisterProviderOutput <TOutputModel> output);
Esempio n. 17
0
 /// <inheritdoc cref="IRegisterProvider.ExecuteAsync(RegisterProviderContext, object)" />
 protected abstract Task <TOutputModel> ExecuteAsync(RegisterProviderContext context, TInputModel model);
Esempio n. 18
0
 /// <inheritdoc cref="IRegisterProvider.ValidateAsync(RegisterProviderContext, object, ModelStateDictionary)" />
 protected abstract Task ValidateAsync(RegisterProviderContext context, TInputModel model, ModelStateDictionary modelState);
Esempio n. 19
0
 /// <inheritdoc cref="IRegisterProvider.CreateInputModelAsync(RegisterProviderContext)" />
 protected abstract Task <TInputModel> CreateInputModelAsync(RegisterProviderContext context);
Esempio n. 20
0
 protected override bool IsAvailable(RegisterProviderContext context)
 => base.IsAvailable(context) && context.TryGetStudent(out _, out _, out _, out _);
Esempio n. 21
0
 /// <inheritdoc />
 protected override sealed Task RenderOutputAsync(RegisterProviderContext context, RegisterProviderOutput <StatusMessageModel> output)
 => throw new System.NotSupportedException();
Esempio n. 22
0
 Task IRegisterProvider.RenderOutputAsync(RegisterProviderContext context, RegisterProviderOutput output)
 => RenderOutputAsync(context, (RegisterProviderOutput <TOutputModel>)output);
Esempio n. 23
0
 Task IRegisterProvider.ValidateAsync(RegisterProviderContext context, object model, ModelStateDictionary modelState)
 => ValidateAsync(context, (TInputModel)model, modelState);
Esempio n. 24
0
 protected override Task <EmptyModel> CreateInputModelAsync(RegisterProviderContext context)
 => EmptyModel.CompletedTask;
Esempio n. 25
0
 async Task <object> IRegisterProvider.ExecuteAsync(RegisterProviderContext context, object model)
 => await ExecuteAsync(context, (TInputModel)model);
Esempio n. 26
0
        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);
            }
        }
Esempio n. 27
0
 Task <bool> IRegisterProvider.IsAvailableAsync(RegisterProviderContext context)
 => IsAvailableAsync(context);
Esempio n. 28
0
 protected override Task <InputModel> CreateInputModelAsync(RegisterProviderContext context)
 => Task.FromResult(new InputModel());
Esempio n. 29
0
 /// <inheritdoc />
 protected override bool IsAvailable(RegisterProviderContext context)
 => !context.User.IsInRole("TemporaryTeamAccount") &&
 (context.Contest.Settings.RegisterCategory?.ContainsKey(FancyName) ?? false);
Esempio n. 30
0
 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)));