public static string GetDomainFromDn(this LdapObject ldapObject)
        {
            if (ldapObject == null || string.IsNullOrEmpty(ldapObject.DistinguishedName))
            {
                return(null);
            }

            return(LdapUtils.DistinguishedNameToDomain(ldapObject.DistinguishedName));
        }
        public static GroupInfo ToGroupInfo(this LdapObject ldapGroup, LdapSettings settings, ILog log = null)
        {
            var name = ldapGroup.GetAttribute(settings.GroupNameAttribute, log);

            if (string.IsNullOrEmpty(name))
            {
                throw new Exception("LDAP GroupNameAttribute is empty");
            }

            var group = new GroupInfo
            {
                Name = name,
                Sid  = ldapGroup.Sid
            };

            return(group);
        }
        public static string GetAttribute(this LdapObject ldapObject, string attribute, ILog log = null)
        {
            if (string.IsNullOrEmpty(attribute))
            {
                return(string.Empty);
            }

            try
            {
                return(ldapObject.GetValue(attribute) as string);
            }
            catch (Exception e)
            {
                if (log != null)
                {
                    log.ErrorFormat("Can't get attribute from ldap object (attr = {0}, dn = {1}) error: {2}",
                                    attribute, ldapObject.DistinguishedName, e);
                }

                return(string.Empty);
            }
        }
        private static List <string> GetContacts(this LdapObject ldapUser, Mapping key, LdapSettings settings, ILog log = null)
        {
            if (!settings.LdapMapping.ContainsKey(key))
            {
                return(null);
            }

            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)
                {
                    list.AddRange(ldapUser.GetAttributes(bind, log));
                }
                return(list);
            }
            else
            {
                return(ldapUser.GetAttributes(bindings[0], log));
            }
        }
        public static List <string> GetAttributes(this LdapObject ldapObject, string attribute, ILog log = null)
        {
            var list = new List <string>();

            if (string.IsNullOrEmpty(attribute))
            {
                return(list);
            }

            try
            {
                return(ldapObject.GetValues(attribute));
            }
            catch (Exception e)
            {
                if (log != null)
                {
                    log.ErrorFormat("Can't get attributes from ldap object (attr = {0}, dn = {1}) error: {2}",
                                    attribute, ldapObject.DistinguishedName, e);
                }

                return(list);
            }
        }
        public static UserInfo ToUserInfo(this LdapObject ldapUser, LdapUserImporter ldapUserImporter, ILog log = null)
        {
            var settings = ldapUserImporter.Settings;
            var resource = ldapUserImporter.Resource;

            var userName = ldapUser.GetAttribute(settings.LoginAttribute, log);

            var firstName    = settings.LdapMapping.ContainsKey(Mapping.FirstNameAttribute) ? ldapUser.GetAttribute(settings.LdapMapping[Mapping.FirstNameAttribute], log) : string.Empty;
            var secondName   = settings.LdapMapping.ContainsKey(Mapping.SecondNameAttribute) ? ldapUser.GetAttribute(settings.LdapMapping[Mapping.SecondNameAttribute], log) : string.Empty;
            var birthDay     = settings.LdapMapping.ContainsKey(Mapping.BirthDayAttribute) ? ldapUser.GetAttribute(settings.LdapMapping[Mapping.BirthDayAttribute], log) : string.Empty;
            var gender       = settings.LdapMapping.ContainsKey(Mapping.GenderAttribute) ? ldapUser.GetAttribute(settings.LdapMapping[Mapping.GenderAttribute], log) : string.Empty;
            var primaryPhone = settings.LdapMapping.ContainsKey(Mapping.MobilePhoneAttribute) ? ldapUser.GetAttribute(settings.LdapMapping[Mapping.MobilePhoneAttribute], log) : string.Empty;
            var mail         = settings.LdapMapping.ContainsKey(Mapping.MailAttribute) ? ldapUser.GetAttribute(settings.LdapMapping[Mapping.MailAttribute], log) : string.Empty;
            var title        = settings.LdapMapping.ContainsKey(Mapping.TitleAttribute) ? ldapUser.GetAttribute(settings.LdapMapping[Mapping.TitleAttribute], log) : string.Empty;
            var location     = settings.LdapMapping.ContainsKey(Mapping.LocationAttribute) ? ldapUser.GetAttribute(settings.LdapMapping[Mapping.LocationAttribute], log) : string.Empty;

            var phones       = ldapUser.GetContacts(Mapping.AdditionalPhone, settings, log);
            var mobilePhones = ldapUser.GetContacts(Mapping.AdditionalMobilePhone, settings, log);
            var emails       = ldapUser.GetContacts(Mapping.AdditionalMail, settings, log);
            var skype        = ldapUser.GetContacts(Mapping.Skype, settings, log);


            if (string.IsNullOrEmpty(userName))
            {
                throw new Exception("LDAP LoginAttribute is empty");
            }

            var contacts = new List <string>();

            PopulateContacts(contacts, EXT_PHONE, phones);
            PopulateContacts(contacts, EXT_MOB_PHONE, mobilePhones);
            PopulateContacts(contacts, EXT_MAIL, emails);
            PopulateContacts(contacts, EXT_SKYPE, skype);

            var user = new UserInfo
            {
                ID               = Guid.Empty,
                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(),
                Contacts         = contacts
            };

            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;
            }

            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;
            }

            if (!string.IsNullOrEmpty(birthDay))
            {
                DateTime date;
                if (DateTime.TryParse(birthDay, out date))
                {
                    user.BirthDate = date;
                }
            }

            if (!string.IsNullOrEmpty(gender))
            {
                bool b;
                if (bool.TryParse(gender, out b))
                {
                    user.Sex = b;
                }
                else
                {
                    switch (gender.ToLowerInvariant())
                    {
                    case "male":
                    case "m":
                        user.Sex = true;
                        break;

                    case "female":
                    case "f":
                        user.Sex = false;
                        break;
                    }
                }
            }

            if (string.IsNullOrEmpty(mail))
            {
                user.Email            = userName.Contains("@") ? userName : string.Format("{0}@{1}", userName, ldapUserImporter.LDAPDomain);
                user.ActivationStatus = EmployeeActivationStatus.AutoGenerated;
            }
            else
            {
                user.Email = mail;
            }

            user.MobilePhone = string.IsNullOrEmpty(primaryPhone)
                ? null : primaryPhone;

            return(user);
        }
