user mgmt, project mgmt, clean up and styling
[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 let msg = xhr.responseText;
38 if(xhr.errorMessage) {
39 msg = xhr.errorMessage
40 }
41 reject(msg);
42 });
43 });
44 },
45 interceptResponse: interceptResponse({
46 'error': 'There was an error retrieving the resource orchestrator information.'
47 }),
48 success: Alt.actions.global.getUsersSuccess,
49 loading: Alt.actions.global.showScreenLoader,
50 error: Alt.actions.global.showNotification
51 },
52 updateUser: {
53 remote: function(state, user) {
54 return new Promise(function(resolve, reject) {
55 $.ajax({
56 url: `/user?api_server=${API_SERVER}`,
57 type: 'PUT',
58 data: user,
59 beforeSend: Utils.addAuthorizationStub,
60 success: function(data, textStatus, jqXHR) {
61 resolve(data);
62 }
63 }).fail(function(xhr){
64 //Authentication and the handling of fail states should be wrapped up into a connection class.
65 Utils.checkAuthentication(xhr.status);
66 let msg = xhr.responseText;
67 if(xhr.errorMessage) {
68 msg = xhr.errorMessage
69 }
70 reject(msg);
71 });
72 });
73 },
74 interceptResponse: interceptResponse({
75 'error': 'There was an error updating the user.'
76 }),
77 success: Alt.actions.global.updateUserSuccess,
78 loading: Alt.actions.global.showScreenLoader,
79 error: Alt.actions.global.showNotification
80 },
81 deleteUser: {
82 remote: function(state, user) {
83 return new Promise(function(resolve, reject) {
84 // setTimeout(function() {
85 // resolve(true);
86 // }, 1000)
87 $.ajax({
88 url: `/user/${user['user-name']}/${user['user-domain']}?api_server=${API_SERVER}`,
89 type: 'DELETE',
90 data: user,
91 beforeSend: Utils.addAuthorizationStub,
92 success: function(data, textStatus, jqXHR) {
93 resolve(data);
94 }
95 }).fail(function(xhr){
96 //Authentication and the handling of fail states should be wrapped up into a connection class.
97 Utils.checkAuthentication(xhr.status);
98 let msg = xhr.responseText;
99 if(xhr.errorMessage) {
100 msg = xhr.errorMessage
101 }
102 reject(msg);
103 });
104 });
105 },
106 interceptResponse: interceptResponse({
107 'error': 'There was an error deleting the user.'
108 }),
109 success: Alt.actions.global.deleteUserSuccess,
110 loading: Alt.actions.global.showScreenLoader,
111 error: Alt.actions.global.showNotification
112 },
113 createUser: {
114 remote: function(state, user) {
115
116 return new Promise(function(resolve, reject) {
117 // setTimeout(function() {
118 // resolve(true);
119 // }, 1000)
120 $.ajax({
121 url: `/user?api_server=${API_SERVER}`,
122 type: 'POST',
123 data: user,
124 beforeSend: Utils.addAuthorizationStub,
125 success: function(data, textStatus, jqXHR) {
126 resolve(data);
127 }
128 }).fail(function(xhr){
129 //Authentication and the handling of fail states should be wrapped up into a connection class.
130 Utils.checkAuthentication(xhr.status);
131 let msg = xhr.responseText;
132 if(xhr.errorMessage) {
133 msg = xhr.errorMessage
134 }
135 reject(msg);
136 });
137 });
138 },
139 interceptResponse: interceptResponse({
140 'error': 'There was an error updating the account.'
141 }),
142 success: Alt.actions.global.createUserSuccess,
143 loading: Alt.actions.global.showScreenLoader,
144 error: Alt.actions.global.showNotification
145 }
146 }
147 }
148
149 function interceptResponse (responses) {
150 return function(data, action, args) {
151 if(responses.hasOwnProperty(data)) {
152 return {
153 type: data,
154 msg: responses[data]
155 }
156 } else {
157 return data;
158 }
159 }
160 }
161
162 function mockUsers() {
163 let data = [];
164 let count = 10;
165 for(let i = 0; i < 10; i++) {
166 data.push({
167 username: `Tester ${i}`,
168 domain: 'Some Domain',
169 platformRoles: {
170 super_admin: true,
171 platform_admin: false,
172 platform_oper: false
173 },
174 disabled: false,
175 projectRoles: [
176 'Project:Role'
177 ]
178 })
179 }
180 return data;
181 }