Skip to content

Commit

Permalink
Updates 'spo group member add', 'spo group member remove', and 'spo u…
Browse files Browse the repository at this point in the history
…ser ensure' commands options. Closes #5745
  • Loading branch information
SmitaNachan authored and Adam-it committed Jan 25, 2024
1 parent 0ec0d8d commit 220c9e8
Show file tree
Hide file tree
Showing 9 changed files with 358 additions and 49 deletions.
26 changes: 19 additions & 7 deletions docs/docs/cmd/spo/group/group-member-add.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,24 @@ m365 spo group member add [options]
`--userIds [userIds]`
: The user Id of the user to add as a member. (Id of the site user, for example: 14) If multiple users need to be added, the Ids have to be comma-separated. Specify either `userIds`, `userNames`, `emails`, `aadGroupIds` or `aadGroupNames`.

`--entraGroupIds [entraGroupIds]`
: The object Id of the Entra group to add as a member. If multiple groups need to be added, the Ids have to be comma-separated. Specify either `userIds`, `userNames`, `emails`, `aadGroupIds`, `entraGroupIds`, `aadGroupNames`, or `entraGroupNames`.

`--aadGroupIds [aadGroupIds]`
: The object Id of the Azure AD group to add as a member. If multiple groups need to be added, the Ids have to be comma-separated. Specify either `userIds`, `userNames`, `emails`, `aadGroupIds` or `aadGroupNames`.
: (deprecated. Use `entraGroupIds` instead) The object Id of the Azure AD group to add as a member. If multiple groups need to be added, the Ids have to be comma-separated. Specify either `userIds`, `userNames`, `emails`, `aadGroupIds`, `entraGroupIds`, `aadGroupNames`, or `entraGroupNames`.

`--entraGroupNames [entraGroupNames]`
: The name of the Entra group to add as a member. If multiple groups need to be added, they have to be comma-separated. Specify either `userIds`, `userNames`, `emails`, `aadGroupIds`, `entraGroupIds`, `aadGroupNames`, or `entraGroupNames`.

`--aadGroupNames [aadGroupNames]`
: The name of the Azure AD group to add as a member. If multiple groups need to be added, they have to be comma-separated. Specify either `userIds`, `userNames`, `emails`, `aadGroupIds` or `aadGroupNames`.
: (deprecated. Use `entraGroupNames` instead) The name of the Azure AD group to add as a member. If multiple groups need to be added, they have to be comma-separated. Specify either `userIds`, `userNames`, `emails`, `aadGroupIds`, `entraGroupIds`, `aadGroupNames`, or `entraGroupNames`.
```

<Global />

## Remarks

For the `userIds`, `userNames`, `emails`, `aadGroupIds` or `aadGroupNames` options you can specify multiple values by separating them with a comma. If one of the specified entries is not valid, the command will fail with an error message showing the list of invalid values.
For the `userIds`, `userNames`, `emails`, `aadGroupIds`, `entraGroupIds`, `aadGroupNames`, or `entraGroupNames` options you can specify multiple values by separating them with a comma. If one of the specified entries is not valid, the command will fail with an error message showing the list of invalid values.

## Examples

Expand Down Expand Up @@ -84,11 +90,17 @@ Add multiple users with the userIds parameter to a SharePoint group with the gro
m365 spo group member add --webUrl https://contoso.sharepoint.com/sites/SiteA --groupId 5 --userIds "5,12"
```

Add multiple users with the aadGroupNames parameter to a SharePoint group with the groupId parameter
Add multiple users with the entraGroupIds parameter to a SharePoint group with the groupId parameter.

```sh
m365 spo group member add --webUrl https://contoso.sharepoint.com/sites/SiteA --groupId 5 --entraGroupIds "f2fb2f10-cfd2-4054-8ffd-64533657a5ab,3e86049e-89e6-4c27-bccb-d7549f0bbd06"
```

Add multiple users with the entraGroupNames parameter to a SharePoint group with the groupId parameter.

```sh
m365 spo group member add --webUrl https://contoso.sharepoint.com/sites/SiteA --groupId 5 --aadGroupNames "Azure group one, Azure group two"
```
```sh
m365 spo group member add --webUrl https://contoso.sharepoint.com/sites/SiteA --groupId 5 --entraGroupNames "Azure group one, Azure group two"
```

## Response

