DocSpace-buildtools/common/ASC.ActiveDirectory/Base/Data/LdapObjectExtension.cs

279 lines
10 KiB
C#
Raw Normal View History

2022-05-05 13:23:05 +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-14 18:58:06 +00:00
using Mapping = ASC.ActiveDirectory.Base.Settings.LdapSettings.MappingFields;
2022-03-17 19:44:34 +00:00
namespace ASC.ActiveDirectory.Base.Data;
/// <summary>
/// LDAP object extensions class
/// </summary>
[Scope]
public class LdapObjectExtension
2022-03-14 18:58:06 +00:00
{
2022-03-17 19:44:34 +00:00
private readonly TenantUtil _tenantUtil;
2022-08-30 08:00:37 +00:00
private readonly SettingsManager _settingsManager;
private readonly TenantManager _tenantManager;
2022-06-15 12:39:37 +00:00
private readonly ILogger<LdapObjectExtension> _logger;
2022-08-30 08:00:37 +00:00
public LdapObjectExtension(TenantUtil tenantUtil, SettingsManager settingsManager, TenantManager tenantManager, ILogger<LdapObjectExtension> logger)
2022-03-17 19:44:34 +00:00
{
_tenantUtil = tenantUtil;
2022-08-30 08:00:37 +00:00
_settingsManager = settingsManager;
_tenantManager = tenantManager;
2022-06-15 12:39:37 +00:00
_logger = logger;
2022-03-17 19:44:34 +00:00
}
2022-06-15 12:39:37 +00:00
public string GetAttribute(LdapObject ldapObject, string attribute)
2022-03-14 18:58:06 +00:00
{
2022-03-17 19:44:34 +00:00
if (string.IsNullOrEmpty(attribute))
2022-06-15 12:39:37 +00:00
{
2022-03-17 19:44:34 +00:00
return string.Empty;
2022-06-15 12:39:37 +00:00
}
2022-03-17 19:44:34 +00:00
try
2022-03-14 18:58:06 +00:00
{
2022-03-17 19:44:34 +00:00
return ldapObject.GetValue(attribute) as string;
2022-03-14 18:58:06 +00:00
}
2022-03-17 19:44:34 +00:00
catch (Exception e)
2022-03-14 18:58:06 +00:00
{
2022-06-15 12:39:37 +00:00
_logger.ErrorCanNotGetAttribute(attribute, ldapObject.DistinguishedName, e);
2022-03-14 18:58:06 +00:00
2022-03-17 19:44:34 +00:00
return string.Empty;
2022-03-14 18:58:06 +00:00
}
2022-03-17 19:44:34 +00:00
}
2022-03-14 18:58:06 +00:00
2022-06-15 12:39:37 +00:00
public List<string> GetAttributes(LdapObject ldapObject, string attribute)
2022-03-17 19:44:34 +00:00
{
var list = new List<string>();
2022-03-14 18:58:06 +00:00
2022-03-17 19:44:34 +00:00
if (string.IsNullOrEmpty(attribute))
2022-06-15 12:39:37 +00:00
{
2022-03-17 19:44:34 +00:00
return list;
2022-06-15 12:39:37 +00:00
}
2022-03-14 18:58:06 +00:00
2022-03-17 19:44:34 +00:00
try
{
return ldapObject.GetValues(attribute);
}
catch (Exception e)
{
2022-06-15 12:39:37 +00:00
_logger.ErrorCanNotGetAttributes(attribute, ldapObject.DistinguishedName, e);
2022-03-14 18:58:06 +00:00
2022-03-17 19:44:34 +00:00
return list;
2022-03-14 18:58:06 +00:00
}
2022-03-17 19:44:34 +00:00
}
2022-03-14 18:58:06 +00:00
2022-03-17 19:44:34 +00:00
private const int MAX_NUMBER_OF_SYMBOLS = 64;
private const string EXT_MOB_PHONE = "extmobphone";
private const string EXT_MAIL = "extmail";
private const string EXT_PHONE = "extphone";
private const string EXT_SKYPE = "extskype";
2022-03-14 18:58:06 +00:00
2022-06-15 12:39:37 +00:00
private List<string> GetContacts(LdapObject ldapUser, Mapping key, LdapSettings settings)
2022-03-17 19:44:34 +00:00
{
if (!settings.LdapMapping.ContainsKey(key))
2022-06-15 12:39:37 +00:00
{
2022-03-17 19:44:34 +00:00
return null;
2022-06-15 12:39:37 +00:00
}
2022-03-14 18:58:06 +00:00
2022-03-17 19:44:34 +00:00
var bindings = settings.LdapMapping[key].Split(',').Select(x => x.Trim()).ToArray();
if (bindings.Length > 1)
{
var list = new List<string>();
foreach (var bind in bindings)
2022-03-14 18:58:06 +00:00
{
2022-06-15 12:39:37 +00:00
list.AddRange(GetAttributes(ldapUser, bind));
2022-03-14 18:58:06 +00:00
}
2022-03-17 19:44:34 +00:00
return list;
2022-03-14 18:58:06 +00:00
}
2022-03-17 19:44:34 +00:00
else
2022-03-14 18:58:06 +00:00
{
2022-06-15 12:39:37 +00:00
return GetAttributes(ldapUser, bindings[0]);
2022-03-14 18:58:06 +00:00
}
2022-03-17 19:44:34 +00:00
}
2022-03-14 18:58:06 +00:00
2022-06-15 12:39:37 +00:00
private void PopulateContacts(List<string> Contacts, string type, List<string> values)
2022-03-17 19:44:34 +00:00
{
if (values == null || !values.Any())
2022-06-15 12:39:37 +00:00
{
2022-03-17 19:44:34 +00:00
return;
2022-06-15 12:39:37 +00:00
}
2022-03-17 19:44:34 +00:00
foreach (var val in values)
2022-03-14 18:58:06 +00:00
{
2022-03-17 19:44:34 +00:00
Contacts.Add(type);
Contacts.Add(val);
}
}
2022-03-14 18:58:06 +00:00
2022-06-15 12:39:37 +00:00
public UserInfo ToUserInfo(LdapObject ldapUser, LdapUserImporter ldapUserImporter)
2022-03-17 19:44:34 +00:00
{
var settings = ldapUserImporter.Settings;
var resource = ldapUserImporter.Resource;
2022-03-14 18:58:06 +00:00
2022-06-15 12:39:37 +00:00
var userName = GetAttribute(ldapUser, settings.LoginAttribute);
2022-03-14 18:58:06 +00:00
2022-06-15 12:39:37 +00:00
var firstName = settings.LdapMapping.ContainsKey(Mapping.FirstNameAttribute) ? GetAttribute(ldapUser, settings.LdapMapping[Mapping.FirstNameAttribute]) : string.Empty;
var secondName = settings.LdapMapping.ContainsKey(Mapping.SecondNameAttribute) ? GetAttribute(ldapUser, settings.LdapMapping[Mapping.SecondNameAttribute]) : string.Empty;
var birthDay = settings.LdapMapping.ContainsKey(Mapping.BirthDayAttribute) ? GetAttribute(ldapUser, settings.LdapMapping[Mapping.BirthDayAttribute]) : string.Empty;
var gender = settings.LdapMapping.ContainsKey(Mapping.GenderAttribute) ? GetAttribute(ldapUser, settings.LdapMapping[Mapping.GenderAttribute]) : string.Empty;
var primaryPhone = settings.LdapMapping.ContainsKey(Mapping.MobilePhoneAttribute) ? GetAttribute(ldapUser, settings.LdapMapping[Mapping.MobilePhoneAttribute]) : string.Empty;
var mail = settings.LdapMapping.ContainsKey(Mapping.MailAttribute) ? GetAttribute(ldapUser, settings.LdapMapping[Mapping.MailAttribute]) : string.Empty;
var title = settings.LdapMapping.ContainsKey(Mapping.TitleAttribute) ? GetAttribute(ldapUser, settings.LdapMapping[Mapping.TitleAttribute]) : string.Empty;
var location = settings.LdapMapping.ContainsKey(Mapping.LocationAttribute) ? GetAttribute(ldapUser, settings.LdapMapping[Mapping.LocationAttribute]) : string.Empty;
2022-03-14 18:58:06 +00:00
2022-06-15 12:39:37 +00:00
var phones = GetContacts(ldapUser, Mapping.AdditionalPhone, settings);
var mobilePhones = GetContacts(ldapUser, Mapping.AdditionalMobilePhone, settings);
var emails = GetContacts(ldapUser, Mapping.AdditionalMail, settings);
var skype = GetContacts(ldapUser, Mapping.Skype, settings);
2022-03-14 18:58:06 +00:00
2022-08-30 08:00:37 +00:00
var quotaSettings = _settingsManager.LoadForTenant<UserQuotaSettings>(_tenantManager.GetCurrentTenant().Id);
var quota = settings.LdapMapping.ContainsKey(Mapping.UserQuotaLimit) ? GetAttribute(ldapUser, settings.LdapMapping[Mapping.UserQuotaLimit]) : quotaSettings.DefaultUserQuota;
2022-03-14 18:58:06 +00:00
2022-03-17 19:44:34 +00:00
if (string.IsNullOrEmpty(userName))
2022-06-15 12:39:37 +00:00
{
2022-03-17 19:44:34 +00:00
throw new Exception("LDAP LoginAttribute is empty");
2022-06-15 12:39:37 +00:00
}
2022-03-14 18:58:06 +00:00
2022-03-17 19:44:34 +00:00
var contacts = new List<string>();
2022-03-14 18:58:06 +00:00
2022-03-17 19:44:34 +00:00
PopulateContacts(contacts, EXT_PHONE, phones);
PopulateContacts(contacts, EXT_MOB_PHONE, mobilePhones);
PopulateContacts(contacts, EXT_MAIL, emails);
PopulateContacts(contacts, EXT_SKYPE, skype);
2022-03-14 18:58:06 +00:00
2022-03-17 19:44:34 +00:00
var user = new UserInfo
{
2022-05-05 13:23:05 +00:00
Id = Guid.Empty,
2022-03-17 19:44:34 +00:00
UserName = userName,
Sid = ldapUser.Sid,
ActivationStatus = settings.SendWelcomeEmail && !string.IsNullOrEmpty(mail) ? EmployeeActivationStatus.Pending : EmployeeActivationStatus.NotActivated,
Status = ldapUser.IsDisabled ? EmployeeStatus.Terminated : EmployeeStatus.Active,
Title = !string.IsNullOrEmpty(title) ? title : string.Empty,
Location = !string.IsNullOrEmpty(location) ? location : string.Empty,
WorkFromDate = _tenantUtil.DateTimeNow(),
2022-08-29 11:07:52 +00:00
ContactsList = contacts,
2022-08-30 08:00:37 +00:00
QuotaLimit = quotaSettings.EnableUserQuota ? quota : ""
2022-03-17 19:44:34 +00:00
};
if (!string.IsNullOrEmpty(firstName))
{
user.FirstName = firstName.Length > MAX_NUMBER_OF_SYMBOLS
? firstName.Substring(0, MAX_NUMBER_OF_SYMBOLS)
: firstName;
}
else
{
user.FirstName = resource.FirstName;
}
2022-03-14 18:58:06 +00:00
2022-03-17 19:44:34 +00:00
if (!string.IsNullOrEmpty(secondName))
{
user.LastName = secondName.Length > MAX_NUMBER_OF_SYMBOLS
? secondName.Substring(0, MAX_NUMBER_OF_SYMBOLS)
: secondName;
}
else
{
user.LastName = resource.LastName;
}
2022-03-14 18:58:06 +00:00
2022-03-17 19:44:34 +00:00
if (!string.IsNullOrEmpty(birthDay))
{
DateTime date;
if (DateTime.TryParse(birthDay, out date))
2022-06-15 12:39:37 +00:00
{
2022-03-17 19:44:34 +00:00
user.BirthDate = date;
2022-06-15 12:39:37 +00:00
}
2022-03-17 19:44:34 +00:00
}
2022-03-14 18:58:06 +00:00
2022-03-17 19:44:34 +00:00
if (!string.IsNullOrEmpty(gender))
{
bool b;
if (bool.TryParse(gender, out b))
2022-03-14 18:58:06 +00:00
{
2022-03-17 19:44:34 +00:00
user.Sex = b;
2022-03-14 18:58:06 +00:00
}
else
{
2022-03-17 19:44:34 +00:00
switch (gender.ToLowerInvariant())
{
case "male":
case "m":
user.Sex = true;
break;
case "female":
case "f":
user.Sex = false;
break;
}
2022-03-14 18:58:06 +00:00
}
}
2022-03-17 19:44:34 +00:00
if (string.IsNullOrEmpty(mail))
2022-03-14 18:58:06 +00:00
{
2022-03-17 19:44:34 +00:00
user.Email = userName.Contains("@") ? userName : string.Format("{0}@{1}", userName, ldapUserImporter.LDAPDomain);
user.ActivationStatus = EmployeeActivationStatus.AutoGenerated;
}
else
{
user.Email = mail;
}
2022-03-14 18:58:06 +00:00
2022-03-17 19:44:34 +00:00
user.MobilePhone = string.IsNullOrEmpty(primaryPhone)
? null : primaryPhone;
2022-03-14 18:58:06 +00:00
2022-03-17 19:44:34 +00:00
return user;
}
2022-03-14 18:58:06 +00:00
2022-06-15 12:39:37 +00:00
public GroupInfo ToGroupInfo(LdapObject ldapGroup, LdapSettings settings)
2022-03-17 19:44:34 +00:00
{
2022-06-15 12:39:37 +00:00
var name = GetAttribute(ldapGroup, settings.GroupNameAttribute);
2022-03-17 19:44:34 +00:00
if (string.IsNullOrEmpty(name))
2022-06-15 12:39:37 +00:00
{
2022-03-17 19:44:34 +00:00
throw new Exception("LDAP GroupNameAttribute is empty");
2022-06-15 12:39:37 +00:00
}
2022-03-14 18:58:06 +00:00
2022-03-17 19:44:34 +00:00
var group = new GroupInfo
2022-03-14 18:58:06 +00:00
{
2022-03-17 19:44:34 +00:00
Name = name,
Sid = ldapGroup.Sid
};
2022-03-14 18:58:06 +00:00
2022-03-17 19:44:34 +00:00
return group;
}
2022-06-15 12:39:37 +00:00
public string GetDomainFromDn(LdapObject ldapObject)
2022-03-17 19:44:34 +00:00
{
if (ldapObject == null || string.IsNullOrEmpty(ldapObject.DistinguishedName))
2022-06-15 12:39:37 +00:00
{
2022-03-17 19:44:34 +00:00
return null;
2022-06-15 12:39:37 +00:00
}
2022-03-17 19:44:34 +00:00
return LdapUtils.DistinguishedNameToDomain(ldapObject.DistinguishedName);
2022-03-14 18:58:06 +00:00
}
}