enum 权限

   [FlagsAttribute] 
    
public enum RoleType
    {
        未登录 
= 0,
        普通用户 
= 1,
        锁定
=2,
        旅行社用户 
= 32,
        旅行社创建者 
= 64,
        外包用户 
= 128,
        编辑 
= 512,
        管理员 
= 1024
    }
    
public class Role
    {
        
private RoleType _RoleType = RoleType.未登录;
        
/// <summary>
        
/// 初始化
        
/// </summary>
        
/// <param name="role"></param>
        public Role(object role)
        {
            
if (role != null)
                _RoleType 
= (RoleType) role;
        }
        
/// <summary>
        
/// 当前类封装的roleType
        
/// </summary>
        public RoleType RoleType
        {
            
get { return _RoleType; }
            
set { _RoleType = value; }
        }
        
/// <summary>
        
/// 当前的role对象是否包含某个TYPE
        
/// </summary>
        
/// <param name="rt"></param>
        
/// <returns></returns>
        public bool Contains(RoleType rt)
        {
            
return (_RoleType & rt) == rt;
        }

        
public bool Contains(params RoleType[] rt) {
            
return this.Contains(rt.ToList());
        }
        
/// <summary>
        
/// 当前的role对象是否包含某些TYPE
        
/// </summary>
        
/// <param name="rts"></param>
        
/// <returns></returns>
        public bool Contains(IEnumerable<RoleType> rts)
        {
            
bool ret = false;
            
foreach (RoleType rt in rts)
                
if (this.Contains(rt))
                {
                    ret 
= true;
                    
break;
                }
            
return ret;
        }
        
/// <summary>
        
/// 为当前对象添加权限
        
/// </summary>
        
/// <param name="rt"></param>
        
/// <returns></returns>
        public void Add(RoleType rt)
        {
             RoleType 
|= rt;
        }
        
/// <summary>
        
/// 当前用户是否登录了
        
/// </summary>
        public bool IsLogin
        {
            
get
            {
                
return GetRoleTypeList.Count != 0;
            }
        }

        
/// <summary>
        
/// 列表,当前所有权限
        
/// </summary>
        public List<String> GetRoleTypeList
        {
            
get
            {
                var lis 
=
                    Enum.GetValues(
typeof (RoleType));
                var list 
= new List<string>();
                
foreach(object o in lis)
                {
                    var r 
= (RoleType) o;
                    
if (Contains(r) && r != RoleType.未登录)
                        list.Add(r.ToString());
                }
                
return list;

            }
        }

    }

 

使用方法如下

 

存数据库int字段

 取后存Session["status"]中,进行如下属性的访问即可(这个存在于CHUser类中)

 

        /// <summary>
        /// 获取当前用户状态
        /// </summary>
        static public Role Status {
            get {
                int status = 0;
                if (HttpContext.Current.Session["status"] != null)
                    int.TryParse(HttpContext.Current.Session["status"].ToString(), out status);
                return new Role(status);
            }

        }

 

 <%
    if (CHUser.Status.Contains(RoleType.管理员))
    {
    %>
    <li><%=Html.ActionLink("用户管理", "UserManage", "Admin")%></li>
    <%
    }

%>

判断时



posted @ 2008-11-20 09:29  重典  阅读(422)  评论(4编辑  收藏  举报