Skip to content

Commit

Permalink
Adds a new command: 'spo site admin remove'. Closes #5884
Browse files Browse the repository at this point in the history
  • Loading branch information
mkm17 committed Jul 24, 2024
1 parent 630e741 commit 752f3d9
Show file tree
Hide file tree
Showing 7 changed files with 1,162 additions and 30 deletions.
67 changes: 67 additions & 0 deletions docs/docs/cmd/spo/site/site-admin-remove.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import Global from '/docs/cmd/_global.mdx';
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

# spo site admin remove

Removes a user or group as site collection administrator

## Usage

```sh
m365 spo site admin remove [options]
```

## Options

```md definition-list
`-u, --siteUrl <siteUrl>`
: The URL of the SharePoint site

`--userId [userId]`
: The ID of the user to remove as a site collection admin

`--userName [userName]`
: The user principal name of the user to remove as a site collection admin

`--groupId [groupId]`
: The ID of the Microsoft Entra ID group to remove as a site collection admin

`--groupName [groupName]`
: The name of the Microsoft Entra ID group to remove as a site collection admin

`--asAdmin`
: If specified, we will use the SharePoint admin center to execute the command

`-f, --force`
: Don't prompt for confirmation
```

<Global />

## Remarks

:::info

To use this command with the `--asAdmin` mode, you have to have permissions to access the tenant admin site.

Without this parameter, you have to have site collection admin permissions for the requested site.

:::

## Examples

Removes user as site collection admin

```sh
m365 spo site admin remove --siteUrl https://contoso.sharepoint.com --userId 600713c5-53c6-4f24-b454-3c35e22b2639
```

Removes group as site collection admin without prompting for confirmation as SharePoint Admin

```sh
m365 spo site admin remove --siteUrl https://contoso.sharepoint.com --groupName SP_Administrators --force --asAdmin
```
## Response

The command won't return a response on success.
5 changes: 5 additions & 0 deletions docs/src/config/sidebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3297,6 +3297,11 @@ const sidebars: SidebarsConfig = {
label: 'site admin list',
id: 'cmd/spo/site/site-admin-list'
},
{
type: 'doc',
label: 'site admin remove',
id: 'cmd/spo/site/site-admin-remove'
},
{
type: 'doc',
label: 'site ensure',
Expand Down
1 change: 1 addition & 0 deletions src/m365/spo/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ export default {
SET: `${prefix} set`,
SITE_ADD: `${prefix} site add`,
SITE_ADMIN_LIST: `${prefix} site admin list`,
SITE_ADMIN_REMOVE: `${prefix} site admin remove`,
SITE_APPCATALOG_ADD: `${prefix} site appcatalog add`,
SITE_APPCATALOG_LIST: `${prefix} site appcatalog list`,
SITE_APPCATALOG_REMOVE: `${prefix} site appcatalog remove`,
Expand Down
32 changes: 2 additions & 30 deletions src/m365/spo/commands/site/site-admin-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class SpoSiteAdminListCommand extends SpoCommand {

const response: string = await request.post<string>(requestOptions);
const responseContent: AdminResult = JSON.parse(response);
const primaryAdminLoginName = await this.getPrimaryAdminLoginNameFromAdmin(adminUrl, siteId);
const primaryAdminLoginName = await spo.getPrimaryAdminLoginNameAsAdmin(adminUrl, siteId);

const mappedResult = responseContent.value.map((u: AdminUserResult): CommandResultItem => ({
Id: null,
Expand All @@ -153,20 +153,6 @@ class SpoSiteAdminListCommand extends SpoCommand {
return match[1];
}

private async getPrimaryAdminLoginNameFromAdmin(adminUrl: string, siteId: string): Promise<string> {
const requestOptions: CliRequestOptions = {
url: `${adminUrl}/_api/SPO.Tenant/sites('${siteId}')?$select=OwnerLoginName`,
headers: {
accept: 'application/json;odata=nometadata',
'content-type': 'application/json;charset=utf-8'
}
};

const response: string = await request.get<string>(requestOptions);
const responseContent = JSON.parse(response);
return responseContent.OwnerLoginName;
}

private async callAction(logger: Logger, args: CommandArgs): Promise<void> {
if (this.verbose) {
await logger.logToStderr('Retrieving site administrators...');
Expand All @@ -182,7 +168,7 @@ class SpoSiteAdminListCommand extends SpoCommand {
};

const responseContent: SiteResult = await request.get<SiteResult>(requestOptions);
const primaryOwnerLogin = await this.getPrimaryOwnerLoginFromSite(args.options.siteUrl);
const primaryOwnerLogin = await spo.getPrimaryOwnerLoginFromSite(args.options.siteUrl);
const mappedResult = responseContent.value.map((u: SiteUserResult): CommandResultItem => ({
Id: u.Id,
LoginName: u.LoginName,
Expand All @@ -194,20 +180,6 @@ class SpoSiteAdminListCommand extends SpoCommand {
}));
await logger.log(mappedResult);
}

private async getPrimaryOwnerLoginFromSite(siteUrl: string): Promise<string | null> {
const requestOptions: CliRequestOptions = {
url: `${siteUrl}/_api/site/owner`,
method: 'GET',
headers: {
'accept': 'application/json;odata=nometadata'
},
responseType: 'json'
};

const responseContent = await request.get<{ LoginName: string }>(requestOptions);
return responseContent?.LoginName ?? null;
}
}

export default new SpoSiteAdminListCommand();
Loading

0 comments on commit 752f3d9

Please sign in to comment.