MOSS Webpart--匿名站点中的非匿名子站点对内部员工可见
需求描述:整个站点都是可匿名访问的,有个子站点(专业研究)需要对内部员工登陆后才可见,匿名访问不可见。
正常解决:将“专业研究”的匿名访问设置为 无,即不可匿名访问,添加用户组“ViewItem”,设置相应权限(只读);然后将公司内部员工都添加到这个用户组中就解决了
现实问题:公司内部员工有好几千,包括全国各地分所。本人感觉用户太多,不好添加;也担心日后有其他权限需求的也需要这样搞一次,很累。
当前解决:建好用户组 ViewItem,编码控制,有用户登陆就添加到“专业研究”子站点的ViewItem用户组
//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;
//非匿名登录做处理
if (user != null)
{
//权限提升
SPSecurity.RunWithElevatedPrivileges(delegate
{
using (SPSite oSite = new SPSite(siteUrl))
{
bool flg = false;
SPWeb oWeb = oSite.OpenWeb(_webName);
oWeb.AllowUnsafeUpdates = true;
//获取用户组及用户
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("需要访问的网站的名称"), WebDisplayName("网站名称。例:News/Web1 [新闻]")]
public string WebName
{
get { return _webName; }
set { _webName = value; }
}
[WebBrowsable(true), Personalizable(true),
WebDescription("用户组名称"), WebDisplayName("权限用户组")]
public string GroupName
{
get { return _groupName; }
set { _groupName = value; }
}
}
}
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;
//非匿名登录做处理
if (user != null)
{
//权限提升
SPSecurity.RunWithElevatedPrivileges(delegate
{
using (SPSite oSite = new SPSite(siteUrl))
{
bool flg = false;
SPWeb oWeb = oSite.OpenWeb(_webName);
oWeb.AllowUnsafeUpdates = true;
//获取用户组及用户
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("需要访问的网站的名称"), WebDisplayName("网站名称。例:News/Web1 [新闻]")]
public string WebName
{
get { return _webName; }
set { _webName = value; }
}
[WebBrowsable(true), Personalizable(true),
WebDescription("用户组名称"), WebDisplayName("权限用户组")]
public string GroupName
{
get { return _groupName; }
set { _groupName = value; }
}
}
}