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

Commit

Permalink
creating group and adding member to grop
Browse files Browse the repository at this point in the history
  • Loading branch information
ScharfViktor committed Apr 28, 2022
1 parent 2b7a046 commit aae4d9c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
55 changes: 55 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,56 @@ 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 === group) {
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'))
}
14 changes: 11 additions & 3 deletions src/stepDefinitions/provisioningContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,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 +182,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

0 comments on commit aae4d9c

Please sign in to comment.