You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1060 lines
31 KiB
1060 lines
31 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using LitJson;
|
|
using MySql.Data.MySqlClient;
|
|
using Sog;
|
|
|
|
//权限-角色-菜单
|
|
namespace Operation
|
|
{
|
|
public class AdminOperator
|
|
{
|
|
private MySqlDB db;
|
|
private const int BuffLengthMax = 500 * 1024;
|
|
public static string Administrator = "Administrator";
|
|
|
|
public AdminOperator(MySqlDB mySql)
|
|
{
|
|
db = mySql;
|
|
}
|
|
|
|
//销毁的时候置空
|
|
public void Dispose()
|
|
{
|
|
db.Dispose();
|
|
db = null;
|
|
}
|
|
|
|
public void KeepAlive()
|
|
{
|
|
if (db == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
string strSql = "select id from admin_role where id=1";
|
|
MySqlDataReader reader = null;
|
|
try
|
|
{
|
|
reader = db.ExecReader(strSql);
|
|
if (reader == null)
|
|
{
|
|
TraceLog.Trace("AdminOperator.KeepAlive no record in account table, return reader is null!");
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
TraceLog.Trace("AdminOperator.KeepAlive error!");
|
|
}
|
|
finally
|
|
{
|
|
//记住一定要Close
|
|
if (reader != null)
|
|
{
|
|
reader.Close();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public List<Menu> GetAllMenus()
|
|
{
|
|
List<Menu> menus = new List<Menu>();
|
|
|
|
|
|
MySqlDataReader reader = null;
|
|
string sql = "select * from admin_menu";
|
|
try
|
|
{
|
|
reader = db.ExecReader(sql);
|
|
if (reader == null)
|
|
{
|
|
return menus;
|
|
}
|
|
|
|
while (reader.Read())
|
|
{
|
|
Menu menu = builderMenu(reader);
|
|
menus.Add(menu);
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
TraceLog.Error("AdminOperator.GetAllMenus sql={0} error info={1}", sql, e.Message);
|
|
}
|
|
finally
|
|
{
|
|
if (reader != null)
|
|
{
|
|
reader.Close();
|
|
}
|
|
}
|
|
|
|
return menus;
|
|
}
|
|
|
|
public List<Menu> GetMenus(string account)
|
|
{
|
|
var menusList = GetMenuIds(account);
|
|
return GetMenus(menusList);
|
|
}
|
|
|
|
public List<Menu> GetMenus(List<int> menusList)
|
|
{
|
|
List<Menu> menus = new List<Menu>();
|
|
if (menusList == null || menusList.Count == 0)
|
|
{
|
|
return menus;
|
|
}
|
|
|
|
string sql = string.Format("select * from admin_menu where menu_id in ({0})", String.Join(",", menusList));
|
|
MySqlDataReader reader = null;
|
|
try
|
|
{
|
|
reader = db.ExecReader(sql);
|
|
while (reader.Read())
|
|
{
|
|
Menu menu = builderMenu(reader);
|
|
menus.Add(menu);
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
TraceLog.Error("AdminOperator.GetMenus error sql={0}, info={1}", sql, e.Message);
|
|
}
|
|
|
|
finally
|
|
{
|
|
if (reader != null)
|
|
{
|
|
reader.Close();
|
|
}
|
|
}
|
|
|
|
return menus;
|
|
}
|
|
|
|
public int AddMenu(string name, string icon, int order, string url, int permission, int parent_id = 0)
|
|
{
|
|
if (String.IsNullOrEmpty(name))
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
bool exist = false;
|
|
string selectSql = "select * from admin_menu where name='" + name + "'";
|
|
MySqlDataReader selectReader = null;
|
|
int parentId = -1;
|
|
try
|
|
{
|
|
selectReader = db.ExecReader(selectSql);
|
|
if (selectReader == null)
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
exist = selectReader.HasRows;
|
|
while (selectReader.Read())
|
|
{
|
|
parentId = selectReader.GetInt32("menu_id");
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
TraceLog.Error("AdminOperator.AddMenu error sql={0},info={1}", selectSql, e.Message);
|
|
}
|
|
finally
|
|
{
|
|
if (selectReader != null)
|
|
{
|
|
selectReader.Close();
|
|
}
|
|
}
|
|
|
|
if (exist)
|
|
{
|
|
string updateSql =
|
|
"update admin_menu set icon=?icon,`order`=?order,url=?url,permission=?permission,`name`=?name where menu_id=" +parentId;
|
|
var param = new List<MySqlParameter>()
|
|
{
|
|
new MySqlParameter("?icon", MySqlDbType.String) { Value = icon },
|
|
new MySqlParameter("?name", MySqlDbType.String) { Value = name },
|
|
new MySqlParameter("?order", MySqlDbType.Int32) { Value = order },
|
|
new MySqlParameter("?url", MySqlDbType.String) { Value = url },
|
|
new MySqlParameter("?permission", MySqlDbType.Int32) { Value = permission },
|
|
}.ToArray();
|
|
MySqlDataReader reader = null;
|
|
try
|
|
{
|
|
reader = db.ExecReader(updateSql, param);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
TraceLog.Error("AdminOperation.AddMenu sql {0} failed,params={1}", updateSql, param);
|
|
}
|
|
finally
|
|
{
|
|
if (reader != null)
|
|
{
|
|
reader.Close();
|
|
}
|
|
}
|
|
|
|
return parentId;
|
|
}
|
|
|
|
string insertSql =
|
|
"insert into admin_menu set name=?name,`order`=?order,url=?url,parent_id=?parent_id,icon=?icon,permission=?permission";
|
|
var paramList = new List<MySqlParameter>()
|
|
{
|
|
new MySqlParameter("?name", MySqlDbType.String) { Value = name },
|
|
new MySqlParameter("?icon", MySqlDbType.String) { Value = icon },
|
|
new MySqlParameter("?order", MySqlDbType.Int32) { Value = order },
|
|
new MySqlParameter("?url", MySqlDbType.String) { Value = url },
|
|
new MySqlParameter("?parent_id", MySqlDbType.Int32) { Value = parent_id },
|
|
new MySqlParameter("?permission", MySqlDbType.Int32) { Value = permission },
|
|
}.ToArray();
|
|
try
|
|
{
|
|
MySqlDataReader reader = db.ExecReader(insertSql, paramList);
|
|
if (reader != null)
|
|
{
|
|
reader.Close();
|
|
}
|
|
|
|
if (parent_id == 0)
|
|
{
|
|
selectReader = db.ExecReader(selectSql);
|
|
if (selectReader == null)
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
exist = selectReader.HasRows;
|
|
if (exist)
|
|
{
|
|
while (selectReader.Read())
|
|
{
|
|
parentId = selectReader.GetInt32("menu_id");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
TraceLog.Error("AdminOperation.AddMenu sql {0} failed,params={1},{2}", insertSql, paramList, e.Message);
|
|
}
|
|
finally
|
|
{
|
|
if (selectReader != null)
|
|
{
|
|
selectReader.Close();
|
|
}
|
|
}
|
|
|
|
return parentId;
|
|
}
|
|
|
|
public void AddPermission(int permission, string name, string url, int pcode)
|
|
{
|
|
string selectSql = "select * from admin_permission where id=" + permission;
|
|
bool exist = false;
|
|
MySqlDataReader reader = null;
|
|
try
|
|
{
|
|
reader = db.ExecReader(selectSql);
|
|
if (reader == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
exist = reader.HasRows;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
TraceLog.Error("AdminOperator.AddPermission sql {0} failed,e={1}", selectSql, e.Message);
|
|
}
|
|
finally
|
|
{
|
|
if (reader != null)
|
|
{
|
|
reader.Close();
|
|
}
|
|
}
|
|
|
|
if (exist)
|
|
{
|
|
string updateSql = "update admin_permission set url=?url,name=?name,parent_id=?parent_id where id=?id;";
|
|
MySqlDataReader upReadr = null;
|
|
try
|
|
{
|
|
var param = new List<MySqlParameter>()
|
|
{
|
|
new MySqlParameter("?id", MySqlDbType.Int32) { Value = permission },
|
|
new MySqlParameter("?name", MySqlDbType.String) { Value = name },
|
|
new MySqlParameter("?url", MySqlDbType.String) { Value = url },
|
|
new MySqlParameter("?parent_id", MySqlDbType.Int32) { Value = pcode },
|
|
}.ToArray();
|
|
upReadr = db.ExecReader(updateSql, param);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
TraceLog.Error("AdminOperator.AddPermission sql {0} failed e={1}", updateSql, e.Message);
|
|
}
|
|
finally
|
|
{
|
|
if (upReadr != null)
|
|
{
|
|
upReadr.Close();
|
|
}
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
string sql = "insert into admin_permission set id=?id,name=?name,url=?url,parent_id=?parent_id;";
|
|
try
|
|
{
|
|
var param = new List<MySqlParameter>()
|
|
{
|
|
new MySqlParameter("?id", MySqlDbType.Int32) { Value = permission },
|
|
new MySqlParameter("?name", MySqlDbType.String) { Value = name },
|
|
new MySqlParameter("?url", MySqlDbType.String) { Value = url },
|
|
new MySqlParameter("?parent_id", MySqlDbType.Int32) { Value = pcode },
|
|
}.ToArray();
|
|
|
|
MySqlDataReader insetrReadr = null;
|
|
try
|
|
{
|
|
insetrReadr = db.ExecReader(sql, param);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
TraceLog.Error("AdminOperator.AddPermission sql {0} failed {1}", sql, e.Message);
|
|
}
|
|
finally
|
|
{
|
|
if (insetrReadr != null)
|
|
{
|
|
insetrReadr.Close();
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
TraceLog.Error("AdminOperator.AddPermission sql {0} failed,{1}", sql, e.Message);
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
|
|
public void UpdateRole(int id, string name, string menus, string permissions)
|
|
{
|
|
string updateSql = "update admin_role set permission=?permission,menu=?menu where name=?name";
|
|
var param = new List<MySqlParameter>()
|
|
{
|
|
new MySqlParameter("?name", MySqlDbType.String) { Value = name },
|
|
new MySqlParameter("?permission", MySqlDbType.String) { Value = permissions },
|
|
new MySqlParameter("?menu", MySqlDbType.String) { Value = menus },
|
|
}.ToArray();
|
|
MySqlDataReader reader = null;
|
|
try
|
|
{
|
|
reader = db.ExecReader(updateSql, param);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
TraceLog.Error("AdminOperator.UpdateRole sql {0} failed,{1}", updateSql, e.Message);
|
|
}
|
|
finally
|
|
{
|
|
if (reader != null)
|
|
{
|
|
reader.Close();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void AddRole(Role role)
|
|
{
|
|
if (role.Name == "")
|
|
{
|
|
return;
|
|
}
|
|
|
|
var ms = string.Join(",", role.Menus);
|
|
var ps = string.Join(",", role.Permissions);
|
|
string sql = string.Format("select * from admin_role where name='{0}'", role.Name);
|
|
bool exists = false;
|
|
MySqlDataReader reader = null;
|
|
try
|
|
{
|
|
reader = db.ExecReader(sql);
|
|
if (reader == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
exists = reader.HasRows;
|
|
while (reader.Read())
|
|
{
|
|
var id = reader.GetInt32("id");
|
|
role.Id = id;
|
|
break;
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
TraceLog.Error("AdminOperator.AddRole sql {0} failed,{1}", sql, e.Message);
|
|
}
|
|
finally
|
|
{
|
|
if (reader != null)
|
|
{
|
|
reader.Close();
|
|
}
|
|
}
|
|
|
|
if (exists)
|
|
{
|
|
UpdateRole(role.Id, role.Name, ms, ps);
|
|
return;
|
|
}
|
|
|
|
if (reader != null)
|
|
{
|
|
reader.Close();
|
|
}
|
|
|
|
string insertSql = "insert into admin_role set name=?name,permission=?permission,menu=?menu;";
|
|
var insert = new List<MySqlParameter>()
|
|
{
|
|
new MySqlParameter("?name", MySqlDbType.String) { Value = role.Name },
|
|
new MySqlParameter("?permission", MySqlDbType.String) { Value = ps },
|
|
new MySqlParameter("?menu", MySqlDbType.String) { Value = ms },
|
|
}.ToArray();
|
|
try
|
|
{
|
|
reader = db.ExecReader(insertSql, insert);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
TraceLog.Error("AdminOperator.AddRole insert db sql {0} failed,{1}", sql, e.Message);
|
|
}
|
|
finally
|
|
{
|
|
if (reader != null)
|
|
{
|
|
reader.Close();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void DeleteRole(int roleId)
|
|
{
|
|
string sql = "delete from admin_role where id=" + roleId;
|
|
MySqlDataReader reader = null;
|
|
try
|
|
{
|
|
reader = db.ExecReader(sql);
|
|
if (reader == null)
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
TraceLog.Error("AdminOperator.DeleteRole sql {0} failed,{1}", sql, e.Message);
|
|
}
|
|
finally
|
|
{
|
|
if (reader != null)
|
|
{
|
|
reader.Close();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public Menu GetMenu(int id)
|
|
{
|
|
string sql = string.Format("select * from admin_menu where menu_id={0}", id);
|
|
MySqlDataReader reader = null;
|
|
Menu menu = null;
|
|
try
|
|
{
|
|
reader = db.ExecReader(sql);
|
|
if (reader == null)
|
|
{
|
|
TraceLog.Error("db error");
|
|
return null;
|
|
}
|
|
|
|
menu = builderMenu(reader);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
TraceLog.Error("AdminOperator.GetMenu sql {0} failed,{1}", sql, e.Message);
|
|
}
|
|
finally
|
|
{
|
|
if (reader != null)
|
|
{
|
|
reader.Close();
|
|
}
|
|
}
|
|
|
|
|
|
return menu;
|
|
}
|
|
|
|
|
|
public static Menu builderMenu(MySqlDataReader reader)
|
|
{
|
|
Menu menu = new Menu();
|
|
menu.MenuId = reader.GetInt32("menu_id");
|
|
menu.Name = reader.GetString("name");
|
|
menu.Order = reader.GetInt32("order");
|
|
menu.Icon = reader.GetString("icon");
|
|
menu.ParentId = reader.GetInt32("parent_id");
|
|
menu.Permission = reader.GetInt32("permission");
|
|
menu.Url = reader.GetString("url");
|
|
return menu;
|
|
}
|
|
|
|
public static Permission builderPermission(MySqlDataReader reader)
|
|
{
|
|
Permission menu = new Permission();
|
|
menu.Id = reader.GetInt32("id");
|
|
menu.Name = reader.GetString("name");
|
|
menu.ParentId = reader.GetInt32("parent_id");
|
|
menu.Urls = reader.GetString("url").Split(";").ToList();
|
|
return menu;
|
|
}
|
|
|
|
|
|
public List<Permission> GetAllPermissions()
|
|
{
|
|
var result = new List<Permission>();
|
|
|
|
string sql = "select * from admin_permission";
|
|
|
|
MySqlDataReader reader = null;
|
|
try
|
|
{
|
|
reader = db.ExecReader(sql);
|
|
if (reader == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
while (reader.Read())
|
|
{
|
|
var permission = builderPermission(reader);
|
|
result.Add(permission);
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
TraceLog.Error("AdminOperator.GetAllPermissions sql {0} failed,{1}", sql, e.Message);
|
|
}
|
|
finally
|
|
{
|
|
if (reader != null)
|
|
{
|
|
reader.Close();
|
|
}
|
|
}
|
|
|
|
|
|
return result;
|
|
}
|
|
|
|
public List<int> GetMenuIds(string account)
|
|
{
|
|
var roles = GetRole(account);
|
|
|
|
List<int> menuList = new List<int>();
|
|
if (roles == null)
|
|
{
|
|
return menuList;
|
|
}
|
|
|
|
foreach (var role in roles)
|
|
{
|
|
var ps = role.Menus;
|
|
menuList.AddRange(ps);
|
|
}
|
|
|
|
return menuList;
|
|
}
|
|
|
|
public List<int> GetPermissionIds(string account)
|
|
{
|
|
var roles = GetRole(account);
|
|
List<int> permissions = new List<int>();
|
|
foreach (var role in roles)
|
|
{
|
|
var ps = role.Permissions;
|
|
permissions.AddRange(ps);
|
|
}
|
|
|
|
return permissions;
|
|
}
|
|
|
|
public List<Permission> GetPermissionList(string account)
|
|
{
|
|
var roles = GetRole(account);
|
|
var result = new List<Permission>();
|
|
if (roles == null)
|
|
{
|
|
return result;
|
|
}
|
|
List<int> permissions = new List<int>();
|
|
foreach (var role in roles)
|
|
{
|
|
var ps = role.Permissions;
|
|
permissions.AddRange(ps);
|
|
}
|
|
|
|
if (permissions.Count == 0)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
var pids = permissions.ToArray().Distinct().ToArray();
|
|
string sql = "select * from admin_permission where id in (" + String.Join(",", pids) + ")";
|
|
MySqlDataReader reader = null;
|
|
try
|
|
{
|
|
reader = db.ExecReader(sql);
|
|
if (reader == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
while (reader.Read())
|
|
{
|
|
var permission = builderPermission(reader);
|
|
result.Add(permission);
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
TraceLog.Error("AdminOperator.GetPermissionList error,sql={0},error={1}", sql, e.Message);
|
|
}
|
|
finally
|
|
{
|
|
if (reader != null)
|
|
{
|
|
reader.Close();
|
|
}
|
|
}
|
|
|
|
|
|
return result;
|
|
}
|
|
|
|
|
|
public List<Role> GetRole(string account)
|
|
{
|
|
var roleIds = GetRoleIds(account);
|
|
if (roleIds.Count == 0)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
string result = String.Join(",", roleIds);
|
|
string roleSql = string.Format("select * from admin_role where id in ({0})", result);
|
|
|
|
List<Role> roles = new List<Role>();
|
|
MySqlDataReader reader = null;
|
|
try
|
|
{
|
|
reader = db.ExecReader(roleSql);
|
|
if (reader == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
while (reader.Read())
|
|
{
|
|
Role role = new Role();
|
|
role.build(reader);
|
|
roles.Add(role);
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
TraceLog.Error("AdminOperator.GetRole error,sql={0},error={1}", roleSql, e.Message);
|
|
}
|
|
finally
|
|
{
|
|
if (reader != null)
|
|
{
|
|
reader.Close();
|
|
}
|
|
}
|
|
|
|
return roles;
|
|
}
|
|
|
|
public List<Role> GetAllRoles()
|
|
{
|
|
string sql = "select * from admin_role";
|
|
List<Role> roles = new List<Role>();
|
|
MySqlDataReader reader = null;
|
|
try
|
|
{
|
|
reader = db.ExecReader(sql);
|
|
if (reader == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
while (reader.Read())
|
|
{
|
|
Role role = new Role();
|
|
role.build(reader);
|
|
roles.Add(role);
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
TraceLog.Error("AdminOperator.GetAllRoles error,sql={0},error={1}", sql, e.Message);
|
|
}
|
|
finally
|
|
{
|
|
if (reader != null)
|
|
{
|
|
reader.Close();
|
|
}
|
|
}
|
|
|
|
|
|
return roles;
|
|
}
|
|
|
|
|
|
public void SaveAdminRole(String account, string rolesList)
|
|
{
|
|
string sql = "select * from admin_account_role where account='" + account + "'";
|
|
var reader = db.ExecReader(sql);
|
|
if (reader == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
bool exists = reader.HasRows;
|
|
reader.Close();
|
|
if (exists)
|
|
{
|
|
MySqlDataReader upReader = null;
|
|
try
|
|
{
|
|
String upSql = "update admin_account_role set role=?role where account='" + account + "'";
|
|
var args = new List<MySqlParameter>()
|
|
{
|
|
new MySqlParameter("?role", MySqlDbType.String) { Value = rolesList },
|
|
}.ToArray();
|
|
upReader = db.ExecReader(upSql, args);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
TraceLog.Error("AdminOperator.SaveAdminRole error,sql={0},error={1}", sql, e.Message);
|
|
return;
|
|
}
|
|
finally
|
|
{
|
|
if (upReader != null)
|
|
{
|
|
upReader.Close();
|
|
}
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
string insertSql = "insert into admin_account_role set account=?account,role=?role";
|
|
var data = new List<MySqlParameter>()
|
|
{
|
|
new MySqlParameter("?account", MySqlDbType.String) { Value = account },
|
|
new MySqlParameter("?role", MySqlDbType.String) { Value = rolesList },
|
|
}.ToArray();
|
|
MySqlDataReader inReader = null;
|
|
try
|
|
{
|
|
inReader = db.ExecReader(insertSql, data);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
TraceLog.Error("AdminOperator.SaveAdminRole error,sql={0},error={1}", sql, e.Message);
|
|
return;
|
|
}
|
|
finally
|
|
{
|
|
if (inReader != null)
|
|
{
|
|
inReader.Close();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void DeleteAdminRole(String account)
|
|
{
|
|
string sql = "delete from admin_account_role where account='" + account + "'";
|
|
MySqlDataReader reader = null;
|
|
try
|
|
{
|
|
reader = db.ExecReader(sql);
|
|
if (reader == null)
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
TraceLog.Error("AdminOperator.DeleteAdminRole error,sql={0},error={1}", sql, e.Message);
|
|
}
|
|
finally
|
|
{
|
|
if (reader != null)
|
|
{
|
|
reader.Close();
|
|
}
|
|
}
|
|
}
|
|
|
|
public List<int> GetRoleIds(string account)
|
|
{
|
|
//用户所有用的角色信息
|
|
string sql = string.Format("select * from admin_account_role where account='{0}'", account);
|
|
|
|
MySqlDataReader reader = null;
|
|
List<int> roleIds = new List<int>();
|
|
try
|
|
{
|
|
reader = db.ExecReader(sql);
|
|
if (reader == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
while (reader.Read())
|
|
{
|
|
string roles = reader.GetString("role");
|
|
string[] s = roles.Split(",");
|
|
for (int i = 0; i < s.Length; i++)
|
|
{
|
|
if (!string.IsNullOrEmpty(s[i]))
|
|
{
|
|
roleIds.Add(Convert.ToInt32(s[i]));
|
|
}
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
TraceLog.Error("AdminOperator.GetRoleIds error,sql={0},error={1}", sql, e.Message);
|
|
}
|
|
finally
|
|
{
|
|
if (reader != null)
|
|
{
|
|
reader.Close();
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < roleIds.Count; i++)
|
|
{
|
|
var id = roleIds[i];
|
|
}
|
|
|
|
return roleIds;
|
|
}
|
|
}
|
|
|
|
public class Account
|
|
{
|
|
public string account;
|
|
public string name;
|
|
public int valid;
|
|
public string lastLoginTime;
|
|
public string cTime;
|
|
public List<Role> roles;
|
|
|
|
public Account()
|
|
{
|
|
roles = new List<Role>();
|
|
}
|
|
|
|
public JsonData ToJson()
|
|
{
|
|
var json = new JsonData();
|
|
json["account"] = account;
|
|
json["name"] = name;
|
|
json["valid"] = valid;
|
|
json["lastLoginTime"] = lastLoginTime;
|
|
json["cTime"] = cTime;
|
|
if (roles != null && roles.Count > 0)
|
|
{
|
|
JsonData rs = new JsonData();
|
|
roles.ForEach(role => { rs.Add(role.ToJson()); });
|
|
json["roles"] = rs;
|
|
}
|
|
|
|
return json;
|
|
}
|
|
}
|
|
|
|
|
|
//菜单
|
|
public class Menu
|
|
{
|
|
public int MenuId;
|
|
public string Url;
|
|
public string Name;
|
|
public String Icon;
|
|
public int Order;
|
|
public int ParentId;
|
|
public int Permission;
|
|
public int Version;
|
|
|
|
public List<Menu> subMenus;
|
|
|
|
public Menu()
|
|
{
|
|
subMenus = new List<Menu>();
|
|
}
|
|
|
|
public JsonData ToJson()
|
|
{
|
|
var json = new JsonData();
|
|
json["MenuId"] = MenuId;
|
|
json["Url"] = Url;
|
|
json["Name"] = Name;
|
|
json["Order"] = Order;
|
|
json["Icon"] = Icon;
|
|
json["ParentId"] = ParentId;
|
|
json["Permission"] = Permission;
|
|
json["Version"] = Version;
|
|
if (subMenus != null && subMenus.Count > 0)
|
|
{
|
|
var list = subMenus.OrderBy(item => item.Order).ToList();
|
|
JsonData subjson = new JsonData();
|
|
foreach (var sub in list)
|
|
{
|
|
subjson.Add(sub.ToJson());
|
|
}
|
|
|
|
json["subMenus"] = subjson;
|
|
}
|
|
|
|
return json;
|
|
}
|
|
}
|
|
|
|
|
|
//权限
|
|
public class Permission
|
|
{
|
|
public int Id;
|
|
public string Name;
|
|
public int ParentId;
|
|
public List<String> Urls;
|
|
public List<Permission> SubPermissions;
|
|
|
|
public Permission()
|
|
{
|
|
Urls = new List<string>();
|
|
SubPermissions = new List<Permission>();
|
|
}
|
|
|
|
public JsonData ToJson()
|
|
{
|
|
JsonData jsonData = new JsonData();
|
|
jsonData["Id"] = Id;
|
|
jsonData["Name"] = Name;
|
|
jsonData["ParentId"] = ParentId;
|
|
jsonData["Urls"] = string.Join(",", Urls);
|
|
if (SubPermissions != null && SubPermissions.Count > 0)
|
|
{
|
|
var list = SubPermissions.OrderBy(item => item.Id).ToList();
|
|
JsonData subjson = new JsonData();
|
|
foreach (var sub in list)
|
|
{
|
|
subjson.Add(sub.ToJson());
|
|
}
|
|
|
|
jsonData["SubPermissions"] = subjson;
|
|
}
|
|
|
|
return jsonData;
|
|
}
|
|
}
|
|
|
|
//角色
|
|
public class Role
|
|
{
|
|
public Role()
|
|
{
|
|
Permissions = new List<int>();
|
|
Menus = new List<int>();
|
|
}
|
|
|
|
public int Id;
|
|
public string Name;
|
|
public List<int> Menus; //当前这个角色下有哪些菜单
|
|
public List<int> Permissions; //当前这个角色下有哪些权限
|
|
|
|
public void build(MySqlDataReader row)
|
|
{
|
|
int roleId = row.GetInt32("id");
|
|
string name = row.GetString("name");
|
|
|
|
this.Id = roleId;
|
|
this.Name = name;
|
|
if (!row.IsDBNull(row.GetOrdinal("permission")))
|
|
{
|
|
string permissions = row.GetString("permission");
|
|
var ps = permissions.Split(",");
|
|
for (int i = 0; i < ps.Length; i++)
|
|
{
|
|
string p = ps[i];
|
|
this.Permissions.Add(p.Toint32(0));
|
|
}
|
|
}
|
|
|
|
if (!row.IsDBNull(row.GetOrdinal("menu")))
|
|
{
|
|
string menus = row.GetString("menu");
|
|
var ms = menus.Split(",");
|
|
for (int i = 0; i < ms.Length; i++)
|
|
{
|
|
string m = ms[i];
|
|
this.Menus.Add(m.Toint32(0));
|
|
}
|
|
}
|
|
}
|
|
|
|
public JsonData ToJson()
|
|
{
|
|
JsonData json = new JsonData();
|
|
json["Id"] = Id;
|
|
json["Name"] = Name;
|
|
json["Menus"] = string.Join(",", Menus);
|
|
json["Permissions"] = string.Join(",", Permissions);
|
|
return json;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 用户 权限 菜单 角色
|
|
|
|
//角色--->用户
|
|
//菜单---->权限
|
|
//角色---->权限
|