DocSpace-client/products/ASC.CRM/Server/Classes/Global.cs

262 lines
9.4 KiB
C#
Raw Normal View History

2020-03-02 15:38:31 +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.
*
*/
2021-03-19 16:56:26 +00:00
using System;
using System.Globalization;
using System.IO;
using System.Text;
using System.Text.Json;
2021-03-19 16:56:26 +00:00
2020-04-14 18:59:32 +00:00
using ASC.Common;
2020-04-02 19:42:03 +00:00
using ASC.Core;
using ASC.Core.Common.Settings;
2020-03-02 15:38:31 +00:00
using ASC.CRM.Core;
using ASC.CRM.Resources;
using ASC.Data.Storage;
using ASC.Web.Core;
using ASC.Web.Core.Files;
using ASC.Web.Studio.Core;
2021-03-19 16:56:26 +00:00
2020-04-02 19:42:03 +00:00
using Microsoft.Extensions.Configuration;
2021-03-19 16:56:26 +00:00
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats;
2020-03-02 15:38:31 +00:00
namespace ASC.Web.CRM.Classes
{
2021-03-13 12:22:14 +00:00
[Scope]
2020-03-02 15:38:31 +00:00
public class Global
{
2021-05-28 17:20:12 +00:00
private IConfiguration _configuration;
private SettingsManager _settingsManager;
protected int _tenantID;
private FilesLinkUtility _filesLinkUtility;
private SetupInfo _setupInfo;
private SecurityContext _securityContext;
private StorageFactory _storageFactory;
private CrmSecurity _crmSecurity;
2020-04-02 19:42:03 +00:00
public Global(StorageFactory storageFactory,
SecurityContext securityContext,
SetupInfo setupInfo,
FilesLinkUtility filesLinkUtility,
2021-05-05 14:09:05 +00:00
CrmSecurity crmSecurity,
2020-04-02 19:42:03 +00:00
TenantManager tenantManager,
SettingsManager settingsManager,
2021-03-02 16:29:07 +00:00
IConfiguration configuration
2020-04-02 19:42:03 +00:00
)
2020-03-02 15:38:31 +00:00
{
2021-05-28 17:20:12 +00:00
_storageFactory = storageFactory;
_filesLinkUtility = filesLinkUtility;
_setupInfo = setupInfo;
_securityContext = securityContext;
_crmSecurity = crmSecurity;
_tenantID = tenantManager.GetCurrentTenant().TenantId;
_settingsManager = settingsManager;
_configuration = configuration;
2020-03-02 15:38:31 +00:00
}
2020-04-02 19:42:03 +00:00
2020-03-02 15:38:31 +00:00
public static readonly int EntryCountOnPage = 25;
public static readonly int VisiblePageCount = 10;
public static readonly int MaxCustomFieldSize = 150;
public static readonly int MaxCustomFieldRows = 25;
public static readonly int MaxCustomFieldCols = 150;
public static readonly int DefaultCustomFieldSize = 40;
public static readonly int DefaultCustomFieldRows = 2;
public static readonly int DefaultCustomFieldCols = 40;
public static readonly int MaxHistoryEventCharacters = 65000;
2021-03-19 16:56:26 +00:00
public static readonly decimal MaxInvoiceItemPrice = (decimal)99999999.99;
2020-03-02 15:38:31 +00:00
2021-03-19 16:56:26 +00:00
2020-04-02 19:42:03 +00:00
public IDataStore GetStore()
2020-03-02 15:38:31 +00:00
{
2021-05-28 17:20:12 +00:00
return _storageFactory.GetStorage(_tenantID.ToString(), "crm");
2020-03-02 15:38:31 +00:00
}
2020-04-02 19:42:03 +00:00
public IDataStore GetStoreTemplate()
2020-03-02 15:38:31 +00:00
{
2021-05-28 17:20:12 +00:00
return _storageFactory.GetStorage(String.Empty, "crm_template");
2020-03-02 15:38:31 +00:00
}
2020-04-02 19:42:03 +00:00
public bool CanCreateProjects()
2020-03-02 15:38:31 +00:00
{
2021-05-15 14:42:16 +00:00
throw new NotImplementedException();
2020-03-02 15:38:31 +00:00
2021-05-15 14:42:16 +00:00
//try
//{
// var apiUrl = String.Format("{0}project/securityinfo.json", SetupInfo.WebApiBaseUrl);
2020-03-02 15:38:31 +00:00
2021-05-15 14:42:16 +00:00
// var cacheKey = String.Format("{0}-{1}", SecurityContext.CurrentAccount.ID, apiUrl);
2020-03-02 15:38:31 +00:00
2021-05-15 14:42:16 +00:00
// bool canCreateProject = false;
2020-03-02 15:38:31 +00:00
2021-05-15 14:42:16 +00:00
// //if (HttpRuntime.Cache[cacheKey] != null)
// // return Convert.ToBoolean(HttpRuntime.Cache[cacheKey]);
2020-03-02 15:38:31 +00:00
2021-05-15 14:42:16 +00:00
// //var apiServer = new Api.ApiServer();
2020-03-02 15:38:31 +00:00
2021-05-15 14:42:16 +00:00
// //var responseApi = JObject.Parse(Encoding.UTF8.GetString(Convert.FromBase64String(apiServer.GetApiResponse(apiUrl, "GET"))))["response"];
2020-04-02 19:42:03 +00:00
2021-05-15 14:42:16 +00:00
// //if (responseApi.HasValues)
// // canCreateProject = Convert.ToBoolean(responseApi["canCreateProject"].Value<String>());
// //else
// // canCreateProject = false;
2020-03-02 15:38:31 +00:00
2021-05-15 14:42:16 +00:00
// //HttpRuntime.Cache.Remove(cacheKey);
// //HttpRuntime.Cache.Insert(cacheKey, canCreateProject, null, System.Web.Caching.Cache.NoAbsoluteExpiration,
// // TimeSpan.FromMinutes(5));
2020-03-02 15:38:31 +00:00
2021-05-15 14:42:16 +00:00
// return canCreateProject;
//}
//catch
//{
// return false;
//}
2020-03-02 15:38:31 +00:00
}
2020-04-02 19:42:03 +00:00
public bool CanDownloadInvoices
2020-03-02 15:38:31 +00:00
{
get
{
2021-05-28 17:20:12 +00:00
var value = _configuration["crm:invoice:download:enable"];
2020-03-02 15:38:31 +00:00
if (string.IsNullOrEmpty(value)) return false;
2020-04-02 19:42:03 +00:00
bool canDownloadFiles = Convert.ToBoolean(value);
2021-05-28 17:20:12 +00:00
if (canDownloadFiles && string.IsNullOrEmpty(_filesLinkUtility.DocServiceConverterUrl))
2020-03-02 15:38:31 +00:00
{
canDownloadFiles = false;
}
return canDownloadFiles;
}
}
2020-04-02 19:42:03 +00:00
public bool CanCreateReports
2020-03-02 15:38:31 +00:00
{
get
{
2021-05-28 17:20:12 +00:00
return !string.IsNullOrEmpty(_filesLinkUtility.DocServiceDocbuilderUrl) && _crmSecurity.IsAdmin;
2020-03-02 15:38:31 +00:00
}
}
2020-04-02 19:42:03 +00:00
public void SaveDefaultCurrencySettings(CurrencyInfo currency)
2020-03-02 15:38:31 +00:00
{
2021-05-28 17:20:12 +00:00
var tenantSettings = _settingsManager.Load<CrmSettings>();
2021-03-23 15:41:56 +00:00
tenantSettings.DefaultCurrency = currency.Abbreviation;
2021-05-28 17:20:12 +00:00
_settingsManager.Save<CrmSettings>(tenantSettings);
2020-03-02 15:38:31 +00:00
}
2021-03-01 17:36:18 +00:00
2021-03-19 16:56:26 +00:00
2020-03-02 15:38:31 +00:00
/// <summary>
/// The method to Decode your Base64 strings.
/// </summary>
/// <param name="encodedData">The String containing the characters to decode.</param>
/// <returns>A String containing the results of decoding the specified sequence of bytes.</returns>
public static string DecodeFrom64(string encodedData)
{
var encodedDataAsBytes = Convert.FromBase64String(encodedData);
return System.Text.Encoding.UTF8.GetString(encodedDataAsBytes);
}
/// <summary>
/// The method create a Base64 encoded string from a normal string.
/// </summary>
/// <param name="toEncode">The String containing the characters to encode.</param>
/// <returns>The Base64 encoded string.</returns>
public static string EncodeTo64(string toEncode)
{
var toEncodeAsBytes = System.Text.Encoding.UTF8.GetBytes(toEncode);
return Convert.ToBase64String(toEncodeAsBytes);
}
public static byte[] ToByteArray(Stream inputStream)
{
var br = new MemoryStream();
var data = new byte[1024];
2022-01-12 12:34:58 +00:00
int readed;
2020-03-02 15:38:31 +00:00
while ((readed = inputStream.Read(data, 0, data.Length)) > 0)
{
br.Write(data, 0, readed);
}
br.Close();
return br.ToArray();
}
2021-03-19 16:56:26 +00:00
public static string GetImgFormatName(IImageFormat format)
2020-03-02 15:38:31 +00:00
{
return format.Name.ToLower();
2020-03-02 15:38:31 +00:00
}
private static readonly string[] Formats = new[]
{
"o",
"yyyy'-'MM'-'dd'T'HH'-'mm'-'ss'.'fffK",
"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffK",
"yyyy-MM-ddTHH:mm:ss"
};
public static DateTime ApiDateTimeParse(string data)
{
if (string.IsNullOrEmpty(data)) throw new ArgumentNullException("data");
if (data.Length < 7) throw new ArgumentException(CRMErrorsResource.DateTimeFormatInvalid);
DateTime dateTime;
if (DateTime.TryParseExact(data, Formats, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out dateTime))
{
return new DateTime(dateTime.Ticks, DateTimeKind.Unspecified);
}
throw new ArgumentException(CRMErrorsResource.DateTimeFormatInvalid);
}
public static JsonDocument JObjectParseWithDateAsString(string data)
2020-03-02 15:38:31 +00:00
{
var readOnlySpan = new ReadOnlySpan<byte>(Encoding.UTF8.GetBytes(data));
Utf8JsonReader reader = new Utf8JsonReader(readOnlySpan);
JsonDocument result;
if (JsonDocument.TryParseValue(ref reader, out result))
{
return result;
}
return null;
2021-03-19 16:56:26 +00:00
}
2020-03-02 15:38:31 +00:00
}
}