예제 #1
0
        internal static async Task <User> InsertNewUser(string name, string password, string authkey)
        {
            using (var ctx = new ScavengePadDbContext())
            {
                var team = await ctx.Teams
                           .Where(t => t.Authkey == authkey)
                           .FirstOrDefaultAsync();

                if (team == null)
                {
                    team = new Team()
                    {
                        Authkey  = authkey,
                        Teamname = "default_teamname"
                    };
                    ctx.Teams.Add(team);
                }
                var user = new User()
                {
                    Username = name.ToByteArray(),
                    Password = password.ToByteArray(), // switch to hashed passwords some day, but not now
                    Team     = team
                };
                ctx.Users.Add(user);
                await ctx.SaveChangesAsync();

                return(user);
            }
        }
예제 #2
0
        public static async Task InsertFilesForObjective(IEnumerable <IFormFile> files, long uploaderId, long objectiveId, long operationId)
        {
            using (var ctx = new ScavengePadDbContext())
            {
                var objective = await ctx.Objectives
                                .Where(obj => obj.Id == objectiveId)
                                .Include(c => c.Files)
                                .FirstAsync();

                foreach (var formFile in files)
                {
                    File file = new File()
                    {
                        Name       = formFile.FileName,
                        UploaderId = uploaderId,
                        MimeType   = "//TODO",
                        Timestamp  = DateTime.UtcNow
                    };
                    objective.Files.Add(file);
                    await ctx.SaveChangesAsync();

                    var filePath = $"{Startup.HostingEnvironment.WebRootPath}/uploads/{file.Id}";
                    using (var stream = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
                    {
                        await formFile.CopyToAsync(stream);
                    }
                }
                await ScavengePadController.DispatchOperationUpdate((await ctx.Users.FindAsync(uploaderId)).TeamId, operationId);
            }
        }
예제 #3
0
        internal static async Task <Operation> ModifyOperation(WebSocketClient client, Operation modifiedOperation)
        {
            Operation dbOperation;
            bool      newOp = false;

            using (var ctx = new ScavengePadDbContext())
            {
                dbOperation = await ctx.Operations
                              .Where(c => c.Id == modifiedOperation.Id)
                              .Where(c => c.TeamId == client.User.TeamId)
                              .AsNoTracking()
                              .FirstOrDefaultAsync();

                if (dbOperation == null)
                {
                    dbOperation = new Operation()
                    {
                        TeamId     = client.User.TeamId,
                        Title      = modifiedOperation.Title,
                        Objectives = modifiedOperation.Objectives
                    };
                    ctx.Operations.Add(dbOperation);
                    newOp = true;
                }
                else
                {
                    ctx.Operations.Update(modifiedOperation);
                    dbOperation = modifiedOperation;
                }
                await ctx.SaveChangesAsync();

                if (newOp)
                {
                    dbOperation.OperationPadSuffix = WebUtility.UrlEncode(ScavengePadUtils.SHA256($"{dbOperation.Id}{ScavengePadUtils.GetRandomInt()}{ScavengePadUtils.GetRandomInt()}{ScavengePadUtils.GetRandomInt()}{ScavengePadUtils.GetRandomInt()}"));
                }
                foreach (var objective in dbOperation.Objectives)
                {
                    if (objective.ObjectivePadSuffix == "default")
                    {
                        objective.ObjectivePadSuffix = WebUtility.UrlEncode(ScavengePadUtils.SHA256($"{objective.Id}{ScavengePadUtils.GetRandomInt()}{ScavengePadUtils.GetRandomInt()}{ScavengePadUtils.GetRandomInt()}{ScavengePadUtils.GetRandomInt()}"));
                    }
                }
                await ctx.SaveChangesAsync();
            }
            return(await GetOperation(dbOperation.Id));
        }
예제 #4
0
        public static async Task ChangeObjectiveStatus(long objectiveId, bool newStatus)
        {
            using (var ctx = new ScavengePadDbContext())
            {
                var objective = await ctx.Objectives
                                .Where(obj => obj.Id == objectiveId)
                                .FirstAsync();

                objective.Solved = newStatus;
                await ctx.SaveChangesAsync();
            }
        }