CS用户角色扩展

Cs的架构比较庞大,前阵子自己写了新闻模块。如果用里面集成的角色验证还是不太好,所以自己动手重新改了一下里面的用户角色,Cs20的用户管理是CommunityServerComponents20项目Components文件夹下面的User类,经常用Asp.NET集成身份验证应该比较容易就看出来应当怎么改了。
主要的角色相关的代码如下
 #region IsRoles

        
public bool IsInRoles( string[] roleNames ) {

            
if(roleNames == null || roleNames.Length == 0)
                
return false;

            
string [] userRoles = Roles.GetUserRoleNames( this.Username );
            
foreachstring userRole in userRoles ) {
                
foreachstring roleName in roleNames ) {
                    
ifstring.Compare(roleName,userRole,true== 0 )
                        
return true;
                }
            }
            
return false;
        }

        
///    <summary>
        
///    Specifies if a user    in a System Administator administrator or not.
        
///    </summary>
        public bool    IsAdministrator    
        {
            
get    
            { 
                
try    
                {
                    
return IsInRoles( new string[] { roles.SystemAdministrator } );
                }
                
catch    {}

                
return false;
            }                     
        }

        
///    <summary>
        
///    Specifies if a user    in an administrator    or not.
        
///    </summary>
        public bool    IsBlogAdministrator    
        {
            
get    
            { 
                
try    
                {
                    
return IsInRoles( new string[] { roles.SystemAdministrator, roles.BlogAdministrator } );
                }
                
catch    {}

                
return false;
            }                     
        }

        
///    <summary>
        
///    Specifies if a user    in an administrator    or not.
        
///    </summary>
        public bool    IsGalleryAdministrator    
        {
            
get    
            { 
                
try    
                {
                    
return IsInRoles( new string[] { roles.SystemAdministrator, roles.GalleryAdministrator } );
                }
                
catch    {}

                
return false;
            }                     
        }

        
///    <summary>
        
///    Specifies if a user    in an administrator    or not.
        
///    </summary>
        public bool    IsFileAdministrator    
        {
            
get    
            { 
                
try    
                {
                    
return IsInRoles( new string[] { roles.SystemAdministrator, roles.FileAdministrator } );
                }
                
catch    {}

                
return false;
            }                     
        }

        
public bool IsReaderAdministrator
        {
            
get
            {
                
try    
                {
                    
return IsInRoles( new string[] { roles.SystemAdministrator, roles.ReaderAdministrator } );
                }
                
catch    {}

                
return false;
            }
        }

        
public bool IsMembershipAdministrator
        {
            
get
            {
                
try    
                {
                    
return IsInRoles( new string[] { roles.SystemAdministrator, roles.MembershipAdministrator } );
                }
                
catch    {}

                
return false;
            }
        }

        
///    <summary>
        
///    Specifies if a user    in an administrator    or not.
        
///    </summary>
        public bool    IsForumAdministrator    
        {
            
get    
            { 
                
try    
                {
                    
return IsInRoles( new string[] { roles.SystemAdministrator, roles.ForumsAdministrator } );
                }
                
catch    {}

                
return false;
            }                     
        }

        
///    <summary>
        
///    Specifies if a user    in an administrator    or not.
        
///    </summary>
        public bool    IsModerator    
        {
            
get    
            { 
                
try    
                {
                    
return IsInRoles( new string[] { roles.SystemAdministrator, roles.Moderator } );
                } 
                
catch    {}

                
return false;
            }
        }



        
/// <summary>
        
/// Lookup to determine if this user belongs to the editor role.
        
/// </summary>
        public bool IsEditor 
        {
            
get 
            {
                
try 
                {
                    
return IsInRoles( new string[] {roles.SystemAdministrator, roles.Editor } );
                }
                
catch {}

                
return false;
            }
        }


        
/// <summary>
        
/// 新增新闻管理员
        
/// </summary>
        public bool IsNewsAdministrator
        {
            
get 
            {
                
try
                {
                    
return IsInRoles(new string[] { roles.SystemAdministrator, roles.NewsAdministrator });
                }
                
catch { }
                
return false;
            }
        }





//        public static bool IsInRole(string rolename) 
//        {
//            return HttpContext.Current.User.IsInRole(rolename);
//        }

        
#endregion
最后一个新闻管理员的角色就是新加的。

然后找到该项目Configuration文件夹下面的RolesConfiguration.cs类
加上这段

        
//新增新闻管理员角色
        [XmlAttribute("newsAdministrator")]
        
public string NewsAdministrator
        {
            
get { return _newsAdministrator; }
            
set { this._newsAdministrator = value; }
        }
并修改一下RoleList方法,把NewsAdministrator加上
 public string RoleList()
        {
            
return string.Format("^({0}|{1}|{2}|{3}|{4}|{5}|{6}|{7}|{8}|{9}|{10}|{11}|{12})$",Everyone,RegisteredUsers,SystemAdministrator,Moderator,Editor,ForumsAdministrator,GalleryAdministrator,BlogAdministrator,Owners,FileAdministrator,ReaderAdministrator,MembershipAdministrator,NewsAdministrator);
        }

最后还需要到CS角色管理中加上同名的NewsAdministrator角色

posted on 2007-04-10 10:15  Neo0820  阅读(636)  评论(1编辑  收藏  举报

导航