DocSpace-buildtools/web/ASC.Web.Core/Utility/CommonLinkUtility.cs

535 lines
19 KiB
C#
Raw Normal View History

2019-06-07 08:59:07 +00:00
/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
*
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
using System;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
2019-10-17 15:55:35 +00:00
using ASC.Common.Logging;
2019-06-07 08:59:07 +00:00
using ASC.Core;
using ASC.Core.Common;
using ASC.Core.Common.Settings;
2019-06-07 08:59:07 +00:00
using ASC.Core.Users;
using ASC.Security.Cryptography;
using ASC.Web.Core;
using ASC.Web.Core.WhiteLabel;
2019-07-02 15:30:31 +00:00
using Microsoft.AspNetCore.Http;
2019-10-31 11:28:30 +00:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
2019-10-17 15:55:35 +00:00
using Microsoft.Extensions.Options;
2019-06-07 08:59:07 +00:00
namespace ASC.Web.Studio.Utility
{
public enum ManagementType
{
General = 0,
Customization = 1,
ProductsAndInstruments = 2,
PortalSecurity = 3,
AccessRights = 4,
Backup = 5,
LoginHistory = 6,
AuditTrail = 7,
LdapSettings = 8,
ThirdPartyAuthorization = 9,
SmtpSettings = 10,
Statistic = 11,
Monitoring = 12,
SingleSignOnSettings = 13,
Migration = 14,
DeletionPortal = 15,
HelpCenter = 16,
DocService = 17,
FullTextSearch = 18,
WhiteLabel = 19,
MailService = 20,
Storage = 21
}
2019-10-10 10:59:22 +00:00
public class CommonLinkUtility : BaseCommonLinkUtility
2019-06-07 08:59:07 +00:00
{
private static readonly Regex RegFilePathTrim = new Regex("/[^/]*\\.aspx", RegexOptions.IgnoreCase | RegexOptions.Compiled);
public const string ParamName_ProductSysName = "product";
public const string ParamName_UserUserName = "user";
public const string ParamName_UserUserID = "uid";
2019-09-19 15:55:44 +00:00
public CommonLinkUtility(
2019-10-10 10:59:22 +00:00
CoreBaseSettings coreBaseSettings,
CoreSettings coreSettings,
TenantManager tenantManager,
UserManager userManager,
WebItemManagerSecurity webItemManagerSecurity,
2019-09-19 15:55:44 +00:00
WebItemManager webItemManager,
2019-10-17 15:55:35 +00:00
EmailValidationKeyProvider emailValidationKeyProvider,
2019-11-06 15:03:09 +00:00
IOptionsMonitor<ILog> options,
2019-11-01 08:49:08 +00:00
IOptions<CommonLinkUtilitySettings> settings) :
this(null, coreBaseSettings, coreSettings, tenantManager, userManager, webItemManagerSecurity, webItemManager, emailValidationKeyProvider, options, settings)
2019-06-07 08:59:07 +00:00
{
}
2019-09-19 15:55:44 +00:00
public CommonLinkUtility(
IHttpContextAccessor httpContextAccessor,
CoreBaseSettings coreBaseSettings,
2019-10-10 10:59:22 +00:00
CoreSettings coreSettings,
2019-09-19 15:55:44 +00:00
TenantManager tenantManager,
UserManager userManager,
WebItemManagerSecurity webItemManagerSecurity,
WebItemManager webItemManager,
2019-10-17 15:55:35 +00:00
EmailValidationKeyProvider emailValidationKeyProvider,
2019-11-06 15:03:09 +00:00
IOptionsMonitor<ILog> options,
2019-11-01 08:49:08 +00:00
IOptions<CommonLinkUtilitySettings> settings) :
base(httpContextAccessor, coreBaseSettings, coreSettings, tenantManager, options, settings) =>
2019-10-17 15:55:35 +00:00
(UserManager, WebItemManagerSecurity, WebItemManager, EmailValidationKeyProvider) = (userManager, webItemManagerSecurity, webItemManager, emailValidationKeyProvider);
2019-06-07 08:59:07 +00:00
2019-09-19 15:55:44 +00:00
public string Logout
2019-06-07 08:59:07 +00:00
{
get { return ToAbsolute("~/auth.aspx") + "?t=logout"; }
}
2019-09-19 15:55:44 +00:00
public UserManager UserManager { get; }
public WebItemManagerSecurity WebItemManagerSecurity { get; }
public WebItemManager WebItemManager { get; }
public EmailValidationKeyProvider EmailValidationKeyProvider { get; }
public string GetDefault()
2019-06-07 08:59:07 +00:00
{
return VirtualRoot;
}
2019-09-19 15:55:44 +00:00
public string GetMyStaff()
2019-06-07 08:59:07 +00:00
{
2019-09-19 15:55:44 +00:00
return CoreBaseSettings.Personal ? ToAbsolute("~/my.aspx") : ToAbsolute("~/products/people/profile.aspx");
2019-06-07 08:59:07 +00:00
}
2019-09-19 15:55:44 +00:00
public string GetEmployees()
2019-06-07 08:59:07 +00:00
{
return GetEmployees(EmployeeStatus.Active);
}
2019-09-19 15:55:44 +00:00
public string GetEmployees(EmployeeStatus empStatus)
2019-06-07 08:59:07 +00:00
{
return ToAbsolute("~/products/people/") +
(empStatus == EmployeeStatus.Terminated ? "#type=disabled" : string.Empty);
}
2019-09-19 15:55:44 +00:00
public string GetDepartment(Guid depId)
2019-06-07 08:59:07 +00:00
{
return depId != Guid.Empty ? ToAbsolute("~/products/people/#group=") + depId.ToString() : GetEmployees();
}
#region user profile link
2019-09-19 15:55:44 +00:00
public string GetUserProfile(Guid userID)
2019-06-07 08:59:07 +00:00
{
2019-09-19 15:55:44 +00:00
if (!UserManager.UserExists(userID))
2019-08-08 09:26:58 +00:00
return GetEmployees();
2019-09-19 15:55:44 +00:00
return GetUserProfile(userID.ToString());
2019-06-07 08:59:07 +00:00
}
2019-09-19 15:55:44 +00:00
public string GetUserProfile(UserInfo user)
2019-08-30 12:40:57 +00:00
{
2019-09-19 15:55:44 +00:00
if (!UserManager.UserExists(user))
2019-08-30 12:40:57 +00:00
return GetEmployees();
2019-09-19 15:55:44 +00:00
return GetUserProfile(user, true);
2019-08-30 12:40:57 +00:00
}
2019-09-19 15:55:44 +00:00
public string GetUserProfile(string user, bool absolute = true)
2019-06-07 08:59:07 +00:00
{
var queryParams = "";
2019-08-15 13:16:39 +00:00
if (!string.IsNullOrEmpty(user))
2019-06-07 08:59:07 +00:00
{
var guid = Guid.Empty;
2019-08-15 13:16:39 +00:00
if (!string.IsNullOrEmpty(user) && 32 <= user.Length && user[8] == '-')
2019-06-07 08:59:07 +00:00
{
try
{
guid = new Guid(user);
}
catch
{
}
}
2019-09-19 15:55:44 +00:00
queryParams = guid != Guid.Empty ? GetUserParamsPair(guid) : ParamName_UserUserName + "=" + HttpUtility.UrlEncode(user);
2019-06-07 08:59:07 +00:00
}
var url = absolute ? ToAbsolute("~/products/people/") : "/products/people/";
url += "profile.aspx?";
url += queryParams;
return url;
}
2019-08-30 12:40:57 +00:00
2019-09-19 15:55:44 +00:00
public string GetUserProfile(UserInfo user, bool absolute = true)
2019-08-30 12:40:57 +00:00
{
2019-09-19 15:55:44 +00:00
var queryParams = user.ID != Guid.Empty ? GetUserParamsPair(user) : ParamName_UserUserName + "=" + HttpUtility.UrlEncode(user.UserName);
2019-08-30 12:40:57 +00:00
var url = absolute ? ToAbsolute("~/products/people/") : "/products/people/";
url += "profile.aspx?";
url += queryParams;
return url;
}
2019-09-19 15:55:44 +00:00
public string GetUserProfile(Guid user, bool absolute = true)
2019-08-08 09:26:58 +00:00
{
2019-09-19 15:55:44 +00:00
var queryParams = GetUserParamsPair(user);
2019-08-08 09:26:58 +00:00
var url = absolute ? ToAbsolute("~/products/people/") : "/products/people/";
url += "profile.aspx?";
url += queryParams;
return url;
}
2019-06-07 08:59:07 +00:00
#endregion
2019-09-19 15:55:44 +00:00
public Guid GetProductID()
2019-06-07 08:59:07 +00:00
{
var productID = Guid.Empty;
2019-09-19 15:55:44 +00:00
if (HttpContext != null)
2019-06-07 08:59:07 +00:00
{
2019-09-19 15:55:44 +00:00
GetLocationByRequest(out var product, out _);
2019-06-07 08:59:07 +00:00
if (product != null) productID = product.ID;
}
return productID;
}
2019-09-19 15:55:44 +00:00
public void GetLocationByRequest(out IProduct currentProduct, out IModule currentModule)
2019-06-07 08:59:07 +00:00
{
var currentURL = string.Empty;
2019-09-19 15:55:44 +00:00
if (HttpContext?.Request != null)
2019-06-07 08:59:07 +00:00
{
2019-09-19 15:55:44 +00:00
currentURL = HttpContext.Request.GetUrlRewriter().AbsoluteUri;
2019-06-07 08:59:07 +00:00
//TODO ?
// http://[hostname]/[virtualpath]/[AjaxPro.Utility.HandlerPath]/[assembly],[classname].ashx
//if (currentURL.Contains("/" + AjaxPro.Utility.HandlerPath + "/") && HttpContext.Current.Request.Headers["Referer"].FirstOrDefault() != null)
//{
// currentURL = HttpContext.Current.Request.Headers["Referer"].FirstOrDefault().ToString();
//}
}
2019-09-19 15:55:44 +00:00
GetLocationByUrl(currentURL, out currentProduct, out currentModule);
2019-06-07 08:59:07 +00:00
}
2019-09-19 15:55:44 +00:00
public IWebItem GetWebItemByUrl(string currentURL)
2019-06-07 08:59:07 +00:00
{
2019-08-15 13:16:39 +00:00
if (!string.IsNullOrEmpty(currentURL))
2019-06-07 08:59:07 +00:00
{
var itemName = GetWebItemNameFromUrl(currentURL);
if (!string.IsNullOrEmpty(itemName))
{
2019-09-19 15:55:44 +00:00
foreach (var item in WebItemManager.GetItemsAll())
2019-06-07 08:59:07 +00:00
{
var _itemName = GetWebItemNameFromUrl(item.StartURL);
2019-08-15 13:16:39 +00:00
if (string.Compare(itemName, _itemName, StringComparison.InvariantCultureIgnoreCase) == 0)
2019-06-07 08:59:07 +00:00
return item;
}
}
else
{
var urlParams = HttpUtility.ParseQueryString(new Uri(currentURL).Query);
2019-09-19 15:55:44 +00:00
var productByName = GetProductBySysName(urlParams[ParamName_ProductSysName]);
2019-06-07 08:59:07 +00:00
var pid = productByName == null ? Guid.Empty : productByName.ID;
2019-08-15 13:16:39 +00:00
if (pid == Guid.Empty && !string.IsNullOrEmpty(urlParams["pid"]))
2019-06-07 08:59:07 +00:00
{
try
{
pid = new Guid(urlParams["pid"]);
}
catch
{
pid = Guid.Empty;
}
}
if (pid != Guid.Empty)
2019-09-19 15:55:44 +00:00
return WebItemManager[pid];
2019-06-07 08:59:07 +00:00
}
}
return null;
}
2019-09-19 15:55:44 +00:00
public void GetLocationByUrl(string currentURL, out IProduct currentProduct, out IModule currentModule)
2019-06-07 08:59:07 +00:00
{
currentProduct = null;
currentModule = null;
2019-08-15 13:16:39 +00:00
if (string.IsNullOrEmpty(currentURL)) return;
2019-06-07 08:59:07 +00:00
var urlParams = HttpUtility.ParseQueryString(new Uri(currentURL).Query);
2019-09-19 15:55:44 +00:00
var productByName = GetProductBySysName(urlParams[ParamName_ProductSysName]);
2019-06-07 08:59:07 +00:00
var pid = productByName == null ? Guid.Empty : productByName.ID;
2019-08-15 13:16:39 +00:00
if (pid == Guid.Empty && !string.IsNullOrEmpty(urlParams["pid"]))
2019-06-07 08:59:07 +00:00
{
try
{
pid = new Guid(urlParams["pid"]);
}
catch
{
pid = Guid.Empty;
}
}
var productName = GetProductNameFromUrl(currentURL);
var moduleName = GetModuleNameFromUrl(currentURL);
if (!string.IsNullOrEmpty(productName) || !string.IsNullOrEmpty(moduleName))
{
2019-09-19 15:55:44 +00:00
foreach (var product in WebItemManager.GetItemsAll<IProduct>())
2019-06-07 08:59:07 +00:00
{
var _productName = GetProductNameFromUrl(product.StartURL);
if (!string.IsNullOrEmpty(_productName))
{
2019-08-15 13:16:39 +00:00
if (string.Compare(productName, _productName, StringComparison.InvariantCultureIgnoreCase) == 0)
2019-06-07 08:59:07 +00:00
{
currentProduct = product;
2019-08-15 13:16:39 +00:00
if (!string.IsNullOrEmpty(moduleName))
2019-06-07 08:59:07 +00:00
{
2019-09-19 15:55:44 +00:00
foreach (var module in WebItemManagerSecurity.GetSubItems(product.ID).OfType<IModule>())
2019-06-07 08:59:07 +00:00
{
var _moduleName = GetModuleNameFromUrl(module.StartURL);
if (!string.IsNullOrEmpty(_moduleName))
{
2019-08-15 13:16:39 +00:00
if (string.Compare(moduleName, _moduleName, StringComparison.InvariantCultureIgnoreCase) == 0)
2019-06-07 08:59:07 +00:00
{
currentModule = module;
break;
}
}
}
}
else
{
2019-09-19 15:55:44 +00:00
foreach (var module in WebItemManagerSecurity.GetSubItems(product.ID).OfType<IModule>())
2019-06-07 08:59:07 +00:00
{
if (!module.StartURL.Equals(product.StartURL) && currentURL.Contains(RegFilePathTrim.Replace(module.StartURL, string.Empty)))
{
currentModule = module;
break;
}
}
}
break;
}
}
}
}
if (pid != Guid.Empty)
2019-09-19 15:55:44 +00:00
currentProduct = WebItemManager[pid] as IProduct;
2019-06-07 08:59:07 +00:00
}
2019-09-19 15:55:44 +00:00
private string GetWebItemNameFromUrl(string url)
2019-06-07 08:59:07 +00:00
{
var name = GetModuleNameFromUrl(url);
2019-08-15 13:16:39 +00:00
if (string.IsNullOrEmpty(name))
2019-06-07 08:59:07 +00:00
{
name = GetProductNameFromUrl(url);
2019-08-15 13:16:39 +00:00
if (string.IsNullOrEmpty(name))
2019-06-07 08:59:07 +00:00
{
try
{
var pos = url.IndexOf("/addons/", StringComparison.InvariantCultureIgnoreCase);
if (0 <= pos)
{
url = url.Substring(pos + 8).ToLower();
pos = url.IndexOf('/');
return 0 < pos ? url.Substring(0, pos) : url;
}
}
catch
{
}
return null;
}
}
return name;
}
2019-09-19 15:55:44 +00:00
private string GetProductNameFromUrl(string url)
2019-06-07 08:59:07 +00:00
{
try
{
var pos = url.IndexOf("/products/", StringComparison.InvariantCultureIgnoreCase);
if (0 <= pos)
{
url = url.Substring(pos + 10).ToLower();
pos = url.IndexOf('/');
return 0 < pos ? url.Substring(0, pos) : url;
}
}
catch
{
}
return null;
}
private static string GetModuleNameFromUrl(string url)
{
try
{
var pos = url.IndexOf("/modules/", StringComparison.InvariantCultureIgnoreCase);
if (0 <= pos)
{
url = url.Substring(pos + 9).ToLower();
pos = url.IndexOf('/');
return 0 < pos ? url.Substring(0, pos) : url;
}
}
catch
{
}
return null;
}
2019-09-19 15:55:44 +00:00
private IProduct GetProductBySysName(string sysName)
2019-06-07 08:59:07 +00:00
{
IProduct result = null;
2019-08-15 13:16:39 +00:00
if (!string.IsNullOrEmpty(sysName))
2019-09-19 15:55:44 +00:00
foreach (var product in WebItemManager.GetItemsAll<IProduct>())
2019-06-07 08:59:07 +00:00
{
2019-08-15 13:16:39 +00:00
if (string.CompareOrdinal(sysName, WebItemExtension.GetSysName(product as IWebItem)) == 0)
2019-06-07 08:59:07 +00:00
{
result = product;
break;
}
}
return result;
}
2019-09-19 15:55:44 +00:00
public string GetUserParamsPair(Guid userID)
2019-06-07 08:59:07 +00:00
{
2019-09-19 15:55:44 +00:00
return GetUserParamsPair(UserManager.GetUsers(userID));
2019-06-07 08:59:07 +00:00
}
2019-09-19 15:55:44 +00:00
public string GetUserParamsPair(UserInfo user)
2019-06-07 08:59:07 +00:00
{
2019-09-19 15:55:44 +00:00
if (user == null || string.IsNullOrEmpty(user.UserName) || !UserManager.UserExists(user))
2019-06-07 08:59:07 +00:00
return "";
2019-08-15 13:16:39 +00:00
return string.Format("{0}={1}", ParamName_UserUserName, HttpUtility.UrlEncode(user.UserName.ToLowerInvariant()));
2019-06-07 08:59:07 +00:00
}
#region Help Centr
public string GetHelpLink(SettingsManager settingsManager, AdditionalWhiteLabelSettingsHelper additionalWhiteLabelSettingsHelper, bool inCurrentCulture = true)
2019-06-07 08:59:07 +00:00
{
if (!AdditionalWhiteLabelSettings.Instance(settingsManager).HelpCenterEnabled)
2019-08-15 13:16:39 +00:00
return string.Empty;
2019-06-07 08:59:07 +00:00
var url = additionalWhiteLabelSettingsHelper.DefaultHelpCenterUrl;
2019-06-07 08:59:07 +00:00
2019-08-15 13:16:39 +00:00
if (string.IsNullOrEmpty(url))
return string.Empty;
2019-06-07 08:59:07 +00:00
return GetRegionalUrl(url, inCurrentCulture ? CultureInfo.CurrentCulture.TwoLetterISOLanguageName : null);
}
#endregion
#region management links
2019-09-19 15:55:44 +00:00
public string GetAdministration(ManagementType managementType)
2019-06-07 08:59:07 +00:00
{
if (managementType == ManagementType.General)
return ToAbsolute("~/management.aspx") + string.Empty;
return ToAbsolute("~/management.aspx") + "?" + "type=" + ((int)managementType).ToString();
}
#endregion
#region confirm links
2019-09-19 15:55:44 +00:00
public string GetConfirmationUrl(string email, ConfirmType confirmType, object postfix = null, Guid userId = default)
2019-06-07 08:59:07 +00:00
{
2019-09-19 15:55:44 +00:00
return GetFullAbsolutePath(GetConfirmationUrlRelative(email, confirmType, postfix, userId));
2019-06-07 08:59:07 +00:00
}
2019-09-19 15:55:44 +00:00
public string GetConfirmationUrlRelative(string email, ConfirmType confirmType, object postfix = null, Guid userId = default)
2019-06-07 08:59:07 +00:00
{
2019-09-19 15:55:44 +00:00
var validationKey = EmailValidationKeyProvider.GetEmailKey(email + confirmType + (postfix ?? ""));
2019-06-07 08:59:07 +00:00
var link = $"confirm/{confirmType}?key={validationKey}";
2019-06-07 08:59:07 +00:00
if (!string.IsNullOrEmpty(email))
{
2019-09-09 12:07:15 +00:00
link += $"&email={HttpUtility.UrlEncode(email)}";
2019-06-07 08:59:07 +00:00
}
2019-08-15 13:00:20 +00:00
if (userId != default)
2019-06-07 08:59:07 +00:00
{
2019-09-09 12:07:15 +00:00
link += $"&uid={userId}";
2019-06-07 08:59:07 +00:00
}
if (postfix != null)
{
link += "&p=1";
}
return link;
}
#endregion
}
2019-10-31 11:28:30 +00:00
public static class CommonLinkUtilityFactory
{
public static IServiceCollection AddCommonLinkUtilityService(this IServiceCollection services)
{
services.TryAddScoped<CommonLinkUtility>();
return services
.AddHttpContextAccessor()
.AddUserManagerService()
.AddBaseCommonLinkUtilityService()
.AddWebItemManagerSecurity()
.AddWebItemManager()
.AddEmailValidationKeyProviderService();
}
}
2019-06-07 08:59:07 +00:00
}