You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Ombi/Old/Ombi.UI/Modules/Admin/AboutModule.cs

225 lines
8.0 KiB

8 years ago
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: AboutModule.cs
// Created By: Jamie Rees
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using System;
using System.Linq;
using System.Reflection;
using System.Text;
8 years ago
using System.Threading.Tasks;
using Nancy;
using Nancy.Extensions;
using Nancy.Linker;
8 years ago
using Nancy.Responses.Negotiation;
using NLog;
using Octokit;
8 years ago
using Ombi.Core;
using Ombi.Core.SettingModels;
using Ombi.Helpers;
using Ombi.Helpers.Permissions;
using Ombi.Store;
8 years ago
using Ombi.UI.Models;
using ISecurityExtensions = Ombi.Core.ISecurityExtensions;
8 years ago
8 years ago
namespace Ombi.UI.Modules.Admin
8 years ago
{
public class AboutModule : BaseModule
{
public AboutModule(ISettingsService<PlexRequestSettings> settingsService,
ISettingsService<SystemSettings> systemService, ISecurityExtensions security,
IStatusChecker statusChecker, IResourceLinker linker, ISqliteConfiguration config) : base("admin", settingsService, security)
8 years ago
{
Before += (ctx) => Security.AdminLoginRedirect(Permissions.Administrator, ctx);
SettingsService = systemService;
StatusChecker = statusChecker;
Linker = linker;
SqlConfig = config;
8 years ago
Get["AboutPage","/about", true] = async (x,ct) => await Index();
8 years ago
Post["/about", true] = async (x,ct) => await ReportIssue();
Get["/OAuth", true] = async (x, ct) => await OAuth();
Get["/authorize", true] = async (x, ct) => await Authorize();
8 years ago
}
private ISettingsService<SystemSettings> SettingsService { get; }
private IStatusChecker StatusChecker { get; }
private IResourceLinker Linker { get; }
private ISqliteConfiguration SqlConfig { get; }
8 years ago
private async Task<Negotiator> Index()
{
var vm = await GetModel();
return View["About", vm];
}
private async Task<AboutAdminViewModel> GetModel()
8 years ago
{
var vm = new AboutAdminViewModel();
var oAuth = Session[SessionKeys.OAuthToken]?.ToString() ?? string.Empty;
if (!string.IsNullOrEmpty(oAuth))
{
vm.OAuthEnabled = true;
}
8 years ago
var systemSettings = await SettingsService.GetSettingsAsync();
var type = Type.GetType("Mono.Runtime");
if (type != null) // mono
{
vm.Os = "Mono";
var displayName = type.GetMethod("GetDisplayName", BindingFlags.NonPublic | BindingFlags.Static);
if (displayName != null)
{
vm.SystemVersion = displayName.Invoke(null, null).ToString();
}
}
else
{
// Windows
vm.Os = OperatingSystemHelper.GetOs();
vm.SystemVersion = Environment.Version.ToString();
}
vm.RunningDir = Environment.CurrentDirectory;
vm.DbLocation = SqlConfig.CurrentPath;
8 years ago
vm.ApplicationVersion = AssemblyHelper.GetFileVersion();
vm.Branch = EnumHelper<Branches>.GetBranchValue<BranchAttribute>(systemSettings.Branch).DisplayName;
8 years ago
vm.LogLevel = LogManager.Configuration.LoggingRules.FirstOrDefault(x => x.NameMatches("database"))?.Levels?.FirstOrDefault()?.Name ?? "Unknown";
return vm;
8 years ago
}
private async Task<Response> ReportIssue()
{
var title = Request.Form["title"];
var body = Request.Form["body"];
if (string.IsNullOrEmpty(title) || string.IsNullOrEmpty(body))
{
return
Response.AsJson(
new
{
result = false,
message = "The title or issue body is empty! Please give me a bit more detail :)"
});
}
var model = await GetModel();
body = CreateReportBody(model, body);
var token = Session[SessionKeys.OAuthToken].ToString();
var result = await StatusChecker.ReportBug(title, body, token);
8 years ago
return Response.AsJson(new {result = true, url = result.HtmlUrl.ToString()});
}
private async Task<Response> OAuth()
{
var path = Request.Url.Path;
Request.Url.Path = path.Replace("oauth", "authorize");
var uri = await StatusChecker.OAuth(Request.Url.ToString(), Session);
return Response.AsJson(new { uri = uri.ToString()});
}
public async Task<Response> Authorize()
{
var code = Request.Query["code"].ToString();
var state = Request.Query["state"].ToString();
var expectedState = Session[SessionKeys.CSRF] as string;
if (state != expectedState)
{
throw new InvalidOperationException("SECURITY FAIL!");
}
Session[SessionKeys.CSRF] = null;
var token = await StatusChecker.OAuthAccessToken(code);
Session[SessionKeys.OAuthToken] = token.AccessToken;
return Context.GetRedirect(Linker.BuildRelativeUri(Context, "AboutPage").ToString());
}
private string CreateReportBody(AboutAdminViewModel model, string body)
{
var sb = new StringBuilder();
sb.AppendLine("#### Ombi Version");
sb.AppendLine($"V {model.ApplicationVersion}");
sb.AppendLine("#### Update Branch:");
sb.AppendLine(model.Branch);
sb.AppendLine("#### Operating System:");
sb.AppendLine(model.Os);
sb.AppendLine("#### Log Level:");
sb.AppendLine(model.LogLevel);
sb.AppendLine(body);
return sb.ToString();
// <!--- //!! Please use the Support / bug report template, otherwise we will close the Github issue !!
//(Pleas submit a feature request over here: http://feathub.com/tidusjar/Ombi) //--->
//#### Ombi Version:
//V 1.XX.XX
//#### Update Branch:
//Stable/Early Access Preview/development
//#### Operating System:
//(Place text here)
//#### Mono Version (only if your not on windows)
//(Place text here)
//#### Applicable Logs (from `/logs/` directory or the Admin page):
//```
//(Logs go here. Don't remove the ``` tags for showing your logs correctly. Please make sure you remove any personal information from the logs)
//```
//#### Problem Description:
//(Place text here)
//#### Reproduction Steps:
//Please include any steps to reproduce the issue, this the request that is causing the problem etc.
}
8 years ago
}
}