SharePoint安全...SPSecurity委托,一个值得我们去思考的问题

今天无意中发现SPSecurity.CodeToRunElevated 这个委托,确实很棒 ,一个无意识的发现,可以引发很多貌似不很重要的东西,却在项目中,引发很大的思考。。。希望大家看啦我的Demo能过明白,来引发出值得大家思考的东西
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;

namespace SPGridViewDemo
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SPSecurity.CodeToRunElevated showLists = new SPSecurity.CodeToRunElevated(ShowLists);
            SPSecurity.RunWithElevatedPrivileges(showLists);

        }


        protected void ShowLists()
        {
            DataTable entries = new DataTable("Entries");

            entries.Columns.Add("ID", typeof(System.Int32));
            entries.Columns.Add("SiteNav", typeof(string));
            entries.Columns.Add("SiteName", typeof(string));
            entries.Columns.Add("SiteURl", typeof(string));
            entries.Columns.Add("ListName", typeof(string));
            entries.Columns.Add("ListID", typeof(string));

            entries.PrimaryKey = new DataColumn[] { entries.Columns["ID"] };

            entries.Columns["ID"].AutoIncrement = true;
            entries.Columns["ID"].AutoIncrementSeed = 1;
            entries.Columns["ID"].AutoIncrementStep = 1;
            entries.Columns["ID"].ReadOnly = true;


            using (SPSite site = new SPSite(@"http://lh-vmpc:6060/"))
            {
                foreach (SPWeb web in site.AllWebs)
                {
                    foreach (SPList list in web.GetListsOfType(SPBaseType.GenericList))
                    {
                        if(list.DoesUserHavePermissions(site.RootWeb.CurrentUser,SPBasePermissions.AddListItems) && list.BaseTemplate == SPListTemplateType.Tasks)
                        {
                            DataRow row = entries.NewRow();
                            row["SiteName"] = web.Title;
                            row["SiteNav"] = web.Url;
                            row["SiteUrl"] = web.Url;
                            row["ListName"] = list.Title;
                            row["ListID"] = list.ID;

                            entries.Rows.Add(row);
                        }
                    }
                }
            }

            this.GridView1.DataSource = entries;
            GridView1.DataBind();

        }

    }
}

posted @ 2010-01-08 10:56  杨子宜  阅读(242)  评论(0编辑  收藏  举报