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

Commit

Permalink
added methods: creating getting and deleting a user via graph Api
Browse files Browse the repository at this point in the history
  • Loading branch information
ScharfViktor committed Apr 26, 2022
1 parent f886d3e commit c64af0c
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const withHttp = (url) => (/^https?:\/\//i.test(url) ? url : `http://${url}`)

const RUN_WITH_LDAP = !!process.env.RUN_WITH_LDAP
const RUN_ON_OCIS = process.env.RUN_ON_OCIS === 'true'
const RUN_WITH_IDM = process.env.RUN_WITH_IDM === 'true'

const LOCAL_BACKEND_URL = withHttp(
process.env.BACKEND_HOST || (RUN_ON_OCIS ? 'https://localhost:9200' : 'http://localhost:8080')
Expand Down Expand Up @@ -42,6 +43,7 @@ const config = {
ldap_base_dn: LDAP_BASE_DN,
testing_data_dir: TESTING_DATA_DIR,
ldap_password: LDAP_ADMIN_PASSWORD,
idm: RUN_WITH_IDM,
},
assert,
}
Expand Down
40 changes: 40 additions & 0 deletions src/helpers/graphHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const httpHelper = require('./httpHelper')
const userHelper = require('./userSettings')


exports.createUser = async function (
user,
password = null,
displayName = null,
email = null
) {
displayName = displayName || userHelper.getDisplayNameForUser(user)
email = email || userHelper.getEmailAddressForUser(user)
password = password || userHelper.getPasswordForUser(user)

const body = JSON.stringify({
'displayName': displayName,
'mail': email,
'onPremisesSamAccountName': user,
'passwordProfile': { 'password': password }
})

console.log('step2 - create')
return httpHelper
.postGraph('users', 'admin', body)
.then(res => httpHelper.checkStatus(res, 'Failed while creating user'))
}

exports.deleteUser = async function (user) {
console.log('step1 - delete')
return httpHelper
.deleteGraph(`users/${user}`, 'admin')
.then(res => httpHelper.checkStatus(res, 'Failed while deleting user'))
}

exports.getUser = async function (user) {
console.log('step3 - init')
return httpHelper
.getGraph(`users/${user}`, 'admin')
.then(res => httpHelper.checkStatus(res, 'user does not found'))
}
25 changes: 24 additions & 1 deletion src/helpers/httpHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@ const requestEndpoint = function (path, params, userId = 'admin', header = {}) {
return fetcher(url, options)
}

/**
*
* @param {string} path
* @param {object} params
* @param {string} userId
* @param {object} header
*
* @returns {node-fetch}
*/
const requestGraphEndpoint = function (path, params, userId = 'admin', header = {}) {
const headers = { ...createAuthHeader(userId), ...header }
const options = { ...params, headers }
const url = join(backendHelper.getCurrentBackendUrl(), 'graph/v1.0', path)
return fetcher(url, options)
}

/**
*
* @param {string} path
Expand Down Expand Up @@ -164,5 +180,12 @@ module.exports = {
proppatch: (url, userId, body, header) =>
requestEndpoint(url, { body, method: 'PROPPATCH' }, userId, header),
lock: (url, userId, body, header) =>
requestEndpoint(url, { body, method: 'LOCK' }, userId, header)
requestEndpoint(url, { body, method: 'LOCK' }, userId, header),
// graph Api requests
postGraph: (url, userId, body, header) =>
requestGraphEndpoint(url, { body, method: 'POST' }, userId, header),
deleteGraph: (url, userId, body, header) =>
requestGraphEndpoint(url, { body, method: 'DELETE' }, userId, header),
getGraph: (url, userId, body, header) =>
requestGraphEndpoint(url, { body, method: 'GET' }, userId, header),
}
12 changes: 10 additions & 2 deletions src/stepDefinitions/provisioningContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const codify = require('../helpers/codify')
const { join } = require('../helpers/path')

const ldap = require('../helpers/ldapHelper')
const graph = require('../helpers/graphHelper')
const sharingHelper = require('../helpers/sharingHelper')
const { setConfigs, getActualSkeletonDir } = require('../helpers/config')

Expand All @@ -20,6 +21,9 @@ function createDefaultUser(userId, skeletonType) {
if (client.globals.ldap) {
return ldap.createUser(client.globals.ldapClient, userId)
}
else if (client.globals.idm) {
return graph.createUser(userId, password, displayname, email)
}
return createUser(userId, password, displayname, email, skeletonType)
}

Expand Down Expand Up @@ -129,8 +133,12 @@ function deleteUser(userId) {
}

function initUser(userId) {
const url = encodeURI(`cloud/users/${userId}`)
return httpHelper.getOCS(url, userId)
if (client.globals.idm) {
return graph.getUser(userId)
} else {
const url = encodeURI(`cloud/users/${userId}`)
return httpHelper.getOCS(url, userId)
}
}

function editUser(userId, key, value) {
Expand Down

0 comments on commit c64af0c

Please sign in to comment.