文章增加设置IP访问限制
This commit is contained in:
@@ -95,6 +95,56 @@ namespace COMCMS.Web.Areas.AdminCP.Controllers
|
||||
tip.Message = "栏目路径不可用,请重新填写!";
|
||||
return Json(tip);
|
||||
}
|
||||
//判断过滤IP地址
|
||||
if (!string.IsNullOrEmpty(model.AllowIp))
|
||||
{
|
||||
string[] strRows = model.AllowIp.Split(new string[] { "\n" }, StringSplitOptions.None);
|
||||
if (strRows != null && strRows.Length > 0)
|
||||
{
|
||||
foreach (var row in strRows)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(row))
|
||||
{
|
||||
if (row.IndexOf("-") > -1)
|
||||
{
|
||||
string[] arrIps = row.Split(new string[] { "-" }, StringSplitOptions.None);
|
||||
if (arrIps != null && arrIps.Length > 0)
|
||||
{
|
||||
if (arrIps.Length != 2)
|
||||
{
|
||||
tip.Message = "每一行只能有一个“-”,如果需要限制多个ip段,请换行再输入!";
|
||||
return Json(tip);
|
||||
}
|
||||
//判断ip是否符合规则
|
||||
foreach (var ip in arrIps)
|
||||
{
|
||||
if (!Utils.IsIP(ip))
|
||||
{
|
||||
tip.Message = "您输入的IP地址有误:" + ip;
|
||||
return Json(tip);
|
||||
}
|
||||
}
|
||||
long ip1 = Utils.GetLongIP(arrIps[0]);
|
||||
long ip2 = Utils.GetLongIP(arrIps[1]);
|
||||
if (ip1 > ip2)
|
||||
{
|
||||
tip.Message = "您输入的IP地址段有误,请按小到大输入";
|
||||
return Json(tip);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!Utils.IsIP(row))
|
||||
{
|
||||
tip.Message = "您输入的IP地址有误:" + row;
|
||||
return Json(tip);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
model.Insert();
|
||||
Core.Admin.WriteLogActions("添加文章栏目(id:" + model.Id + ");");
|
||||
@@ -237,6 +287,61 @@ namespace COMCMS.Web.Areas.AdminCP.Controllers
|
||||
entity.Pic = model.Pic;
|
||||
entity.FilePath = model.FilePath;
|
||||
|
||||
//判断过滤IP地址
|
||||
if (!string.IsNullOrEmpty(model.AllowIp))
|
||||
{
|
||||
string[] strRows = model.AllowIp.Split(new string[] { "\n" }, StringSplitOptions.None);
|
||||
if (strRows != null && strRows.Length > 0)
|
||||
{
|
||||
foreach (var row in strRows)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(row))
|
||||
{
|
||||
if (row.IndexOf("-") > -1)
|
||||
{
|
||||
string[] arrIps = row.Split(new string[] { "-" }, StringSplitOptions.None);
|
||||
if (arrIps != null && arrIps.Length > 0)
|
||||
{
|
||||
if (arrIps.Length != 2)
|
||||
{
|
||||
tip.Message = "每一行只能有一个“-”,如果需要限制多个ip段,请换行再输入!";
|
||||
return Json(tip);
|
||||
}
|
||||
//判断ip是否符合规则
|
||||
foreach (var ip in arrIps)
|
||||
{
|
||||
if (!Utils.IsIP(ip))
|
||||
{
|
||||
tip.Message = "您输入的IP地址有误1:" + ip;
|
||||
return Json(tip);
|
||||
}
|
||||
}
|
||||
long ip1 = Utils.GetLongIP(arrIps[0]);
|
||||
long ip2 = Utils.GetLongIP(arrIps[1]);
|
||||
if (ip1 > ip2)
|
||||
{
|
||||
tip.Message = "您输入的IP地址段有误,请按小到大输入";
|
||||
return Json(tip);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!Utils.IsIP(row))
|
||||
{
|
||||
tip.Message = "您输入的IP地址有误2:" + row;
|
||||
return Json(tip);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
entity.AllowIp = model.AllowIp;
|
||||
}
|
||||
else
|
||||
{
|
||||
entity.AllowIp = "";
|
||||
}
|
||||
|
||||
entity.Save();
|
||||
//修改所有文章的路径
|
||||
|
||||
@@ -146,6 +146,12 @@
|
||||
<textarea id="KindInfo" name="KindInfo" class="form-control" placeholder="可以输入栏目的详细介绍"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="col-sm-3 control-label">IP限制:</label>
|
||||
<div class="col-sm-8">
|
||||
<textarea id="AllowIp" name="AllowIp" class="form-control" placeholder="您可以输入IP限制,每一行一个IP段,请妥当设置。留空不限制" rows="4"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<div class="hr-line-dashed"></div>
|
||||
<div class="form-group">
|
||||
<div class="col-sm-4 col-sm-offset-3">
|
||||
|
||||
@@ -154,6 +154,12 @@
|
||||
<textarea id="KindInfo" name="KindInfo" class="form-control" placeholder="可以输入栏目的详细介绍">@Model.KindInfo</textarea>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="col-sm-3 control-label">IP限制:</label>
|
||||
<div class="col-sm-8">
|
||||
<textarea id="AllowIp" name="AllowIp" class="form-control" placeholder="您可以输入IP限制,每一行一个IP段,请妥当设置。留空不限制" rows="4">@Model.AllowIp</textarea>
|
||||
</div>
|
||||
</div>
|
||||
<div class="hr-line-dashed"></div>
|
||||
<div class="form-group">
|
||||
<div class="col-sm-4 col-sm-offset-3">
|
||||
|
||||
@@ -41,6 +41,51 @@ namespace COMCMS.Web.Controllers
|
||||
}
|
||||
if (page < 1) page = 1;
|
||||
|
||||
//判断是否是限制了IP地址
|
||||
if (!string.IsNullOrEmpty(model.AllowIp) && !Admin.IsAdminLogin()) //2019-04-16 增加如果超级管理员登录。不判断
|
||||
{
|
||||
long myIP = Utils.GetLongIP(Utils.GetIP());
|
||||
|
||||
string[] strRows = model.AllowIp.Split(new string[] { "\n" }, StringSplitOptions.None);
|
||||
if (strRows != null && strRows.Length > 0)
|
||||
{
|
||||
bool isAddressOK = false;
|
||||
foreach (var row in strRows)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(row))
|
||||
{
|
||||
if (row.IndexOf("-") > -1)
|
||||
{
|
||||
string[] arrIps = row.Split(new string[] { "-" }, StringSplitOptions.None);
|
||||
if (arrIps != null && arrIps.Length == 2)
|
||||
{
|
||||
long ip1 = Utils.GetLongIP(arrIps[0]);
|
||||
long ip2 = Utils.GetLongIP(arrIps[1]);
|
||||
if (ip1 <= myIP && myIP <= ip2)
|
||||
{
|
||||
isAddressOK = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
long iplong = Utils.GetLongIP(row);
|
||||
if (myIP == iplong)
|
||||
{
|
||||
isAddressOK = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!isAddressOK)
|
||||
{
|
||||
return EchoTip("您的IP不在允许范围内!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (model.IsList == 1)
|
||||
{
|
||||
IList<Article> list = new List<Article>();
|
||||
@@ -118,6 +163,50 @@ namespace COMCMS.Web.Controllers
|
||||
{
|
||||
return AlertAndGoBack("系统找不到本文章栏目!");
|
||||
}
|
||||
//判断是否是限制了IP地址
|
||||
if (!string.IsNullOrEmpty(kind.AllowIp) && !Admin.IsAdminLogin()) //2019-04-16 增加如果超级管理员登录。不判断
|
||||
{
|
||||
long myIP = Utils.GetLongIP(Utils.GetIP());
|
||||
|
||||
string[] strRows = kind.AllowIp.Split(new string[] { "\n" }, StringSplitOptions.None);
|
||||
if (strRows != null && strRows.Length > 0)
|
||||
{
|
||||
bool isAddressOK = false;
|
||||
foreach (var row in strRows)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(row))
|
||||
{
|
||||
if (row.IndexOf("-") > -1)
|
||||
{
|
||||
string[] arrIps = row.Split(new string[] { "-" }, StringSplitOptions.None);
|
||||
if (arrIps != null && arrIps.Length == 2)
|
||||
{
|
||||
long ip1 = Utils.GetLongIP(arrIps[0]);
|
||||
long ip2 = Utils.GetLongIP(arrIps[1]);
|
||||
if (ip1 <= myIP && myIP <= ip2)
|
||||
{
|
||||
isAddressOK = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
long iplong = Utils.GetLongIP(row);
|
||||
if (myIP == iplong)
|
||||
{
|
||||
isAddressOK = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!isAddressOK)
|
||||
{
|
||||
return EchoTip("您的IP不在允许范围内!");
|
||||
}
|
||||
}
|
||||
}
|
||||
//增加点击
|
||||
entity.Hits++;
|
||||
entity.Update();
|
||||
|
||||
Reference in New Issue
Block a user