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 aef121e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 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
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 (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) {
groupId = await getGroupId(group)
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'))
}
18 changes: 14 additions & 4 deletions src/stepDefinitions/provisioningContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ function createGroup(groupId) {
}
})
}
else if (client.globals.graph) {
return graph.createGroup(groupId)
}
const body = new URLSearchParams()
body.append('groupid', groupId)
userSettings.addGroupToCreatedGroupsList(groupId)
Expand All @@ -179,16 +182,23 @@ function createGroup(groupId) {
* @param {string} groupId
* @returns {*|Promise}
*/
function deleteGroup(groupId) {
userSettings.deleteGroupFromCreatedGroupsList(groupId)
const url = encodeURI(`cloud/groups/${groupId}`)
return httpHelper.deleteOCS(url)
async function deleteGroup(groupId) {
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)
const url = encodeURI(`cloud/users/${userId}/groups`)
Expand Down

0 comments on commit aef121e

Please sign in to comment.