Skip to content

Commit

Permalink
Fixes 'spo folder roleassignment add' command. Closes #6555
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam-it authored and martinlingstuyl committed Jan 9, 2025
1 parent 5de7e27 commit ea5d644
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 53 deletions.
38 changes: 1 addition & 37 deletions src/m365/spo/commands/folder/folder-roleassignment-add.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,7 @@ describe(commands.FOLDER_ROLEASSIGNMENT_ADD, () => {
});

it('add the role assignment to the specified folder based on the upn and role definition id', async () => {
sinon.stub(request, 'post').callsFake(async (opts) => {
if (opts.url === 'https://contoso.sharepoint.com/_api/web/GetFolderByServerRelativePath(DecodedUrl=\'%2FShared%20Documents%2FFolderPermission\')/ListItemAllFields/breakroleinheritance(true)') {
return;
}

sinon.stub(request, 'post').callsFake(async (opts) => {
if (opts.url === 'https://contoso.sharepoint.com/_api/web/GetFolderByServerRelativePath(DecodedUrl=\'%2FShared%20Documents%2FFolderPermission\')/ListItemAllFields/roleassignments/addroleassignment(principalid=\'11\',roledefid=\'1073741827\')') {
return;
}
Expand Down Expand Up @@ -238,10 +234,6 @@ describe(commands.FOLDER_ROLEASSIGNMENT_ADD, () => {

it('add the role assignment to the specified root folder based on the principal id and role definition id', async () => {
sinon.stub(request, 'post').callsFake(async (opts) => {
if (opts.url === 'https://contoso.sharepoint.com/_api/web/GetList(\'%2FShared%20Documents\')/breakroleinheritance(true)') {
return;
}

if (opts.url === 'https://contoso.sharepoint.com/_api/web/GetList(\'%2FShared%20Documents\')/roleassignments/addroleassignment(principalid=\'11\',roledefid=\'1073741827\')') {
return;
}
Expand Down Expand Up @@ -269,10 +261,6 @@ describe(commands.FOLDER_ROLEASSIGNMENT_ADD, () => {

it('correctly handles error when upn does not exist', async () => {
sinon.stub(request, 'post').callsFake(async (opts) => {
if (opts.url === 'https://contoso.sharepoint.com/_api/web/GetFolderByServerRelativePath(DecodedUrl=\'%2FShared%20Documents\')/ListItemAllFields/breakroleinheritance(true)') {
return;
}

if (opts.url === 'https://contoso.sharepoint.com/_api/web/GetFolderByServerRelativePath(DecodedUrl=\'%2FShared%20Documents%2FFolderPermission\')/ListItemAllFields/roleassignments/addroleassignment(principalid=\'11\',roledefid=\'1073741827\')') {
return;
}
Expand Down Expand Up @@ -306,10 +294,6 @@ describe(commands.FOLDER_ROLEASSIGNMENT_ADD, () => {

it('add the role assignment to the specified folder based on the group name and role definition id', async () => {
sinon.stub(request, 'post').callsFake(async (opts) => {
if (opts.url === 'https://contoso.sharepoint.com/_api/web/GetFolderByServerRelativePath(DecodedUrl=\'%2FShared%20Documents%2FFolderPermission\')/ListItemAllFields/breakroleinheritance(true)') {
return;
}

if (opts.url === 'https://contoso.sharepoint.com/_api/web/GetFolderByServerRelativePath(DecodedUrl=\'%2FShared%20Documents%2FFolderPermission\')/ListItemAllFields/roleassignments/addroleassignment(principalid=\'11\',roledefid=\'1073741827\')') {
return;
}
Expand Down Expand Up @@ -345,10 +329,6 @@ describe(commands.FOLDER_ROLEASSIGNMENT_ADD, () => {

it('correctly handles error when group does not exist', async () => {
sinon.stub(request, 'post').callsFake(async (opts) => {
if (opts.url === 'https://contoso.sharepoint.com/_api/web/GetFolderByServerRelativePath(DecodedUrl=\'%2FShared%20Documents%2FFolderPermission\')/ListItemAllFields/breakroleinheritance(true)') {
return;
}

if (opts.url === 'https://contoso.sharepoint.com/_api/web/GetFolderByServerRelativePath(DecodedUrl=\'%2FShared%20Documents%2FFolderPermission\')/ListItemAllFields/roleassignments/addroleassignment(principalid=\'11\',roledefid=\'1073741827\')') {
return;
}
Expand Down Expand Up @@ -382,10 +362,6 @@ describe(commands.FOLDER_ROLEASSIGNMENT_ADD, () => {

it('add the role assignment to the specified folder based on the principal id and role definition name', async () => {
sinon.stub(request, 'post').callsFake(async (opts) => {
if (opts.url === 'https://contoso.sharepoint.com/_api/web/GetFolderByServerRelativePath(DecodedUrl=\'%2FShared%20Documents%2FFolderPermission\')/ListItemAllFields/breakroleinheritance(true)') {
return;
}

if (opts.url === 'https://contoso.sharepoint.com/_api/web/GetFolderByServerRelativePath(DecodedUrl=\'%2FShared%20Documents%2FFolderPermission\')/ListItemAllFields/roleassignments/addroleassignment(principalid=\'11\',roledefid=\'1073741827\')') {
return;
}
Expand Down Expand Up @@ -421,10 +397,6 @@ describe(commands.FOLDER_ROLEASSIGNMENT_ADD, () => {

it('correctly handles error when role definition does not exist', async () => {
sinon.stub(request, 'post').callsFake(async (opts) => {
if (opts.url === 'https://contoso.sharepoint.com/_api/web/GetFolderByServerRelativePath(DecodedUrl=\'%2FShared%20Documents%2FFolderPermission\')/ListItemAllFields/breakroleinheritance(true)') {
return;
}

if (opts.url === 'https://contoso.sharepoint.com/_api/web/GetFolderByServerRelativePath(DecodedUrl=\'%2FShared%20Documents%2FFolderPermission\')/ListItemAllFields/roleassignments/addroleassignment(principalid=\'11\',roledefid=\'1073741827\')') {
return;
}
Expand Down Expand Up @@ -462,10 +434,6 @@ describe(commands.FOLDER_ROLEASSIGNMENT_ADD, () => {
sinon.stub(spo, 'ensureEntraGroup').withArgs('https://contoso.sharepoint.com', graphGroup).resolves(entraGroupResponse);

sinon.stub(request, 'post').callsFake(async (opts) => {
if (opts.url === 'https://contoso.sharepoint.com/_api/web/GetList(\'%2FShared%20Documents\')/breakroleinheritance(true)') {
return;
}

if (opts.url === 'https://contoso.sharepoint.com/_api/web/GetList(\'%2FShared%20Documents\')/roleassignments/addroleassignment(principalid=\'11\',roledefid=\'1073741827\')') {
return;
}
Expand Down Expand Up @@ -496,10 +464,6 @@ describe(commands.FOLDER_ROLEASSIGNMENT_ADD, () => {
sinon.stub(spo, 'ensureEntraGroup').withArgs('https://contoso.sharepoint.com', graphGroup).resolves(entraGroupResponse);

sinon.stub(request, 'post').callsFake(async (opts) => {
if (opts.url === 'https://contoso.sharepoint.com/_api/web/GetList(\'%2FShared%20Documents\')/breakroleinheritance(true)') {
return;
}

if (opts.url === 'https://contoso.sharepoint.com/_api/web/GetList(\'%2FShared%20Documents\')/roleassignments/addroleassignment(principalid=\'11\',roledefid=\'1073741827\')') {
return;
}
Expand Down
17 changes: 1 addition & 16 deletions src/m365/spo/commands/folder/folder-roleassignment-add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,29 +180,14 @@ class SpoFolderRoleAssignmentAddCommand extends SpoCommand {
const siteUser = await spo.ensureEntraGroup(args.options.webUrl, group);
principalId = siteUser.Id;
}

await this.breakRoleAssignment(requestUrl);

await this.addRoleAssignment(requestUrl, principalId!, roleDefinitionId);
}
catch (err: any) {
this.handleRejectedODataJsonPromise(err);
}
}

private async breakRoleAssignment(requestUrl: string): Promise<void> {
const requestOptions: CliRequestOptions = {
url: `${requestUrl}/breakroleinheritance(true)`,
method: 'POST',
headers: {
'accept': 'application/json;odata=nometadata',
'content-type': 'application/json'
},
responseType: 'json'
};

return request.post(requestOptions);
}

private async addRoleAssignment(requestUrl: string, principalId: number, roleDefinitionId: number): Promise<void> {
const requestOptions: CliRequestOptions = {
url: `${requestUrl}/roleassignments/addroleassignment(principalid='${principalId}',roledefid='${roleDefinitionId}')`,
Expand Down

0 comments on commit ea5d644

Please sign in to comment.