user mgmt, project mgmt, clean up and styling
[osm/UI.git] / skyquake / plugins / user_management / src / platformRoleManagement / platformRoleManagementSource.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 Projects = mockProjects();
17 let Users = mockUsers();
18
19
20 module.exports = function(Alt) {
21 return {
22
23 getUsers: {
24 remote: function() {
25 return new Promise(function(resolve, reject) {
26 // setTimeout(function() {
27 // resolve(Users);
28 // }, 1000);
29 $.ajax({
30 url: `/user?api_server=${API_SERVER}`,
31 type: 'GET',
32 beforeSend: Utils.addAuthorizationStub,
33 success: function(data, textStatus, jqXHR) {
34 resolve(data.users);
35 }
36 }).fail(function(xhr){
37 //Authentication and the handling of fail states should be wrapped up into a connection class.
38 Utils.checkAuthentication(xhr.status);
39 let msg = xhr.responseText;
40 if(xhr.errorMessage) {
41 msg = xhr.errorMessage
42 }
43 reject(msg);
44 });
45 });
46 },
47 interceptResponse: interceptResponse({
48 'error': 'There was an error retrieving the resource orchestrator information.'
49 }),
50 success: Alt.actions.global.getPlatformRoleUsersSuccess,
51 loading: Alt.actions.global.showScreenLoader,
52 error: Alt.actions.global.showNotification
53 },
54 getPlatform: {
55 remote: function() {
56 return new Promise(function(resolve, reject) {
57 // setTimeout(function() {
58 // resolve(Projects);
59 // }, 1000)
60 $.ajax({
61 url: `/platform?api_server=${API_SERVER}`,
62 type: 'GET',
63 beforeSend: Utils.addAuthorizationStub,
64 success: function(data, textStatus, jqXHR) {
65 resolve(data.platform);
66 }
67 }).fail(function(xhr){
68 //Authentication and the handling of fail states should be wrapped up into a connection class.
69 Utils.checkAuthentication(xhr.status);
70 let msg = xhr.responseText;
71 if(xhr.errorMessage) {
72 msg = xhr.errorMessage
73 }
74 reject(msg);
75 });
76 });
77 },
78 interceptResponse: interceptResponse({
79 'error': 'There was an error retrieving the resource orchestrator information.'
80 }),
81 success: Alt.actions.global.getPlatformSuccess,
82 loading: Alt.actions.global.showScreenLoader,
83 error: Alt.actions.global.showNotification
84 },
85 updatePlatform: {
86 remote: function(state, project) {
87 return new Promise(function(resolve, reject) {
88 $.ajax({
89 url: `/platform?api_server=${API_SERVER}`,
90 type: 'PUT',
91 data: project,
92 beforeSend: Utils.addAuthorizationStub,
93 success: function(data, textStatus, jqXHR) {
94 resolve(data);
95 }
96 }).fail(function(xhr){
97 //Authentication and the handling of fail states should be wrapped up into a connection class.
98 Utils.checkAuthentication(xhr.status);
99 let msg = xhr.responseText;
100 if(xhr.errorMessage) {
101 msg = xhr.errorMessage
102 }
103 reject(msg);
104 });
105 });
106 },
107 interceptResponse: interceptResponse({
108 'error': 'There was an error updating the project.'
109 }),
110 success: Alt.actions.global.updateProjectSuccess,
111 loading: Alt.actions.global.showScreenLoader,
112 error: Alt.actions.global.showNotification
113 },
114 deleteProject: {
115 remote: function(state, project) {
116 return new Promise(function(resolve, reject) {
117 // setTimeout(function() {
118 // resolve(true);
119 // }, 1000)
120 $.ajax({
121 url: `/project/${project['name']}?api_server=${API_SERVER}`,
122 type: 'DELETE',
123 beforeSend: Utils.addAuthorizationStub,
124 success: function(data, textStatus, jqXHR) {
125 resolve(data);
126 }
127 }).fail(function(xhr){
128 //Authentication and the handling of fail states should be wrapped up into a connection class.
129 Utils.checkAuthentication(xhr.status);
130 let msg = xhr.responseText;
131 if(xhr.errorMessage) {
132 msg = xhr.errorMessage
133 }
134 reject(msg);
135 });
136 });
137 },
138 interceptResponse: interceptResponse({
139 'error': 'There was an error deleting the user.'
140 }),
141 success: Alt.actions.global.deleteProjectSuccess,
142 loading: Alt.actions.global.showScreenLoader,
143 error: Alt.actions.global.showNotification
144 },
145 createProject: {
146 remote: function(state, project) {
147
148 return new Promise(function(resolve, reject) {
149 // setTimeout(function() {
150 // resolve(true);
151 // }, 1000)
152 $.ajax({
153 url: `/project?api_server=${API_SERVER}`,
154 type: 'POST',
155 data: project,
156 beforeSend: Utils.addAuthorizationStub,
157 success: function(data, textStatus, jqXHR) {
158 resolve(data);
159 }
160 }).fail(function(xhr){
161 //Authentication and the handling of fail states should be wrapped up into a connection class.
162 Utils.checkAuthentication(xhr.status);
163 let msg = xhr.responseText;
164 if(xhr.errorMessage) {
165 msg = xhr.errorMessage
166 }
167 reject(msg);
168 });
169 });
170 },
171 interceptResponse: interceptResponse({
172 'error': 'There was an error updating the account.'
173 }),
174 success: Alt.actions.global.createProjectSuccess,
175 loading: Alt.actions.global.showScreenLoader,
176 error: Alt.actions.global.showNotification
177 }
178 }
179 }
180
181 function interceptResponse (responses) {
182 return function(data, action, args) {
183 if(responses.hasOwnProperty(data)) {
184 return {
185 type: data,
186 msg: responses[data]
187 }
188 } else {
189 return data;
190 }
191 }
192 }
193
194 function mockProjects() {
195 let data = [];
196 let count = 10;
197 for(let i = 0; i < 3; i++) {
198 data.push({
199 name: `Test Project ${i}`,
200 description: 'Some description',
201 roles: ['Some-Role', 'Some-Other-Role'],
202 users: [
203 {
204 'user-name': 'Some-User',
205 'user-domain': 'system',
206 role: [
207 {
208 'role': 'Some-Role',
209 'key-set' : 'some key'
210 },
211 {
212 'role': 'Some-Other-Role',
213 'key-set' : 'some key'
214 }
215 ]
216 },
217 {
218 'user-name': 'Some-User',
219 'user-domain': 'system',
220 role: [
221 {
222 'role': 'Some-Role',
223 'key-set' : 'some key'
224 }
225 ]
226 }
227 ]
228 })
229 }
230 return data;
231 }
232 function mockUsers() {
233 let data = [];
234 let count = 10;
235 for(let i = 0; i < 10; i++) {
236 data.push({
237 'user-name': `Tester ${i}`,
238 'user-domain': 'Some Domain',
239 platformRoles: {
240 super_admin: true,
241 platform_admin: false,
242 platform_oper: false
243 },
244 disabled: false,
245 projectRoles: [
246 'Project:Role'
247 ]
248 })
249 }
250 return data;
251 }