summaryrefslogtreecommitdiffstats
path: root/server/lib/external-backends/index.js
blob: c882522edf1e29a72e435ef664535c10e3e1c4e1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* global __appdir */
const fs = require('fs')
const path = require('path')

class ExternalBackends {
  /*
   * Returns a list of all backends which have a .js file.
   */
  getBackends () {
    var files = fs.readdirSync(path.join(__appdir, 'lib', 'external-backends', 'backends'))
    return files
  }

  /*
   * Returns the credential structure / fields, defined in the backends.
   *
   * If type === switch there can be child elements in the elements attribute.
   * Those elements are only shown, if the switch is set to true.
   * [{ type: 'text', id: 1, name: '<NAME>', icon: '<ICON_NAME>' },
   *  { type: 'password', id: 2, name: '<NAME>', icon: '<ICON_NAME>' },
   *  { type: 'switch', id: 3, name: '<NAME>', icon: '<ICON_NAME>' , elements: [ { type: ... } ] },
   *  { type: 'select', id: 4, name: '<NAME>', icon: '<ICON_NAME>' }, ...]
   */
  getCredentials () {
    console.log('If this method gets called the backend class has NOT IMPLEMENTED the getCredentials method!')
    return null
  }

  /*
   * type: <BACKEND_TYPE>
   *
   * Returns the instance of a given backend type.
   */
  getInstance (type) {
    const bList = this.getBackends()
    const bType = type + '-backend.js'

    // Check if it's a valid backend type.
    if (bList.indexOf(bType) === -1) {
      console.log(bType + ' is not a valid backend type.')
      return null
    }

    const backend = new (require(path.join(__appdir, 'lib', 'external-backends', 'backends', bType)))()
    return backend
  }

  /* Returns an empty array [] if the backends doesn't have the function implemented.
   *
   * return: ['<SYNC_TYPE>', ...]
   */
  getSyncTypes () {
    return []
  }

  /*
   * credendtials: <BACKEND_CREDENTIALS>
   *
   * Returns a list of all objects in the backend.
   */
  async getObjects (credendtials) {
    return { status: 'NOT_IMPLEMENTED_EXCEPTION', error: 'The provided backend does not have a getObjects method' }
  }

  /*
   * credendtials: <BACKEND_CREDENTIALS>
   * oid: <OBJECT_ID>
   *
   * Call the API of the backend and returns the information to the object including a list of childs.
   */
  async getObject (credentials, oid) {
    return { status: 'NOT_IMPLEMENTED_EXCEPTION', error: 'The provided backend does not have a getObject method' }
  }

  /*
   * credentials: <BACKEND_CREDENTIALS>
   * objects: [{ eid: <EXTERNAL_ID>, gid: <GROUP_ID> }, ...]
   * EXTERNAL_ID is the id of the objects in the external backend requestet.
   *
   * return: [{ gid: <GROUP_ID>, childs: [{ id: <EXTERNAL_ID>, }, ...]}, ...]
   */
  getDataTree (credendtials, objects) {
    return { status: 'NOT_IMPLEMENTED_EXCEPTION', error: 'The provided backend does not have a getDataTree method' }
  }

  /*
   * Checks the connection of a given backend.
   *
   * return: { success: <boolean>, status: '<STATUS_CODE_IF_ERROR>', error: '<ERROR_MESSAGE>' }
   */
  async checkConnection (backend) {
    return { success: false, status: 'NOT_IMPLEMENTED_EXCEPTION', error: 'The provided backend does not have a checkConnection method' }
  }

  /* Returns an empty array [] if the backends doesn't have such a function.
   *
   * return: [{id: <id>, title: <title>}, ...]
   */
  async getObjectTypes (credentials) {
    return []
  }
}

module.exports = ExternalBackends