コード例 #1
0
        public async Task <ActionResult <Computer> > PutSelf(Computer computer)
        {
            var requestor = await _TokenManager.GetTokenOwner();

            if (requestor.Id != computer.ID)
            {
                return(Unauthorized());
            }

            _Context.Entry(computer).State = EntityState.Modified;

            try {
                await _Context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException) {
                if (!ComputerExists(computer.ID))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #2
0
        public async Task <ActionResult <LogItem> > PostLogItem(LogItem logItem)
        {
            _Context.Log.Add(logItem);
            await _Context.SaveChangesAsync();

            return(CreatedAtAction("GetLogItem", new { id = logItem.ID }, logItem));
        }
コード例 #3
0
        public async Task <IActionResult> PutTemplate(TemplateOut templateOut)
        {
            var unpacked = TemplateOutToTemplateAndPaths(templateOut);

            _Context.Entry(unpacked.Item1).State = EntityState.Modified;

            var storedPaths = _Context.Paths.AsNoTracking().Where(path => path.TemplateID == unpacked.Item1.ID).ToList();

            foreach (var p in unpacked.Item2)
            {
                var stored = false;
                for (var i = 0; i < storedPaths.Count; i++)
                {
                    if (storedPaths[i].ID == p.ID)
                    {
                        stored = true;
                        storedPaths.RemoveAt(i);
                        _Context.Entry(p).State = EntityState.Modified;
                        break;
                    }
                }
                if (!stored)
                {
                    _Context.Paths.Add(p);
                }
            }

            foreach (var deleted in storedPaths)
            {
                _Context.Paths.Remove(deleted);
            }

            try {
                await _Context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException) {
                if (!TemplateExists(unpacked.Item1.ID))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #4
0
        public async Task <ActionResult <JobOut[]> > GetComputerJobs()
        {
            Computer requestor = await _TokenManager.GetTokenOwner();

            requestor.LastSeen = DateTime.Now;
            _Context.Entry(requestor).State = EntityState.Modified;

            await _Context.SaveChangesAsync();

            var templates = _Context.Templates.FromSqlRaw(@"
                SELECT t.*
                FROM Templates t
                    INNER JOIN Jobs j ON t.ID = j.TemplateID
                WHERE t.Paused != true AND j.Active = true AND ComputerID = " + requestor.ID
                                                          ).ToArray();

            var jobsOut = new JobOut[templates.Length];

            for (var i = 0; i < templates.Length; i++)
            {
                var schedule       = GetSchedule(Period.FromJson(templates[i].Period).GetCron(), templates[i].Start, templates[i].End);
                var templateReturn = JobOut.FromTemplate(templates[i], 0, schedule); //LOL FIX THIS LATER
                jobsOut[i] = templateReturn;

                var paths = _Context.Paths.FromSqlRaw(@"
                    SELECT *
                    FROM Paths p
                    WHERE TemplateID = " + templates[i].ID
                                                      ).ToArray();

                foreach (var path in paths)
                {
                    if (path.Source)
                    {
                        jobsOut[i].Sources.Add(path);
                    }
                    else
                    {
                        jobsOut[i].Targets.Add(path);
                    }
                }
            }

            return(jobsOut);
        }
コード例 #5
0
 public async Task InvalidateToken(string token)
 {
     _Context.TokenBlacklist.Add(new InvalidatedToken()
     {
         Token = token,
         // Could be less if true expiration would be extracted from token
         Expires = DateTime.Now.AddDays(Convert.ToInt32(_Configuration["JWT:DaysValid"]))
     });
     await _Context.SaveChangesAsync();
 }
コード例 #6
0
        public async Task <IActionResult> PutAccount(Account account)
        {
            var requestor = await _TokenManager.GetTokenOwner();

            if (!(requestor.Admin || account.ID == requestor.ID))
            {
                return(Unauthorized());
            }

            if (account.Password != "" && account.ID == requestor.ID)
            {
                account.Password = _PasswordHelper.CreatePasswordHash(account.Password);
            }
            else
            {
                await ReturnPassword(account);
            }

            _Context.Entry(account).State = EntityState.Modified;

            try {
                await _Context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException) {
                if (!AccountExists(account.ID))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }