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

Fetch settings values from settings service #475

Merged
merged 3 commits into from
May 22, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"webpack-node-externals": "^1.7.2"
},
"dependencies": {
"axios": "^0.19.2",
"browser-request": "^0.3.3",
"davclient.js": "https://github.com/owncloud/davclient.js.git",
"promise": "^8.0.3",
Expand Down
13 changes: 11 additions & 2 deletions src/helperFunctions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const axios = require('axios')
const Promise = require('promise')
const request = require('browser-request')
const parser = require('./xmlParser.js')
Expand Down Expand Up @@ -51,10 +52,18 @@ class helpers {

/**
* sets the username
* @param {string} authHeader authorization header; either basic or bearer or what ever
* @param {string | null} authHeader authorization header; either basic or bearer or what ever
*/
setAuthorization (authHeader) {
this._authHeader = authHeader
axios.interceptors.request.use(config => {
if (authHeader && authHeader.startsWith('Bearer ')) {
config.headers.Authorization = authHeader
} else {
config.headers.Authorization = undefined
}
return config
})
}

getAuthorization () {
Expand All @@ -66,7 +75,7 @@ class helpers {
}

logout () {
this._authHeader = null
this.setAuthorization(null)
this._currentUser = null
}

Expand Down
2 changes: 2 additions & 0 deletions src/owncloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const FileVersion = require('./fileVersionManagement.js')
const SystemTags = require('./systemTags.js')
const FilesTrash = require('./filesTrash.js')
const PublicFiles = require('./publicLinkAccess.js')
const Settings = require('./settings.js')

/**
* @class ownCloud
Expand Down Expand Up @@ -71,6 +72,7 @@ class ownCloud {
this.systemTags = new SystemTags(this.helpers)
this.fileTrash = new FilesTrash(this.helpers)
this.publicFiles = new PublicFiles(this.helpers)
this.settings = new Settings(this.helpers)
this.requests = {
ocs: function (options = {}) {
return helpers.ocs(options)
Expand Down
54 changes: 54 additions & 0 deletions src/settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const SettingsClient = require('../vendor/settingsClient')
const Promise = require('promise')

/**
* @class SettingsValues
* @classdesc
* <b><i> The SettingsValues class provides access to all settings values of the (most of the time authenticated) user.</i></b>
*
* @author Benedikt Kulmann
* @version 1.0.0
* @param {object} helperFile instance of the helpers class
*/
class SettingsValues {
constructor (helpers) {
this.helpers = helpers
}

/**
* Gets all settings values for the given account uuid. If no uuid is provided, settings values
* for the authenticated user will be fetched. If the settings service is unavailable, a set of
* default values will be returned (if there are any defaults).
* @param {string} accountUuid The accountUuid to fetch settings for. Most of the time we want to fetch settings
* for the authenticated user. So this defaults to `me`, which will resolve the
* account uuid of the authenticated user in the settings service.
* @returns {Promise.<settings>} array: all available settings values
* @returns {Promise.<error>} string: error message, if any.
*/
async getSettingsValues (accountUuid = 'me') {
kulmann marked this conversation as resolved.
Show resolved Hide resolved
try {
const baseUrl = this.helpers.getInstance().replace(/\/$/, '')
const response = await SettingsClient.ValueService_ListSettingsValues({
$domain: baseUrl,
body: {
identifier: {
account_uuid: accountUuid
}
}
})
if (response.status === 201) {
return Promise.resolve(response.data.settingsValues)
}
} catch (error) {
// fail on anything except settings service being unavailable
// TODO: if this changes to GET requests in the future, this will be 404 instead of 502
if (error.response.status !== 502) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now thinking, there might also be a risk that if the service is just down (but configured) that Phoenix would revert to default values instead of showing an error. we can consider this case in a separate issue

return Promise.reject(error)
}
}
// TODO: build a sensible set of defaults here, if necessary.
return Promise.resolve([])
}
}

module.exports = SettingsValues
Loading