18cb83d489925d043b05145fc1c3c18e585e1408
[osm/UI.git] / skyquake / plugins / user-management / src / dashboard / userMgmtSource.js
1 /*
2 * STANDARD_RIFT_IO_COPYRIGHT
3 */
4 import $ from 'jquery';
5 var Utils = require('utils/utils.js');
6 let API_SERVER = require('utils/rw.js').getSearchParams(window.location).api_server;
7 let HOST = API_SERVER;
8 let NODE_PORT = require('utils/rw.js').getSearchParams(window.location).api_port || ((window.location.protocol == 'https:') ? 8443 : 8000);
9 let DEV_MODE = require('utils/rw.js').getSearchParams(window.location).dev_mode || false;
10
11 if (DEV_MODE) {
12 HOST = window.location.protocol + '//' + window.location.hostname;
13 }
14
15
16 let Users = mockUsers();
17
18
19 module.exports = function(Alt) {
20 return {
21 getUsers: {
22 remote: function() {
23 return new Promise(function(resolve, reject) {
24 // setTimeout(function() {
25 // resolve(Users);
26 // }, 1000)
27 $.ajax({
28 url: `/user?api_server=${API_SERVER}`,
29 type: 'GET',
30 beforeSend: Utils.addAuthorizationStub,
31 success: function(data, textStatus, jqXHR) {
32 resolve(data.users);
33 }
34 }).fail(function(xhr){
35 //Authentication and the handling of fail states should be wrapped up into a connection class.
36 Utils.checkAuthentication(xhr.status);
37 });
38 });
39 },
40 interceptResponse: interceptResponse({
41 'error': 'There was an error retrieving the resource orchestrator information.'
42 }),
43 success: Alt.actions.global.getUsersSuccess,
44 loading: Alt.actions.global.showScreenLoader,
45 error: Alt.actions.global.showNotification
46 },
47 updateUser: {
48 remote: function(state, user) {
49 return new Promise(function(resolve, reject) {
50 setTimeout(function() {
51 resolve(true);
52 }, 1000)
53 });
54 },
55 interceptResponse: interceptResponse({
56 'error': 'There was an error updating the user.'
57 }),
58 success: Alt.actions.global.updateUserSuccess,
59 loading: Alt.actions.global.showScreenLoader,
60 error: Alt.actions.global.showNotification
61 },
62 deleteUser: {
63 remote: function(state, user) {
64 return new Promise(function(resolve, reject) {
65 // setTimeout(function() {
66 // resolve(true);
67 // }, 1000)
68 $.ajax({
69 url: `/user/${user['user-name']}/${user['user-domain']}?api_server=${API_SERVER}`,
70 type: 'DELETE',
71 data: user,
72 beforeSend: Utils.addAuthorizationStub,
73 success: function(data, textStatus, jqXHR) {
74 resolve(data);
75 }
76 }).fail(function(xhr){
77 //Authentication and the handling of fail states should be wrapped up into a connection class.
78 Utils.checkAuthentication(xhr.status);
79 });
80 });
81 },
82 interceptResponse: interceptResponse({
83 'error': 'There was an error deleting the user.'
84 }),
85 success: Alt.actions.global.deleteUserSuccess,
86 loading: Alt.actions.global.showScreenLoader,
87 error: Alt.actions.global.showNotification
88 },
89 createUser: {
90 remote: function(state, user) {
91
92 return new Promise(function(resolve, reject) {
93 // setTimeout(function() {
94 // resolve(true);
95 // }, 1000)
96 $.ajax({
97 url: `/user?api_server=${API_SERVER}`,
98 type: 'POST',
99 data: user,
100 beforeSend: Utils.addAuthorizationStub,
101 success: function(data, textStatus, jqXHR) {
102 resolve(data);
103 }
104 }).fail(function(xhr){
105 //Authentication and the handling of fail states should be wrapped up into a connection class.
106 Utils.checkAuthentication(xhr.status);
107 });
108 });
109 },
110 interceptResponse: interceptResponse({
111 'error': 'There was an error updating the account.'
112 }),
113 success: Alt.actions.global.createUserSuccess,
114 loading: Alt.actions.global.showScreenLoader,
115 error: Alt.actions.global.showNotification
116 }
117 }
118 }
119
120 function interceptResponse (responses) {
121 return function(data, action, args) {
122 if(responses.hasOwnProperty(data)) {
123 return {
124 type: data,
125 msg: responses[data]
126 }
127 } else {
128 return data;
129 }
130 }
131 }
132
133 function mockUsers() {
134 let data = [];
135 let count = 10;
136 for(let i = 0; i < 10; i++) {
137 data.push({
138 username: `Tester ${i}`,
139 domain: 'Some Domain',
140 platformRoles: {
141 super_admin: true,
142 platform_admin: false,
143 platform_oper: false
144 },
145 disabled: false,
146 projectRoles: [
147 'Project:Role'
148 ]
149 })
150 }
151 return data;
152 }