Exemplo n.º 1
0
        public bool TryAddLDAPUser(UserInfo ldapUserInfo, bool onlyGetChanges, out UserInfo portalUserInfo)
        {
            portalUserInfo = Constants.LostUser;

            try
            {
                if (ldapUserInfo == null)
                {
                    throw new ArgumentNullException("ldapUserInfo");
                }

                _log.DebugFormat("TryAddLDAPUser(SID: {0}): Email '{1}' UserName: {2}", ldapUserInfo.Sid,
                                 ldapUserInfo.Email, ldapUserInfo.UserName);

                if (!CheckUniqueEmail(ldapUserInfo.ID, ldapUserInfo.Email))
                {
                    _log.DebugFormat("TryAddLDAPUser(SID: {0}): Email '{1}' already exists.",
                                     ldapUserInfo.Sid, ldapUserInfo.Email);

                    return(false);
                }

                if (!TryChangeExistingUserName(ldapUserInfo.UserName, onlyGetChanges))
                {
                    _log.DebugFormat("TryAddLDAPUser(SID: {0}): Username '{1}' already exists.",
                                     ldapUserInfo.Sid, ldapUserInfo.UserName);

                    return(false);
                }

                if (!ldapUserInfo.WorkFromDate.HasValue)
                {
                    ldapUserInfo.WorkFromDate = TenantUtil.DateTimeNow();
                }

                if (onlyGetChanges)
                {
                    portalUserInfo = ldapUserInfo;
                    return(true);
                }

                _log.DebugFormat("CoreContext.UserManager.SaveUserInfo({0})", ldapUserInfo.GetUserInfoString());

                portalUserInfo = CoreContext.UserManager.SaveUserInfo(ldapUserInfo);

                if (PasswordSettings == null)
                {
                    _log.DebugFormat("PasswordSettings.Load()");

                    PasswordSettings = PasswordSettings.Load();
                }

                var password = LdapUtils.GeneratePassword(PasswordSettings);

                _log.DebugFormat("SecurityContext.SetUserPassword(ID:{0})", portalUserInfo.ID);

                SecurityContext.SetUserPassword(portalUserInfo.ID, password);

                return(true);
            }
            catch (Exception ex)
            {
                if (ldapUserInfo != null)
                {
                    _log.ErrorFormat("TryAddLDAPUser(UserName='******' Sid='{1}') failed: Error: {2}", ldapUserInfo.UserName,
                                     ldapUserInfo.Sid, ex);
                }
            }

            return(false);
        }
Exemplo n.º 2
0
        public bool TryAddLDAPUser(UserInfo ldapUserInfo, bool onlyGetChanges, out UserInfo portalUserInfo)
        {
            portalUserInfo = Constants.LostUser;

            try
            {
                if (ldapUserInfo == null)
                {
                    throw new ArgumentNullException("ldapUserInfo");
                }

                _log.DebugFormat("TryAddLDAPUser(SID: {0}): Email '{1}' UserName: {2}", ldapUserInfo.Sid,
                                 ldapUserInfo.Email, ldapUserInfo.UserName);

                if (!CheckUniqueEmail(ldapUserInfo.ID, ldapUserInfo.Email))
                {
                    _log.DebugFormat("TryAddLDAPUser(SID: {0}): Email '{1}' already exists.",
                                     ldapUserInfo.Sid, ldapUserInfo.Email);

                    return(false);
                }

                if (!TryChangeExistingUserName(ldapUserInfo.UserName, onlyGetChanges))
                {
                    _log.DebugFormat("TryAddLDAPUser(SID: {0}): Username '{1}' already exists.",
                                     ldapUserInfo.Sid, ldapUserInfo.UserName);

                    return(false);
                }

                var q = CoreContext.TenantManager.GetTenantQuota(CoreContext.TenantManager.GetCurrentTenant().TenantId);
                if (q.ActiveUsers <= CoreContext.UserManager.GetUsersByGroup(Constants.GroupUser.ID).Length)
                {
                    _log.DebugFormat("TryAddLDAPUser(SID: {0}): Username '{1}' adding this user would exceed quota.",
                                     ldapUserInfo.Sid, ldapUserInfo.UserName);
                    throw new TenantQuotaException(string.Format("Exceeds the maximum active users ({0})", q.ActiveUsers));
                }

                if (!ldapUserInfo.WorkFromDate.HasValue)
                {
                    ldapUserInfo.WorkFromDate = TenantUtil.DateTimeNow();
                }

                if (onlyGetChanges)
                {
                    portalUserInfo = ldapUserInfo;
                    return(true);
                }

                _log.DebugFormat("CoreContext.UserManager.SaveUserInfo({0})", ldapUserInfo.GetUserInfoString());

                portalUserInfo = CoreContext.UserManager.SaveUserInfo(ldapUserInfo, syncCardDav: true);

                var passwordHash = LdapUtils.GeneratePassword();

                _log.DebugFormat("SecurityContext.SetUserPassword(ID:{0})", portalUserInfo.ID);

                SecurityContext.SetUserPasswordHash(portalUserInfo.ID, passwordHash);

                return(true);
            }
            catch (TenantQuotaException ex)
            {
                // rethrow if quota
                throw ex;
            }
            catch (Exception ex)
            {
                if (ldapUserInfo != null)
                {
                    _log.ErrorFormat("TryAddLDAPUser(UserName='******' Sid='{1}') failed: Error: {2}", ldapUserInfo.UserName,
                                     ldapUserInfo.Sid, ex);
                }
            }

            return(false);
        }