工作流中任务权限控制问题

[关键字]: sharepoint Designer 2007, taks,list,Asigned to,permission

[问题描述] 最近有客户问到一个问题,用SharePoint Designer 2007做的一个批准的工作流,发现只要有权限的用户都可以去执行分配(Assign to)给别人的任务,按理说应该是每个人只对分配给自己的任务有修改的权限,对别人的只有读的权限。

[研究过程]

1.对Sharepoint server 2007自带的"approval"模板 和 用sharepoint Designer 2007 做的approve测试,发现创建的任务都没有提供修改权限的接口。

2.想到是否可以针对“Task”这个列表再绑定一个工作流,每当批准的那个工作流产生一个任务到这个"Task"列表中,就自动启动这个工作流来修改任务的权限属性。但是经测试发现不行,原因是由于sharepoint 2007产生的任务虽然放在“Task”这个列表中,但是他不是一个listitem类,细心的话,你会在"site content types"里发现它的Parent是workflow task,而不是item.我们知道workflow 只能绑定在document和list item 上的。

3.再试着使用"create list item"这个操作代替"assign to-do item",这样就可以把上一个方法中提到的工作流用上了,但是发现"create list item" 是一个列表操作,用它创建一个item来表示一个任务的话,不能使工作流保持状态等待这个任务结束后再往下执行。

所以通过种种测试,结论是,不能通过再创工作流的办法来对task这个任务列表进行权限修改。

[解决方案]  最终我们只能回到使用"EventHandler"上来解决这个问题。

代码如下

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
namespace ItemAddedEventHandler
{
    
public class ItemAddedPermissionControl : SPItemEventReceiver
    
{
        
public override void ItemAdded (SPItemEventProperties properties)
        
{
            
//run the default event handlers on the item
            base.ItemAdded(properties);
                       
            
try
            
{
                    
using (SPSite site = new SPSite(properties.SiteId))
                    
{
                        
using (SPWeb web = site.OpenWeb(properties.RelativeWebUrl))
                        
{

                            
//call the function that changes the permissions on the list item. 
                            
//Do not use properties.ListItem since that will break impersonation!
                            SetOwnerAsOnlyEditor(web.Lists[properties.ListId].GetItemById(properties.ListItemId));
                        }

                    }

                

            }

            
catch (Exception ex)
            
{
             
            }

        }

        
private void SetOwnerAsOnlyEditor(SPListItem item)
        
{
            SPWeb myweb1 
= item.Web;
                        

            
////get the task owner from the item. 'Assigned To' is a built-in property, so it should be in all lists.
            string authorValue = item["Assigned To"].ToString();

            SPFieldUserValue authorUserValue 
= new SPFieldUserValue(myweb1, authorValue);

            SPUser authorUser 
= authorUserValue.User;

            
//break the security of the item from the list, but keep the permissions

            item.BreakRoleInheritance(
true);

            SPRoleAssignment roleassignment 
= new SPRoleAssignment(authorUser.LoginName, authorUser.Email, authorUser.Name, authorUser.Name);
            roleassignment.RoleDefinitionBindings.Add(myweb1.RoleDefinitions[
"Contribute"]);


            
foreach (SPRoleAssignment roleAssignment in item.RoleAssignments)
            
{
                
//delete the existing permissions
                roleAssignment.RoleDefinitionBindings.RemoveAll();
                
//add the reader permission

                
//change the permissions of everyone with access to this item so they are readers only
                roleAssignment.RoleDefinitionBindings.Add(myweb1.RoleDefinitions["Read"]);
                roleAssignment.Update();
                
//  item.Update();

            }

            item.RoleAssignments.Add(roleassignment);
            item.Update();

        }

        

    }

}

 

《如果你想知道如何一步步创建一个Event Handler Feature,请参见http://msdn2.microsoft.com/en-us/library/ms453149.aspx

[后记] 除了使用EventHandler之外,我们也可以尝试使用自定义的custome acticity 来做。

posted @ 2007-04-22 17:34  bluehat  阅读(4667)  评论(9编辑  收藏  举报