/// <summary>
        /// Updates an IP network
        /// </summary>
        /// <param name="ipNetwork">IP network</param>
        public void UpdateBannedIpNetwork(BannedIpNetwork ipNetwork)
        {
            if (ipNetwork == null)
            {
                throw new ArgumentNullException("ipNetwork");
            }

            ipNetwork.StartAddress = CommonHelper.EnsureNotNull(ipNetwork.StartAddress);
            ipNetwork.StartAddress = ipNetwork.StartAddress.Trim();
            ipNetwork.StartAddress = CommonHelper.EnsureMaximumLength(ipNetwork.StartAddress, 50);
            ipNetwork.EndAddress   = CommonHelper.EnsureNotNull(ipNetwork.EndAddress);
            ipNetwork.EndAddress   = ipNetwork.EndAddress.Trim();
            ipNetwork.EndAddress   = CommonHelper.EnsureMaximumLength(ipNetwork.EndAddress, 50);
            ipNetwork.Comment      = CommonHelper.EnsureNotNull(ipNetwork.Comment);
            ipNetwork.Comment      = CommonHelper.EnsureMaximumLength(ipNetwork.Comment, 500);
            ipNetwork.IpException  = CommonHelper.EnsureNotNull(ipNetwork.IpException);


            if (!_context.IsAttached(ipNetwork))
            {
                _context.BannedIpNetworks.Attach(ipNetwork);
            }

            _context.SaveChanges();

            if (this.CacheEnabled)
            {
                _cacheManager.RemoveByPattern(BLACKLIST_NETWORK_PATTERN_KEY);
            }
        }
Пример #2
0
        /// <summary>
        /// Gets an IP network by its ID
        /// </summary>
        /// <param name="bannedIpNetworkID">IP network unique identifier</param>
        /// <returns>IP network</returns>
        public static BannedIpNetwork GetBannedIpNetworkById(int bannedIpNetworkID)
        {
            if (bannedIpNetworkID == 0)
            {
                return(null);
            }

            DBBannedIpNetwork dbItem = DBProviderManager <DBBlacklistProvider> .Provider.GetIpNetworkByID(bannedIpNetworkID);

            BannedIpNetwork ipNetwork = DBMapping(dbItem);

            return(ipNetwork);
        }
Пример #3
0
        /// <summary>
        /// Updates an IP network
        /// </summary>
        /// <param name="bannedIpNetwork">IP network unique identifier</param>
        /// <param name="startAddress">First IP address in the range</param>
        /// <param name="endAddress">Last IP address in the range</param>
        /// <param name="comment">Reason why the IP network was banned</param>
        /// <param name="ipException">Exception IPs in the range</param>
        /// <param name="createdOn">When the record was created</param>
        /// <param name="updatedOn">When the record was last updated</param>
        /// <returns></returns>
        public static BannedIpNetwork UpdateBannedIpNetwork(int bannedIpNetwork, string startAddress, string endAddress, string comment, string ipException, DateTime createdOn, DateTime updatedOn)
        {
            createdOn = DateTimeHelper.ConvertToUtcTime(createdOn);
            updatedOn = DateTimeHelper.ConvertToUtcTime(updatedOn);

            DBBannedIpNetwork dbItem = DBProviderManager <DBBlacklistProvider> .Provider.UpdateBannedIpNetwork(bannedIpNetwork, startAddress, endAddress, comment, ipException, createdOn, updatedOn);

            BannedIpNetwork ipNetwork = DBMapping(dbItem);

            if (IpBlacklistManager.CacheEnabled)
            {
                NopCache.RemoveByPattern(BLACKLIST_NETWORK_PATTERN_KEY);
            }
            return(ipNetwork);
        }
Пример #4
0
        private static BannedIpNetworkCollection DBMapping(DBBannedIpNetworkCollection dbCollection)
        {
            if (dbCollection == null)
            {
                return(null);
            }

            BannedIpNetworkCollection ipCollection = new BannedIpNetworkCollection();

            foreach (DBBannedIpNetwork dbItem in dbCollection)
            {
                BannedIpNetwork item = DBMapping(dbItem);
                ipCollection.Add(item);
            }
            return(ipCollection);
        }
Пример #5
0
        private static BannedIpNetwork DBMapping(DBBannedIpNetwork dbItem)
        {
            if (dbItem == null)
            {
                return(null);
            }

            var ipNetwork = new BannedIpNetwork();

            ipNetwork.BannedIpNetworkId = dbItem.BannedIpNetworkId;
            ipNetwork.StartAddress      = dbItem.StartAddress;
            ipNetwork.EndAddress        = dbItem.EndAddress;
            ipNetwork.IpException       = dbItem.IpException;
            ipNetwork.Comment           = dbItem.Comment;
            ipNetwork.CreatedOn         = dbItem.CreatedOn;
            ipNetwork.UpdatedOn         = dbItem.UpdatedOn;
            return(ipNetwork);
        }
        /// <summary>
        /// Saves a BannedIpNetwork
        /// </summary>
        /// <returns>BannedIpNetwork</returns>
        public BannedIpNetwork SaveBannedIpNetworkInfo()
        {
            DateTime nowDT = DateTime.UtcNow;
            //split the text in the BannedIP to get the current IPs
            string[] rangeItems = txtBannedIP.Text.ToString().Split("-".ToCharArray());

            // Check if the 1st IP is valid
            if (!this.BlacklistService.IsValidIp(rangeItems[0].Trim()))
                throw new NopException("The following isn't a valid IP address: " + rangeItems[0]);

            // Check if the 2nd IP is valid
            if (!this.BlacklistService.IsValidIp(rangeItems[1].Trim()))
                throw new NopException("The following isn't a valid IP address: " + rangeItems[1]);

            BannedIpNetwork ipNetwork = this.BlacklistService.GetBannedIpNetworkById(this.BannedIpNetworkId);
            //if ip network is not null update
            if (ipNetwork != null)
            {
                ipNetwork.StartAddress = rangeItems[0];
                ipNetwork.EndAddress = rangeItems[1];
                ipNetwork.Comment = txtComment.Text;
                ipNetwork.IpException = txtIpException.Text;
                ipNetwork.UpdatedOn = nowDT;

               this.BlacklistService.UpdateBannedIpNetwork(ipNetwork);
            }
            else //insert
            {
                ipNetwork = new BannedIpNetwork()
                {
                    StartAddress = rangeItems[0],
                    EndAddress = rangeItems[1],
                    Comment = txtComment.Text,
                    IpException = txtIpException.Text,
                    CreatedOn = nowDT,
                    UpdatedOn = nowDT
                };
                this.BlacklistService.InsertBannedIpNetwork(ipNetwork);
            }

            return ipNetwork;
        }