杞人忧天上掉下个林妹妹

穿越旷野的妹妹啊,慢些走;请不要用你的沉默告诉我,你不回头!

导航

使用缓存控制用户权限类

using System;
using System.Data;
using System.Web;
using System.Web.Caching;
using TIMS.WEB.PublicClass.DataBase;

namespace TIMS.WEB.PublicClass.User
{
 /*
  Explain:
   用户权限
   */
 /// <summary>
 /// 用户角色
 /// </summary>
 public class UserRole
 {
  private static string USERROLEKEY = "ROLEDIST";
  private static string TABLENAME = "TM_SYS_ROLEDIST";
  private static string SQL = "Select LogName, RoleName From TM_SYS_ROLEDIST";
  private static string MANAROLE = "Admin";
  //--------------------------------------------------------------------------------------------------------
  public UserRole()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }
  //--------------------------------------------------------------------------------------------------------
  /// <summary>
  /// 取得角色分配
  /// </summary>
  /// <returns></returns>
  private static DataSet GetRoleDist()
  {
   DataSet RoleDist = null;
   if(! IsNULL())
   {
    RoleDist = (DataSet)HttpContext.Current.Cache[USERROLEKEY];  /* */
   }

   return(RoleDist);
  }
  //--------------------------------------------------------------------------------------------------------
  /// <summary>
  /// 判断缓存中用户信息是否存在
  /// </summary>
  /// <returns></returns>
  private static bool IsNULL()
  {
   if(HttpContext.Current.Cache[USERROLEKEY] != null)  /* */
   {
    return(false);
   }
   else
   {
    return(! LoadRoleDist());
   }
  }
  //--------------------------------------------------------------------------------------------------------
  /// <summary>
  /// 加载角色分配
  /// </summary>
  /// <returns></returns>
  public static bool LoadRoleDist()
  {
   DataSet dsRoleDist = null;
   OracleDB oracleDB = new OracleDB();
   try
   {
    dsRoleDist = oracleDB.GetDataSet(SQL,TABLENAME);
   }
   catch
   {}
   oracleDB.CloseCon();
   if(dsRoleDist != null)
   {
    HttpContext.Current.Cache.Insert(USERROLEKEY,dsRoleDist); /* */    
    return(true);
   }   
   else
   {
    return(false);
   }
  }
  //--------------------------------------------------------------------------------------------------------
  /// <summary>
  /// 判断用户是否是专工
  /// </summary>
  /// <param name="SystemAB">true:专工;false:非专工;</param>
  /// <returns>用户登录名</returns>
  /// <returns>系统缩写</returns>
  public static bool Power(string LogName, string RoleName)
  {
   string filter = "LogName = '" + LogName + "' and RoleName = '" + RoleName + "'";
   DataSet RoleDist = GetRoleDist();

   if(RoleDist != null)
   {
    DataRow [] SelRow;
    SelRow = RoleDist.Tables[TABLENAME].Select(filter);
    if(SelRow.Length == 1)
    {
     return(true);
    }
    else
    {
     return(false);
    }
   }
   else
   {
    return(false); 
   }  
  }
  //--------------------------------------------------------------------------------------------------------
  /// <summary>
  /// 判断用户是否是管理员
  /// </summary>
  /// <param name="LogName"></param>
  /// <returns></returns>
  public static bool IsAdmin(string LogName)
  {
   string filter = "LogName = '" + LogName + "' and RoleName = '" + MANAROLE + "'";
   DataSet RoleDist = GetRoleDist();

   if(RoleDist != null)
   {
    DataRow [] SelRow;
    SelRow = RoleDist.Tables[TABLENAME].Select(filter);
    if(SelRow.Length == 1)
    {
     return(true);
    }
    else
    {
     return(false);
    }
   }
   else
   {
    return(false); 
   }
  }
  //--------------------------------------------------------------------------------------------------------
  /// <summary>
  /// 清空缓存
  /// </summary>
  /// <returns></returns>
  public static bool ClearCache()
  {
   try
   {
    HttpContext.Current.Cache.Remove(USERROLEKEY);  /* */
    return(true);
   }
   catch
   {
   }
   return(false);
  }
  //--------------------------------------------------------------------------------------------------------
 }
}

posted on 2006-05-18 23:03  杞人  阅读(629)  评论(0编辑  收藏  举报