在Form认证的网站下,我们要判断我们添加的权限是否存在.可以用下面的方法:

 1
 2protected bool GetPermissions_Groups(string permissionsName)
 3        {
 4            try 
 5            {
 6               string username = Context.User.Identity.Name;
 7               PermissionsMoss.Permissions permService = new PermissionsMoss.Permissions();
 8               permService.Url = SPControl.GetContextWeb(Context).Url + "/_vti_bin/Permissions.asmx";
                    
 9               System.Net.Cookie authCookie = GenerateFormsAuthenticationCookie.GetCookie(ConfigurationManager.AppSettings["username"], ConfigurationManager.AppSettings["userpass"]);
10               permService.CookieContainer = new CookieContainer();
11               permService.CookieContainer.Add(authCookie);
12               XmlNode ndPermissions = permService.GetPermissionCollection(SPControl.GetContextWeb(Context).Url, "Web");
13
14               XmlNodeList xnl = ndPermissions.ChildNodes;
15
16               if (xnl == nullreturn false;
17               foreach (XmlNode xn in xnl)
18               {
19                   if (xn != null)
20                   {
21                       XmlNodeList xnl_permission = xn.ChildNodes;
22
23                       foreach (XmlNode xn_permission in xnl_permission)
24                       {
25                           if (xnl_permission != null)
26                           {
27                               if (xn_permission.Attributes["MemberIsUser"].Value != "True")
28                               {
29                                   if (xn_permission.Attributes["GroupName"].Value == permissionsName)
30                                   {
31                                       return true;
32                                   }

33                               }

34
35                           }

36                       }

37
38                   }

39                 
40               }

41               return false;
42            }

43            catch(Exception ex)
44            {
45                return false;
46            }

47          
48
49        }

50

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using GetPermissionsAndGroups.FormsLogin;
// FormsLogin是添加的Form 模拟登陆引用的asmx的文件夹名字
//GetPermissionsAndGroups是这个webpart的命名空间
/// <summary>
/// Summary description for GenerateFormsAuthenticationCookie
/// </summary>

public class GenerateFormsAuthenticationCookie
{
    
protected GenerateFormsAuthenticationCookie()
    
{
        
//
        
// TODO: Add constructor logic here
        
//
    }

    
public static Cookie GetCookie(string UserName, string Password)
    
{
        Authentication auth 
= new Authentication();
        auth.CookieContainer 
= new CookieContainer();
        LoginResult result 
= auth.Login(UserName, Password);
        
if (result.ErrorCode == LoginErrorCode.NoError)
        
{
            CookieCollection cookies 
= auth.CookieContainer.GetCookies(new Uri(auth.Url));
            Cookie authCookie 
= cookies[result.CookieName];
            
return authCookie;
        }

        
throw new Exception("403 Forbidden");
    }

}





当然可以不用Webservies 直接用代码:

添加一个组及其给一个组权限并给组添加用户:
  protected bool AddPermissions()
        
{
            
try
            
{
                SPSite sps 
= SPControl.GetContextSite(Context);
                SPWeb spw 
= sps.RootWeb;
                
for (int i = 0; i <= spw.RoleDefinitions.Count - 1; i++)
                
{
                    
if (spw.RoleDefinitions[i].Name == "Test_Example_last")
                    
{
                        
return false;
                    }

                }

                
string permissionLevelName = "Test_Example_last";
                
                SPRoleDefinition newPermissionlevel 
= new SPRoleDefinition();
                newPermissionlevel.Name 
= permissionLevelName;
                newPermissionlevel.Description 
= "Example Permission Level";
                newPermissionlevel.BasePermissions 
= SPBasePermissions.AddListItems| SPBasePermissions.EditListItems |
                    SPBasePermissions.ViewPages
|
                    SPBasePermissions.ViewVersions 
| SPBasePermissions.OpenItems | 
                    SPBasePermissions.ViewUsageData 
| SPBasePermissions.Open|
                    SPBasePermissions.BrowseUserInfo 
| SPBasePermissions.EditMyUserInfo |
                    SPBasePermissions.ManagePersonalViews ;
                
//add the permission level to web
                spw.AllowUnsafeUpdates = true;
                
                spw.RoleDefinitions.Add(newPermissionlevel);
                
// Bind to the permission level we just added
                newPermissionlevel = spw.RoleDefinitions[permissionLevelName];

              
                
//Create a new group
                SPGroupCollection collGroups = spw.SiteGroups;
                
for (int i = 0; i <= spw.SiteGroups.Count - 1; i++)
                
{
                    
if (collGroups[i].Name== "Test_Example_last")
                    
{
                        
return false;
                    }

                }


                SPMember oMember 
= spw.SiteUsers["Uers_name"];
                SPUser oUeser 
= spw.SiteUsers["User_name"];
                collGroups.Add(
"Test_Example_last", oMember, oUeser, "this is group is example");
                spw.SiteGroups[
"Test_Example_last"].AddUser(spw.SiteUsers["User_name"]);
                
                
// Create a new role Assignment using the SharePoint Group "Test_Example"
                SPRoleAssignment roleAssignment = new SPRoleAssignment((SPPrincipal)spw.SiteGroups["Test_Example_last"]);

                
// Add the Permission Level to the Test_Example SharePoint Group
                roleAssignment.RoleDefinitionBindings.Add(newPermissionlevel);
                
// Add the new Role Assignment to the web
                spw.RoleAssignments.Add(roleAssignment);
                
return true;
            }

            
catch (Exception ex)
            
{
                
return false;
            }


        }
posted on 2008-01-17 16:48  ◎寶☆呗  阅读(549)  评论(0)    收藏  举报