154 lines
4.8 KiB
C#
154 lines
4.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using NOVA.DevFx.Security.Model;
|
|
using System.Data;
|
|
using NOVA.DevFx.Data.Utils;
|
|
using System.Data.SqlClient;
|
|
|
|
namespace NOVA.DevFx.Security.DataHandler
|
|
{
|
|
/// <summary>
|
|
/// 系统用户数据库访问类
|
|
/// </summary>
|
|
public static class UserHandler
|
|
{
|
|
|
|
/// <summary>
|
|
/// 将数据转换成实体类
|
|
/// </summary>
|
|
public static User DataReaderToEntites(IDataReader dr)
|
|
{
|
|
User model= new User();
|
|
if (dr["id"].ToString() != "")
|
|
{
|
|
model.id = int.Parse(dr["id"].ToString());
|
|
}
|
|
model.staff_id = dr["staff_id"].ToString();
|
|
model.name = dr["name"].ToString();
|
|
model.password = dr["password"].ToString();
|
|
if (dr["super_flag"].ToString() != "")
|
|
{
|
|
model.super_flag = bool.Parse(dr["super_flag"].ToString());
|
|
}
|
|
model.last_login_ip = dr["last_login_ip"].ToString();
|
|
if (dr["last_login_time"].ToString() != "")
|
|
{
|
|
model.last_login_time = DateTime.Parse(dr["last_login_time"].ToString());
|
|
}
|
|
model.description = dr["description"].ToString();
|
|
if (dr["sort_index"].ToString() != "")
|
|
{
|
|
model.sort_index = int.Parse(dr["sort_index"].ToString());
|
|
}
|
|
return model;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获得数据列表
|
|
/// </summary>
|
|
public static List<User> GetList(string strWhere)
|
|
{
|
|
StringBuilder strSql = new StringBuilder();
|
|
strSql.Append("select id,staff_id,name,password,super_flag,last_login_ip,last_login_time,description,sort_index ");
|
|
strSql.Append(" FROM s_user ");
|
|
if (strWhere.Trim() != "")
|
|
{
|
|
strSql.Append(" where " + strWhere);
|
|
}
|
|
SqlDataReader read = SqlHelper.ExecuteReader(strSql.ToString());
|
|
|
|
List<User> m_lst = new List<User>();
|
|
while (read.Read())
|
|
{
|
|
User item = DataReaderToEntites(read);
|
|
if (item != null)
|
|
{
|
|
m_lst.Add(item);
|
|
}
|
|
}
|
|
read.Close();
|
|
return m_lst;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据角色编号获取所有用户
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static List<User> GetUserListByRoleID(int roleid)
|
|
{
|
|
return GetList(" [id] in(select user_id from s_user_in_role where role_id=" + roleid.ToString() + ")");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取指定编号的用户信息
|
|
/// </summary>
|
|
/// <param name="uid"></param>
|
|
/// <returns></returns>
|
|
public static User GetItem(int uid)
|
|
{
|
|
StringBuilder strSql = new StringBuilder();
|
|
strSql.Append("select id,staff_id,name,password,super_flag,last_login_ip,last_login_time,description,sort_index ");
|
|
strSql.Append(" FROM s_user ");
|
|
strSql.Append(" WHERE [id]=@id ");
|
|
SqlParameter[] parameters = {
|
|
new SqlParameter("@id", SqlDbType.Int,4)};
|
|
int n = 0;
|
|
parameters[n++].Value = uid;
|
|
|
|
SqlDataReader read = SqlHelper.ExecuteReader(strSql.ToString(), parameters);
|
|
|
|
User item=null;
|
|
if (read.Read())
|
|
{
|
|
item = DataReaderToEntites(read);
|
|
}
|
|
if (item != null)
|
|
{
|
|
read.Close();
|
|
return item;
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取指定用户名的用户信息
|
|
/// </summary>
|
|
/// <param name="uid"></param>
|
|
/// <returns></returns>
|
|
public static User GetItemByName(string uname)
|
|
{
|
|
StringBuilder strSql = new StringBuilder();
|
|
strSql.Append("select id,staff_id,name,password,super_flag,last_login_ip,last_login_time,description,sort_index ");
|
|
strSql.Append(" FROM s_user ");
|
|
strSql.Append(" WHERE [name]=@name ");
|
|
SqlParameter[] parameters = {
|
|
new SqlParameter("@name", SqlDbType.VarChar,20)};
|
|
int n = 0;
|
|
parameters[n++].Value = uname;
|
|
|
|
SqlDataReader read = SqlHelper.ExecuteReader(strSql.ToString(), parameters);
|
|
|
|
User item = null;
|
|
if (read.Read())
|
|
{
|
|
item = DataReaderToEntites(read);
|
|
}
|
|
if (item != null)
|
|
{
|
|
read.Close();
|
|
return item;
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|