Moss webpart-non-Anonymous sub-sites in anonymous sites are visible to internal employees

Source: Internet
Author: User

Requirement Description: the whole site can be accessed anonymously. A subsite (professional research) must be visible to internal staff after login, But anonymous access is invisible.

Normal Solution: Set "professional research"'s Anonymous Access to none, that is, anonymous access is not allowed. Add the user group "viewitem" and set the corresponding permissions (read-only ); then, add all employees in the company to this user group to solve the problem.

Reality: there are thousands of employees in the company, including offices all over the country. I feel that there are too many users and it is not easy to add them. I am also worried that I need to do this once if I have other permissions in the future. I am very tired.

Current solution: viewitem is set up for the user group, encoding control, and viewitem user groups added to the "professional research" subsite after users log on

 

 

// Add assemblyinfo. CS

Using system. Security;

[Assembly: allowpartiallytrustedcallers ()]

Using system;
Using system. text;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using Microsoft. SharePoint;
Using Microsoft. Sharepoint. webcontrols;
Using system. Web. UI. htmlcontrols;
Using Microsoft. Sharepoint. Navigation;

Namespace myanonycheck
{
Public class myanonycheck: webpart, idisposable
{
Private spuser user = NULL;
Private string _ webname = string. empty;
Private string _ groupname = "viewitem ";

Protected override void onload (eventargs E)
{
Base. onload (E );
}

Protected override void render (htmltextwriter writer)
{
Ensurechildcontrols ();
Base. Render (writer );
}

Protected override void createchildcontrols ()
{
Init ();
Base. createchildcontrols ();
}

Public override void dispose ()
{
Base. Dispose ();
}

Private void Init ()
{
Try
{
String siteurl = spcontext. Current. Site. url;
String username = string. empty;
If (_ webname = string. Empty)
{
_ Webname = spcontext. Current. Web. Name;
}

User = spcontext. Current. Site. allwebs [_ webname]. currentuser;
Username = user. loginname;
// Process non-Anonymous Logon
If (user! = NULL)
{
// Permission Improvement
Spsecurity. runwithelevatedprivileges (delegate
{
Using (spsite osite = new spsite (siteurl ))
{
Bool flg = false;
Spweb oweb = osite. openweb (_ webname );
Oweb. allowunsafeupdates = true;

// Obtain user groups and users
Spgroup groups = oweb. sitegroups [_ groupname];
For (INT I = 0; I <groups. Users. Count; I ++)
{
If (groups. Users [I]. loginname. Equals (username ))
{
Flg = true;
Break;
}
}

If (! Flg)
{
Groups. adduser (User );
Groups. Update ();
}

}
});
}
}
Catch (exception E)
{
}
}

[Webbrowsable (true), personalizable (true ),
Webdescription ("Name of the website to be accessed"), webdisplayname ("website name. Example: News/web1 [News] ")]
Public String webname
{
Get {return _ webname ;}
Set {_ webname = value ;}
}

[Webbrowsable (true), personalizable (true ),
Webdescription ("user group name"), webdisplayname ("permission user group")]
Public String groupname
{
Get {return _ groupname ;}
Set {_ groupname = value ;}
}
}
}

 

 

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.