Interpreting the object model of permission in Document Library

Source: Internet
Author: User
By default, the permissions of document library are inherited from the site. We can manually modify these permissions. First, we need to cut off the inheritance relationship of permissions, and then
To modify the permissions of any group on the current list:
The following describes how to modify the permissions of a document library in the Code:

First, let's look at an instance:
When you click lock, set this document library to read-only for all groups/users (excluding site owners). When you click resume, restore the original permissions of each group/user on this list.
:
After locking:

After the permission is restored:

From the menu changes, we can see the permission changes.

Core Code As follows: 1 Spweb currentweb = Spcontext. Current. Web;
2 Splistcollection lists = Currentweb. getlistsoftype (spbasetype. documentlibrary );
3 Splist list = Lists [ This . Doclibname];
4 // Determine whether the inheritance relationship has been cut off. If not, cut off the inheritance relationship and copy the permission from the parent site.
5 // If inheritance is not cut off, its permissions cannot be modified.
6 If ( ! List. hasuniqueroleassignments)
7 {
8List. breakroleinheritance (True);
9}
10 // Traverse all groups/users in the list permission, that is, all groups/users in the permission list
11 Foreach (Sproleassignment Ra In List. roleassignments)
12 {
13 Spprincipal sp = RA. member;
14 // Obtaining a group/user is invalid for the group operation of site owners. They always have the fullcontrol permission.
15 If ( ! Sp. Name. Contains ( " Owners " ))
16 {
17 // Permission to traverse the Group/user
18 Foreach (Sproledefinition roledefinition In RA. roledefinitionbindings)
19 {
20 // Limited access is special and will not be removed.
21 If (Roledefinition. Name ! =   " Limited access " )
22 {
23 // Back up current Permissions
24 Splistitem newitem = Original. Items. Add ();
25 Newitem [ " Title " ] = Sp. Name;
26 Newitem [columnpermission] = Roledefinition. Name;
27 Newitem. Update ();
28 }
29 }
30
31 // Remove all permissions of the Group
32 RA. roledefinitionbindings. removeall ();
33
34 // Get all permissions of the site
35 Sproledefinitioncollection roles = Currentweb. roledefinitions;
36 // Get read permission
37 Sproledefinition viewroledefinition = Roles [ " Read " ];
38 // Determine whether the group/user has the read permission. If not, add the read permission.
39 If ( ! RA. roledefinitionbindings. Contains (viewroledefinition ))
40 {
41RA. roledefinitionbindings. Add (viewroledefinition );
42RA. Update ();
43}
44 }
45 }

The main categories are described as follows:
Sproleassignment: class that represents a group/user
Sproledefinition: indicates a permission class.

Foreach (Sproleassignment Ra In List. roleassignments)

Roleassignments is a collection of all group/users in this list.

RA. roledefinitionbindings. removeall ();

Roledefinitionbindings is a set of permissions owned by a group or user.

Sproledefinitioncollection roles = Currentweb. roledefinitions;

Roledefinitions is a set of site permissions. That is to say, it is worth noting that WSS and Moss are different.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.