Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Creating group and adding member to group via GraphApi #113

Merged
merged 3 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/helpers/graphHelper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const httpHelper = require('./httpHelper')
const userSettings = require('./userSettings')
const userHelper = require('./userSettings')
const backendHelper = require('../helpers/backendHelper')
const { join } = require('./path')


exports.createUser = function (
Expand Down Expand Up @@ -35,3 +37,57 @@ exports.getUser = function (user) {
.getGraph(`users/${user}`, 'admin')
.then(res => httpHelper.checkStatus(res, 'user does not found'))
}

exports.createGroup = function (group) {
const body = JSON.stringify({ displayName: group })
return httpHelper
.postGraph('groups', 'admin', body)
.then(res => httpHelper.checkStatus(res, 'Failed while creating group'))
}

exports.getGroup = function (group) {
return httpHelper
.getGraph(`groups/${group}`, 'admin')
}

exports.deleteGroup = async function (group) {
// deleting group does not work with the groupname. so we find groupId
const groupId = await getGroupId(group)
return httpHelper
.deleteGraph(`groups/${groupId}`, 'admin')
}

async function getGroupId(group) {
const response = await httpHelper
.getGraph('groups/', 'admin')
.then(res => res.json())

for (const key in response.value) {
if (response.value[key].displayName.toLowerCase() === group.toLowerCase()) {
ScharfViktor marked this conversation as resolved.
Show resolved Hide resolved
console.log(response.value[key].id)
ScharfViktor marked this conversation as resolved.
Show resolved Hide resolved
return response.value[key].id
}
}
}

async function getUserId(user) {
const response = await httpHelper
.getGraph(`users/${user}`, 'admin')
.then(res => res.json())

return response.id
}

exports.addToGroup = async function (user, group) {
const groupId = await getGroupId(group)
const userId = await getUserId(user)

const url = join(backendHelper.getCurrentBackendUrl(), 'graph/v1.0/users', userId)
const body = JSON.stringify({
'@odata.id': url
})

return httpHelper
.postGraph(`groups/${groupId}/members/$ref`, 'admin', body)
.then(res => httpHelper.checkStatus(res, 'Failed while adding member to group'))
}
20 changes: 16 additions & 4 deletions src/stepDefinitions/provisioningContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ async function createUserWithAttributes(
) {
password = password || userSettings.getPasswordForUser(username)
await deleteUser(username)
await createUser(username, password, displayname, email, skeletonType)
if (client.globals.graph) {
return graph.createUser(username, password, displayname, email)
} else {
await createUser(username, password, displayname, email, skeletonType)
}
if (initialize) {
await initUser(username)
}
Expand Down Expand Up @@ -166,6 +170,8 @@ function createGroup(groupId) {
userSettings.addGroupToCreatedGroupsList(groupId)
}
})
} else if (client.globals.graph) {
return graph.createGroup(groupId)
}
const body = new URLSearchParams()
body.append('groupid', groupId)
Expand All @@ -180,14 +186,20 @@ function createGroup(groupId) {
* @returns {*|Promise}
*/
function deleteGroup(groupId) {
userSettings.deleteGroupFromCreatedGroupsList(groupId)
const url = encodeURI(`cloud/groups/${groupId}`)
return httpHelper.deleteOCS(url)
if (client.globals.graph) {
return graph.deleteGroup(groupId)
} else {
userSettings.deleteGroupFromCreatedGroupsList(groupId)
const url = encodeURI(`cloud/groups/${groupId}`)
return httpHelper.deleteOCS(url)
}
}

function addToGroup(userId, groupId) {
if (client.globals.ldap) {
return ldap.addUserToGroup(client.globals.ldapClient, userId, groupId)
} else if (client.globals.graph) {
return graph.addToGroup(userId, groupId)
}
const body = new URLSearchParams()
body.append('groupid', groupId)
Expand Down