Skip to content

Commit

Permalink
Fix #1882 - issue with Set-PnPListPermission (#1891)
Browse files Browse the repository at this point in the history
  • Loading branch information
gautamdsheth committed May 22, 2022
1 parent 45aa92d commit 93a176b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 43 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Fixed `Get-PnPFile` issue with every 3rd file download in PS 5.
- Fixed `Add-PnPContentTypesFromContentTypeHub`, if `Site` parameter is specified, it will be used now to sync content types from content type hub site.
- Fixed `Get-PnPTeamsTeam`, the cmdlet now also returns additional properties like `WebUrl, CreatedDateTime, InternalId`. [#1825](https://github.com/pnp/powershell/pull/1825)
- Fixed `Set-PnPListPermission`, it will now throw error if the list does not exist. [#1891](https://github.com/pnp/powershell/pull/1891)
- Fixed `Invoke-PnPSPRestMethod` invalid parsing for SharePoint number columns. [#1877](https://github.com/pnp/powershell/pull/1879)


### Removed
- Removed `Get-PnPAvailableClientSideComponents`. Use `Get-PnPPageComponent -Page -ListAvailable` instead. [#1833](https://github.com/pnp/powershell/pull/1833)

Expand Down
88 changes: 45 additions & 43 deletions src/Commands/Lists/SetListPermission.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,60 +31,62 @@ protected override void ExecuteCmdlet()
{
var list = Identity.GetList(CurrentWeb);

if (list != null)
if (list == null)
{
Principal principal = null;
if (ParameterSetName == "Group")
throw new PSArgumentException($"No list found with id, title or url '{Identity}'", "Identity");
}

Principal principal = null;
if (ParameterSetName == "Group")
{
if (Group.Id != -1)
{
if (Group.Id != -1)
{
principal = CurrentWeb.SiteGroups.GetById(Group.Id);
}
else if (!string.IsNullOrEmpty(Group.Name))
{
principal = CurrentWeb.SiteGroups.GetByName(Group.Name);
}
else if (Group.Group != null)
{
principal = Group.Group;
}
principal = CurrentWeb.SiteGroups.GetById(Group.Id);
}
else if (!string.IsNullOrEmpty(Group.Name))
{
principal = CurrentWeb.SiteGroups.GetByName(Group.Name);
}
else
else if (Group.Group != null)
{
principal = CurrentWeb.EnsureUser(User);
principal = Group.Group;
}
}
else
{
principal = CurrentWeb.EnsureUser(User);
ClientContext.ExecuteQueryRetry();
}
if (principal != null)
{
if (!string.IsNullOrEmpty(AddRole))
{
var roleDefinition = CurrentWeb.RoleDefinitions.GetByName(AddRole);
var roleDefinitionBindings = new RoleDefinitionBindingCollection(ClientContext);
roleDefinitionBindings.Add(roleDefinition);
var roleAssignments = list.RoleAssignments;
roleAssignments.Add(principal, roleDefinitionBindings);
ClientContext.Load(roleAssignments);
ClientContext.ExecuteQueryRetry();
}
if (principal != null)
if (!string.IsNullOrEmpty(RemoveRole))
{
if (!string.IsNullOrEmpty(AddRole))
{
var roleDefinition = CurrentWeb.RoleDefinitions.GetByName(AddRole);
var roleDefinitionBindings = new RoleDefinitionBindingCollection(ClientContext);
roleDefinitionBindings.Add(roleDefinition);
var roleAssignments = list.RoleAssignments;
roleAssignments.Add(principal, roleDefinitionBindings);
ClientContext.Load(roleAssignments);
ClientContext.ExecuteQueryRetry();
}
if (!string.IsNullOrEmpty(RemoveRole))
var roleAssignment = list.RoleAssignments.GetByPrincipal(principal);
var roleDefinitionBindings = roleAssignment.RoleDefinitionBindings;
ClientContext.Load(roleDefinitionBindings);
ClientContext.ExecuteQueryRetry();
foreach (var roleDefinition in roleDefinitionBindings.Where(roleDefinition => roleDefinition.Name == RemoveRole))
{
var roleAssignment = list.RoleAssignments.GetByPrincipal(principal);
var roleDefinitionBindings = roleAssignment.RoleDefinitionBindings;
ClientContext.Load(roleDefinitionBindings);
roleDefinitionBindings.Remove(roleDefinition);
roleAssignment.Update();
ClientContext.ExecuteQueryRetry();
foreach (var roleDefinition in roleDefinitionBindings.Where(roleDefinition => roleDefinition.Name == RemoveRole))
{
roleDefinitionBindings.Remove(roleDefinition);
roleAssignment.Update();
ClientContext.ExecuteQueryRetry();
break;
}
break;
}
}
else
{
WriteError(new ErrorRecord(new Exception("Principal not found"), "1", ErrorCategory.ObjectNotFound, null));
}
}
else
{
WriteError(new ErrorRecord(new Exception("Principal not found"), "1", ErrorCategory.ObjectNotFound, null));
}
}
}
Expand Down

0 comments on commit 93a176b

Please sign in to comment.