React Check User Group membership using Graph API (#1153)
* Updated react-page-navigator links sort order * React Check User Group membership using Graph API Co-authored-by: Aakash Bhardwaj <aakash.bhardwaj@consultant.lego.com>
This commit is contained in:
parent
76ea322aac
commit
cb9a61ba00
|
@ -0,0 +1,25 @@
|
|||
# EditorConfig helps developers define and maintain consistent
|
||||
# coding styles between different editors and IDEs
|
||||
# editorconfig.org
|
||||
|
||||
root = true
|
||||
|
||||
|
||||
[*]
|
||||
|
||||
# change these settings to your own preference
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
|
||||
# we recommend you to keep these unchanged
|
||||
end_of_line = lf
|
||||
charset = utf-8
|
||||
trim_trailing_whitespace = true
|
||||
insert_final_newline = true
|
||||
|
||||
[*.md]
|
||||
trim_trailing_whitespace = false
|
||||
|
||||
[{package,bower}.json]
|
||||
indent_style = space
|
||||
indent_size = 2
|
|
@ -0,0 +1,32 @@
|
|||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
|
||||
# Dependency directories
|
||||
node_modules
|
||||
|
||||
# Build generated files
|
||||
dist
|
||||
lib
|
||||
solution
|
||||
temp
|
||||
*.sppkg
|
||||
|
||||
# Coverage directory used by tools like istanbul
|
||||
coverage
|
||||
|
||||
# OSX
|
||||
.DS_Store
|
||||
|
||||
# Visual Studio files
|
||||
.ntvs_analysis.dat
|
||||
.vs
|
||||
bin
|
||||
obj
|
||||
|
||||
# Resx Generated Code
|
||||
*.resx.ts
|
||||
|
||||
# Styles Generated Code
|
||||
*.scss.ts
|
|
@ -0,0 +1,12 @@
|
|||
{
|
||||
"@microsoft/generator-sharepoint": {
|
||||
"isCreatingSolution": true,
|
||||
"environment": "spo",
|
||||
"version": "1.10.0",
|
||||
"libraryName": "react-check-user-group",
|
||||
"libraryId": "0a9e21ff-70b4-4cf6-82ea-68372e7c52a7",
|
||||
"packageManager": "npm",
|
||||
"isDomainIsolated": false,
|
||||
"componentType": "webpart"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
# Check User Group
|
||||
|
||||
## Summary
|
||||
|
||||
This web part finds all the Office 365 or AAD Security groups a user is a member of or all the members present in such a group. It can be used as an admin utility to quickly check the membership of any user or group from within a SharePoint page itself. The retrieved results can also be exported to a CSV file.
|
||||
|
||||
![Check User Group](./assets/CheckUserGroup.gif)
|
||||
|
||||
## Used SharePoint Framework Version
|
||||
|
||||
![version](https://img.shields.io/badge/version-1.10.0-green.svg)
|
||||
|
||||
## Features
|
||||
|
||||
This web part uses Microsoft Graph API to get all the Office 365 or AAD Security Groups a user is a member of or all the members in such a group.
|
||||
|
||||
This extension illustrates the following concepts:
|
||||
|
||||
* Requesting **Directory.Read.All** permission scope for Microsoft Graph through the webApiPermissionRequests property in package-solution.json
|
||||
* Using MSGraphClient to call the **/groups/{groupId}/members** API to get all the members in a group
|
||||
* Using MSGraphClient to call the **/users/${email}/memberOf** API to get all the groups a user is member of
|
||||
* Exporting the results to a CSV file using [**react-csv**](https://www.npmjs.com/package/react-csv) third party package
|
||||
|
||||
## Solution
|
||||
|
||||
Solution|Author(s)
|
||||
--------|---------
|
||||
react-check-user-group | [Aakash Bhardwaj](https://twitter.com/aakash_316)
|
||||
|
||||
## Minimal Path to Awesome
|
||||
|
||||
* Clone this repository
|
||||
* `npm install`
|
||||
* `gulp bundle --ship`
|
||||
* `gulp package-solution --ship`
|
||||
* Add to Site Collection App Catalog and Install the App
|
||||
* Go to the API Management section in the new SharePoint Admin Center (*https://{tenantname}-admin.sharepoint.com/_layouts/15/online/AdminHome.aspx#/webApiPermissionManagement*)
|
||||
* Approve the permission request for Directory.Read.All to Microsoft Graph
|
||||
|
||||
## Disclaimer
|
||||
|
||||
**THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.**
|
||||
|
||||
---
|
Binary file not shown.
After Width: | Height: | Size: 1.6 MiB |
|
@ -0,0 +1,19 @@
|
|||
{
|
||||
"$schema": "https://developer.microsoft.com/json-schemas/spfx-build/config.2.0.schema.json",
|
||||
"version": "2.0",
|
||||
"bundles": {
|
||||
"check-user-group-web-part": {
|
||||
"components": [
|
||||
{
|
||||
"entrypoint": "./lib/webparts/checkUserGroup/CheckUserGroupWebPart.js",
|
||||
"manifest": "./src/webparts/checkUserGroup/CheckUserGroupWebPart.manifest.json"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"externals": {},
|
||||
"localizedResources": {
|
||||
"CheckUserGroupWebPartStrings": "lib/webparts/checkUserGroup/loc/{locale}.js",
|
||||
"ControlStrings": "node_modules/@pnp/spfx-controls-react/lib/loc/{locale}.js"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"$schema": "https://developer.microsoft.com/json-schemas/spfx-build/copy-assets.schema.json",
|
||||
"deployCdnPath": "temp/deploy"
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"$schema": "https://developer.microsoft.com/json-schemas/spfx-build/deploy-azure-storage.schema.json",
|
||||
"workingDir": "./temp/deploy/",
|
||||
"account": "<!-- STORAGE ACCOUNT NAME -->",
|
||||
"container": "react-check-user-group",
|
||||
"accessKey": "<!-- ACCESS KEY -->"
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
{
|
||||
"$schema": "https://developer.microsoft.com/json-schemas/spfx-build/package-solution.schema.json",
|
||||
"solution": {
|
||||
"name": "react-check-user-group",
|
||||
"id": "0a9e21ff-70b4-4cf6-82ea-68372e7c52a7",
|
||||
"version": "1.0.0.0",
|
||||
"includeClientSideAssets": true,
|
||||
"isDomainIsolated": false,
|
||||
"webApiPermissionRequests": [
|
||||
{
|
||||
"resource": "Microsoft Graph",
|
||||
"scope": "Directory.Read.All"
|
||||
}
|
||||
]
|
||||
},
|
||||
"paths": {
|
||||
"zippedPackage": "solution/react-check-user-group.sppkg"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
{
|
||||
"$schema": "https://developer.microsoft.com/json-schemas/core-build/serve.schema.json",
|
||||
"port": 4321,
|
||||
"https": true,
|
||||
"initialPage": "https://localhost:5432/workbench",
|
||||
"api": {
|
||||
"port": 5432,
|
||||
"entryPath": "node_modules/@microsoft/sp-webpart-workbench/lib/api/"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"$schema": "https://developer.microsoft.com/json-schemas/spfx-build/write-manifests.schema.json",
|
||||
"cdnBasePath": "<!-- PATH TO CDN -->"
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
'use strict';
|
||||
|
||||
const build = require('@microsoft/sp-build-web');
|
||||
|
||||
build.addSuppression(`Warning - [sass] The local CSS class 'ms-Grid' is not camelCase and will not be type-safe.`);
|
||||
|
||||
build.initialize(require('gulp'));
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,44 @@
|
|||
{
|
||||
"name": "react-check-user-group",
|
||||
"version": "0.0.1",
|
||||
"private": true,
|
||||
"main": "lib/index.js",
|
||||
"engines": {
|
||||
"node": ">=0.10.0"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "gulp bundle",
|
||||
"clean": "gulp clean",
|
||||
"test": "gulp test"
|
||||
},
|
||||
"dependencies": {
|
||||
"@microsoft/sp-core-library": "1.10.0",
|
||||
"@microsoft/sp-lodash-subset": "1.10.0",
|
||||
"@microsoft/sp-office-ui-fabric-core": "1.10.0",
|
||||
"@microsoft/sp-property-pane": "1.10.0",
|
||||
"@microsoft/sp-webpart-base": "1.10.0",
|
||||
"@pnp/spfx-controls-react": "^1.16.0",
|
||||
"@types/es6-promise": "0.0.33",
|
||||
"@types/react": "16.8.8",
|
||||
"@types/react-dom": "16.8.3",
|
||||
"@types/webpack-env": "1.13.1",
|
||||
"office-ui-fabric-react": "6.189.2",
|
||||
"react": "16.8.5",
|
||||
"react-csv": "^2.0.1",
|
||||
"react-dom": "16.8.5"
|
||||
},
|
||||
"resolutions": {
|
||||
"@types/react": "16.8.8"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@microsoft/sp-build-web": "1.10.0",
|
||||
"@microsoft/sp-tslint-rules": "1.10.0",
|
||||
"@microsoft/sp-module-interfaces": "1.10.0",
|
||||
"@microsoft/sp-webpart-workbench": "1.10.0",
|
||||
"@microsoft/rush-stack-compiler-3.3": "0.3.5",
|
||||
"gulp": "~3.9.1",
|
||||
"@types/chai": "3.4.34",
|
||||
"@types/mocha": "2.2.38",
|
||||
"ajv": "~5.2.2"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
export interface IGroupItem {
|
||||
name: string;
|
||||
description: string;
|
||||
groupTypes: string;
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
export interface IUserItem {
|
||||
displayName: string;
|
||||
mail: string;
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
import { WebPartContext } from '@microsoft/sp-webpart-base';
|
||||
import { MSGraphClient } from '@microsoft/sp-http';
|
||||
|
||||
export class MSGraphService {
|
||||
/**
|
||||
* Gets all the groups the selected user is part of using MS Graph API
|
||||
* @param context Web part context
|
||||
* @param email Email ID of the selected user
|
||||
*/
|
||||
public static async GetUserGroups(context: WebPartContext, email: string): Promise<any[]> {
|
||||
let groups: string[] = [];
|
||||
try {
|
||||
let client: MSGraphClient = await context.msGraphClientFactory.getClient().then();
|
||||
let response = await client
|
||||
.api(`/users/${email}/memberOf`)
|
||||
.version('v1.0')
|
||||
.select(['groupTypes', 'displayName', 'mailEnabled', 'securityEnabled', 'description'])
|
||||
.get();
|
||||
response.value.map((item: any) => {
|
||||
groups.push(item);
|
||||
});
|
||||
} catch (error) {
|
||||
console.log('MSGraphService.GetUserGroups Error: ', error);
|
||||
}
|
||||
console.log('MSGraphService.GetUserGroups: ', groups);
|
||||
return groups;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all the members in the selected group using MS Graph API
|
||||
* @param context Web part context
|
||||
* @param groupId Group ID of the selected group
|
||||
*/
|
||||
public static async GetGroupMembers(context: WebPartContext, groupId: string): Promise<any[]> {
|
||||
let users: string[] = [];
|
||||
try {
|
||||
let client: MSGraphClient = await context.msGraphClientFactory.getClient().then();
|
||||
let response = await client
|
||||
.api(`/groups/${groupId}/members`)
|
||||
.version('v1.0')
|
||||
.select(['mail', 'displayName'])
|
||||
.get();
|
||||
response.value.map((item: any) => {
|
||||
users.push(item);
|
||||
});
|
||||
} catch (error) {
|
||||
console.log('MSGraphService.GetGroupMembers Error: ', error);
|
||||
}
|
||||
console.log('MSGraphService.GetGroupMembers: ', users);
|
||||
return users;
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
// A file is required to be in the root of the /src directory by the TypeScript compiler
|
|
@ -0,0 +1,27 @@
|
|||
{
|
||||
"$schema": "https://developer.microsoft.com/json-schemas/spfx/client-side-web-part-manifest.schema.json",
|
||||
"id": "579542cc-c76f-4b8d-8219-6a1f852c1e4d",
|
||||
"alias": "CheckUserGroupWebPart",
|
||||
"componentType": "WebPart",
|
||||
|
||||
// The "*" signifies that the version should be taken from the package.json
|
||||
"version": "*",
|
||||
"manifestVersion": 2,
|
||||
|
||||
// If true, the component can only be installed on sites where Custom Script is allowed.
|
||||
// Components that allow authors to embed arbitrary script code should set this to true.
|
||||
// https://support.office.com/en-us/article/Turn-scripting-capabilities-on-or-off-1f2c515f-5d7e-448a-9fd7-835da935584f
|
||||
"requiresCustomScript": false,
|
||||
"supportedHosts": ["SharePointWebPart"],
|
||||
|
||||
"preconfiguredEntries": [{
|
||||
"groupId": "5c03119e-3074-46fd-976b-c60198311f70", // Other
|
||||
"group": { "default": "Other" },
|
||||
"title": { "default": "CheckUserGroup" },
|
||||
"description": { "default": "Web part to find the users added to a group" },
|
||||
"officeFabricIconFontName": "Page",
|
||||
"properties": {
|
||||
"description": "CheckUserGroup"
|
||||
}
|
||||
}]
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
import * as React from 'react';
|
||||
import * as ReactDom from 'react-dom';
|
||||
import { Version } from '@microsoft/sp-core-library';
|
||||
import {
|
||||
IPropertyPaneConfiguration,
|
||||
PropertyPaneTextField
|
||||
} from '@microsoft/sp-property-pane';
|
||||
import { BaseClientSideWebPart } from '@microsoft/sp-webpart-base';
|
||||
|
||||
import * as strings from 'CheckUserGroupWebPartStrings';
|
||||
import CheckUserGroup from './components/CheckUserGroup';
|
||||
import { ICheckUserGroupProps } from './components/ICheckUserGroupProps';
|
||||
|
||||
export interface ICheckUserGroupWebPartProps {
|
||||
description: string;
|
||||
title: string;
|
||||
}
|
||||
|
||||
export default class CheckUserGroupWebPart extends BaseClientSideWebPart<ICheckUserGroupWebPartProps> {
|
||||
|
||||
public render(): void {
|
||||
const element: React.ReactElement<ICheckUserGroupProps> = React.createElement(
|
||||
CheckUserGroup,
|
||||
{
|
||||
description: this.properties.description,
|
||||
context: this.context,
|
||||
title: this.properties.title,
|
||||
displayMode: this.displayMode,
|
||||
updateProperty: (value: string) => {
|
||||
this.properties.title = value;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
ReactDom.render(element, this.domElement);
|
||||
}
|
||||
|
||||
protected onDispose(): void {
|
||||
ReactDom.unmountComponentAtNode(this.domElement);
|
||||
}
|
||||
|
||||
protected get dataVersion(): Version {
|
||||
return Version.parse('1.0');
|
||||
}
|
||||
|
||||
protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
|
||||
return {
|
||||
pages: [
|
||||
{
|
||||
header: {
|
||||
description: strings.PropertyPaneDescription
|
||||
},
|
||||
groups: [
|
||||
{
|
||||
groupName: strings.BasicGroupName,
|
||||
groupFields: [
|
||||
PropertyPaneTextField('description', {
|
||||
label: strings.DescriptionFieldLabel
|
||||
})
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,185 @@
|
|||
import * as React from 'react';
|
||||
import styles from '../CheckUserGroup.module.scss';
|
||||
import { PeoplePicker, PrincipalType } from '@pnp/spfx-controls-react/lib/PeoplePicker';
|
||||
import { TextField } from 'office-ui-fabric-react/lib/TextField';
|
||||
import { DetailsList, DetailsListLayoutMode, IColumn, SelectionMode } from 'office-ui-fabric-react/lib/DetailsList';
|
||||
import { CommandBarButton } from 'office-ui-fabric-react/lib/Button';
|
||||
import { Spinner } from 'office-ui-fabric-react/lib/Spinner';
|
||||
import { ICheckGroupMembersProps } from './ICheckGroupMembersProps';
|
||||
import { ICheckGroupMembersState } from './ICheckGroupMembersState';
|
||||
import { MSGraphService } from '../../../../Service/MSGraphService';
|
||||
import { IUserItem } from '../../../../Common/IUserItem';
|
||||
import { CSVLink } from 'react-csv';
|
||||
|
||||
export class CheckGroupMembers extends React.Component<ICheckGroupMembersProps, ICheckGroupMembersState> {
|
||||
private userItems: IUserItem[] = [];
|
||||
private headers = [
|
||||
{ label: 'Name', key: 'displayName' },
|
||||
{ label: 'Email', key: 'mail' }
|
||||
];
|
||||
|
||||
constructor(props: ICheckGroupMembersProps) {
|
||||
super(props);
|
||||
|
||||
const columns: IColumn[] = [
|
||||
{
|
||||
key: 'column1',
|
||||
name: 'Name',
|
||||
isRowHeader: true,
|
||||
isSorted: true,
|
||||
isSortedDescending: false,
|
||||
sortAscendingAriaLabel: 'Sorted A to Z',
|
||||
sortDescendingAriaLabel: 'Sorted Z to A',
|
||||
fieldName: 'displayName',
|
||||
onColumnClick: this._onColumnClick,
|
||||
minWidth: 100,
|
||||
maxWidth: 400,
|
||||
isResizable: true
|
||||
},
|
||||
{
|
||||
key: 'column2',
|
||||
name: 'Email',
|
||||
fieldName: 'mail',
|
||||
isSorted: true,
|
||||
isSortedDescending: false,
|
||||
onColumnClick: this._onColumnClick,
|
||||
minWidth: 300,
|
||||
maxWidth: 700,
|
||||
isResizable: true
|
||||
}
|
||||
];
|
||||
|
||||
this.state = {
|
||||
userItems: this.userItems,
|
||||
columns: columns,
|
||||
memberStatus: '',
|
||||
loading: false
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Column click event handler to sort the results
|
||||
*/
|
||||
private _onColumnClick = (ev: React.MouseEvent<HTMLElement>, column: IColumn): void => {
|
||||
const { columns, userItems } = this.state;
|
||||
const newColumns: IColumn[] = columns.slice();
|
||||
const currColumn: IColumn = newColumns.filter(currCol => column.key === currCol.key)[0];
|
||||
newColumns.forEach((newCol: IColumn) => {
|
||||
if (newCol === currColumn) {
|
||||
currColumn.isSortedDescending = !currColumn.isSortedDescending;
|
||||
currColumn.isSorted = true;
|
||||
} else {
|
||||
newCol.isSorted = false;
|
||||
newCol.isSortedDescending = true;
|
||||
}
|
||||
});
|
||||
const newItems = this._copyAndSort(userItems, currColumn.fieldName!, currColumn.isSortedDescending);
|
||||
this.setState({
|
||||
columns: newColumns,
|
||||
userItems: newItems
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort results on column click
|
||||
*/
|
||||
private _copyAndSort<T>(items: T[], columnKey: string, isSortedDescending?: boolean): T[] {
|
||||
const key = columnKey as keyof T;
|
||||
return items.slice(0).sort((a: T, b: T) => ((isSortedDescending ? a[key] < b[key] : a[key] > b[key]) ? 1 : -1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter results by name
|
||||
*/
|
||||
private _onFilter = (ev: React.FormEvent<HTMLInputElement | HTMLTextAreaElement>, text: string): void => {
|
||||
this.setState({
|
||||
userItems: text ? this.state.userItems.filter(i => i.displayName.toLowerCase().indexOf(text.toLowerCase()) > -1) : this.userItems
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get users which are part of the selected group
|
||||
*/
|
||||
private _getGroupMembers = (items: any[]) => {
|
||||
this.setState({ loading: true }, async () => {
|
||||
let userItems: IUserItem[] = [];
|
||||
let memberStatus: string = '';
|
||||
try {
|
||||
if (items.length > 0) {
|
||||
let users = await MSGraphService.GetGroupMembers(this.props.context, items[0].id.split('|').pop());
|
||||
if (users.length === 0) {
|
||||
memberStatus = 'The selected group does not have any members';
|
||||
} else {
|
||||
users.map((user, i) => {
|
||||
userItems.push({
|
||||
displayName: user.displayName,
|
||||
mail: user.mail
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.log('CheckGroupMembers._getGroupMembers Error: ', error);
|
||||
}
|
||||
console.log('CheckGroupMembers._getGroupMembers: ', userItems);
|
||||
this.userItems = userItems;
|
||||
this.setState({ userItems, memberStatus, loading: false });
|
||||
});
|
||||
}
|
||||
|
||||
public render(): React.ReactElement<{}> {
|
||||
return (
|
||||
<div className={styles.checkUserGroup}>
|
||||
<PeoplePicker
|
||||
context={this.props.context}
|
||||
peoplePickerWPclassName={styles.peoplePickerWPClass}
|
||||
titleText='Select Group:'
|
||||
personSelectionLimit={1}
|
||||
showtooltip={true}
|
||||
disabled={false}
|
||||
selectedItems={this._getGroupMembers}
|
||||
showHiddenInUI={false}
|
||||
principalTypes={[PrincipalType.DistributionList, PrincipalType.SecurityGroup, PrincipalType.SharePointGroup]}
|
||||
resolveDelay={1000} />
|
||||
<br />
|
||||
{this.state.loading &&
|
||||
<Spinner label='Loading...' ariaLive='assertive' />
|
||||
}
|
||||
{this.state.userItems.length > 0 &&
|
||||
<div className={styles.detailsList}>
|
||||
<div className={styles.row}>
|
||||
<div className={styles.column}>
|
||||
<TextField
|
||||
label='Filter by Name:'
|
||||
onChange={this._onFilter}
|
||||
className={styles.filterTextfield}
|
||||
/>
|
||||
</div>
|
||||
<div className={styles.column}>
|
||||
<CSVLink className={styles.csvLink}
|
||||
data={this.state.userItems}
|
||||
headers={this.headers}
|
||||
filename={'GroupMembers.csv'}
|
||||
>
|
||||
<CommandBarButton className={styles.exportIcon} iconProps={{ iconName: 'ExcelLogoInverse' }} text='Export to Excel' />
|
||||
</CSVLink>
|
||||
</div>
|
||||
</div><br />
|
||||
<DetailsList
|
||||
items={this.state.userItems}
|
||||
columns={this.state.columns}
|
||||
isHeaderVisible={true}
|
||||
setKey='set'
|
||||
layoutMode={DetailsListLayoutMode.justified}
|
||||
selectionMode={SelectionMode.none}
|
||||
ariaLabelForSelectionColumn='Toggle selection'
|
||||
ariaLabelForSelectAllCheckbox='Toggle selection for all items'
|
||||
checkButtonAriaLabel='Row checkbox'
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
<p className={styles.memberStatus}>{this.state.memberStatus}</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
import { WebPartContext } from '@microsoft/sp-webpart-base';
|
||||
|
||||
export interface ICheckGroupMembersProps {
|
||||
context: WebPartContext;
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
import { IUserItem } from "../../../../Common/IUserItem";
|
||||
import { IColumn } from 'office-ui-fabric-react/lib/DetailsList';
|
||||
|
||||
export interface ICheckGroupMembersState {
|
||||
userItems: IUserItem[];
|
||||
columns: IColumn[];
|
||||
memberStatus: string;
|
||||
loading: boolean;
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
@import '~office-ui-fabric-react/dist/sass/References.scss';
|
||||
|
||||
.checkUserGroup {
|
||||
.container {
|
||||
margin: 10px auto;
|
||||
}
|
||||
|
||||
.row {
|
||||
display: flex;
|
||||
}
|
||||
|
||||
.column {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.peoplePickerWPClass {
|
||||
padding-left: 8px;
|
||||
}
|
||||
|
||||
.detailsList {
|
||||
padding-left: 8px;
|
||||
}
|
||||
|
||||
.filterTextfield {
|
||||
max-width: 300px;
|
||||
}
|
||||
|
||||
.csvLink {
|
||||
float: right;
|
||||
margin-top: 5px;
|
||||
}
|
||||
|
||||
.exportIcon {
|
||||
background-color: transparent;
|
||||
}
|
||||
|
||||
.memberStatus {
|
||||
margin-left: 8px;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
import * as React from 'react';
|
||||
import styles from './CheckUserGroup.module.scss';
|
||||
import { WebPartTitle } from '@pnp/spfx-controls-react/lib/WebPartTitle';
|
||||
import { Pivot, PivotItem } from 'office-ui-fabric-react/lib/Pivot';
|
||||
import { ICheckUserGroupProps } from './ICheckUserGroupProps';
|
||||
import { ICheckUserGroupState } from './ICheckUserGroupState';
|
||||
import { CheckGroupMembers } from './CheckGroupMembers/CheckGroupMembers';
|
||||
import { CheckUserMembership } from './CheckUserMembership/CheckUserMembership';
|
||||
|
||||
export default class CheckUserGroup extends React.Component<ICheckUserGroupProps, ICheckUserGroupState> {
|
||||
constructor(props: ICheckUserGroupProps) {
|
||||
super(props);
|
||||
|
||||
this.state = {
|
||||
selectedKey: 'UserMembership'
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Pivot Item click event handler to update the selected key
|
||||
*/
|
||||
private handleLinkClick = (item: PivotItem): void => {
|
||||
this.setState({
|
||||
selectedKey: item.props.itemKey
|
||||
});
|
||||
}
|
||||
|
||||
public render(): React.ReactElement<ICheckUserGroupProps> {
|
||||
return (
|
||||
<div className={styles.checkUserGroup}>
|
||||
<div className={styles.container}>
|
||||
<div className={styles.row}>
|
||||
<div className={styles.column}>
|
||||
<WebPartTitle displayMode={this.props.displayMode}
|
||||
title={this.props.title}
|
||||
updateProperty={this.props.updateProperty}
|
||||
/>
|
||||
<Pivot headersOnly={true}
|
||||
selectedKey={this.state.selectedKey}
|
||||
onLinkClick={this.handleLinkClick}
|
||||
>
|
||||
<PivotItem headerText='Check User Membership' itemKey='UserMembership' />
|
||||
<PivotItem headerText='Check Group Members' itemKey='GroupMembers' />
|
||||
</Pivot><br />
|
||||
{this.state.selectedKey === 'UserMembership' &&
|
||||
<CheckUserMembership context={this.props.context} />
|
||||
}
|
||||
{this.state.selectedKey === 'GroupMembers' &&
|
||||
<CheckGroupMembers context={this.props.context} />
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,196 @@
|
|||
import * as React from 'react';
|
||||
import styles from '../CheckUserGroup.module.scss';
|
||||
import { PeoplePicker, PrincipalType } from '@pnp/spfx-controls-react/lib/PeoplePicker';
|
||||
import { TextField } from 'office-ui-fabric-react/lib/TextField';
|
||||
import { DetailsList, DetailsListLayoutMode, IColumn, SelectionMode } from 'office-ui-fabric-react/lib/DetailsList';
|
||||
import { CommandBarButton } from 'office-ui-fabric-react/lib/Button';
|
||||
import { Spinner } from 'office-ui-fabric-react/lib/Spinner';
|
||||
import { ICheckUserMembershipProps } from './ICheckUserMembershipProps';
|
||||
import { ICheckUserMembershipState } from './ICheckUserMembershipState';
|
||||
import { MSGraphService } from '../../../../Service/MSGraphService';
|
||||
import { IGroupItem } from '../../../../Common/IGroupItem';
|
||||
import { CSVLink } from 'react-csv';
|
||||
|
||||
export class CheckUserMembership extends React.Component<ICheckUserMembershipProps, ICheckUserMembershipState> {
|
||||
private groupItems: IGroupItem[] = [];
|
||||
private headers = [
|
||||
{ label: 'Name', key: 'name' },
|
||||
{ label: 'Description', key: 'description' },
|
||||
{ label: 'Group Type', key: 'groupTypes' }
|
||||
];
|
||||
|
||||
constructor(props: ICheckUserMembershipProps) {
|
||||
super(props);
|
||||
|
||||
const columns: IColumn[] = [
|
||||
{
|
||||
key: 'column1',
|
||||
name: 'Name',
|
||||
isRowHeader: true,
|
||||
isSorted: true,
|
||||
isSortedDescending: false,
|
||||
sortAscendingAriaLabel: 'Sorted A to Z',
|
||||
sortDescendingAriaLabel: 'Sorted Z to A',
|
||||
fieldName: 'name',
|
||||
onColumnClick: this._onColumnClick,
|
||||
minWidth: 100,
|
||||
maxWidth: 400,
|
||||
isResizable: true
|
||||
},
|
||||
{
|
||||
key: 'column2',
|
||||
name: 'Description',
|
||||
fieldName: 'description',
|
||||
isSorted: true,
|
||||
isSortedDescending: false,
|
||||
onColumnClick: this._onColumnClick,
|
||||
minWidth: 300,
|
||||
maxWidth: 700,
|
||||
isResizable: true
|
||||
},
|
||||
{
|
||||
key: 'column3',
|
||||
name: 'Group Type',
|
||||
fieldName: 'groupTypes',
|
||||
onColumnClick: this._onColumnClick,
|
||||
minWidth: 100,
|
||||
maxWidth: 400,
|
||||
isResizable: true
|
||||
}
|
||||
];
|
||||
|
||||
this.state = {
|
||||
groupItems: this.groupItems,
|
||||
columns: columns,
|
||||
memberStatus: '',
|
||||
loading: false
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Column click event handler to sort the results
|
||||
*/
|
||||
private _onColumnClick = (ev: React.MouseEvent<HTMLElement>, column: IColumn): void => {
|
||||
const { columns, groupItems } = this.state;
|
||||
const newColumns: IColumn[] = columns.slice();
|
||||
const currColumn: IColumn = newColumns.filter(currCol => column.key === currCol.key)[0];
|
||||
newColumns.forEach((newCol: IColumn) => {
|
||||
if (newCol === currColumn) {
|
||||
currColumn.isSortedDescending = !currColumn.isSortedDescending;
|
||||
currColumn.isSorted = true;
|
||||
} else {
|
||||
newCol.isSorted = false;
|
||||
newCol.isSortedDescending = true;
|
||||
}
|
||||
});
|
||||
const newItems = this._copyAndSort(groupItems, currColumn.fieldName!, currColumn.isSortedDescending);
|
||||
this.setState({
|
||||
columns: newColumns,
|
||||
groupItems: newItems
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort results on column click
|
||||
*/
|
||||
private _copyAndSort<T>(items: T[], columnKey: string, isSortedDescending?: boolean): T[] {
|
||||
const key = columnKey as keyof T;
|
||||
return items.slice(0).sort((a: T, b: T) => ((isSortedDescending ? a[key] < b[key] : a[key] > b[key]) ? 1 : -1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter results by name
|
||||
*/
|
||||
private _onFilter = (ev: React.FormEvent<HTMLInputElement | HTMLTextAreaElement>, text: string): void => {
|
||||
this.setState({
|
||||
groupItems: text ? this.state.groupItems.filter(i => i.name.toLowerCase().indexOf(text.toLowerCase()) > -1) : this.groupItems
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get groups of which the selected user is member of
|
||||
*/
|
||||
private _getUserGroups = (items: any[]) => {
|
||||
this.setState({ loading: true }, async () => {
|
||||
let groupItems: IGroupItem[] = [];
|
||||
let memberStatus: string = '';
|
||||
try {
|
||||
if (items.length > 0) {
|
||||
let groups = await MSGraphService.GetUserGroups(this.props.context, items[0].secondaryText);
|
||||
if (groups.length === 0) {
|
||||
memberStatus = 'The selected user does not belong to any group';
|
||||
} else {
|
||||
groups.map((group) => {
|
||||
groupItems.push({
|
||||
name: group.displayName,
|
||||
description: group.description,
|
||||
groupTypes: group.groupTypes && group.groupTypes.length > 0 ? 'Office 365 Group' : group.securityEnabled === true ? 'Security Group' : 'Distribution Group'
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.log('CheckUserMembership._getUserGroups Error: ', error);
|
||||
}
|
||||
console.log('CheckUserMembership._getUserGroups: ', groupItems);
|
||||
this.groupItems = groupItems;
|
||||
this.setState({ groupItems, memberStatus, loading: false });
|
||||
});
|
||||
}
|
||||
|
||||
public render(): React.ReactElement<ICheckUserMembershipProps> {
|
||||
return (
|
||||
<div className={styles.checkUserGroup}>
|
||||
<PeoplePicker
|
||||
context={this.props.context}
|
||||
peoplePickerWPclassName={styles.peoplePickerWPClass}
|
||||
titleText='Select User:'
|
||||
personSelectionLimit={1}
|
||||
showtooltip={true}
|
||||
disabled={false}
|
||||
selectedItems={this._getUserGroups}
|
||||
showHiddenInUI={false}
|
||||
principalTypes={[PrincipalType.User]}
|
||||
resolveDelay={1000} />
|
||||
<br />
|
||||
{this.state.loading &&
|
||||
<Spinner label='Loading...' ariaLive='assertive' />
|
||||
}
|
||||
{this.state.groupItems.length > 0 &&
|
||||
<div className={styles.detailsList}>
|
||||
<div className={styles.row}>
|
||||
<div className={styles.column}>
|
||||
<TextField
|
||||
label='Filter by Name:'
|
||||
onChange={this._onFilter}
|
||||
className={styles.filterTextfield}
|
||||
/>
|
||||
</div>
|
||||
<div className={styles.column}>
|
||||
<CSVLink className={styles.csvLink}
|
||||
data={this.state.groupItems}
|
||||
headers={this.headers}
|
||||
filename={'UserGroups.csv'}
|
||||
>
|
||||
<CommandBarButton className={styles.exportIcon} iconProps={{ iconName: 'ExcelLogoInverse' }} text='Export to Excel' />
|
||||
</CSVLink>
|
||||
</div>
|
||||
</div><br />
|
||||
<DetailsList
|
||||
items={this.state.groupItems}
|
||||
columns={this.state.columns}
|
||||
isHeaderVisible={true}
|
||||
setKey='set'
|
||||
layoutMode={DetailsListLayoutMode.justified}
|
||||
selectionMode={SelectionMode.none}
|
||||
ariaLabelForSelectionColumn='Toggle selection'
|
||||
ariaLabelForSelectAllCheckbox='Toggle selection for all items'
|
||||
checkButtonAriaLabel='Row checkbox'
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
<p className={styles.memberStatus}>{this.state.memberStatus}</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
import { WebPartContext } from "@microsoft/sp-webpart-base";
|
||||
|
||||
export interface ICheckUserMembershipProps {
|
||||
context: WebPartContext;
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
import { IGroupItem } from "../../../../Common/IGroupItem";
|
||||
import { IColumn } from 'office-ui-fabric-react/lib/DetailsList';
|
||||
|
||||
export interface ICheckUserMembershipState {
|
||||
groupItems: IGroupItem[];
|
||||
columns: IColumn[];
|
||||
memberStatus: string;
|
||||
loading: boolean;
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
import { WebPartContext } from '@microsoft/sp-webpart-base';
|
||||
import { DisplayMode } from '@microsoft/sp-core-library';
|
||||
|
||||
export interface ICheckUserGroupProps {
|
||||
description: string;
|
||||
context: WebPartContext;
|
||||
title: string;
|
||||
displayMode: DisplayMode;
|
||||
updateProperty: (value: string) => void;
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
export interface ICheckUserGroupState {
|
||||
selectedKey: string;
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
define([], function() {
|
||||
return {
|
||||
"PropertyPaneDescription": "Description",
|
||||
"BasicGroupName": "Group Name",
|
||||
"DescriptionFieldLabel": "Description Field"
|
||||
}
|
||||
});
|
10
samples/react-check-user-group/src/webparts/checkUserGroup/loc/mystrings.d.ts
vendored
Normal file
10
samples/react-check-user-group/src/webparts/checkUserGroup/loc/mystrings.d.ts
vendored
Normal file
|
@ -0,0 +1,10 @@
|
|||
declare interface ICheckUserGroupWebPartStrings {
|
||||
PropertyPaneDescription: string;
|
||||
BasicGroupName: string;
|
||||
DescriptionFieldLabel: string;
|
||||
}
|
||||
|
||||
declare module 'CheckUserGroupWebPartStrings' {
|
||||
const strings: ICheckUserGroupWebPartStrings;
|
||||
export = strings;
|
||||
}
|
Binary file not shown.
After Width: | Height: | Size: 3.0 KiB |
Binary file not shown.
After Width: | Height: | Size: 1.4 KiB |
|
@ -0,0 +1,38 @@
|
|||
{
|
||||
"extends": "./node_modules/@microsoft/rush-stack-compiler-3.3/includes/tsconfig-web.json",
|
||||
"compilerOptions": {
|
||||
"target": "es5",
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"module": "esnext",
|
||||
"moduleResolution": "node",
|
||||
"jsx": "react",
|
||||
"declaration": true,
|
||||
"sourceMap": true,
|
||||
"experimentalDecorators": true,
|
||||
"skipLibCheck": true,
|
||||
"outDir": "lib",
|
||||
"inlineSources": false,
|
||||
"strictNullChecks": false,
|
||||
"noUnusedLocals": false,
|
||||
"typeRoots": [
|
||||
"./node_modules/@types",
|
||||
"./node_modules/@microsoft"
|
||||
],
|
||||
"types": [
|
||||
"es6-promise",
|
||||
"webpack-env"
|
||||
],
|
||||
"lib": [
|
||||
"es5",
|
||||
"dom",
|
||||
"es2015.collection"
|
||||
]
|
||||
},
|
||||
"include": [
|
||||
"src/**/*.ts"
|
||||
],
|
||||
"exclude": [
|
||||
"node_modules",
|
||||
"lib"
|
||||
]
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
{
|
||||
"extends": "@microsoft/sp-tslint-rules/base-tslint.json",
|
||||
"rules": {
|
||||
"class-name": false,
|
||||
"export-name": false,
|
||||
"forin": false,
|
||||
"label-position": false,
|
||||
"member-access": true,
|
||||
"no-arg": false,
|
||||
"no-console": false,
|
||||
"no-construct": false,
|
||||
"no-duplicate-variable": true,
|
||||
"no-eval": false,
|
||||
"no-function-expression": true,
|
||||
"no-internal-module": true,
|
||||
"no-shadowed-variable": true,
|
||||
"no-switch-case-fall-through": true,
|
||||
"no-unnecessary-semicolons": true,
|
||||
"no-unused-expression": true,
|
||||
"no-use-before-declare": true,
|
||||
"no-with-statement": true,
|
||||
"semicolon": true,
|
||||
"trailing-comma": false,
|
||||
"typedef": false,
|
||||
"typedef-whitespace": false,
|
||||
"use-named-parameter": true,
|
||||
"variable-name": false,
|
||||
"whitespace": false
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue