MOSS字段編輯權限控制方案的實現(1)-管理頁面的開發和配置信息的持久化

MOSS字段編輯權限控制方案,實現了控制列表項,或文檔屬性的字段級權限控制,本篇講述如何開發配置頁面以及如何將配置信息持久化。

我們先看一下配置界面的樣子:


    wss(moss)的所有配置頁面都放到C:/Program Files/Common Files/Microsoft Shared/web server extensions/12/TEMPLATE/LAYOUTS目錄
或子目錄中,我們將字段權限配置頁面(FieldEditControl.aspx)放到Layouts的子目錄CodeArt中。
    配置界面的核心邏輯開發成一個webcontrol(FieldRightSettingPart),將這個webpart直接嵌入到管理頁面,管理頁面的代碼如下:
<%@ Assembly Name="Microsoft.SharePoint.ApplicationPages, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"%> 
<%@ Page Language="C#" Inherits="Microsoft.SharePoint.ApplicationPages.NewListPage" MasterPageFile="~/_layouts/application.master"      %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %> <%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Assembly="CodeArt.SharePoint, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2c606279787b575f" Namespace="CodeArt.SharePoint.WebPart" TagPrefix="codeArt" %>
<asp:Content ContentPlaceHolderId="PlaceHolderPageTitleInTitleArea" runat="server">
列表字段權限設置
 
</asp:Content>
<asp:Content ID="Content6" ContentPlaceHolderId="PlaceHolderMain" runat="server"> 
<codeArt:FieldRightSettingPart runat="server" id="fSetting" />
</asp:Content>

wss開發中經常會碰到配置信息存儲的問題,如果是webpart,我們可以用webpart的屬性來存儲,其他情況下,我們可以考慮用數據庫或List來存儲。
這裏我選擇用文檔庫來存儲,將配置類序列化成xml存儲到一個文檔庫中。考慮到以後還會碰到這類配置信息存儲的情況,把這個功能開發成一個通用的類CongfigManager:(爲了以後“可能的”替換數據存儲方式,用了工程模式)


  public abstract class ConfigManager
    
{        

        
public static ConfigManager GetConfigManager(string key)
        
{
            
return new DocLibConfigManager( key );
        }


        
public virtual T GetConfigData<T>(Guid id) where T : class , new() 
        
{
            
object obj = this.GetConfigData(typeof(T), id);

            
if( obj == null )
                
return null;
            
return
                (T)obj;
        }


        
public virtual object GetConfigData(Type t, Guid id)
        
{
            
return null ;
        }


        
public virtual void SetConfigData(Guid id , object obj)
        
{
            
        }


        
public virtual void ClearConfigData(Guid id)
        
{
        }

    }
 

 class DocLibConfigManager : ConfigManager
    
{
        
private string _key;
        
public DocLibConfigManager(string key)
        
{
            _key 
= key;
        }


        SPList EnsureList(SPWeb web)
        
{
            SPList list 
= null;

            
try
            
{
                list 
= web.Lists[_key];
            }
 
            
catch { }

            
if (list == null)
            
{
                web.AllowUnsafeUpdates 
= true;
                Guid listId 
= web.Lists.Add(_key, "List for config , never delete this list.", SPListTemplateType.DocumentLibrary);
                list 
= web.Lists[listId];
            }


            
return list;
        }


        
public override void SetConfigData( Guid id ,object obj)
        
{
            SPList list 
= null;

            
string xml = SerializeUtil.Seralize(obj);
            
byte[] content = Encoding.UTF8.GetBytes(xml);

            SPSecurity.RunWithElevatedPrivileges(
delegate()
            
{
                
using (SPSite elevatedsiteColl = new SPSite(SPContext.Current.Site.ID))
                
{
                    
using (SPWeb elevatedWeb = elevatedsiteColl.OpenWeb(SPContext.Current.Web.ID))
                    
{
                        list 
= this.EnsureList(elevatedWeb);
                        elevatedWeb.AllowUnsafeUpdates 
= true;
                        SPFile file 
= list.RootFolder.Files.Add( id.ToString() + ".xml", content, true);
                    }

                }

            }
);

        }


        
private SPListItem GetItem(SPList list, Guid id)
        
{            
            SPQuery q 
= new SPQuery();
            q.Query 
= "<Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='Text'>" + id.ToString() + ".xml</Value></Eq></Where>";
            q.RowLimit 
= 1;

            SPListItemCollection items 
= list.GetItems(q);

            
if (items.Count == 0)
                
return null;
            
else
                
return items[0];
        }


        
public override void ClearConfigData(Guid id)
        
{
            SPSecurity.RunWithElevatedPrivileges(
delegate()
              
{
                  
using (SPSite elevatedsiteColl = new SPSite(SPContext.Current.Site.ID))
                  
{
                      
using (SPWeb elevatedWeb = elevatedsiteColl.OpenWeb(SPContext.Current.Web.ID))
                      
{
                          
try
                          
{
                              SPList list 
= this.EnsureList(elevatedWeb);
                              elevatedWeb.AllowUnsafeUpdates 
= true;

                              SPListItem item 
= this.GetItem(list, id);
                              
if( item != null )
                                  item.Delete();
                          }

                          
catch throw; }
                      }

                  }

              }
);

        }


        
public override object GetConfigData(Type t, Guid id)
        
