List of users with a filter on authority_hrn
[unfold.git] / portal / templates / slice-tab-users-view.html
1 <script>
2     function get_users_in_slice(authority_hrn){
3         console.log(authority_hrn);
4         $("table#user-tab").html("<tr><th>+/-</th><th>Email</th><th>User hrn</th></tr>");
5         var slice_users = [];
6         $.post("/rest/user/",{'filters':{'parent_authority': authority_hrn}}, function( data ) {
7                 var list_users = [];
8                 var table_users = [];
9                 /* Available fields
10                 user_gid, user_enabled, slices, pi_authorities, keys, parent_authority, user_first_name,
11             user_urn, user_last_name, user_phone, user_hrn, user_email, user_type
12                 */
13                 $.each( data, function( key, val ) {
14                 list_users.push( "<li><a href=\"portal/user/"+val.user_email+"\">" + val.user_email + "</a></li>" );
15                 user_row = "<tr>";
16                         // checking the box for the users that belong to the selected slice
17                         var flag_checkbox = 0;
18                         for(var data in val.slices) {
19                                 var element = val.slices[data];
20                                 if (element == "{{slice}}"){
21                                         //console.log("the slice is: "+ element);       
22                                         flag_checkbox = 1;
23                                         slice_users.push (val.user_hrn)
24                                         user_row += "<td><input type='checkbox' checked='True' class='user'></td>";
25                                 }
26                         }
27                         if(flag_checkbox != 1){
28                         user_row += "<td><input type='checkbox' class='user'></td>";
29                         }
30                 user_row += "<td>"+val.user_email+"</td>";
31                 user_row += "<td>"+val.user_hrn+"</td>";
32                         //user_row += "<td>"+val.user_enabled+"</td>";
33                 user_row += "</tr>";
34                 table_users.push(user_row);
35             
36                 });
37                 $("table#user-tab tr:first").after(table_users.join( "" ));
38                 $("div#user-tab-loaded").css("display","block");
39                 $("div#user-tab-loading").css("display","none");
40             $("input:checkbox.user").click(function() {
41                 user_hrn = $(this).closest('tr').find('td:eq(2)').html();
42                 if(this.checked){
43                     var record_id = this.id;
44                     slice_users.push (user_hrn);
45                 }else{
46                     console.log(slice_users);
47                 }
48             });
49         }); // post rest/user
50     }
51
52     $(document).ready(function() {
53             // TODO: Add a filter based on the list of authorities
54                 $.post("/rest/authority/",{'fields':['authority_hrn']}, function( data ) {
55                 var list_authorities = [];
56                 $.each( data, function( key, val ) {
57                     auth_hrn = val.authority_hrn;
58                     // Keep only the sub authorities, remove root auth
59                     if(auth_hrn.indexOf(".") > -1){
60                         list_authorities.push(auth_hrn);
61                         if(auth_hrn=="{{user_details.parent_authority}}"){
62                             $("#auth_list").append("<option value='"+auth_hrn+"' selected='selected'>"+auth_hrn+"</option>");
63                         }else{
64                             $("#auth_list").append("<option value='"+auth_hrn+"'>"+auth_hrn+"</option>");
65                         }
66                     }
67                 });
68             }); // post rest/authority
69
70             get_users_in_slice("{{user_details.parent_authority}}");
71
72                 $('button#addusers').click(function() {
73                 $.post("/update/slice/",{'filters':{'slice_hrn':'{{slice}}'},'params':{'users':slice_users}}, function(data) {
74                         if(data.success){
75                         // TODO: highlight row after success
76                         //$('tr[id="'+record_id+'"]').highlight();
77                         mysliceAlert('Success: slice updated','success', true);
78                         }else{
79                         mysliceAlert('Rest Error for: '+data.error,'warning', true);
80                         }
81                 }); // post update slice
82             
83                 // TODO: refresh table
84                 //window.location="/portal/institution#slices";
85
86             }); // button#addusers click
87
88     }); // document ready
89 </script>
90
91         <div class="col-md-2">
92                 <div id="select-platform" class="list-group">
93                 </div>
94                         
95                 <ul class="list-group">
96                   <li class="list-group-item">Authority:<b> {{user_details.parent_authority}}</b>
97                   </li>
98                   <li class="list-group-item" style="padding-left:5px;">
99                         <select id="auth_list" onchange="get_users_in_slice(this.value);" style ="min-width:190px;">
100             </select>
101           </li>
102                 </ul>
103         
104         </div>
105         <div class="col-md-10">
106                 <div class="row">
107                 </div>
108                 <div id="user-tab-loading"><img src="{{ STATIC_URL }}img/loading.gif" alt="Loading Useres" /></div>
109                 <div id="user-tab-loaded" style="display:none;">
110                 <table id="user-tab" class="table">
111                 </table>
112
113                 <!-- XXX TODO: for the moment only PIs have access to Update but users that have slice credentials should also have access to that -->
114
115                                 {% if 'is_pi'  in pi %}
116                                 <button id="addusers" type="button" class="btn btn-default"><span class="glyphicon glyphicon-ok"></span> Update users</button>
117                                 {%endif%}
118                 </div>
119