sharepoint listItem设置权限

 SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                SPSite site = new SPSite("http://john1/");
                SPWeb web = site.OpenWeb();
                Console.WriteLine("Currently logged in as: " + web.CurrentUser.ToString() + "(" + web.CurrentUser.Name + ")");

                SPUserToken userToken = web.AllUsers[@"LANGKAI\administrator"].UserToken;

                site = new SPSite("http://john1/", userToken);
                web = site.OpenWeb();
                // Be sure the current user has permission to check permissions.
                if (!web.DoesUserHavePermissions(SPBasePermissions.EnumeratePermissions))
                {
                    return;
                }
                SPUser currentUser = web.CurrentUser;
                Console.WriteLine(currentUser.LoginName);
                SPList list = web.Lists["DZG Expense"];
                SPListItem item = list.Items[0];
                for (int i = 0; i < item.AllRolesForCurrentUser.Count; i++)
                {
                    Console.WriteLine(item.AllRolesForCurrentUser[i].BasePermissions.ToString());
                }
                if (!item.HasUniqueRoleAssignments)
                {
                    item.BreakRoleInheritance(false);
                }

                //查看某用户在当前item上的权限级别

                SPUser user = web.EnsureUser(@"LANGKAI\liuxin");
                Console.WriteLine(user.LoginName);
                SPRoleAssignment ass = item.RoleAssignments.GetAssignmentByPrincipal((SPPrincipal)user);

                foreach (SPRoleDefinition t in ass.RoleDefinitionBindings)
                {
                    t.BasePermissions |= SPBasePermissions.ViewListItems;
                    //t.BasePermissions &= ~SPBasePermissions.ViewListItems;
                    Console.WriteLine(t.Name);
                    Console.WriteLine(t.BasePermissions.ToString());
                    t.Update();
                }

                //SPRoleDefinition role = web.RoleDefinitions.GetByType(SPRoleType.Reader);
                //ass.RoleDefinitionBindings.Add(role);
                //ass.Update();
                foreach (SPRoleDefinition t in ass.RoleDefinitionBindings)
                {
                    Console.WriteLine(t.Name);
                    Console.WriteLine(t.BasePermissions.ToString());
                    
                }
               Console.WriteLine(list.DoesUserHavePermissions(SPBasePermissions.AddListItems));
                //SPBasePermissions p = item.GetUserEffectivePermissions(user.LoginName);
                //Console.WriteLine(p.ToString());
                //SPPermissionInfo info = item.GetUserEffectivePermissionInfo(user.LoginName);
                //Console.WriteLine(info.Permissions.ToString());

                //Console.WriteLine("SPRoleAssignment....");
                //SPRoleAssignment ra = new SPRoleAssignment(user);
                //SPRoleDefinition role = web.RoleDefinitions.GetByType(SPRoleType.Reader);
                //ra.RoleDefinitionBindings.Add(role);
                //item.RoleAssignments.Add(ra);
                //item.Update(); 
                //SPPermissionInfo info2 =  item.GetUserEffectivePermissionInfo(user.LoginName);
                //Console.WriteLine(info2.Permissions.ToString());


                //for (int i = 0; i < item.AllRolesForCurrentUser.Count; i++)
                //{
                //    Console.WriteLine(item.AllRolesForCurrentUser[i].BasePermissions.ToString());
                //}
                //SPListItem item = properties.ListItem;
                //SPRoleDefinitionBindingCollection usersRoles = web.AllRolesForCurrentUser;
                //for (int i = 0; i < usersRoles.Count; i++)
                //{
                //    //sw.Write(usersRoles[i].BasePermissions.ToString());
                //    Console.WriteLine(usersRoles[i].BasePermissions.ToString());
                //}
                //sw.Close();
            });

 

posted @ 2013-03-19 10:27  louiskoo  阅读(1391)  评论(0编辑  收藏  举报