{
            
object obj = null;

            SPSecurity.RunWithElevatedPrivileges(
delegate()
              
{
                  
using (SPSite elevatedsiteColl = new SPSite(SPContext.Current.Site.ID))
                  
{
                      
using (SPWeb elevatedWeb = elevatedsiteColl.OpenWeb(SPContext.Current.Web.ID))
                      
{
                          SPList list 
= this.EnsureList(elevatedWeb);

                          
try
                          
{
                              SPListItem item 
= this.GetItem(list, id);

                              
if (item != null)
                              
{
                                  SPFile file 
= item.File;

                                  XmlDocument doc 
= new XmlDocument();
                                  doc.Load(item.File.OpenBinaryStream());

                                  obj 
= SerializeUtil.Deserialize(t, doc.OuterXml);
                              }

                          }

                          
catch throw; }
                      }

                  }

              }
);

            
return obj;
        }

    }

這個CongfigManager實現了對一個配置類的保存,獲取和刪除。

下面考慮配置類如何抽象化
用以下類來表示每個字段的配置信息:
    [Serializable]
    
public class FieldEditSetting
    
{
        
public string FieldName;

        
public bool CreatorCanEdit;

        
public bool AllUserCanEdit ;

        
public string SpecialAccounts;

        
public bool IsInSpecialAccounts(string account)
        
{
            
if (String.IsNullOrEmpty(SpecialAccounts))
                
return false;

            
string checkList = "," + this.SpecialAccounts.ToLower() + ",";

            
return checkList.IndexOf("," + account.ToLower() + ","!= -1;
        }


        
public bool CanEdit( SPUser currentUser , SPUser creatUser )
        
{
            
//if (currentUser.IsSiteAdmin) return true;

            
if (this.AllUserCanEdit) return true;

            
if (this.CreatorCanEdit && String.Compare(currentUser.LoginName, creatUser.LoginName, true== 0)
                
return true;

            
return this.IsInSpecialAccounts(currentUser.LoginName);
        }

    }
用一個集合類來表示整個列表的所有字段的配置信息(本來想用字典的,單字典類型不能序列化,這能放棄):
  [Serializable]
    
public class ListFieldEditSetting : List<FieldEditSetting> 
    
{
        
public const string Config_List = "__CodeArt_ListFieldEditSetting";

        
public FieldEditSetting GetByFieldName(string fieldName)
        
{
            
foreach (FieldEditSetting fSetting in this)
            
{
                
if (String.Compare(fSetting.FieldName, fieldName, true== 0)
                    
return fSetting;
            }

            
return null;
        }


        
public void Save(Guid listId)
        
{
            ConfigManager cmg 
= ConfigManager.GetConfigManager(ListFieldEditSetting.Config_List);
            cmg.SetConfigData(listId, 
this);
        }
        

        
public static ListFieldEditSetting GetListSetting(Guid listId)
        
{
            ConfigManager cmg 
= ConfigManager.GetConfigManager(ListFieldEditSetting.Config_List);

            ListFieldEditSetting setting 
= cmg.GetConfigData<ListFieldEditSetting>(listId);

            
return setting;
        }

    }

以上的基礎類建好了,可以開始FieldRightSettingPart的開發了:
FieldRightSettingPart 的代碼

FieldRightSettingPart會在內部生成一個佈局表格和很多的子控件,爲了便於操作這些子 控件,
聲明瞭三個字典類型的變量,來放置生成的控件,字典key爲字段名,值爲對應的編輯控件:
 private Dictionary<string, CheckBox> _AllUserCanEditControls = new Dictionary<string, CheckBox>();
private Dictionary<string, CheckBox> _CreatorCanEditControls = new Dictionary<string, CheckBox>();
private Dictionary<string, Microsoft.SharePoint.WebControls.PeopleEditor> _SpecialAccountsControls =
        
new Dictionary<string, Microsoft.SharePoint.WebControls.PeopleEditor>();
在CreateChildControls,遍歷列表的字段,生成設置界面,同時若已經存在配置信息,則按照配置信息初始化控件初始值:
CreateChildControls

按鈕提交時,遍歷編輯子控件,組裝配置類,調用ConfigManage類保存數據。:
_btnSubmit_Click

game over!

附1:存放配置信息的文檔庫:


附2:配置信息序列化後的xml格式:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfFieldEditSetting xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<FieldEditSetting>
<FieldName>fileleafref</FieldName>
<CreatorCanEdit>false</CreatorCanEdit>
<AllUserCanEdit>false</AllUserCanEdit>
<SpecialAccounts /></FieldEditSetting>
<FieldEditSetting>
<FieldName>title</FieldName>
<CreatorCanEdit>false</CreatorCanEdit>
<AllUserCanEdit>false</AllUserCanEdit>
<SpecialAccounts /></FieldEditSetting>
</ArrayOfFieldEditSetting>


本系列的所有文章:
CodeArt WSS3.0(MOSS)字段編輯權限控制解決方案(v1.0)
CodeArt WSS3.0(MOSS)字段編輯權限控制解決方案的實現 -- 概要
MOSS字段編輯權限控制方案的實現(1)-管理頁面的開發和配置信息的持久化
WSS頁面定製系列(2)---定製單個列表的表單頁面
WSS頁面定製系列(3)---重寫表單的保存邏輯
MOSS字段編輯權限控制方案(4)-打包解決方案
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章