⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userinfo.cs

📁 一个ASP.NET下的中文内容管理和社区系统
💻 CS
字号:
namespace ASPNET.StarterKit.Communities {

    using System;
    using System.Web;


    //*********************************************************************
    //
    // UserInfo Class
    //
    // Represents all the information about a user that
    // can be retrieved from a cookie. It represents the username
    // and the user roles.
    //
    //*********************************************************************
    
    public class UserInfo {

        bool _isAuthenticated = false;
        bool _isAdministrator = false;
        
        string _username = "Anonymous";
        bool _mayView = false;
        bool _mayAdd = false;
        bool _mayEdit = false;
        bool _mayDelete = false;
        bool _mayComment = false;
        bool _mayModerate = false;
        bool _mayRate = false;


    
        //*********************************************************************
        //
        // IsAuthenticated Property
        //
        // Indicates whether the user is anonymous or authenticated.
        //
        //*********************************************************************

        public bool IsAuthenticated {
            get { return _isAuthenticated; }
        }



        //*********************************************************************
        //
        // IsAdministrator Property
        //
        // Indicates whether the current user is a member of the 
        // Administrators role.
        //
        //*********************************************************************

        public bool IsAdministrator {
            get { return _isAdministrator; }
        }


        //*********************************************************************
        //
        // Username Property
        //
        // The Username of the current user.
        //
        //*********************************************************************

        public string Username {
            get { return _username; }
        }


        //*********************************************************************
        //
        // MayView Property
        //
        // This property is calculated relative to the current section.
        // When true, user can view pages in this section.
        //
        //*********************************************************************
        
        public bool MayView {
            get { return _mayView; }
        
        }


        //*********************************************************************
        //
        // MayAdd Property
        //
        // This property is calculated relative to the current section.
        // When true, user can add pages to this section.
        //
        //*********************************************************************
        
        public bool MayAdd {
            get { return _mayAdd; }
        
        }


        //*********************************************************************
        //
        // MayEdit Property
        //
        // This property is calculated relative to the current section.
        // When true, user can edit pages in this section.
        //
        //*********************************************************************
        
        public bool MayEdit {
            get { return _mayEdit; }
        
        }
        

        //*********************************************************************
        //
        // MayDelete Property
        //
        // This property is calculated relative to the current section.
        // When true, user can delete pages from this section.
        //
        //*********************************************************************
                
        public bool MayDelete {
            get { return _mayDelete; }
        
        }


        //*********************************************************************
        //
        // MayComment Property
        //
        // This property is calculated relative to the current section.
        // When true, user can comment on pages in this section.
        //
        //*********************************************************************
        
        public bool MayComment {
            get { return _mayComment; }
        }


        //*********************************************************************
        //
        // MayModerate Property
        //
        // This property is calculated relative to the current section.
        // When true, user can moderate pages in this section.
        //
        //*********************************************************************
        
        public bool MayModerate {
            get { return _mayModerate; }
        }


        //*********************************************************************
        //
        // MayRate Property
        //
        // This property is calculated relative to the current section.
        // When true, user can rate pages in this section.
        //
        //*********************************************************************
        
        public bool MayRate {
            get { return _mayRate; }
        
        }
        


        //*********************************************************************
        //
        // IsInRole Method
        //
        // Checks whether a user is in a particular role.
        //
        //*********************************************************************
        
        public bool IsInRole(string roleName) {
            if (roleName == "Everyone")
                return true;
            if (roleName == "Authenticated" && _isAuthenticated)
                return true;
            if (! _isAuthenticated)
                return false;
            return (HttpContext.Current.User.IsInRole(roleName));
        }        



        //*********************************************************************
        //
        // UserInfo Constructor
        //
        // Initializes a new UserInfo object given a section. All
        // user permissions are calculated relative to a section.
        //
        //*********************************************************************
        
        public UserInfo(SectionInfo objSectionInfo) {
            HttpContext Context = HttpContext.Current;
            
            // Check whether authenticated
            _isAuthenticated = Context.Request.IsAuthenticated;
            
            // Determine permissions based on page object
            if (! _isAuthenticated) {
                // if anonymous, then user can view if everyone can
                if (Array.IndexOf( objSectionInfo.ViewRoles, "Community-Everyone" ) != -1)
                    _mayView = true;
                // if anonymous, then user can add if everyone can
                if (Array.IndexOf( objSectionInfo.AddRoles, "Community-Everyone" ) != -1)
                    _mayAdd = true;
                // if anonymous, then user can edit if everyone can
                if (Array.IndexOf( objSectionInfo.EditRoles, "Community-Everyone" ) != -1)
                    _mayEdit = true;
                // if anonymous, then user can delete if everyone can
                if (Array.IndexOf( objSectionInfo.DeleteRoles, "Community-Everyone" ) != -1)
                    _mayDelete = true;
                // if anonymous, then user can comment if everyone can
                if (Array.IndexOf( objSectionInfo.CommentRoles, "Community-Everyone" ) != -1)
                    _mayComment = true;
           
            } else {

                _username = Context.User.Identity.Name;

                // Check if Administrator
                if (HttpContext.Current.User.IsInRole("Community-Administrators"))
                    _isAdministrator = true;
            
                // Check View Permissions
                _mayView = CheckAuthenticatedPermission(objSectionInfo.ViewRoles);            

                // Check Add Permissions
                _mayAdd = CheckAuthenticatedPermission(objSectionInfo.AddRoles);            

                // Check Edit Permissions
                _mayEdit = CheckAuthenticatedPermission(objSectionInfo.EditRoles);            

                // Check Delete Permissions
                _mayDelete = CheckAuthenticatedPermission(objSectionInfo.DeleteRoles);            

                // Check Comment Permissions
                _mayComment = CheckAuthenticatedPermission(objSectionInfo.CommentRoles);            

                // Check Moderate Permissions
                _mayModerate = CheckAuthenticatedPermission(objSectionInfo.ModerateRoles);            

                // Check Rate Permissions
                _mayRate = CheckAuthenticatedPermission(objSectionInfo.RateRoles);            
            }
        }
        

        //*********************************************************************
        //
        // CheckAuthenticatedPermission Method
        //
        // Checks whether a user is in the necessary role.
        //
        //*********************************************************************
        
        private bool CheckAuthenticatedPermission(string[] PageRoles) {
            // If Everyone has permission, return true        
            if (Array.IndexOf( PageRoles, "Community-Everyone" ) != -1)
                return true;
                
            // If Authenticated has permission, return true
            if (Array.IndexOf( PageRoles, "Community-Authenticated" ) != -1)
                return true;
                
            // Otherwise, check each role
            foreach (string role in PageRoles)
            if (HttpContext.Current.User.IsInRole(role))
                return true;
            
            // Nope, doesn't have permissions
            return false;
        }
 

        
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -