commodore73

NONPROD INSECURE ContentstackController (Developer Conveniences) -

Jul 24th, 2020
91
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. namespace Root.Apps.Web.Csweb.Controllers
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Net;
  6.  
  7.     using Microsoft.AspNetCore.Mvc;
  8.     using Microsoft.Extensions.Logging;
  9.    
  10.     using Newtonsoft.Json.Linq;
  11.    
  12.     using Contentstack.Core;
  13.     using Contentstack.Core.Models;
  14.     using Microsoft.AspNetCore.Hosting; //TODO: which?
  15.     using Microsoft.Extensions.Hosting;
  16.     using Deliverystack.Core.Models.Repositories;
  17.  
  18.     public class ContentstackController : Controller
  19.     {
  20.         private readonly ILogger<ContentstackController> _logger;
  21.         private IRepository _repository;
  22.         private ContentstackClient _client;
  23.         private IHostApplicationLifetime _applicationLifetime;
  24.  
  25.         public ContentstackController(
  26.             ContentstackClient client,
  27.             IRepository repository,
  28.             ILogger<ContentstackController> logger,
  29.             IHostApplicationLifetime applicationLifetime)
  30.         {
  31.             _client = client;
  32.             _repository = repository;
  33.             _logger = logger;
  34.             _applicationLifetime = applicationLifetime;
  35.         }
  36.        
  37.         public ContentResult ContentTypes()
  38.         {
  39.             string result = "{" + Environment.NewLine;
  40.  
  41.             foreach (JObject contentType in _client.GetContentTypes(new Dictionary<string, object>()).Result)
  42.             {
  43.                 result += "  \"" + contentType["uid"] + "\": \"" + contentType["title"] +
  44.                     "\", url: \"/Contentstack/ContentType?ctid=" + contentType["uid"] + "\"," + Environment.NewLine;
  45.             }
  46.  
  47.             return Content(result + '}');
  48.         }
  49.  
  50.         //        public RedirectResult Reset()
  51.         public ActionResult Reset()
  52.         {
  53. //            _applicationLifetime.StopApplication();
  54. //        Program.Shutdown();
  55. //            _applicationLifetime.StopApplication();
  56.             _repository.Reset();
  57. //            return Redirect("/");
  58.             return Content("Reset() requested at " + DateTime.Now); // TODO: pause and redirect
  59.         }
  60.  
  61.         public ActionResult Refresh()
  62.         {
  63.             _repository.Refresh();
  64.             return Content("Refresh() requested at " + DateTime.Now); // TODO: pause and redirect
  65.         }
  66.  
  67.  
  68.  
  69.         public ContentResult Stack()
  70.         {
  71.             return Content("{\"ContentstackOptions\": {\"ApiKey\": \""
  72.                 + _client.GetApplicationKey()
  73.                 + "\", \"AccessToken\": \""
  74.                 + _client.GetAccessToken()
  75.                 + ", \"Environment\": \""
  76.                 + _client.GetEnvironment()
  77.                 + "\"}}");
  78.         }
  79.  
  80.         public ContentResult ContentType([FromQuery(Name = "ctid")] string ctid)
  81.         {
  82.             foreach (JObject contentType in _client.GetContentTypes(new Dictionary<string, object>()).Result)
  83.             {
  84.                 if (String.Equals(contentType["uid"].ToString().ToLower(), ctid, StringComparison.InvariantCultureIgnoreCase))
  85.                 {
  86.                     // render the JSON representation of the Content Type identified by the ctid query string
  87.                     return Content(contentType.ToString());
  88.                 }
  89.             }
  90.  
  91.             return ContentTypes();
  92.         }
  93.        
  94.         public IActionResult Query([FromQuery(Name = "query")] string query)
  95.         {
  96.             using (var w = new WebClient())
  97.             {
  98.                 string url = "https://graphql.contentstack.com/stacks/" +
  99.                                 _client.GetApplicationKey() + "?access_token=" +
  100.                                 _client.GetAccessToken() + "&environment=" +
  101.                                 _client.GetEnvironment() + "&query=" + query;
  102.                 return Content(JObject.Parse(w.DownloadString(url)).ToString());
  103.             }
  104.         }
  105.  
  106.         public ContentResult Entries([FromQuery(Name = "ctid")] string ctid)
  107.         {
  108.             string result = "{" + Environment.NewLine;
  109.  
  110.             foreach (Entry element in _client.ContentType(ctid).Query().Find<Entry>().Result)
  111.             {
  112.                 result += "  { ctid: \"" + ctid + "\", uid: \"" + element.Uid + "\", title: \"" + element.Title + "\" }" + Environment.NewLine;
  113.             }
  114.  
  115.             return Content(result + '}');
  116.         }
  117.  
  118.         public IActionResult Entry([FromQuery(Name = "ctid")] string ctid, [FromQuery(Name = "uid")] string uid, [FromQuery(Name = "json")] bool json = false)
  119.         {
  120.             Entry entry = _client.ContentType(ctid).Entry(uid).Fetch<Entry>().Result;
  121.  
  122.             if (json)
  123.             {
  124.                 return Content(entry.ToJson().ToString());
  125.             }
  126.  
  127.             if (entry.Object.ContainsKey("view"))
  128.             {
  129.                 return this.View(entry.Object["view"].ToString(), entry);
  130.             }
  131.  
  132.             return this.View(entry);
  133.         }
  134.     }
  135. }
RAW Paste Data