Expand Down
18 changes: 12 additions & 6 deletions docs/docs/cmd/spo/group/group-member-remove.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,17 @@ m365 spo group member remove [options]
`--userId [userId]`
: The user Id (Id of the site user, eg. 14) of the user to remove as a member. Specify either `userName`, `email`, `userId`, `aadGroupId` or `aadGroupName`.

`--entraGroupId [entraGroupId]`
: The object Id of the Entra group to remove as a member. Specify either `userName`, `email`, `userId`, `aadGroupId`, `entraGroupId`, `aadGroupName`, or `entraGroupName`.

`--aadGroupId [aadGroupId]`
: The object Id of the Azure AD group to remove as a member. Specify either `userName`, `email`, `userId`, `aadGroupId` or `aadGroupName`.
: (deprecated. Use `entraGroupId` instead) The object Id of the Azure AD group to remove as a member. Specify either `userName`, `email`, `userId`, `aadGroupId`, `entraGroupId`, `aadGroupName`, or `entraGroupName`.

`--entraGroupName [entraGroupName]`
: The name of the Entra group to remove as a member. Specify either `userName`, `email`, `userId`, `aadGroupId`, `entraGroupId`, `aadGroupName`, or `entraGroupName`.

`--aadGroupName [aadGroupName]`
: The name of the Azure AD group to remove as a member. Specify either `userName`, `email`, `userId`, `aadGroupId` or `aadGroupName`.
: (deprecated. Use `entraGroupName` instead) The name of the Azure AD group to remove as a member. Specify either `userName`, `email`, `userId`, `aadGroupId`, `entraGroupId`, `aadGroupName`, or `entraGroupName`.
```

<Global />
Expand All @@ -60,16 +66,16 @@ Remove a user from a SharePoint group by email.
m365 spo group member remove --webUrl https://contoso.sharepoint.com/sites/SiteA --groupName "Site A Visitors" --userId 14
```

Remove an Azure AD group from a SharePoint group based on the Azure AD group name on a given web.
Remove an Entra group from a SharePoint group based on the Entra group name on a given web.

```sh
m365 spo group member remove --webUrl https://contoso.sharepoint.com/sites/SiteA --groupId 5 --aadGroupName "Azure AD Security Group"
m365 spo group member remove --webUrl https://contoso.sharepoint.com/sites/SiteA --groupId 5 --entraGroupName "Azure AD Security Group"
```

Remove an Azure AD group from a SharePoint group based on the Azure AD group ID on a given web.
Remove an Entra group from a SharePoint group based on the Entra group ID on a given web.

```sh
m365 spo group member remove --webUrl https://contoso.sharepoint.com/sites/SiteA --groupName "Site A Visitors" --aadGroupId "5786b8e8-c495-4734-b345-756733960730"
m365 spo group member remove --webUrl https://contoso.sharepoint.com/sites/SiteA --groupName "Site A Visitors" --entraGroupId "5786b8e8-c495-4734-b345-756733960730"
```

## Response
Expand Down
13 changes: 8 additions & 5 deletions docs/docs/cmd/spo/user/user-ensure.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,27 @@ m365 spo user ensure [options]
`-u, --webUrl <webUrl>`
: Absolute URL of the site.

`--entraId [--entraId]`
: Id of the user in Entra. Specify either `aadId`, `entraId`, or `userName`.

`--aadId [--aadId]`
: Id of the user in Azure AD. Specify either `aadId` or `userName` but not both.
: (deprecated. Use `entraId` instead) Id of the user in Azure AD. Specify either `aadId`, `entraId`, or `userName`.

`--userName [userName]`
: User's UPN (user principal name, e.g. john@contoso.com). Specify either `aadId` or `userName` but not both.
: User's UPN (user principal name, e.g. john@contoso.com). Specify either `aadId`, `entraId`, or `userName`.
```

<Global />

## Examples

Ensures a user by its Azure AD Id
Ensures a user by its Entra Id.

```sh
m365 spo user ensure --webUrl https://contoso.sharepoint.com/sites/project --aadId e254750a-eaa4-44f6-9517-b74f65cdb747
m365 spo user ensure --webUrl https://contoso.sharepoint.com/sites/project --entraId e254750a-eaa4-44f6-9517-b74f65cdb747
```

Ensures a user by its user principal name
Ensures a user by its user principal name.

```sh
m365 spo user ensure --webUrl https://contoso.sharepoint.com/sites/project --userName john@contoso.com
Expand Down
121 changes: 118 additions & 3 deletions src/m365/spo/commands/group/group-member-add.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,26 @@ describe(commands.GROUP_MEMBER_ADD, () => {
assert.notStrictEqual(actual, true);
});

