This commit is contained in:
64
Api/Ewide.Core/Ewide.Core.Common/Authorized/ApiAuthorized.cs
Normal file
64
Api/Ewide.Core/Ewide.Core.Common/Authorized/ApiAuthorized.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using Dapper;
|
||||
using DapperExtensions;
|
||||
using Ewide.Core.Data;
|
||||
using Ewide.Core.Model;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Ewide.Core.Common
|
||||
{
|
||||
public class ApiAuthorized
|
||||
{
|
||||
public static bool Authorized(string url, string userID = "")
|
||||
{
|
||||
using (var db = new DapperHelper())
|
||||
{
|
||||
// 将 /api/controller/action 形式转换成 api::controller::action
|
||||
url = url.ToLower().Substring(1).Replace("/", "::");
|
||||
var appliaction = db.Conn.GetList<EC_Appliaction>(new { Url = url }).FirstOrDefault();
|
||||
|
||||
// 默认没有配置的接口为开放接口
|
||||
if (appliaction == null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// 不需要验证权限和登录
|
||||
if (appliaction.AuthorityType == (int)AuthorityType.开放接口)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// 只需要验证登录
|
||||
if (appliaction.AuthorityType == (int)AuthorityType.内部接口)
|
||||
{
|
||||
return !String.IsNullOrEmpty(userID);
|
||||
}
|
||||
|
||||
// 需要验证权限
|
||||
if (!String.IsNullOrEmpty(userID))
|
||||
{
|
||||
var roles = db.Conn.GetList<EC_User_Role>(new { UserID = userID }).Select(p => p.RoleID).ToList();
|
||||
var apps = db.Conn.ExecuteScalar<int>(@"SELECT COUNT(0) FROM ec_user_role UR
|
||||
INNER JOIN ec_role_menu RM ON UR.roleID = RM.roleID
|
||||
INNER JOIN ec_menu M ON RM.menuID = M.id AND M.enabled = 1
|
||||
INNER JOIN ec_role_menu_appliaction RMA ON RM.ID = RMA.roleMenuID
|
||||
INNER JOIN ec_menu_appliaction MA ON RMA.menuAppliactionID = MA.id AND MA.relationType = 2
|
||||
WHERE UR.userID = @UserID AND MA.relationID = @RelationID", new
|
||||
{
|
||||
UserID = userID,
|
||||
RelationID = appliaction.ID
|
||||
});
|
||||
|
||||
return apps > 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,11 +14,9 @@ namespace Ewide.Core.Common
|
||||
{
|
||||
public class AuthorizedHelper : AuthorizedWhiteListHelper
|
||||
{
|
||||
private static readonly string JWT_Secret = System.Configuration.ConfigurationManager.ConnectionStrings["Token_JwtSecret"]?.ToString();
|
||||
private static readonly string JWT_Secret = System.Configuration.ConfigurationManager.AppSettings["Token_JwtSecret"]?.ToString();
|
||||
|
||||
private static readonly string JWT_User = System.Configuration.ConfigurationManager.ConnectionStrings["Token_JwtUser"]?.ToString();
|
||||
|
||||
private static readonly string WhiteList_Key = System.Configuration.ConfigurationManager.ConnectionStrings["Token_WhiteList"]?.ToString();
|
||||
private static readonly string JWT_User = System.Configuration.ConfigurationManager.AppSettings["Token_JwtUser"]?.ToString();
|
||||
|
||||
/// <summary>
|
||||
/// 从Request中获取Token值。
|
||||
@@ -69,6 +67,20 @@ namespace Ewide.Core.Common
|
||||
}
|
||||
}
|
||||
|
||||
public static string[] CurrentUserRoles
|
||||
{
|
||||
get
|
||||
{
|
||||
var userID = CurrentUserID;
|
||||
if (String.IsNullOrEmpty(userID))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
var roles = GetWhiteListUserRoles(userID);
|
||||
return roles;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将字符串转成JwtToken类型,并指示是否转换成功。
|
||||
/// </summary>
|
||||
@@ -207,7 +219,9 @@ namespace Ewide.Core.Common
|
||||
{
|
||||
var str = password.Trim().ToLower();
|
||||
|
||||
str = BitConverter.ToString(new MD5CryptoServiceProvider().ComputeHash(UTF8Encoding.Default.GetBytes(str))).Replace("-", "");
|
||||
str = BitConverter.ToString(new MD5CryptoServiceProvider().ComputeHash(UTF8Encoding.Default.GetBytes(str)))
|
||||
.Replace("-", "")
|
||||
.ToLower();
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace Ewide.Core.Common
|
||||
{
|
||||
public class AuthorizedWhiteListHelper
|
||||
{
|
||||
private static readonly string WhiteList_Key = System.Configuration.ConfigurationManager.ConnectionStrings["Token_WhiteList"]?.ToString();
|
||||
protected static readonly string WhiteList_Key = System.Configuration.ConfigurationManager.AppSettings["Token_WhiteList"]?.ToString();
|
||||
|
||||
private static readonly string CachePath = HttpContext.Current.Server.MapPath("\\" + Path.Combine("Cache", WhiteList_Key + ".bin"));
|
||||
|
||||
@@ -128,25 +128,29 @@ namespace Ewide.Core.Common
|
||||
using (var db = new DapperHelper())
|
||||
{
|
||||
var user = db.Conn.Get<EC_User>(userID);
|
||||
var roles = db.Conn.GetList<EC_User_Role>(new { UserID = userID }).Select(p => p.RoleID).ToArray();
|
||||
var exp = DateTime.Now.AddDays(30);
|
||||
if (list == null)
|
||||
{
|
||||
list = new List<WhiteListToken>
|
||||
{
|
||||
new WhiteListToken
|
||||
{
|
||||
UserID = userID,
|
||||
User = user,
|
||||
Token = token,
|
||||
ExpDate = exp
|
||||
}
|
||||
};
|
||||
new WhiteListToken
|
||||
{
|
||||
UserID = userID,
|
||||
User = user,
|
||||
Roles = roles,
|
||||
Token = token,
|
||||
ExpDate = exp
|
||||
}
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
var item = list.FirstOrDefault(p => p.UserID.Equals(userID, StringComparison.CurrentCultureIgnoreCase));
|
||||
if (item != null)
|
||||
{
|
||||
item.User = user;
|
||||
item.Roles = roles;
|
||||
item.Token = token;
|
||||
item.ExpDate = exp;
|
||||
}
|
||||
@@ -156,6 +160,7 @@ namespace Ewide.Core.Common
|
||||
{
|
||||
UserID = userID,
|
||||
User = user,
|
||||
Roles = roles,
|
||||
Token = token,
|
||||
ExpDate = exp
|
||||
});
|
||||
@@ -218,6 +223,22 @@ namespace Ewide.Core.Common
|
||||
WhiteList = list;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据UserID获取白名单中的角色编号
|
||||
/// </summary>
|
||||
/// <param name="userID"></param>
|
||||
/// <returns></returns>
|
||||
protected static string[] GetWhiteListUserRoles(string userID)
|
||||
{
|
||||
var item = GetWhiteListByUserID(userID);
|
||||
if (item == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return item.Roles;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
|
||||
58
Api/Ewide.Core/Ewide.Core.Common/Authorized/LoginHelper.cs
Normal file
58
Api/Ewide.Core/Ewide.Core.Common/Authorized/LoginHelper.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using Ewide.Core.Data;
|
||||
using DapperExtensions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Ewide.Core.Model;
|
||||
using Ewide.Core.DTO;
|
||||
|
||||
namespace Ewide.Core.Common
|
||||
{
|
||||
public class LoginHelper
|
||||
{
|
||||
public LoginRDTO Login(string account, string password, out string token, out string message)
|
||||
{
|
||||
token = String.Empty;
|
||||
message = String.Empty;
|
||||
using (var db = new DapperHelper())
|
||||
{
|
||||
var user = db.Conn.GetList<EC_User>(new { Account = account }).FirstOrDefault();
|
||||
|
||||
if (user == null)
|
||||
{
|
||||
message = "此用户不存在";
|
||||
return null;
|
||||
}
|
||||
|
||||
var passwordMD5 = AuthorizedHelper.GetPasswordMD5(password);
|
||||
if (!passwordMD5.Equals(user.Password))
|
||||
{
|
||||
message = "密码错误";
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!user.Enabled)
|
||||
{
|
||||
message = "当前用户已被禁止登录";
|
||||
return null;
|
||||
}
|
||||
|
||||
message = "登录成功";
|
||||
token = AuthorizedHelper.AddToken(user);
|
||||
return new LoginRDTO
|
||||
{
|
||||
Account = user.Account,
|
||||
Name = user.Name,
|
||||
Type = user.Type,
|
||||
Sex = user.Sex,
|
||||
Avatar = user.Avatar,
|
||||
Code = user.Code,
|
||||
Phone = user.Phone,
|
||||
Desc = user.Desc
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ namespace Ewide.Core.Common
|
||||
{
|
||||
public string UserID { get; set; }
|
||||
public EC_User User { get; set; }
|
||||
public string[] Roles { get; set; }
|
||||
public string Token { get; set; }
|
||||
public DateTime ExpDate { get; set; }
|
||||
}
|
||||
|
||||
26
Api/Ewide.Core/Ewide.Core.Common/EnumCode/AuthorityType.cs
Normal file
26
Api/Ewide.Core/Ewide.Core.Common/EnumCode/AuthorityType.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Ewide.Core.Common
|
||||
{
|
||||
public enum AuthorityType
|
||||
{
|
||||
/// <summary>
|
||||
/// 一般接口,需要验证权限
|
||||
/// </summary>
|
||||
一般接口 = 1,
|
||||
|
||||
/// <summary>
|
||||
/// 内部接口,只需要验证登录
|
||||
/// </summary>
|
||||
内部接口 = 2,
|
||||
|
||||
/// <summary>
|
||||
/// 开放接口,不需要验证权限和登录
|
||||
/// </summary>
|
||||
开放接口 = 3,
|
||||
}
|
||||
}
|
||||
@@ -59,15 +59,18 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Authorized\ApiAuthorized.cs" />
|
||||
<Compile Include="Authorized\AuthorizedHelper.cs" />
|
||||
<Compile Include="Authorized\AuthorizedWhiteListHelper.cs" />
|
||||
<Compile Include="Authorized\LoginHelper.cs" />
|
||||
<Compile Include="Authorized\WhiteListToken.cs" />
|
||||
<Compile Include="BaseDisplayJSON.cs" />
|
||||
<Compile Include="EnumCode\AuthorityType.cs" />
|
||||
<Compile Include="Menu\MenuHelper.cs" />
|
||||
<Compile Include="Menu\MenuStructure.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="EnumCode\" />
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
<ItemGroup>
|
||||
<None Include="app.config" />
|
||||
<None Include="packages.config" />
|
||||
@@ -77,6 +80,10 @@
|
||||
<Project>{b5b46bad-81e3-4df0-83ef-75148236f7ce}</Project>
|
||||
<Name>Ewide.Core.Data</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\Ewide.Core.DTO\Ewide.Core.DTO.csproj">
|
||||
<Project>{590704FF-28C5-4536-B587-AC213858CC42}</Project>
|
||||
<Name>Ewide.Core.DTO</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\Ewide.Core.Model\Ewide.Core.Model.csproj">
|
||||
<Project>{31C3CA3D-14A1-453A-866D-76D4C74A9BDC}</Project>
|
||||
<Name>Ewide.Core.Model</Name>
|
||||
|
||||
56
Api/Ewide.Core/Ewide.Core.Common/Menu/MenuHelper.cs
Normal file
56
Api/Ewide.Core/Ewide.Core.Common/Menu/MenuHelper.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using DapperExtensions;
|
||||
using Ewide.Core.Data;
|
||||
using Ewide.Core.Model;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Ewide.Core.Common
|
||||
{
|
||||
public class MenuHelper
|
||||
{
|
||||
public List<MenuStructure> GetMenu()
|
||||
{
|
||||
using (var db = new DapperHelper())
|
||||
{
|
||||
var roles = AuthorizedHelper.CurrentUserRoles;
|
||||
var menuIDList = db.Conn.GetList<EC_Role_Menu>(new { RoleID = roles }).Select(p => p.MenuID).ToList();
|
||||
var menuList = db.Conn.GetList<EC_Menu>(new { ID = menuIDList, Enabled = true })
|
||||
.Select(p => new MenuStructure
|
||||
{
|
||||
ID = p.ID,
|
||||
ParentID = p.ParentID,
|
||||
Type = p.Type,
|
||||
Name = p.Name,
|
||||
Icon = p.Icon,
|
||||
Color = p.Color,
|
||||
Sort = p.Sort,
|
||||
Children = null
|
||||
})
|
||||
.ToList();
|
||||
return GetMenuStructure(menuList);
|
||||
}
|
||||
}
|
||||
|
||||
private List<MenuStructure> GetMenuStructure(List<MenuStructure> list, string parentID = "")
|
||||
{
|
||||
List<MenuStructure> menu;
|
||||
if (String.IsNullOrEmpty(parentID))
|
||||
{
|
||||
menu = list.Where(p => String.IsNullOrEmpty(p.ParentID)).OrderBy(p => p.Sort).ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
menu = list.Where(p => parentID == p.ParentID).OrderBy(p => p.Sort).ToList();
|
||||
}
|
||||
|
||||
foreach (var m in menu)
|
||||
{
|
||||
m.Children = GetMenuStructure(list, m.ID);
|
||||
}
|
||||
return menu;
|
||||
}
|
||||
}
|
||||
}
|
||||
21
Api/Ewide.Core/Ewide.Core.Common/Menu/MenuStructure.cs
Normal file
21
Api/Ewide.Core/Ewide.Core.Common/Menu/MenuStructure.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Ewide.Core.Common
|
||||
{
|
||||
public class MenuStructure
|
||||
{
|
||||
public string ID { get; set; }
|
||||
public string ParentID { get; set; }
|
||||
public int Type { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Component { get; set; }
|
||||
public string Icon { get; set; }
|
||||
public string Color { get; set; }
|
||||
public int Sort { get; set; }
|
||||
public List<MenuStructure> Children { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user