MOSS/SharePoint control view page access permission development issues (code method)

Source: Internet
Author: User

In the development of access permissions on the MOSS/SharePoint control view page, these features are difficult to solve through the existing features of SharePoint. I will introduce the solution below.

First:

Create a listviewpermissionconfig list to store the configuration information of the view permission.

List bar: listname (single-line text), viewname (single-line text), viewguid (single-line text), and permission (people or user groups, multiple options are allowed ).

 

Next:

Custom webpart is added to the page for permission control. If the current user does not belong to the specified group, the user is not allowed to access the current page.

First, implement a public method of the two methods, and check the permissions.

For the sake of simplicity, only view permissions are implemented here. If you want to implement page permissions, read this article.Article, Slightly modifyCodeThat's OK.

Code

/// / <Summary>
/// Can the user view the current figure?
///   </Summary>
///   <Param name = "viewid"> </param>
///   <Param name = "userorgroup"> </param>
///   <Returns> </returns>
Private   Bool Canseeview (guid viewid, spuser currentuser)
{
Bool B =   False ;
Using (Spsite site =   New Spsite ( " Http: // liust-server: 100 " ))
{
Using (Spweb icmweb = Site. allwebs [ " Itsimportancecustomermanagement " ])
{
Splist listviewconfig = Icmweb. Lists [ " Listviewpermissionconfig " ];
String Strquery =   String . Format ( " <Where> <EQ> <fieldref name = 'viewgu'/> <value type = 'text'> {0} </value> </EQ> </where> " , Viewid. tostring ());
Spquery =   New Spquery ();
Query. Query = Strquery;

Splistitemcollection items = Listviewconfig. getitems (query );
If (Items. Count >   0 )
{
Foreach (Splistitem item In Items)
{
Object OBJ = Item [ " Permission " ];
Spfielduservaluecollection uservalues = (Spfielduservaluecollection) OBJ;
Foreach (Spfielduservalue Value In Uservalues)
{
// If value. user is not empty, it indicates that the current value is a user, and the opposite is a group. If (value. User! = NULL)
{
If (Currentuser. loginname. Equals (value. User. loginname ))
Return   True ;
}
Else
{
// Determine whether the current user is in the user group
Spgroup Group = Icmweb. Groups. getbyid (value. lookupid );
Foreach (Spuser user In Group. Users)
{
If (Currentuser. loginname. Equals (user. loginname ))
Return   True ;}
}
}
}
}
}
}
Return   False ;
}

 

Then, define a control and reload the method.

Protected   Override   Void Onload (eventargs E)
{
Whether you have the permission to view canseeview
If you do not have permission
Sputility. transfertoerrorpage ( " You are not authorized to access this view. " );
}

 

In this way, we need to consider how to protect our list from malicious changes. For details, seeMOSS/SharePoint use code to set item-level permissions for Sharepoint/Moss

You can also implement more fine-grained permissions. For details, seeMOSS/SharePoint column-level security and column-level Permissions

The following is a simpler method to implement webpart, so you do not need to use the list. Of course, the disadvantage is also obvious.

Code

Using System;
Using System. Collections. Generic;
Using System. collections;
Using System. componentmodel;
Using System. text;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. webcontrols;
Using System. Web. UI. webcontrols. webparts;
Using System. Web. UI. htmlcontrols;

UsingMicrosoft. SharePoint;
UsingSystem. Collections. Specialized;
UsingMicrosoft. Sharepoint. utilities;

NamespaceUsing office. webparts
{

Public   Class Rightcontrolpart: webpart
{
Private   String _ Groups =   "" ;
[Webbrowsable]
[Webdescription ( " The group to be accessed. Interval " )]
[Personalizable (personalizationscope. Shared)]
Public   String Groups
{
Get { Return _ Groups ;}
Set {_ Groups = Value ;}
}

Protected Override VoidOnload (eventargs E)
{
Base. Onload (E );

This. Title= "";

Base. Chrometype=Partchrometype. None;

Spuser user=Spcontext. Current. Web. currentuser;

// administrator access
If (user. issiteadmin && string. isnullorempty ( This . groups)
return ;

// check whether the user belongs to the configured group
string [] arr = This . _ groups. split ( ' ; ' );

stringcollection usergroups = New stringcollection ();
foreach (spgroup G in User. groups)
{< br> usergroups. add (G. name. tolower ();
}

foreach ( string G in ARR)
{< br> If (usergroups. contains (G. tolower ()
return ;
}

Sputility. transfertoerrorpage ("You are not authorized to access this view.");
}
}
}

An applicable scenario of this method:
A list has two views:
View 1 (default view): Use a user field = [filter by myself]. Common users can view their own data.
View 2: show all records. On the View 2 page, place this permission control webpart and set only one group to be accessible.

 OthersSeeMOSS/SharePoint sets a specific view and visible view for a specific user group, creates a page for a custom list, modifies the page, and displays the page (no code)

 

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.