Manage Roles and user and document library permissions in the site

Source: Internet
Author: User

I. operation site role

 

Core category:

Spuser: represents a site user (http://msdn.microsoft.com/library/en-us/spptsdk/html/tscSPUser.asp? Frame = true)

Spusercollection: represents a collection containing multiple users (http://msdn.microsoft.com/library/en-us/spptsdk/html/tscSPUserCollection.asp? Frame = true)

Sprole: a role object (http://msdn.microsoft.com/library/en-us/spptsdk/html/tscSPRole.asp? Frame = true)

Sprolecollection: represents a collection of multiple roles (http://msdn.microsoft.com/library/en-us/spptsdk/html/tscSPRoleCollection.asp? Frame = true)

 

Spweb (indicating a site) contains a roles attribute (sprolecollection type), indicating all roles of the site. Sprole contains a users attribute (spusercollection type), indicating all users in the role. Spuser contains a roles attribute (sprolecollection attribute), which indicates all roles that the user belongs.

 

The add () method of sprole is used to add a user to this role. The removeuser () method is used to exclude a user from this role. After changing the role information, call the update () method to update the data source.

 

The following sample code traverses all roles under the current site and then traverses all users in each role. Since each user can belong to multiple roles, the output user information is duplicated.

 

Foreach (sprole role in spcontrol. getcontextweb (httpcontext. Current). Roles)

{

Response. Write ("role" + role. Name + "include member :");

Foreach (spuser user in role. Users)

{

Response. Write (user. Name + ";");
}

Response. Write ("<br> ");

}

 

 

Ii. Operation Document Library Permissions

 

Core category:

Splist: A list. The document library is also a list. (Http://msdn.microsoft.com/library/en-us/spptsdk/html/tscSPList.asp? Frame = true)

Sppermission: represents an authorization (http://msdn.microsoft.com/library/en-us/spptsdk/html/tscSPPermission.asp? Frame = true)

Sppermissioncollection: indicates a collection containing multiple authorizations (http://msdn.microsoft.com/library/en-us/spptsdk/html/tscSPPermissionCollection.asp? Frame = true)

Sprights: enumeration used to indicate permissions (http://msdn.microsoft.com/library/en-us/spptsdk/html/tsenSPRights.asp? Frame = true)

 

Splist has a Permissions attribute (sppermissioncollection type), indicating all the authorizations on this list. Each sppermission corresponds to an authorized member (may be a role or a user) and certain permissions. The member attribute of sppermission (the spmember type is the parent class of spuser and sprole) corresponds to the authorized member of this authorization, and the permissionmask attribute (sprights type) corresponds to the authorization permission.

 

The add () method of sppermissioncollection is used to add an authorization, and the remove () method is used to delete an authorization.

 

The following code example traverses all the document libraries on the site and displays the permissions that the user "kaneboy" has on each document library. If the user does not have any permissions on a document library, add a permission to view documents in the document library.

 

Spweb web = spcontrol. getcontextweb (httpcontext. Current );

Spuser user = web. Users ["kaneboy"];

Splistcollection lists = web. getlistsoftype (spbasetype. documentlibrary );

Foreach (splist list in lists)

{

Boolean bincludemember = false;

Foreach (sppermission perm in list. permissions)

{

If (Perm. member. ID = user. ID)

{

Bincludemember = true;

Response. write ("in document library" + list. title + "Top, user" + User. name + "permission:" + perm. permissionmask. tostring () + "<br> ");
}

}

If (! Bincludemember)

{

List. permissions. Add (user, sprights. viewlistitems );

}

}

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.