-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathisAdmin.ts
More file actions
32 lines (27 loc) · 1.07 KB
/
isAdmin.ts
File metadata and controls
32 lines (27 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { AdminDirectoryException } from "../../exception";
import { nonFunction } from "../../lang";
/**
* Checks if the current user is an administrator of the Google Workspace domain.
*
* **Note:** Requires the [Admin SDK Directory Service](https://developers.google.cn/apps-script/advanced/admin-sdk-directory) to be enabled.
*
* @returns {boolean} `true` if the user is an administrator; otherwise, `false`.
* @see <a href="https://developers.google.com/apps-script/reference/base/user"><code>User</code></a>
* @see [Admin SDK Directory Service](https://developers.google.cn/apps-script/advanced/admin-sdk-directory)
* @since 1.5.0
* @version 1.1.0
* @environment `Google Apps Script`
*/
export function isAdmin(): boolean {
try {
if (nonFunction(AdminDirectory?.Users?.get)) {
throw new AdminDirectoryException();
}
const email = Session.getActiveUser().getEmail();
const user = AdminDirectory.Users.get(email);
return !!user.isAdmin;
} catch (err: unknown) {
console.warn(`[Error]: ${err}`);
}
return false;
}