it('fails validation if both emails and entraGroupIds options are passed', async () => {
sinon.stub(cli, 'getSettingWithDefaultValue').callsFake((settingName, defaultValue) => {
if (settingName === settingsNames.prompt) {
return false;
}

return defaultValue;
});

const actual = await command.validate({
options: {
webUrl: "https://contoso.sharepoint.com/sites/SiteA",
groupId: 32,
emails: "Alex.Wilber@contoso.com",
entraGroupIds: "56ca9023-3449-4e98-a96a-69e81a6f4983"
}
}, commandInfo);
assert.notStrictEqual(actual, true);
});

it('fails validation if both emails and aadGroupIds options are passed', async () => {
sinon.stub(cli, 'getSettingWithDefaultValue').callsFake((settingName, defaultValue) => {
if (settingName === settingsNames.prompt) {
Expand All @@ -209,6 +229,26 @@ describe(commands.GROUP_MEMBER_ADD, () => {
assert.notStrictEqual(actual, true);
});

it('fails validation if both userIds and entraGroupNames options are passed', async () => {
sinon.stub(cli, 'getSettingWithDefaultValue').callsFake((settingName, defaultValue) => {
if (settingName === settingsNames.prompt) {
return false;
}

return defaultValue;
});

const actual = await command.validate({
options: {
webUrl: "https://contoso.sharepoint.com/sites/SiteA",
groupId: 32,
userIds: 5,
entraGroupNames: "Azure AD Group name"
}
}, commandInfo);
assert.notStrictEqual(actual, true);
});

it('fails validation if both userIds and aadGroupNames options are passed', async () => {
sinon.stub(cli, 'getSettingWithDefaultValue').callsFake((settingName, defaultValue) => {
if (settingName === settingsNames.prompt) {
Expand Down Expand Up @@ -249,7 +289,7 @@ describe(commands.GROUP_MEMBER_ADD, () => {
assert.notStrictEqual(actual, true);
});

it('fails validation if userNames, emails, userIds, aadGroupIds or aadGroupNames options are not passed', async () => {
it('fails validation if userNames, emails, userIds, entraGroupIds, aadGroupIds, entraGroupNames, or aadGroupNames options are not passed', async () => {
sinon.stub(cli, 'getSettingWithDefaultValue').callsFake((settingName, defaultValue) => {
if (settingName === settingsNames.prompt) {
return false;
Expand Down Expand Up @@ -292,6 +332,11 @@ describe(commands.GROUP_MEMBER_ADD, () => {
assert.notStrictEqual(actual, true);
});

it('fails validation if entraGroupIds is Invalid', async () => {
const actual = await command.validate({ options: { webUrl: "https://contoso.sharepoint.com/sites/SiteA", groupId: 32, entraGroupIds: "56ca9023-3449-4e98-a96a-69e81a6f4983,9" } }, commandInfo);
assert.notStrictEqual(actual, true);
});

it('fails validation if aadGroupIds is Invalid', async () => {
const actual = await command.validate({ options: { webUrl: "https://contoso.sharepoint.com/sites/SiteA", groupId: 32, aadGroupIds: "56ca9023-3449-4e98-a96a-69e81a6f4983,9" } }, commandInfo);
assert.notStrictEqual(actual, true);
Expand Down Expand Up @@ -431,6 +476,35 @@ describe(commands.GROUP_MEMBER_ADD, () => {
assert(loggerLogSpy.calledWith(jsonSingleUser.UsersAddedToGroup));
});

it('adds user to a SharePoint Group by groupId and entraGroupIds (Debug)', async () => {
sinon.stub(request, 'post').callsFake(async opts => {
if (opts.url === 'https://contoso.sharepoint.com/sites/SiteA/_api/SP.Web.ShareObject' &&
opts.data) {
return jsonSingleUser;
}

throw `Invalid request ${JSON.stringify(opts)}`;
});

sinon.stub(request, 'get').callsFake(async (opts) => {
if (opts.url === `https://contoso.sharepoint.com/sites/SiteA/_api/web/sitegroups/GetById('32')?$select=Id`) {
return groupResponse;
}

throw 'Invalid request';
});

await command.action(logger, {
options: {
debug: true,
webUrl: "https://contoso.sharepoint.com/sites/SiteA",
groupId: 32,
entraGroupIds: "56ca9023-3449-4e98-a96a-69e81a6f4983"
}
});
assert(loggerLogSpy.calledWith(jsonSingleUser.UsersAddedToGroup));
});

it('adds user to a SharePoint Group by groupId and aadGroupIds (Debug)', async () => {
sinon.stub(request, 'post').callsFake(async opts => {
if (opts.url === 'https://contoso.sharepoint.com/sites/SiteA/_api/SP.Web.ShareObject' &&
Expand Down Expand Up @@ -460,6 +534,44 @@ describe(commands.GROUP_MEMBER_ADD, () => {
assert(loggerLogSpy.calledWith(jsonSingleUser.UsersAddedToGroup));
});

it('adds user to a SharePoint Group by groupId and entraGroupNames (Debug)', async () => {
sinon.stub(request, 'post').callsFake(async opts => {
if (opts.url === 'https://contoso.sharepoint.com/sites/SiteA/_api/SP.Web.ShareObject' &&
opts.data) {
return jsonSingleUser;
}

throw `Invalid request ${JSON.stringify(opts)}`;
});

sinon.stub(request, 'get').callsFake(async opts => {
if (opts.url === `https://graph.microsoft.com/v1.0/groups?$filter=displayName eq 'Azure%20AD%20Group%20name'&$select=id`) {
return {
value: [{
id: 'Group name'
}]
};
}

if (opts.url === `https://contoso.sharepoint.com/sites/SiteA/_api/web/sitegroups/GetById('32')?$select=Id`) {
return groupResponse;
}

throw `Invalid request ${JSON.stringify(opts)}`;
});

await command.action(logger, {
options: {
debug: true,
webUrl: "https://contoso.sharepoint.com/sites/SiteA",
groupId: 32,
entraGroupNames: "Azure AD Group name"
}
});

assert(loggerLogSpy.calledWith(jsonSingleUser.UsersAddedToGroup));
});

it('adds user to a SharePoint Group by groupId and aadGroupNames (Debug)', async () => {
sinon.stub(request, 'post').callsFake(async opts => {
if (opts.url === 'https://contoso.sharepoint.com/sites/SiteA/_api/SP.Web.ShareObject' &&
Expand All @@ -469,6 +581,7 @@ describe(commands.GROUP_MEMBER_ADD, () => {

throw `Invalid request ${JSON.stringify(opts)}`;
});

sinon.stub(request, 'get').callsFake(async opts => {
if (opts.url === `https://graph.microsoft.com/v1.0/groups?$filter=displayName eq 'Azure%20AD%20Group%20name'&$select=id`) {
return {
Expand All @@ -484,6 +597,7 @@ describe(commands.GROUP_MEMBER_ADD, () => {

throw `Invalid request ${JSON.stringify(opts)}`;
});

await command.action(logger, {
options: {
debug: true,
Expand All @@ -492,6 +606,7 @@ describe(commands.GROUP_MEMBER_ADD, () => {
aadGroupNames: "Azure AD Group name"
}
});

assert(loggerLogSpy.calledWith(jsonSingleUser.UsersAddedToGroup));
});

Expand Down Expand Up @@ -593,7 +708,7 @@ describe(commands.GROUP_MEMBER_ADD, () => {
options: {
webUrl: "https://contoso.sharepoint.com/sites/SiteA",
groupId: 32,
aadGroupNames: "Azure AD Group name"
entraGroupNames: "Azure AD Group name"
}
}), new CommandError("Resource 'Azure AD Group name' does not exist."));
});
Expand Down Expand Up @@ -629,7 +744,7 @@ describe(commands.GROUP_MEMBER_ADD, () => {

await command.action(logger, {
options: {
debug: true, webUrl: "https://contoso.sharepoint.com/sites/SiteA", groupId: 32, aadGroupNames: "Azure AD Group name"
debug: true, webUrl: "https://contoso.sharepoint.com/sites/SiteA", groupId: 32, entraGroupNames: "Azure AD Group name"
}
});
assert(loggerLogSpy.calledWith(jsonSingleUser.UsersAddedToGroup));
Expand Down
Loading

0 comments on commit 220c9e8

Please sign in to comment.