fix bug 823; fix bug 800
[osm/LW-UI.git] / static / src / userhandler / user_list.js
1 /*
2 Copyright 2018 EveryUP srl
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 function openModalCreateUser(args) {
18
19 select2_groups = $('#projects').select2({
20 placeholder: 'Select Projects',
21 width: '100%',
22 ajax: {
23 url: args.projects_list_url,
24 dataType: 'json',
25 processResults: function (data) {
26 projects = [];
27 if (data['projects']) {
28 for (d in data['projects']) {
29 var project = data['projects'][d];
30 projects.push({id: project['_id'], text: project['name']})
31 }
32 }
33
34 return {
35 results: projects
36 };
37 }
38 }
39 });
40
41 $('#modal_new_user').modal('show');
42 }
43
44 function openModalEditUserCredentials(args) {
45 var url = '/admin/users/' + args.user_id;
46 var user_projects = args.projects ? args.projects.split(',') : [];
47 $("#formEditUser").attr("action", url);
48 $("#projects_old").val(user_projects.toString());
49 $('#projects_edit').val(null).trigger('change');
50 $('#default_project_edit').val(null).trigger('change');
51 $('#edit_password').val('');
52 if (user_projects.length > 0) {
53 // Create a DOM Option and pre-select by default
54 var newOption = new Option(user_projects[0], user_projects[0], true, true);
55 // Append it to the select
56 $('#default_project_edit').append(newOption).trigger('change');
57
58 for (var d in user_projects) {
59 var project = user_projects[d];
60 // Create a DOM Option and pre-select by default
61 var newOption = new Option(project, project, true, true);
62 // Append it to the select
63 $('#projects_edit').append(newOption).trigger('change');
64 }
65
66 }
67
68
69 $('#modal_edit_user_credentials').modal('show');
70 }
71
72 function openModalEditUserRoleMap(user_id) {
73 $("#formEditUserRoleMap").attr("action", '/admin/users/' + user_id);
74 var dialog = bootbox.dialog({
75 message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Loading...</div>',
76 closeButton: false
77 });
78 $.ajax({
79 url: '/admin/users/' + user_id + '/info',
80 dataType: "json",
81 contentType: "application/json;charset=utf-8",
82 success: function (result) {
83 dialog.modal('hide');
84 resetMapGroup(result);
85 $('#modal_edit_user_role_map').modal('show');
86 },
87 error: function (result) {
88 dialog.modal('hide');
89 bootbox.alert("An error occurred.");
90 }
91 });
92
93 }
94
95 function resetMapGroup(args){
96 var $formGroup = $('#modal_edit_proj_role_map_body');
97 $formGroup.empty();
98 $formGroup.append('<div class="proj-role-map-group-head">' +
99
100 '<button type="button" class="btn btn-success btn-add btn-sm">+</button>'+
101 '</div></br>');
102 if(args['project_role_mappings'] && args['project_role_mappings'].length > 0) {
103
104 for(i = 0; i < args['project_role_mappings'].length; ++i){
105 var mapping = args['project_role_mappings'][i];
106
107 $formGroup.append('<div class="proj-role-map-group"> <div class="form-group">' +
108 '<label class="col-sm-2">Project* </label><div class="col-sm-3">'+
109 '<input name="map_project_name" value="'+mapping.project_name+'" class="form-control input-sm" required></div>'+
110 '<label class="col-sm-2">Role* </label>'+
111 '<div class="col-sm-3">'+
112 '<input name="map_role_name" value="'+mapping.role_name+'" class="form-control input-sm" required>'+
113 '</div>'+
114 '<button type="button" class="btn btn-danger btn-remove btn-sm">-</button></div></div>');
115 }
116 }
117
118
119
120
121 }
122
123 var addMapGroup = function (event) {
124 event.preventDefault();
125
126 var $formGroup = $('#modal_edit_proj_role_map_body');
127 $formGroup.append('<div class="proj-role-map-group"> <div class="form-group">' +
128 '<label class="col-sm-2">Project* </label><div class="col-sm-3">'+
129 '<input name="map_project_name" class="form-control input-sm" required></div>'+
130 '<label class="col-sm-2">Role* </label>'+
131 '<div class="col-sm-3">'+
132 '<input name="map_role_name" class="form-control input-sm" required>'+
133 '</div>'+
134 '<button type="button" class="btn btn-danger btn-remove btn-sm">-</button></div></div>');
135
136 };
137
138 var removeMapGroup = function (event) {
139 event.preventDefault();
140 var $formGroup = $(this).closest('.proj-role-map-group');
141 $formGroup.remove();
142 };
143
144 function deleteUser(user_id, name) {
145 var delete_url = '/admin/users/' + user_id + '/delete';
146 bootbox.confirm("Are you sure want to delete " + name + "?", function (confirm) {
147 if (confirm) {
148 var dialog = bootbox.dialog({
149 message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Loading...</div>',
150 closeButton: false
151 });
152 $.ajax({
153 url: delete_url,
154 dataType: "json",
155 contentType: "application/json;charset=utf-8",
156 success: function (result) {
157 //$('#modal_show_vim_body').empty();
158 dialog.modal('hide');
159 table.ajax.reload();
160 },
161 error: function (result) {
162 dialog.modal('hide');
163 bootbox.alert("An error occurred.");
164 }
165 });
166 }
167 })
168
169 }
170
171 function validatePswOnCreate() {
172
173 var confirm_password = document.getElementById("password2");
174 if($("#password").val() == $("#password2").val()){
175 $("#pwmatch").removeClass("glyphicon-remove");
176 $("#pwmatch").addClass("glyphicon-ok");
177 $("#pwmatch").css("color","#00A41E");
178 confirm_password.setCustomValidity("");
179 }else{
180 $("#pwmatch").removeClass("glyphicon-ok");
181 $("#pwmatch").addClass("glyphicon-remove");
182 $("#pwmatch").css("color","#FF0004");
183 confirm_password.setCustomValidity("Passwords Don't Match");
184 }
185 }
186
187 function validatePswOnEdit() {
188
189 var confirm_password = document.getElementById("edit_password2");
190 if($("#edit_password").val() == $("#edit_password2").val()){
191 $("#pwmatch_edit").removeClass("glyphicon-remove");
192 $("#pwmatch_edit").addClass("glyphicon-ok");
193 $("#pwmatch_edit").css("color","#00A41E");
194 confirm_password.setCustomValidity("");
195 }else{
196 $("#pwmatch_edit").removeClass("glyphicon-ok");
197 $("#pwmatch_edit").addClass("glyphicon-remove");
198 $("#pwmatch_edit").css("color","#FF0004");
199 confirm_password.setCustomValidity("Passwords Don't Match");
200 }
201 }
202
203 $(document).ready(function () {
204 $("#formEditUserRoleMap").submit(function (event) {
205 event.preventDefault(); //prevent default action
206 var post_url = $(this).attr("action"); //get form action url
207 var request_method = $(this).attr("method"); //get form GET/POST method
208 var form_data = new FormData(this); //Encode form elements for submission
209 $.ajax({
210 url: post_url,
211 type: request_method,
212 data: form_data,
213 headers: {
214 "Accept": 'application/json'
215 },
216 contentType: false,
217 processData: false
218 }).done(function (response, textStatus, jqXHR) {
219 table.ajax.reload();
220 $('#modal_edit_user_role_map').modal('hide');
221 }).fail(function (result) {
222 var data = result.responseJSON;
223 var title = "Error " + (data.code ? data.code : 'unknown');
224 var message = data.detail ? data.detail : 'No detail available.';
225 bootbox.alert({
226 title: title,
227 message: message
228 });
229 });
230 });
231
232 })