Пример #1
0
        public IActionResult Put(int id, [FromBody] Product p)
        {
            bool update = false;

            if (Request != null && Request.Query.ContainsKey("update"))
            {
                update = Request.Query["update"].ToString() == "true";
            }

            bool uploadhist = false;

            if (Request != null && Request.Query.ContainsKey("uploadhist"))
            {
                uploadhist = Request.Query["uploadhist"].ToString() == "true";
            }

            string username = "";

            if (HttpContext != null && HttpContext.Request != null)
            {
                username = getUsername(HttpContext.Request.Headers["Authorization"]);
            }
            if (string.IsNullOrEmpty(username))
            {
                username = getUsername2(HttpContext.Request.Headers["Cookie"]);
            }

            if (username == Startup.Configuration["readonlyUser"] &&
                (string.IsNullOrEmpty(p.Command) || p.Command != "getProperties")
                )
            {
                return(Unauthorized());
            }

            if (!string.IsNullOrEmpty(p.Command))
            {
                System.Console.WriteLine(p.Command);
            }

            ProductsRepo repo = new ProductsRepo();

            if (repo.GetOne(id) == null)
            {
                return(NotFound(p));
            }

            string ret = "good";

            repo.SetOne(id, p, HttpContext.Connection.RemoteIpAddress.ToString());

            Product product = repo.GetOne(id);

            //TODO: use set method instead of updating product

            if (update)
            {
                bool doSave = p.Version != product.Version;
                if (string.IsNullOrEmpty(p.Status))
                {
                    product.Percent = p.Percent;
                    product.IP      = p.IP;
                }
                product.Version = p.Version;
                product.OS      = p.OS;
                product.Profile = p.Profile;
                if (doSave)
                {
                    repo.Save();
                }
            }
            else if (!string.IsNullOrEmpty(p.Command))
            {
                //increment the cmd number
                if (string.IsNullOrEmpty(product.Command) || !product.Command.Contains(":"))
                {
                    product.Command = "1:" + p.Command;
                }
                else
                {
                    int i = 1 + System.Convert.ToInt32(product.Command.Substring(0, product.Command.IndexOf(':')));
                    product.Command = i.ToString() + ":" + p.Command;
                }
                ret = product.Command;
                //don't update lastReport time if received command from UI
                return(Ok(product));
            }
            else if (!string.IsNullOrEmpty(p.Response))
            {
                product.Response = p.Response;
            }
            else if (!string.IsNullOrEmpty(p.Output))
            {
                product.setOutput(p.Output);
            }
            else if (!string.IsNullOrEmpty(p.Desc))
            {
                product.Desc = p.Desc;
                repo.Save();
            }
            else if (!string.IsNullOrEmpty(p.Name))
            {
                if (username != Startup.Configuration["adminUser"])
                {
                    return(Unauthorized());
                }
                product.Name = p.Name;
                writeIds();
            }
            else if (uploadhist)
            {
                Console.WriteLine("UploadHistory " + p.UploadHistory);
                product.UploadHistory = p.UploadHistory;
                //repo.Save();
            }
            else
            {
                product.Percent = p.Percent;
            }

            product.IP2        = HttpContext.Connection.RemoteIpAddress.ToString();
            product.LastReport = DateTime.Now;

            string ip = GetHeaderValueAs <string>("X-Forwarded-For");

            //Console.WriteLine("forwarded from "+ip);
            if (!string.IsNullOrEmpty(ip))
            {
                product.IP2 = ip;
            }


            repo.AddOne(0, new Product()
            {
                Id = 0, Name = ""
            });
            Product product0 = repo.GetOne(0);

            if (product0 != null)
            {
                product0.LastReport = DateTime.Now;
            }

            //must return existing product NOT incoming product, so that we're returning latest command
            return(Ok(product));
        }