forked from pnp/powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetWebPermission.cs
45 lines (38 loc) · 1.45 KB
/
GetWebPermission.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System.Linq;
using System.Management.Automation;
using Microsoft.SharePoint.Client;
using PnP.Core.Model.Security;
using PnP.PowerShell.Commands.Base.PipeBinds;
namespace PnP.PowerShell.Commands.Principals
{
[Cmdlet(VerbsCommon.Get, "PnPWebPermission")]
[OutputType(typeof(IRoleDefinition))]
public class GetWebPermission : PnPWebCmdlet
{
[Parameter(Mandatory = false, Position = 0, ValueFromPipeline = true, ParameterSetName = "ByName")]
public WebPipeBind Identity;
[Parameter(Mandatory = true)]
public int PrincipalId;
protected override void ExecuteCmdlet()
{
Web web = CurrentWeb;
if (ParameterSpecified(nameof(Identity)))
{
web = Identity.GetWeb(ClientContext);
}
var roleAssignments = web.RoleAssignments;
web.Context.Load(roleAssignments);
web.Context.ExecuteQueryRetry();
RoleAssignment roleAssignment = roleAssignments.FirstOrDefault((RoleAssignment ra) => ra.PrincipalId.Equals(PrincipalId));
if (roleAssignment != null)
{
web.Context.Load(roleAssignment.RoleDefinitionBindings);
web.Context.ExecuteQueryRetry();
if (roleAssignment.RoleDefinitionBindings.Count > 0)
{
WriteObject(roleAssignment.RoleDefinitionBindings, true);
}
}
}
}
}