/* * * (c) Copyright Ascensio System Limited 2010-2021 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ namespace ASC.ActiveDirectory.Base.Settings; [Scope] [Serializable] [DataContract] public class LdapSettings : ISettings, ICloneable { public Guid ID { get { return new Guid("{197149b3-fbc9-44c2-b42a-232f7e729c16}"); } } public LdapSettings() { LdapMapping = new Dictionary(); AccessRights = new Dictionary(); } public enum MappingFields { FirstNameAttribute, SecondNameAttribute, BirthDayAttribute, GenderAttribute, MobilePhoneAttribute, MailAttribute, TitleAttribute, LocationAttribute, AvatarAttribute, AdditionalPhone, AdditionalMobilePhone, AdditionalMail, Skype } public enum AccessRight { FullAccess, Documents, Projects, CRM, Community, People, Mail } public static readonly Dictionary AccessRightsGuids = new Dictionary() { { AccessRight.FullAccess, Guid.Empty }, { AccessRight.Documents, WebItemManager.DocumentsProductID }, { AccessRight.Projects, WebItemManager.ProjectsProductID }, { AccessRight.CRM, WebItemManager.CRMProductID }, { AccessRight.Community, WebItemManager.CommunityProductID }, { AccessRight.People, WebItemManager.PeopleProductID }, { AccessRight.Mail, WebItemManager.MailProductID } }; public ISettings GetDefault(IServiceProvider serviceProvider) { var isMono = WorkContext.IsMono; var settings = new LdapSettings() { Server = "", UserDN = "", PortNumber = LdapConstants.STANDART_LDAP_PORT, UserFilter = string.Format("({0}=*)", isMono ? LdapConstants.RfcLDAPAttributes.UID : LdapConstants.ADSchemaAttributes.USER_PRINCIPAL_NAME), LoginAttribute = isMono ? LdapConstants.RfcLDAPAttributes.UID : LdapConstants.ADSchemaAttributes.ACCOUNT_NAME, FirstNameAttribute = LdapConstants.ADSchemaAttributes.FIRST_NAME, SecondNameAttribute = LdapConstants.ADSchemaAttributes.SURNAME, MailAttribute = LdapConstants.ADSchemaAttributes.MAIL, TitleAttribute = LdapConstants.ADSchemaAttributes.TITLE, MobilePhoneAttribute = LdapConstants.ADSchemaAttributes.MOBILE, LocationAttribute = LdapConstants.ADSchemaAttributes.STREET, GroupDN = "", GroupFilter = string.Format("({0}={1})", LdapConstants.ADSchemaAttributes.OBJECT_CLASS, isMono ? LdapConstants.ObjectClassKnowedValues.POSIX_GROUP : LdapConstants.ObjectClassKnowedValues.GROUP), UserAttribute = isMono ? LdapConstants.RfcLDAPAttributes.UID : LdapConstants.ADSchemaAttributes.DISTINGUISHED_NAME, GroupAttribute = isMono ? LdapConstants.RfcLDAPAttributes.MEMBER_UID : LdapConstants.ADSchemaAttributes.MEMBER, GroupNameAttribute = LdapConstants.ADSchemaAttributes.COMMON_NAME, Authentication = true, AcceptCertificate = false, AcceptCertificateHash = null, StartTls = false, Ssl = false, SendWelcomeEmail = false }; return settings; } public override bool Equals(object obj) { var settings = obj as LdapSettings; return settings != null && EnableLdapAuthentication == settings.EnableLdapAuthentication && StartTls == settings.StartTls && Ssl == settings.Ssl && SendWelcomeEmail == settings.SendWelcomeEmail && (string.IsNullOrEmpty(Server) && string.IsNullOrEmpty(settings.Server) || Server == settings.Server) && (string.IsNullOrEmpty(UserDN) && string.IsNullOrEmpty(settings.UserDN) || UserDN == settings.UserDN) && PortNumber == settings.PortNumber && UserFilter == settings.UserFilter && LoginAttribute == settings.LoginAttribute && LdapMapping.Count == settings.LdapMapping.Count && LdapMapping.All(pair => settings.LdapMapping.ContainsKey(pair.Key) && pair.Value == settings.LdapMapping[pair.Key]) && AccessRights.Count == settings.AccessRights.Count && AccessRights.All(pair => settings.AccessRights.ContainsKey(pair.Key) && pair.Value == settings.AccessRights[pair.Key]) && GroupMembership == settings.GroupMembership && (string.IsNullOrEmpty(GroupDN) && string.IsNullOrEmpty(settings.GroupDN) || GroupDN == settings.GroupDN) && GroupFilter == settings.GroupFilter && UserAttribute == settings.UserAttribute && GroupAttribute == settings.GroupAttribute && (string.IsNullOrEmpty(Login) && string.IsNullOrEmpty(settings.Login) || Login == settings.Login) && Authentication == settings.Authentication; } public override int GetHashCode() { var hash = 3; hash = (hash * 2) + EnableLdapAuthentication.GetHashCode(); hash = (hash * 2) + StartTls.GetHashCode(); hash = (hash * 2) + Ssl.GetHashCode(); hash = (hash * 2) + SendWelcomeEmail.GetHashCode(); hash = (hash * 2) + Server.GetHashCode(); hash = (hash * 2) + UserDN.GetHashCode(); hash = (hash * 2) + PortNumber.GetHashCode(); hash = (hash * 2) + UserFilter.GetHashCode(); hash = (hash * 2) + LoginAttribute.GetHashCode(); hash = (hash * 2) + GroupMembership.GetHashCode(); hash = (hash * 2) + GroupDN.GetHashCode(); hash = (hash * 2) + GroupNameAttribute.GetHashCode(); hash = (hash * 2) + GroupFilter.GetHashCode(); hash = (hash * 2) + UserAttribute.GetHashCode(); hash = (hash * 2) + GroupAttribute.GetHashCode(); hash = (hash * 2) + Authentication.GetHashCode(); hash = (hash * 2) + Login.GetHashCode(); foreach (var pair in LdapMapping) hash = (hash * 2) + pair.Value.GetHashCode(); foreach (var pair in AccessRights) hash = (hash * 2) + pair.Value.GetHashCode(); return hash; } public object Clone() { return MemberwiseClone(); } [DataMember] public bool EnableLdapAuthentication { get; set; } [DataMember] public bool StartTls { get; set; } [DataMember] public bool Ssl { get; set; } [DataMember] public bool SendWelcomeEmail { get; set; } [DataMember] public string Server { get; set; } [DataMember] // ReSharper disable once InconsistentNaming public string UserDN { get; set; } [DataMember] public int PortNumber { get; set; } [DataMember] public string UserFilter { get; set; } [DataMember] public string LoginAttribute { get; set; } [DataMember] public Dictionary LdapMapping { get; set; } //ToDo: use SId instead of group name [DataMember] public Dictionary AccessRights { get; set; } [DataMember] public string FirstNameAttribute { get { return GetOldSetting(MappingFields.FirstNameAttribute); } set { SetOldSetting(MappingFields.FirstNameAttribute, value); } } [DataMember] public string SecondNameAttribute { get { return GetOldSetting(MappingFields.SecondNameAttribute); } set { SetOldSetting(MappingFields.SecondNameAttribute, value); } } [DataMember] public string MailAttribute { get { return GetOldSetting(MappingFields.MailAttribute); } set { SetOldSetting(MappingFields.MailAttribute, value); } } [DataMember] public string TitleAttribute { get { return GetOldSetting(MappingFields.TitleAttribute); } set { SetOldSetting(MappingFields.TitleAttribute, value); } } [DataMember] public string MobilePhoneAttribute { get { return GetOldSetting(MappingFields.MobilePhoneAttribute); } set { SetOldSetting(MappingFields.MobilePhoneAttribute, value); } } [DataMember] public string LocationAttribute { get { return GetOldSetting(MappingFields.LocationAttribute); } set { SetOldSetting(MappingFields.LocationAttribute, value); } } [DataMember] public bool GroupMembership { get; set; } [DataMember] // ReSharper disable once InconsistentNaming public string GroupDN { get; set; } [DataMember] public string GroupNameAttribute { get; set; } [DataMember] public string GroupFilter { get; set; } [DataMember] public string UserAttribute { get; set; } [DataMember] public string GroupAttribute { get; set; } [DataMember] public bool Authentication { get; set; } [DataMember] public string Login { get; set; } [DataMember] public string Password { get; set; } [DataMember] public byte[] PasswordBytes { get; set; } [DataMember] public bool IsDefault { get; set; } [DataMember] public bool AcceptCertificate { get; set; } [DataMember] public string AcceptCertificateHash { get; set; } private string GetOldSetting(MappingFields field) { if (LdapMapping == null) LdapMapping = new Dictionary(); if (LdapMapping.ContainsKey(field)) return LdapMapping[field]; else return ""; } private void SetOldSetting(MappingFields field, string value) { if (LdapMapping == null) LdapMapping = new Dictionary(); if (string.IsNullOrEmpty(value)) { if (LdapMapping.ContainsKey(field)) { LdapMapping.Remove(field); } return; } if (LdapMapping.ContainsKey(field)) { LdapMapping[field] = value; } else { LdapMapping.Add(field, value); } } } [Scope] [Serializable] [DataContract] public class LdapCronSettings : ISettings { public Guid ID { get { return new Guid("{58C42C54-56CD-4BEF-A3ED-C60ACCF6E975}"); } } public ISettings GetDefault(IServiceProvider serviceProvider) { return new LdapCronSettings() { Cron = null }; } [DataMember] public string Cron { get; set; } } [Serializable] [DataContract] public class LdapCurrentAcccessSettings : ISettings { public Guid ID { get { return new Guid("{134B5EAA-F612-4834-AEAB-34C90515EA4E}"); } } public ISettings GetDefault(IServiceProvider serviceProvider) { return new LdapCurrentAcccessSettings() { CurrentAccessRights = null }; } public LdapCurrentAcccessSettings() { CurrentAccessRights = new Dictionary>(); } [DataMember] public Dictionary> CurrentAccessRights { get; set; } } [Serializable] [DataContract] public class LdapCurrentUserPhotos : ISettings { public Guid ID { get { return new Guid("{50AE3C2B-0783-480F-AF30-679D0F0A2D3E}"); } } public ISettings GetDefault(IServiceProvider serviceProvider) { return new LdapCurrentUserPhotos() { CurrentPhotos = null }; } public LdapCurrentUserPhotos() { CurrentPhotos = new Dictionary(); } [DataMember] public Dictionary CurrentPhotos { get; set; } } [Serializable] [DataContract] public class LdapCurrentDomain : ISettings { public Guid ID { get { return new Guid("{75A5F745-F697-4418-B38D-0FE0D277E258}"); } } public ISettings GetDefault(IServiceProvider serviceProvider) { return new LdapCurrentDomain() { CurrentDomain = null }; } [DataMember] public string CurrentDomain { get; set; } }