Monday, December 20, 2010

(ZT)MOSS/Sharepoint 开发中代码运行的权限级别的讨论

 

FROM : http://www.cnblogs.com/Creator/archive/2010/12/20/1909284.html

 

用MOSS 2007来进行工作流和其它的开发,其中会涉及到一些权限的问题。现在将我碰到的一些问题与大家分享:
一.WebPart和事件处理程序EventHandler,运行是的权限是以当前登录用户的权限为准。
二.SPD中的工作流活动,所有活动,比如更新列表,删除项目,等等都是以当前的用户权限进行。
三.自己开发的工作流代码,都是以系统管理员的权限进行,比如你在进行工作流的Activity的开发时不需要提升程序的运行级别就可以删除列表中的项目。

那么如何提升MOSS Event Handler事件处理程序运行级别呢?

方法一:模拟管理员权限

不论是在工作流或是EventHandler中,我们经常希望模拟管理员权限,从而可以通过程序自定义列表项的权限。

在工作流中可以用如下代码来提升权限:(以下代码实现的功能是断开列表项所继承的权限,除管理员以外)

SPListItem item = workflowProperties.Item;

SPSecurity.RunWithElevatedPrivileges(delegate()      //用此方法模拟管理员账户运行此事件处理程序
                {
                    using (SPSite site = new SPSite(workflowProperties.SiteId))    //用此方法的话就不用dispose()了
                    {
                        using (SPWeb web = site.OpenWeb(workflowProperties.WebId))  //注意获得web的方法!!!
                        {
                            try
                            {
                                if (!item.HasUniqueRoleAssignments) item.BreakRoleInheritance(false);   //将此条目取消权限继承,如果是“false”,则将去除所有权限,只保留系统账户,如果是“true”,则将上一级权限复制过来。
                            }
                            catch (Exception ex)
                            {

                            }
                        }

                    }
                }
                    );

但是,在 EventHandler中沿用以上代码,在调用列表项item实例进行操作时(如item.BreakRoleInheritance),会提示“没有权限”。根本原因是item实例不是在RunWithElevatedPrivileges代码段中实例化的。也就是说item必须在 RunWithElevatedPrivileges中进行实例化,代码如下:

SPSecurity.RunWithElevatedPrivileges(delegate()      //用此方法模拟管理员账户运行此事件处理程序
                {
                    using (SPSite site = new SPSite(properties.SiteId))    //用此方法的话就不用dispose()了
                    {
                        using (SPWeb web = site.OpenWeb(properties.OpenWeb().ID)) //注意获得web的方法!!!
                        {
                            try
                            {
                                SPList list = web.Lists[properties.ListId];
                                SPListItem item = list.Items.GetItemById(Convert.ToInt32(properties.ListItemId));
                                //SPListItem item = properties.ListItem.ID;
                                //清空所有权限
                                if (!item.HasUniqueRoleAssignments) item.BreakRoleInheritance(false);   //将此条目取消权限继承,如果是“false”,则将去除所有权限,只保留系统账户,如果是“true”,则将上一级权限复制过来。
                            }
                            catch (Exception ex)
                            {

                            }
                        }

                    }
                }
                    );

方法二:用API模拟管理员登录的方法



//以下用户管理用户来登录

protected static WindowsIdentity CreateIdentity(string User, string
            Domain, string Password)
{
// The Windows NT user token.
            IntPtr tokenHandle = new IntPtr(0);

const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_LOGON_NETWORK = 3;

            tokenHandle = IntPtr.Zero;

// Call LogonUser to obtain a handle to an access token.
bool returnValue = LogonUser(User, Domain, Password, 
                LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT,
ref tokenHandle);

if (false == returnValue)
{
int ret = Marshal.GetLastWin32Error();
throw new Exception("LogonUser failed with error code: " +  ret);
            }

            System.Diagnostics.Debug.WriteLine("Created user token: " +
                tokenHandle);

//The WindowsIdentity class makes a new copy of the token.
//It also handles calling CloseHandle for the copy.
            WindowsIdentity id = new WindowsIdentity(tokenHandle);
            CloseHandle(tokenHandle);
return id;
        }

        [DllImport("advapi32.dll", SetLastError=true)]
private static extern bool LogonUser(String lpszUsername, String
            lpszDomain, String lpszPassword,
int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

        [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
private extern static bool CloseHandle(IntPtr handle);

使用的时候, 应该这样使用:

WindowsImpersonationContext wic = CreateIdentity ("用户","域名","密码") .Impersonate();

使用完毕,应该释放权限:

wic.Undo ();

0 Comments:

Post a Comment

<< Home