Dịch vụ nâng cao của Nhóm danh tính trên đám mây (CIG) cung cấp tính năng tương đương với API Dịch vụ nhóm và có thể được sử dụng thay thế.
Xem các phương thức trợ giúp được cung cấp để tìm hiểu cách đạt được các chức năng tương đương thông qua Dịch vụ nâng cao của CIG.
Thiết lập
Để sử dụng Dịch vụ nâng cao của CIG, trước tiên, hãy bật dịch vụ này trong dự án tập lệnh.
Để rút ngắn một số chữ ký phương thức trong hướng dẫn này, chúng tôi đã xác định biến sau:
const groups = CloudIdentityGroups.Groups;
Phương thức GroupsApp
Các phương thức trợ giúp sau đây tương ứng với các phương thức của Dịch vụ nhóm GroupsApp
.
Trong hướng dẫn này, thuật ngữ nhóm đề cập đến Tài nguyên nhóm, trái ngược với đối tượng Lớp nhóm. Tài nguyên nhóm là các đối tượng JavaScript không có phương thức, nhưng bạn có thể sử dụng các đối tượng này trong Dịch vụ nâng cao của CIG để truy xuất thông tin tương tự như trong các đối tượng Lớp nhóm.
getGroupByEmail
/**
* Given a group's email, returns that group's resource
*
* @param {String} email: The email address to lookup a group by
* @return {Group} group: The group resource associated with the email
*/
function groupsAppGetGroupByEmail(email) {
// Retrieve the name ID of the group
const groupName = groups.lookup({
'groupKey.id': email,
'groupKey.namespace': '' // Optional for google groups, dynamic groups, and security groups
// Necessary for identity-mapped groups (see https://developers.google.com/cloud-search/docs/guides/identity-mapping)
}).name;
// Retrieve the group resource
return groups.get(groupName);
}
getGroups
Phương thức trợ giúp sau đây trả về danh sách Tài nguyên thành viên.
Truy cập vào trường group
của một phần tử để tìm mã nhận dạng tên của phần tử đó. Điều này rất hữu ích cho nhiều phương thức của Dịch vụ nâng cao CIG. Tương tự, hãy truy cập vào groupKey.id
của một phần tử để tìm email của phần tử đó.
/**
* Retrieves all the membership relation resources to groups which you are a
* direct member (or a pending member).
*
* @return {Array<MembershipRelation>} groups : List of direct memberships where
* you are the member.
*/
function groupsAppGetGroups() {
const myEmail = Session.getActiveUser().getEmail();
let pageToken = '';
let membershipList = [];
do {
const queryParams = {
query:`member_key_id=='${myEmail}'`,
pageToken:pageToken
};
const searchResult = groups.Memberships.searchDirectGroups('groups/-', queryParams);
membershipList = membershipList.concat(searchResult.memberships);
pageToken = searchResult.nextPageToken;
} while (pageToken);
return membershipList;
}
Phương thức nhóm
Các phương thức trợ giúp sau đây tương ứng với các phương thức của Dịch vụ nhóm Groups Class
.
getEmail
/**
* Gets a group's email address
*
* @param {Object} group: A group resource
* @return {String} email: The email associated with the group resource.
*/
function getEmail(group) {
return group.groupKey.id;
}
getGroups
Phương thức sau sử dụng Memberships.list
. Phương thức này sẽ tìm nạp mọi gói thành viên cho nhóm đã cho. Danh sách này có thể bao gồm cả tư cách thành viên của người dùng cũng như nhóm.
Để ước chừng chính xác hơn phương thức getGroups
của Dịch vụ nhóm, chúng ta có thể lọc gói thành viên theo Type
.
Chúng ta có thể truy cập vào trường này bằng cách cung cấp FULL
View
làm tham số truy vấn cho Memberships.list
hoặc bằng cách thực hiện một Memberships.lookup
riêng lẻ cho mỗi gói thành viên nhất định.
/**
* Fetch a list of memberships with provided group as its parent
*
* @param {Group} group: A group resource
* @return {Array<Membership>} membershipList: The memberships where the parent
* is the provided group and member is a also a group.
*/
function getGroups(group) {
let membershipList = [];
let pageToken = '';
do {
// Fetch a page of memberships
const queryParams = {
view: 'FULL',
pageToken: pageToken
}
const response = groups.Memberships.list(group.name, queryParams);
// Filter non-group memberships
const onlyGroupMemberships = response.memberships.filter(
membership => membership.type == 'GROUP'
);
membershipList = membershipList.concat(onlyGroupMemberships);
// Set up next page
pageToken = response.nextPageToken;
} while(pageToken);
return membershipList;
}
getRole và getRoles
Mặc dù Dịch vụ nhóm có thể chỉ trả về vai trò có mức độ ưu tiên cao nhất trong getRole()
, nhưng trường roles
trong tài nguyên thành viên chứa một phần tử riêng biệt cho mỗi vai trò mà thành viên đủ điều kiện (ví dụ: THÀNH VIÊN, CHỦ SỞ HỮU, QUẢN TRỊ VIÊN).
/**
* Retrieve the membership roles of a member to a group.
*
* @param {Group} containingGroup: The group whom the member belongs to
* @param {String} email: The email address associated with a member that
* belongs to the containingGroup
* @return {Array<Role>} roles: List of roles the member holds with respect to
* the containingGroup.
*/
function getRoleWithEmail(containingGroup, email) {
// First fetch the membership
const membershipName = groups.Memberships.lookup(containingGroup.name, { 'memberKey.id': email }).name;
const membership = groups.Memberships.get(membershipName);
// Then retrieve the role
return membership.roles;
}
/**
* Retrieve the membership roles of a member to a group.
*
* @param {Group} containingGroup: The group resource whom the member belongs to
* @param {User} user: The user associated with a member that belongs to the
* containingGroup
* @return {Array<Role>} roles: List of roles the member holds with respect to
* the containingGroup
*/
function getRoleWithUser(containingGroup, user) {
return getRoleWithEmail(containingGroup, user.getEmail());
}
/**
* Retrieve the membership roles of a group of members to a group
*
* @param {Group} containingGroup: The group resource to which roles are
* relevant
* @param {Array<User>} users: List of users to fetch roles from
* @return {Array<Array<Role>>} roles: A list where every element is a list of
* roles of member to the containingGroup
*/
function getRoles(containingGroup, users) {
let roles = [];
for (const user of users) {
roles.push(getRoleWithUser(containingGroup, user));
}
return roles;
}
getUsers
Tương tự như phương pháp trong getGroups, chúng ta có thể tìm nạp thành viên của một nhóm bằng Memberships.list
và lọc kết quả để chỉ giữ lại mục tiêu Type
.
/**
* Given a group, retrieve its direct members and banned members of the group
* that have a known corresponding Google Account.
*
* @param {Group} group: The group Resource whom the users being queried belong
* to
* @return {Array<String>} users: A list of emails associated with members of
* the given group
*/
function getUsers(group) {
let userList = [];
let pageToken = '';
do {
// Fetch a page of memberships from the group
const queryParams = {
view: 'FULL',
pageToken: pageToken
}
const listResponse = groups.Memberships.list(group.name, queryParams);
// Filter non-users and keep member emails
const users = listResponse.memberships
.filter(membership => membership.type == 'USER')
.map(membership => membership.preferredMemberKey.id);
userList = userList.concat(users);
// Prepare next page
pageToken = listResponse.nextPageToken;
} while (pageToken);
return userList;
}
hasGroup và hasUser
Cả Dịch vụ nhóm hasGroup
và hasUser
đều xác nhận xem một thực thể có phải là thành viên của một nhóm nhất định hay không. Do cả Nhóm và Người dùng đều có thể được biểu thị bằng địa chỉ email, nên bạn có thể sử dụng phương thức sau để xác nhận xem một trong hai thuộc về một nhóm nhất định hay không.
/**
* Tests if the given email has an associated direct member to the given group.
*
* @param {Group} group: Group resource to which the entity is being checked as
* a member
* @param {String} email: Email that can represent a Group or User entity
* @return {Boolean} isMember: Whether the entity is a direct member to the
* group or not
*/
function checkDirectGroupMembership(group, email) {
try {
groups.Memberships.lookup(group.name, {'memberKey.id': email});
} catch(e) {
// Log failure if exception is not related to membership existence
if (!e.message.includes('Membership does not exist.')) {
console.error(e);
}
return false;
}
return true;
}