DocSpace-client/web/ASC.Web.Core/SsoUserData.cs

208 lines
6.4 KiB
C#
Raw Normal View History

2022-04-21 10:35:49 +00:00
// (c) Copyright Ascensio System SIA 2010-2022
//
// This program is a free software product.
// You can redistribute it and/or modify it under the terms
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
// Foundation. In accordance with Section 7(a) of the GNU AGPL 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 details, see
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
//
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
//
// The interactive user interfaces in modified source and object code versions of the Program must
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
//
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
// trademark law for use of our trademarks.
//
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
2022-03-16 10:32:30 +00:00
2022-04-28 13:56:12 +00:00
using Constants = ASC.Core.Users.Constants;
using JsonSerializer = System.Text.Json.JsonSerializer;
2022-04-21 10:35:49 +00:00
namespace ASC.Web.Studio.UserControls.Management.SingleSignOnSettings;
2022-04-19 11:02:06 +00:00
[Serializable]
public class SsoUserData
2022-03-16 10:32:30 +00:00
{
2022-04-19 11:02:06 +00:00
private readonly UserManager _userManager;
2022-04-21 10:35:49 +00:00
private readonly TenantUtil _tenantUtil;
2022-04-19 11:02:06 +00:00
public SsoUserData(
UserManager userManager,
TenantUtil tenantUtil)
2022-03-16 10:32:30 +00:00
{
2022-04-19 11:02:06 +00:00
_userManager = userManager;
2022-04-21 10:35:49 +00:00
_tenantUtil = tenantUtil;
2022-04-19 11:02:06 +00:00
}
private const int MAX_NUMBER_OF_SYMBOLS = 64;
[DataMember(Name = "nameID")]
public string NameId { get; set; }
2022-03-16 10:32:30 +00:00
2022-04-19 11:02:06 +00:00
[DataMember(Name = "sessionID")]
public string SessionId { get; set; }
2022-03-16 10:32:30 +00:00
2022-04-19 11:02:06 +00:00
[DataMember(Name = "email")]
public string Email { get; set; }
2022-03-16 10:32:30 +00:00
2022-04-19 11:02:06 +00:00
[DataMember(Name = "firstName")]
public string FirstName { get; set; }
2022-03-16 10:32:30 +00:00
2022-04-19 11:02:06 +00:00
[DataMember(Name = "lastName")]
public string LastName { get; set; }
2022-03-16 10:32:30 +00:00
2022-04-19 11:02:06 +00:00
[DataMember(Name = "location")]
public string Location { get; set; }
2022-03-16 10:32:30 +00:00
2022-04-19 11:02:06 +00:00
[DataMember(Name = "phone")]
public string Phone { get; set; }
2022-03-16 10:32:30 +00:00
2022-04-19 11:02:06 +00:00
[DataMember(Name = "title")]
public string Title { get; set; }
2022-03-16 10:32:30 +00:00
2022-04-19 11:02:06 +00:00
public override string ToString()
{
return JsonSerializer.Serialize(this);
}
2022-03-16 10:32:30 +00:00
2022-04-19 11:02:06 +00:00
private const string MOB_PHONE = "mobphone";
private const string EXT_MOB_PHONE = "extmobphone";
2022-03-16 10:32:30 +00:00
2022-04-19 11:02:06 +00:00
public UserInfo ToUserInfo(bool checkExistance = false)
{
var firstName = TrimToLimit(FirstName);
var lastName = TrimToLimit(LastName);
if (string.IsNullOrEmpty(Email) || string.IsNullOrEmpty(firstName) || string.IsNullOrEmpty(lastName))
2022-03-16 10:32:30 +00:00
{
2022-04-19 11:02:06 +00:00
return Constants.LostUser;
2022-03-16 10:32:30 +00:00
}
2022-04-19 11:02:06 +00:00
var userInfo = Constants.LostUser;
2022-03-16 10:32:30 +00:00
2022-04-19 11:02:06 +00:00
if (checkExistance)
2022-03-16 10:32:30 +00:00
{
2022-04-19 11:02:06 +00:00
userInfo = _userManager.GetSsoUserByNameId(NameId);
2022-03-16 10:32:30 +00:00
2022-04-19 11:02:06 +00:00
if (Equals(userInfo, Constants.LostUser))
2022-03-16 10:32:30 +00:00
{
2022-04-19 11:02:06 +00:00
userInfo = _userManager.GetUserByEmail(Email);
2022-03-16 10:32:30 +00:00
}
2022-04-19 11:02:06 +00:00
}
2022-03-16 10:32:30 +00:00
2022-04-19 11:02:06 +00:00
if (Equals(userInfo, Constants.LostUser))
{
userInfo = new UserInfo
2022-03-16 10:32:30 +00:00
{
2022-04-19 11:02:06 +00:00
Email = Email,
FirstName = firstName,
LastName = lastName,
SsoNameId = NameId,
SsoSessionId = SessionId,
Location = Location,
Title = Title,
ActivationStatus = EmployeeActivationStatus.NotActivated,
2022-04-21 10:35:49 +00:00
WorkFromDate = _tenantUtil.DateTimeNow()
2022-04-19 11:02:06 +00:00
};
if (string.IsNullOrEmpty(Phone))
return userInfo;
var contacts = new List<string> { EXT_MOB_PHONE, Phone };
userInfo.ContactsList = contacts;
}
else
{
userInfo.Email = Email;
userInfo.FirstName = firstName;
userInfo.LastName = lastName;
userInfo.SsoNameId = NameId;
userInfo.SsoSessionId = SessionId;
userInfo.Location = Location;
userInfo.Title = Title;
2022-03-16 10:32:30 +00:00
2022-04-19 11:02:06 +00:00
var portalUserContacts = userInfo.ContactsList;
2022-03-16 10:32:30 +00:00
2022-04-19 11:02:06 +00:00
var newContacts = new List<string>();
var phones = new List<string>();
var otherContacts = new List<string>();
2022-03-16 10:32:30 +00:00
2022-04-19 11:02:06 +00:00
for (int i = 0, n = portalUserContacts.Count; i < n; i += 2)
{
if (i + 1 >= portalUserContacts.Count)
continue;
2022-03-16 10:32:30 +00:00
2022-04-19 11:02:06 +00:00
var type = portalUserContacts[i];
var value = portalUserContacts[i + 1];
2022-03-16 10:32:30 +00:00
2022-04-19 11:02:06 +00:00
switch (type)
2022-03-16 10:32:30 +00:00
{
2022-04-19 11:02:06 +00:00
case EXT_MOB_PHONE:
break;
case MOB_PHONE:
phones.Add(value);
break;
default:
otherContacts.Add(type);
otherContacts.Add(value);
break;
2022-03-16 10:32:30 +00:00
}
2022-04-19 11:02:06 +00:00
}
2022-03-16 10:32:30 +00:00
2022-04-19 11:02:06 +00:00
if (!string.IsNullOrEmpty(Phone))
{
if (phones.Exists(p => p.Equals(Phone)))
2022-03-16 10:32:30 +00:00
{
2022-04-19 11:02:06 +00:00
phones.Remove(Phone);
2022-03-16 10:32:30 +00:00
}
2022-04-19 11:02:06 +00:00
newContacts.Add(EXT_MOB_PHONE);
newContacts.Add(Phone);
2022-03-16 10:32:30 +00:00
}
2022-04-19 11:02:06 +00:00
phones.ForEach(p =>
{
newContacts.Add(MOB_PHONE);
newContacts.Add(p);
});
2022-03-16 10:32:30 +00:00
2022-04-19 11:02:06 +00:00
newContacts.AddRange(otherContacts);
2022-03-16 10:32:30 +00:00
2022-04-19 11:02:06 +00:00
userInfo.ContactsList = newContacts;
2022-03-16 10:32:30 +00:00
}
2022-04-19 11:02:06 +00:00
return userInfo;
2022-03-16 10:32:30 +00:00
}
2022-04-19 11:02:06 +00:00
private static string TrimToLimit(string str, int limit = MAX_NUMBER_OF_SYMBOLS)
2022-03-16 10:32:30 +00:00
{
2022-04-19 11:02:06 +00:00
if (string.IsNullOrEmpty(str))
return "";
2022-03-16 10:32:30 +00:00
2022-04-19 11:02:06 +00:00
var newStr = str.Trim();
return newStr.Length > limit
? newStr.Substring(0, MAX_NUMBER_OF_SYMBOLS)
: newStr;
2022-03-16 10:32:30 +00:00
}
}
2022-04-19 11:02:06 +00:00
[Serializable]
public class LogoutSsoUserData
{
[DataMember(Name = "nameID")]
public string NameId { get; set; }
[DataMember(Name = "sessionID")]
public string SessionId { get; set; }
}