manage institution: hide slices, show only in projects
[myslice.git] / portal / templates / fed4fire / fed4fire_institution.html
1 {% extends "layout_wide.html" %}
2
3 {% block head %} 
4 <script type="text/javascript" src="{{STATIC_URL}}/js/institution.js"></script>
5 {% endblock head %}
6
7 {% block content %}
8 <div class="container">
9         <div class="row">
10                 <div class="col-md-12">
11                          <div class="breadcrumbs">
12                 {% if project %}
13                                 Management &nbsp;>&nbsp; Project: <span id="authority_name">{{user_details.parent_authority}}</span>
14                 {% else %}
15                                 Management &nbsp;>&nbsp; Institution: <span id="authority_name">{{user_details.parent_authority}}</span>
16                 {% endif %}
17                          </div>
18                 </div>
19         </div>
20 </div>
21 <div class="container">
22         <div class="row">
23                 <div class="col-md-12">
24                         <ul class="nav nav-tabs nav-section">
25                                 <li class="active"><a href="#about">About</a></li>
26                                 <li><a href="#users">Users</a></li>
27                 {% if not project %}
28                                 <li><a href="#projects">Projects</a></li>
29                 {% else %}
30                                 <li><a href="#slices">Slices</a></li>
31                 {% endif %}
32                                 <li><a href="#requests">Requests</a></li>
33                         </ul>
34             </div>
35         </div>
36 </div>
37 <div class="container tab-content">
38         <div class="tab-pane active row" id="about">
39         <div class="col-md-12 el">
40         <h2>{{user_details.parent_authority}}</h2>
41         </div>
42         </div>
43         
44         <div class="tab-pane row" id="users" data-authority="{{user_details.parent_authority}}">
45                 <div class="col-md-12 el">
46                         <div id="user-tab-loading"><img src="{{ STATIC_URL }}img/loading.gif" alt="Loading Slices" /></div>
47                                 <div id="user-tab-loaded" style="display:none;">
48                                 <table id="user-tab" class="table">
49                                         <tr>
50                                         <th>+/-</th>
51                             {% if not project %}
52                                         <th>Email</th>
53                             {% endif %}
54                                         <th>User hrn</th>
55                             <!--
56                                         <th>First name</th>
57                                         <th>Last name</th>
58                                         <th>Enabled</th>
59                             -->
60                                         </tr>
61                                 </table>
62                                 
63                         </div>
64                         {%if  pi %}     
65                         <div>
66                 {% if project %}
67                                 <button id="removepi" type="button" class="btn btn-danger"><span class="glyphicon glyphicon-remove"></span> Remove from project</button>
68                 {% else %}
69                                 <button id="makepi" type="button" class="btn btn-primary"><span class="glyphicon glyphicon-arrow-up"></span> Upgrade to PI</button>
70                                 <button id="removepi" type="button" class="btn"><span class="glyphicon glyphicon-arrow-down"></span> Downgrade to  user</button>
71                                 <button id="deleteusers" type="button" class="btn btn-danger"><span class="glyphicon glyphicon-remove"></span> Delete selected users</button>
72                 {% endif %}
73                         </div>
74                         {% endif %}
75                 </div>
76         </div>
77
78     {% if not project %}
79         <div class="tab-pane row" id="projects" data-authority="{{user_details.parent_authority}}">
80                 <div class="col-md-12 el">
81                         <div id="project-tab-loading"><img src="{{ STATIC_URL }}img/loading.gif" alt="Loading Projects" /></div>
82                                 <div id="project-tab-loaded" style="display:none;">
83                                 <table id="project-tab" class="table">
84                                         <tr>
85                                         <th>+/-</th>
86                                         <th>Project hrn</th>
87                                         </tr>
88                                 </table>
89                                 
90                         </div>
91                         <div>
92                 {% if  pi %}
93                         <button id="createproject" type="button" class="btn btn-default"><span class="glyphicon glyphicon-plus"></span> Create project</button>
94                         <button id="deleteprojects" type="button" class="btn btn-danger"><span class="glyphicon glyphicon-remove"></span> Delete selected projects</button>
95                         {% else %}
96                         <button id="createproject" type="button" class="btn btn-default"><span class="glyphicon glyphicon-plus"></span> Request project</button>
97                         {% endif %}
98                         </div>
99                 </div>
100         </div>
101    {% endif %}
102         <div class="tab-pane row" id="slices">
103                 <div class="col-md-12 el">
104             <div id="slice-tab-loading"><img src="{{ STATIC_URL }}img/loading.gif" alt="Loading Slices" /></div>
105             <div id="slice-tab-loaded" style="display:none;">
106                 <table id="slice-tab" class="table">
107                     <tr>
108                         <th>+/-</th>
109                         <th>Slice hrn</th>
110                         <th>Users</th>
111                         <th>Url</th>
112                         <!-- <th>nodes</th> -->
113                         <th>Creation</th>
114                     </tr>
115                 </table>                        
116             </div>
117         <div>
118                 {% if pi %}
119                         <button id="createslice" type="button" class="btn btn-default"><span class="glyphicon glyphicon-plus"></span> Create slice</button>
120             <button id="renewslices" type="button" class="btn btn-primary"><span class="glyphicon glyphicon-refresh"></span> Renew Slices</button>
121             <button id="deleteslices" type="button" class="btn btn-danger"><span class="glyphicon glyphicon-remove"></span> Delete Slices</button>
122                         {% else %}
123                         <button id="createslice" type="button" class="btn btn-default"><span class="glyphicon glyphicon-plus"></span> Request slice</button>
124                         {% endif %}
125         </div>
126            </div>
127         </div>
128         <div class="tab-pane row" id="requests">
129         <div id="spinner" style="padding-top:40px; padding-left:40px;"></div>
130         </div>
131 </div>
132 <script>
133 $(document).ready(function() {
134     {% if person %}
135     {% if user_details.parent_authority %}
136         $('#spinner').spin();
137         $('#loading').spin();
138         
139         $.post("/rest/myslice:slice/",{'fields':['slice_hrn','users','url','slice_date_created'],'filters':{'parent_authority':'{{user_details.parent_authority}}'}}, function( data ) {
140             var list_slices = [];
141             var table_slices = [];
142             /* "slice_hrn", "slice_description", "slice_type", "parent_authority", "created", "nodes", "slice_url", "slice_last_updated", "users", "slice_urn", "slice_expires" */
143             $.each( data, function( key, val ) {
144                 list_slices.push( "<li><a href=\"portal/slice/"+val.slice_hrn+"\">" + val.slice_hrn + "</a></li>" );
145                 if(val.nodes=="undefined" || val.nodes==null){
146                     nodes_length=0;
147                 }else{
148                     nodes_length=val.nodes.length;
149                 }
150                 //console.log(val);
151                 if(val.users=="undefined" || val.users==null){
152                     users_length=0;
153                 }else{
154                     users_length=val.users.length;
155                 }
156
157                 if(val.url=="undefined" || val.url==null){
158                     slice_url="";
159                 }else{
160                     slice_url="<a href='"+val.url+"' target='_blank'>"+val.url+"</a>";
161                 }
162                 
163                 slice_row = "<tr id='"+val.slice_hrn+"'>";
164                 slice_row += "<td><input type='checkbox' class='slice' id='"+val.slice_hrn+"'><div id='loading' style='float: right;'></div></td>";
165
166                 // filter links to slices, only those that the user has credentials for
167                 s = myslice.user.slices;
168                 if(s.length > 0 && $.inArray(val.slice_hrn, myslice.user.slices)!=-1){
169                     slice_row += "<td><a href='/slice/"+val.slice_hrn+"'>" + val.slice_hrn + "</a></td>";
170                 }else{
171                     slice_row += "<td>" + val.slice_hrn + "</td>";
172                 }
173                 slice_row += "<td>"+users_length+"</td>";
174                 slice_row += "<td>"+slice_url+"</td>";
175                 //slice_row += "<td>"+nodes_length+"</td>";
176                 slice_row += "<td>"+val.slice_date_created+"</td>";
177                 slice_row += "</tr>";
178                 table_slices.push(slice_row);
179                 
180             });
181            
182             /* $("div#slice-list").html($( "<ul/>", { html: list_slices.join( "" ) })); */
183             $("table#slice-tab tr:last").after(table_slices.join( "" ));
184             $("div#slice-tab-loaded").css("display","block");
185             $("div#slice-tab-loading").css("display","none");
186         });
187                 
188         {% if project %}
189                 $.post("/rest/myslice:authority/",{'fields':['pi_users'],'filters':{'authority_hrn':'{{user_details.parent_authority}}'}}, function( data ) {
190            
191             var table_users = [];
192             $.each( data[0].pi_users, function( key, val ) {
193                 //console.log(val);
194                 user_row = "<tr id='"+val+"'>";
195                 user_row += "<td><input type='checkbox' class='user' id='"+val+"'></td>";
196                 user_row += "<td>"+val+"</td>";
197                 user_row += "</tr>";
198                 table_users.push(user_row);
199             });
200             $("table#user-tab tr:last").after(table_users.join( "" ));
201             $("div#user-tab-loaded").css("display","block");
202             $("div#user-tab-loading").css("display","none");
203
204         });
205         {% else %}
206         $.post("/rest/myslice:user/",{'filters':{'parent_authority':'{{user_details.parent_authority}}'}}, function( data ) {
207             var list_users = [];
208             var table_users = [];
209                     /* Available fields
210                     user_gid, user_enabled, slices, pi_authorities, keys, parent_authority, user_first_name,
211                     user_urn, user_last_name, user_phone, user_hrn, user_email, user_type
212                     */
213             $.each( data, function( key, val ) {
214                 list_users.push( "<li><a href=\"portal/user/"+val.user_email+"\">" + val.user_email + "</a></li>" );
215                 user_row = "<tr id='"+val.user_hrn+"'>";
216                 user_row += "<td><input type='checkbox' class='user' id='"+val.user_hrn+"' data-email='"+val.user_email+"'></td>";
217                 user_row += "<td>"+val.user_email+"</td>";
218                 user_row += "<td>"+val.user_hrn+"</td>";
219                 /*
220                 user_row += "<td>"+val.user_first_name+"</td>";
221                 user_row += "<td>"+val.user_last_name+"</td>";
222                             user_row += "<td>"+val.user_enabled+"</td>";
223                 */
224                 user_row += "</tr>";
225                 table_users.push(user_row);
226             });
227             $("table#user-tab tr:last").after(table_users.join( "" ));
228             $("div#user-tab-loaded").css("display","block");
229             $("div#user-tab-loading").css("display","none");
230         });
231                 $.post("/rest/myslice:authority/",{'fields':['authority_hrn','pi_users'],'filters':{'authority_hrn':'CONTAINS{{user_details.parent_authority}}'}}, function( data ) {
232            
233             var table_projects = [];
234             $.each( data, function( key, val ) {
235                 console.log(val);
236                 project_row = "<tr id='"+val.authority_hrn+"'>";
237                 project_row += "<td><input type='checkbox' class='project' id='"+val.authority_hrn+"'></td>";
238
239                 // filter links to projects, only those that the user has credentials for
240                 p = myslice.projects;
241                 if(p.length > 0 && $.inArray(val.authority_hrn, p)!=-1){
242                     project_row += "<td><a href='/portal/project/"+val.authority_hrn+"'>"+val.authority_hrn+"</a></td>";
243                 }else{
244                     project_row += "<td>"+val.authority_hrn+"</td>";
245                 }
246                 project_row += "</tr>";
247                 table_projects.push(project_row);
248             });
249             $("table#project-tab tr:last").after(table_projects.join( "" ));
250             $("div#project-tab-loaded").css("display","block");
251             $("div#project-tab-loading").css("display","none");
252
253         });
254
255         {% endif %}
256     {% endif %}
257     {% endif %}
258
259 }); // End document.ready
260
261 $(document).ready(function() {
262         $('.nav-tabs a').click(function (e) {
263                 e.preventDefault();
264                 $(this).tab('show');
265         var id = $(this).attr('href').substr(1);
266         if ((id == 'requests')){
267                 $("#" + id).load('/management/' + id);
268         }
269         /*
270         if ((id == 'requests') || (id == 'about'))
271                 $("#" + id).load('/management/' + id);
272         */
273         });
274         var hash = window.location.hash;
275         if (hash) {
276                 $('.nav-tabs a[href='+hash+']').click();
277         } else {
278                 $('.nav-tabs a[href=#about]').click();
279         }
280 });
281
282 //upgrade users to PI
283 $(document).ready(function() {
284     $('button#makepi').click(function() {
285         var flag = false;
286         var pi_users = [];
287         $.post("/rest/myslice:authority/",{'filters':{'authority_hrn': '{{user_details.parent_authority}}'}}, function( data ) {
288             pi_users = data[0]["pi_users"];
289             $('input:checkbox.user').each(function (index) {
290                 if(this.checked){
291                     var record_id = this.id;
292                     pi_users.push(record_id)
293                     $.post("/credentials/clear/",{'emails':[this.dataset['email']]}, function(data) {
294                     }); // post credentials clear
295                     flag = true;                
296                 }
297             });
298             if(flag == true){
299                 $.post("/update/myslice:authority/",{'filters':{'authority_hrn':'{{user_details.parent_authority}}'},'params':{'pi_users':pi_users}}, function(data) {
300                     if(data.success){
301                         mysliceAlert('Success: User rights added to: {{user_details.parent_authority}}','success', true);
302                     }else{
303                         mysliceAlert('Rest Error for: '+data.error,'warning', true);
304                         //alert("Rest Error for "+record_id+": "+data.error);
305                     }
306                 });
307             }
308         });
309     });
310 });
311 //downgrade to user [in projects:remove from project]
312 $(document).ready(function() {
313     $('button#removepi').click(function() {
314         var flag = false;
315         var pi_users = [];
316         $.post("/rest/myslice:authority/",{'filters':{'authority_hrn': '{{user_details.parent_authority}}'}}, function( data ) {
317             pi_users = data[0]["pi_users"];
318                         // http://stackoverflow.com/questions/9792927/javascript-array-search-and-remove-string
319             $('input:checkbox.user').each(function (index) {
320                 if(this.checked){
321                     var record_id = this.id;
322                                         for (var i=pi_users.length-1; i>=0; i--) {
323                                         if (pi_users[i] === record_id) {
324                                                 pi_users.splice(i, 1);
325                             $.post("/credentials/clear/",{'emails':[this.dataset['email']]}, function(data) {
326                             }); // post credentials clear
327                             flag = true;                
328                                         }
329                                         }
330                 }
331             });
332             if(flag == true){
333                 $.post("/update/myslice:authority/",{'filters':{'authority_hrn':'{{user_details.parent_authority}}'},'params':{'pi_users':pi_users}}, function(data) {
334                     if(data.success){
335                         mysliceAlert('Success: User rights removed from: {{user_details.parent_authority}}','success', true);
336                         {% if project %}
337                         // remove the user from the list of pis of the project displayed
338                         $('input:checkbox.user').each(function (index) {
339                            if(this.checked){
340                                var record_id = this.id;
341                                $('tr[id="'+record_id+'"]').fadeOut("slow");
342                                $('tr[id="'+record_id+'"]').remove();
343                             }
344
345                         });
346                         {% endif %}
347                     }else{
348                         mysliceAlert('Rest Error for: '+data.error,'warning', true);
349                         //alert("Rest Error for "+record_id+": "+data.error);
350                     }
351                 });
352             }else{
353                 mysliceAlert('No action: User had no rights on: {{user_details.parent_authority}}','success', true);
354             }
355         });
356     });
357 });
358
359
360
361 </script>
362 {% endblock %}