Cloud Identity Groups (CIG) Advanced Service, Groups Service API ile aynı özelliklere sahiptir ve onun yerine kullanılabilir.
CIG Advanced Service aracılığıyla eşdeğer özelliklere nasıl ulaşacağınızı öğrenmek için sağlanan yardımcı yöntemlere bakın.
Kurulum
CIG Advanced Service'i kullanmak için önce komut dosyası projenizde etkinleştirmeniz gerekir.
Bu kılavuzdaki bazı yöntem imzalarını kısaltmak için aşağıdaki değişkeni tanımladık:
const groups = CloudIdentityGroups.Groups;
GroupsApp Yöntemleri
Aşağıdaki yardımcı yöntemler, Groups Service GroupsApp
yöntemleriyle aynıdır.
Bu kılavuzda grup terimi, Group Class nesnesinin aksine bir Group Resource'u ifade eder. Grup Kaynakları, yöntemleri olmayan JavaScript nesneleridir ancak Grup Sınıfı nesnelerindeki bilgilere benzer bilgileri almak için CIGAdvancedService'te kullanılabilirler.
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
Aşağıdaki yardımcı yöntem, Membership Resources listesini döndürür.
Bir öğenin ad kimliğini bulmak için öğenin group
alanına erişin. Bu, CIG Advanced Service'in birçok yöntemi için kullanışlıdır. Benzer şekilde, bir öğenin e-posta adresini bulmak için groupKey.id
simgesine erişin.
/**
* 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;
}
Grup Yöntemleri
Aşağıdaki yardımcı yöntemler, Groups Service Groups Class
yöntemleriyle aynıdır.
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
Aşağıdaki yöntemde, Memberships.list
kullanılır. Bu yöntem, belirtilen gruptaki tüm üyelikleri getirir. Buna kullanıcıların ve grupların üyelikleri dahil olabilir.
Gruplar Hizmeti getGroups
yöntemini daha iyi tahmin etmek için üyelikleri Type
'lerine göre filtreleyebiliriz.
Bu alana, Memberships.list
için sorgu parametresi olarak FULL
View
sağlayarak veya verilen her üyelik için ayrı bir Memberships.lookup
işlemi gerçekleştirerek erişiriz.
/**
* 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 ve getRoles
Gruplar Hizmeti, getRole()
alanında yalnızca en yüksek öncelikli rolü döndürmüş olsa da üyelik kaynağındaki roles
alanı, üyenin uygun olduğu her rol için ayrı bir öğe içerir (örnek: MEMBER, OWNER, ADMIN).
/**
* 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
getGroups'taki yaklaşımımıza benzer şekilde, Memberships.list
ile bir grubun üyeliklerini getirebilir ve sonuçları yalnızca hedef Type
'i tutacak şekilde filtreleyebiliriz.
/**
* 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 ve hasUser
Hem Gruplar Hizmeti hasGroup
hem de hasUser
bir varlığın belirli bir grubun üyesi olup olmadığını onaylar. Hem bir grubun hem de bir kullanıcının e-posta adresiyle temsil edilebileceği göz önüne alındığında, aşağıda belirtilen yöntem, her ikisinin de belirli bir gruba ait olup olmadığını doğrulamak için kullanılabilir.
/**
* 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;
}