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

Commit

Permalink
Creating a user via graph Api (#107)
Browse files Browse the repository at this point in the history
* added methods: creating getting and deleting a user via graph Api

* Rebased and make lint happy

Signed-off-by: Kiran Parajuli <[email protected]>

* removed the check in deleting a user

* fix after review

Co-authored-by: Kiran Parajuli <[email protected]>
  • Loading branch information
ScharfViktor and kiranparajuli589 authored Apr 27, 2022
1 parent 3ad87fe commit 10cd296
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 5 deletions.
5 changes: 5 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ Note: the `testing` app is not available on oCIS. When such steps are used again
| `LDAP_ADMIN_PASSWORD` | admin password of the ldap server | admin |
| `LDAP_BASE_DN` | base DN of the admin server | cn=admin,dc=owncloud,dc=com |

#### Graph Specific config variables
| setting | meaning | default |
| -- | -- | -- |
| `TEST_WITH_GRAPH_API` | use graph api to create users | false |

### Starting the server
To start the middleware server change into the checkout directory of owncloud-test-middleware and use the following commands
```
Expand Down
2 changes: 2 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,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 TEST_WITH_GRAPH_API = process.env.TEST_WITH_GRAPH_API === 'true'

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


exports.createUser = 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,
mail: email,
onPremisesSamAccountName: user,
passwordProfile: { password }
})
userSettings.addUserToCreatedUsersList(user, password, displayName, email)
return httpHelper
.postGraph('users', 'admin', body)
.then(res => httpHelper.checkStatus(res, 'Failed while creating user'))
}

exports.deleteUser = function (user) {
return httpHelper
.deleteGraph(`users/${user}`, 'admin')
}

exports.getUser = function (user) {
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),
}
19 changes: 15 additions & 4 deletions src/stepDefinitions/provisioningContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const codify = require('../helpers/codify')
const httpHelper = require('../helpers/httpHelper')
const ldap = require('../helpers/ldapHelper')
const userSettings = require('../helpers/userSettings')
const graph = require('../helpers/graphHelper')
const sharingHelper = require('../helpers/sharingHelper')
const { setConfigs, getActualSkeletonDir } = require('../helpers/config')

Expand All @@ -18,6 +19,8 @@ function createDefaultUser(userId, skeletonType) {
const email = userSettings.getEmailAddressOfDefaultUser(userId)
if (client.globals.ldap) {
return ldap.createUser(client.globals.ldapClient, userId)
} else if (client.globals.graph) {
return graph.createUser(userId, password, displayname, email)
}
return createUser(userId, password, displayname, email, skeletonType)
}
Expand Down Expand Up @@ -123,13 +126,21 @@ async function createUserWithAttributes(

function deleteUser(userId) {
userSettings.deleteUserFromCreatedUsersList(userId)
const url = encodeURI(`cloud/users/${userId}`)
return httpHelper.deleteOCS(url)
if (client.globals.graph) {
return graph.deleteUser(userId)
} else {
const url = encodeURI(`cloud/users/${userId}`)
return httpHelper.deleteOCS(url)
}
}

function initUser(userId) {
const url = encodeURI(`cloud/users/${userId}`)
return httpHelper.getOCS(url, userId)
if (client.globals.graph) {
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 10cd296

Please sign in to comment.