예제 #7
0
        public static UserInfo ToUserInfo(this LdapObject ldapUser, LdapUserImporter ldapUserImporter, ILog log = null)
        {
            var settings = ldapUserImporter.Settings;
            var resource = ldapUserImporter.Resource;

            var userName    = ldapUser.GetAttribute(settings.LoginAttribute, log);
            var firstName   = ldapUser.GetAttribute(settings.FirstNameAttribute, log);
            var secondName  = ldapUser.GetAttribute(settings.SecondNameAttribute, log);
            var mail        = ldapUser.GetAttribute(settings.MailAttribute, log);
            var emails      = ldapUser.GetAttributes(settings.MailAttribute, log);
            var mobilePhone = ldapUser.GetAttribute(settings.MobilePhoneAttribute, log);
            var title       = ldapUser.GetAttribute(settings.TitleAttribute, log);
            var location    = ldapUser.GetAttribute(settings.LocationAttribute, log);

            if (string.IsNullOrEmpty(userName))
            {
                throw new Exception("LDAP LoginAttribute is empty");
            }

            var contacts = new List <string>();

            if (!string.IsNullOrEmpty(mobilePhone))
            {
                contacts.Add(EXT_MOB_PHONE);
                contacts.Add(mobilePhone);
            }

            if (emails.Any())
            {
                foreach (var email in emails)
                {
                    if (email.Equals(mail))
                    {
                        continue;
                    }

                    contacts.Add(EXT_MAIL);
                    contacts.Add(email);
                }
            }

            var user = new UserInfo
            {
                ID               = Guid.Empty,
                UserName         = userName,
                Sid              = ldapUser.Sid,
                ActivationStatus = 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(),
                Contacts         = contacts
            };

            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;
            }

            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;
            }

            user.Email = string.IsNullOrEmpty(mail)
                ? (userName.Contains("@")
                    ? userName
                    : string.Format("{0}@{1}", userName, ldapUserImporter.LDAPDomain))
                : mail;

            return(user);
        }