Skip to content

Commit a4dec07

Browse files
feat(appsscript/admin/isAdmin): Add isAdmin utility function
1 parent 505c685 commit a4dec07

File tree

8 files changed

+41
-7
lines changed

8 files changed

+41
-7
lines changed

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Functions specifically designed for Google Apps Script environments, including u
4646
<details open><summary>Functions</summary>
4747

4848
| Function | Description |
49-
|:-----------------------------------------------------------------|:------------------------------------------------------------------------------------------------------------------|
49+
| :--------------------------------------------------------------- | :---------------------------------------------------------------------------------------------------------------- |
5050
| [`checkMultipleAccount`](src/appsscript/checkMultipleAccount.ts) | Checks if multiple Google accounts are in use. |
5151
| [`getByteSize`](src/appsscript/getByteSize.ts) | Returns the size of a string in bytes. |
5252
| [`isHtmlOutput`](src/appsscript/isHtmlOutput.ts) | Checks if an object is an [`HtmlOutput`](https://developers.google.com/apps-script/reference/html/html-output). |
@@ -62,7 +62,7 @@ A collection of functions to simplify working with Google Sheets.
6262
<details open><summary>Functions</summary>
6363

6464
| Function | Description |
65-
|:----------------------------------------------------------------------------------|:---------------------------------------------------------------------------------------------------------------------------------|
65+
| :-------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------- |
6666
| [`appendColumn`](src/appsscript/sheets/appendColumn.ts) | Appends a single column of data to the sheet. |
6767
| [`appendColumns`](src/appsscript/sheets/appendColumns.ts) | Appends multiple columns of data to the sheet. |
6868
| [`appendRow`](src/appsscript/sheets/appendRow.ts) | Appends a single row of data to the sheet. |
@@ -102,7 +102,7 @@ General utility functions that can be useful in any JavaScript/TypeScript projec
102102
<details open><summary>Functions</summary>
103103

104104
| Function | Description |
105-
|:-------------------------------------------------------------|:-----------------------------------------------------------------------------------------------|
105+
| :----------------------------------------------------------- | :--------------------------------------------------------------------------------------------- |
106106
| [`chunk`](src/base/chunk.ts) | Splits an array into chunks of a specified size. |
107107
| [`decodeHtml`](src/base/decodeHtml.ts) | Decodes HTML entities. |
108108
| [`encodeHtml`](src/base/encodeHtml.ts) | Encodes a string for safe use in HTML. |
@@ -139,6 +139,7 @@ General utility functions that can be useful in any JavaScript/TypeScript projec
139139
| [`nonArray`](src/base/nonArray.ts) 🆕 | Returns `true` if not `Array`. |
140140
| [`nonBoolean`](src/base/nonBoolean.ts) 🆕 | Returns `true` if not `boolean`. |
141141
| [`nonEmpty`](src/base/nonEmpty.ts) 🆕 | Returns `true` if not "empty". |
142+
| [`nonFunction`](src/base/nonFunction.ts) 🆕 | Returns `true` if not `Function`. |
142143
| [`nonNil`](src/base/nonNil.ts) | Returns `true` if not `null` or `undefined`. |
143144
| [`nonNull`](src/base/nonNull.ts) | Returns `true` if not `null`. |
144145
| [`nonNumber`](src/base/nonNumber.ts) | Returns `true` if not a `number`. |
@@ -172,7 +173,7 @@ A set of custom exception classes for more specific error handling.
172173
<details open><summary>Functions</summary>
173174

174175
| Exception | Description |
175-
|:-------------------------------------------------------------------------------|:------------------------------------|
176+
| :----------------------------------------------------------------------------- | :---------------------------------- |
176177
| [`Exception`](src/exceptions/Exception.ts) | Base exception class. |
177178
| [`RuntimeException`](src/exceptions/RuntimeException.ts) | Exception for runtime errors. |
178179
| [`EmptyStringException`](src/exceptions/EmptyStringException.ts) | Exception for empty strings. |
@@ -189,7 +190,7 @@ Functions for working with file paths and URLs.
189190
<details open><summary>Functions</summary>
190191

191192
| Function | Description |
192-
|:---------------------------------------------|:----------------------------------------------------------------|
193+
| :------------------------------------------- | :-------------------------------------------------------------- |
193194
| [`isAbsolute`](src/path/isAbsolute.ts) | Checks if a path is absolute. |
194195
| [`isRelative`](src/path/isRelative.ts) | Checks if a path is relative. |
195196
| [`isValidDomain`](src/path/isValidDomain.ts) | Checks if a string is a valid domain name. |
@@ -204,15 +205,15 @@ Functions for working with file paths and URLs.
204205
<details open><summary>Functions</summary>
205206

206207
| Abstract | Description |
207-
|:---------------------------------|:------------|
208+
| :------------------------------- | :---------- |
208209
| [`Class`](src/abstract/Class.ts) | |
209210

210211
</details>
211212

212213
<details open><summary>Functions</summary>
213214

214215
| Interface | Description |
215-
|:-----------------------------------------|:-------------------------|
216+
| :--------------------------------------- | :----------------------- |
216217
| [`Iterator`](src/interfaces/Iterator.ts) | Interface for iterators. |
217218

218219
</details>

src/appsscript/admin/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { isAdmin } from "./isAdmin";

src/appsscript/admin/isAdmin.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { nonFunction } from "../../base";
2+
3+
/**
4+
* ## isAdmin
5+
*
6+
* Checks if the current user is an administrator of the Google Workspace domain.
7+
* Requires the `Admin SDK Directory Service` to be enabled.
8+
*
9+
* @returns `true` if the user is an administrator; otherwise, `false`.
10+
*/
11+
export function isAdmin(): boolean {
12+
try {
13+
if (nonFunction(AdminDirectory?.Users?.get)) {
14+
throw new Error(
15+
"Admin SDK Directory Service is not available or not enabled."
16+
);
17+
}
18+
19+
const email = Session.getActiveUser().getEmail();
20+
const user = AdminDirectory.Users.get(email);
21+
22+
return !!user.isAdmin;
23+
} catch (err: unknown) {
24+
console.warn(`[Error]: ${err}`);
25+
return false;
26+
}
27+
}

src/appsscript/docs/index.ts

Whitespace-only changes.

src/appsscript/drive/index.ts

Whitespace-only changes.

src/appsscript/forms/index.ts

Whitespace-only changes.

src/appsscript/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
export * from "./admin";
2+
export * from "./docs";
3+
export * from "./drive";
4+
export * from "./forms";
15
export * from "./sheets";
6+
export * from "./slides";
27

38
export { checkMultipleAccount } from "./checkMultipleAccount";
49
export { getByteSize } from "./getByteSize";

src/appsscript/slides/index.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)