protected LdapSettingsChecker(LdapUserImporter importer)
 {
     LdapImporter = importer;
     log          = LogManager.GetLogger("ASC");
 }
Esempio n. 2
0
        private bool TryCheckAndSyncToLdapUser(Tuple <UserInfo, LdapObject> ldapUserInfo, LdapUserImporter importer,
                                               out UserInfo userInfo)
        {
            try
            {
                SecurityContext.AuthenticateMe(Core.Configuration.Constants.CoreSystem);

                userInfo = SyncLDAPUser(ldapUserInfo.Item1);

                if (userInfo == null || userInfo.Equals(Constants.LostUser))
                {
                    throw new Exception("The user did not pass the configuration check by ldap user settings");
                }

                var newLdapUserInfo = new Tuple <UserInfo, LdapObject>(userInfo, ldapUserInfo.Item2);

                if (!importer.Settings.GroupMembership)
                {
                    return(true);
                }

                if (!importer.TrySyncUserGroupMembership(newLdapUserInfo))
                {
                    throw new Exception("The user did not pass the configuration check by ldap group settings");
                }

                return(true);
            }
            catch (Exception ex)
            {
                _log.ErrorFormat("TrySyncLdapUser(SID: '{0}', Email: {1}) failed. Error: {2}", ldapUserInfo.Item1.Sid,
                                 ldapUserInfo.Item1.Email, ex);
            }
            finally
            {
                SecurityContext.Logout();
            }

            userInfo = Constants.LostUser;
            return(false);
        }
        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);
        }
Esempio n. 4
0
 public NovellLdapSettingsChecker(LdapUserImporter importer) :
     base(importer)
 {
 }
Esempio n. 5
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);
        }