Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes 'spo folder roleassignment add' command. Closes #6555 #6557

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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> {
martinlingstuyl marked this conversation as resolved.
Show resolved Hide resolved
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
Loading