users.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /**
  2. * Copyright (c) 2011, Robin Appelman <icewind1991@gmail.com>
  3. * This file is licensed under the Affero General Public License version 3 or later.
  4. * See the COPYING-README file.
  5. */
  6. UserList={
  7. useUndo:true,
  8. /**
  9. * @brief Initiate user deletion process in UI
  10. * @param string uid the user ID to be deleted
  11. *
  12. * Does not actually delete the user; it sets them for
  13. * deletion when the current page is unloaded, at which point
  14. * finishDelete() completes the process. This allows for 'undo'.
  15. */
  16. do_delete:function( uid ) {
  17. UserList.deleteUid = uid;
  18. // Set undo flag
  19. UserList.deleteCanceled = false;
  20. // Hide user in table to reflect deletion
  21. $(this).parent().parent().hide();
  22. $('tr').filterAttr( 'data-uid', UserList.deleteUid ).hide();
  23. // Provide user with option to undo
  24. $('#notification').text(t('files','undo delete user'));
  25. $('#notification').data('deleteuser',true);
  26. $('#notification').fadeIn();
  27. },
  28. /**
  29. * @brief Delete a user via ajax
  30. * @param bool ready whether to use ready() upon completion
  31. *
  32. * Executes deletion via ajax of user identified by property deleteUid
  33. * if 'undo' has not been used. Completes the user deletion procedure
  34. * and reflects success in UI.
  35. */
  36. finishDelete:function( ready ){
  37. // Check deletion has not been undone
  38. if( !UserList.deleteCanceled && UserList.deleteUid ){
  39. // Delete user via ajax
  40. $.post(
  41. OC.filePath('settings','ajax','removeuser.php'),
  42. {username:UserList.deleteUid},
  43. function(result){
  44. // Remove undo option, & remove user from table
  45. boolOperationFinished(
  46. data, function(){
  47. $('#notification').fadeOut();
  48. $('tr').filterAttr( 'data-uid', username ).remove();
  49. UserList.deleteCanceled=true;
  50. UserList.deleteFiles=null;
  51. if( ready ){
  52. ready();
  53. }
  54. }
  55. );
  56. }
  57. );
  58. }
  59. }
  60. }
  61. $(document).ready(function(){
  62. function setQuota(uid,quota,ready){
  63. $.post(
  64. OC.filePath('settings','ajax','setquota.php'),
  65. {username:uid,quota:quota},
  66. function(result){
  67. if(ready){
  68. ready(result.data.quota);
  69. }
  70. }
  71. );
  72. }
  73. function applyMultiplySelect(element){
  74. var checked=[];
  75. var user=element.data('username');
  76. if(element.data('userGroups')){
  77. checked=element.data('userGroups').split(', ');
  78. }
  79. if(user){
  80. var checkHandeler=function(group){
  81. if(user==OC.currentUser && group=='admin'){
  82. return false;
  83. }
  84. $.post(
  85. OC.filePath('settings','ajax','togglegroups.php'),
  86. {
  87. username:user,
  88. group:group
  89. },
  90. function(){}
  91. );
  92. };
  93. }else{
  94. checkHandeler=false;
  95. }
  96. var addGroup = function(group) {
  97. $('select[multiple]').each(function(index, element) {
  98. if ($(element).find('option[value="'+group +'"]').length == 0) {
  99. $(element).append('<option value="'+group+'">'+group+'</option>');
  100. }
  101. })
  102. };
  103. element.multiSelect({
  104. createCallback:addGroup,
  105. createText:'add group',
  106. checked:checked,
  107. oncheck:checkHandeler,
  108. onuncheck:checkHandeler,
  109. minWidth: 100,
  110. });
  111. }
  112. $('select[multiple]').each(function(index,element){
  113. applyMultiplySelect($(element));
  114. });
  115. $('td.remove>img').live('click',function(event){
  116. var uid = $(this).parent().parent().data('uid');
  117. // Call function for handling delete/undo
  118. UserList.do_delete( uid );
  119. });
  120. $('td.password>img').live('click',function(event){
  121. event.stopPropagation();
  122. var img=$(this);
  123. var uid=img.parent().parent().data('uid');
  124. var input=$('<input type="password">');
  125. img.css('display','none');
  126. img.parent().children('span').replaceWith(input);
  127. input.focus();
  128. input.keypress(function(event) {
  129. if(event.keyCode == 13) {
  130. if($(this).val().length>0){
  131. $.post(
  132. OC.filePath('settings','ajax','changepassword.php'),
  133. {username:uid,password:$(this).val()},
  134. function(result){}
  135. );
  136. input.blur();
  137. }else{
  138. input.blur();
  139. }
  140. }
  141. });
  142. input.blur(function(){
  143. $(this).replaceWith($('<span>●●●●●●●</span>'));
  144. img.css('display','');
  145. });
  146. });
  147. $('td.password').live('click',function(event){
  148. $(this).children('img').click();
  149. });
  150. $('select.quota, select.quota-user').live('change',function(){
  151. var select=$(this);
  152. var uid=$(this).parent().parent().parent().data('uid');
  153. var quota=$(this).val();
  154. var other=$(this).next();
  155. if(quota!='other'){
  156. other.hide();
  157. select.data('previous',quota);
  158. setQuota(uid,quota);
  159. }else{
  160. other.show();
  161. select.addClass('active');
  162. other.focus();
  163. }
  164. });
  165. $('select.quota, select.quota-user').each(function(i,select){
  166. $(select).data('previous',$(select).val());
  167. })
  168. $('input.quota-other').live('change',function(){
  169. var uid=$(this).parent().parent().data('uid');
  170. var quota=$(this).val();
  171. var select=$(this).prev();
  172. var other=$(this);
  173. if(quota){
  174. setQuota(uid,quota,function(quota){
  175. select.children().attr('selected',null);
  176. var existingOption=select.children().filter(function(i,option){
  177. return ($(option).val()==quota);
  178. });
  179. if(existingOption.length){
  180. existingOption.attr('selected','selected');
  181. }else{
  182. var option=$('<option/>');
  183. option.attr('selected','selected').attr('value',quota).text(quota);
  184. select.children().last().before(option);
  185. }
  186. select.val(quota);
  187. select.removeClass('active');
  188. other.val(null);
  189. other.hide();
  190. });
  191. }else{
  192. var previous=select.data('previous');
  193. select.children().attr('selected',null);
  194. select.children().each(function(i,option){
  195. if($(option).val()==previous){
  196. $(option).attr('selected','selected');
  197. }
  198. });
  199. select.removeClass('active');
  200. other.hide();
  201. }
  202. });
  203. $('input.quota-other').live('blur',function(){
  204. $(this).change();
  205. })
  206. $('#newuser').submit(function(event){
  207. event.preventDefault();
  208. var username=$('#newusername').val();
  209. var password=$('#newuserpassword').val();
  210. if($('#content table tbody tr').filterAttr('data-uid',username).length>0){
  211. OC.dialogs.alert('The username is already being used', 'Error creating user');
  212. return;
  213. }
  214. if($.trim(username) == '') {
  215. OC.dialogs.alert('A valid username must be provided', 'Error creating user');
  216. return false;
  217. }
  218. if($.trim(password) == '') {
  219. OC.dialogs.alert('A valid password must be provided', 'Error creating user');
  220. return false;
  221. }
  222. var groups=$('#newusergroups').prev().children('div').data('settings').checked;
  223. $('#newuser').get(0).reset();
  224. $.post(
  225. OC.filePath('settings','ajax','createuser.php'),
  226. {
  227. username:username,
  228. password:password,
  229. groups:groups,
  230. },
  231. function(result){
  232. if(result.status!='success'){
  233. OC.dialogs.alert(result.data.message, 'Error creating user');
  234. }
  235. else {
  236. var tr=$('#content table tbody tr').first().clone();
  237. tr.attr('data-uid',username);
  238. tr.find('td.name').text(username);
  239. var select=$('<select multiple="multiple" data-placehoder="Groups" title="Groups">');
  240. select.data('username',username);
  241. select.data('userGroups',groups.join(', '));
  242. tr.find('td.groups').empty();
  243. var allGroups=$('#content table').data('groups').split(', ');
  244. for(var i=0;i<groups.length;i++){
  245. if(allGroups.indexOf(groups[i])==-1){
  246. allGroups.push(groups[i]);
  247. }
  248. }
  249. $.each(allGroups,function(i,group){
  250. select.append($('<option value="'+group+'">'+group+'</option>'));
  251. });
  252. tr.find('td.groups').append(select);
  253. if(tr.find('td.remove img').length==0){
  254. tr.find('td.remove').append($('<img alt="Delete" title="'+t('settings','Delete')+'" class="svg action" src="'+OC.imagePath('core','actions/delete')+'"/>'));
  255. }
  256. applyMultiplySelect(select);
  257. $('#content table tbody').last().append(tr);
  258. tr.find('select.quota-user option').attr('selected',null);
  259. tr.find('select.quota-user option').first().attr('selected','selected');
  260. tr.find('select.quota-user').data('previous','default');
  261. }
  262. }
  263. );
  264. });
  265. // Handle undo notifications
  266. $('#notification').hide();
  267. $('#notification').click(function(){
  268. if($('#notification').data('deleteuser'))
  269. {
  270. $( 'tr' ).filterAttr( 'data-uid', UserList.deleteUid ).show();
  271. UserList.deleteCanceled=true;
  272. UserList.deleteFiles=null;
  273. }
  274. $('#notification').fadeOut();
  275. });
  276. UserList.useUndo=('onbeforeunload' in window)
  277. $(window).bind('beforeunload', function (){
  278. UserList.finishDelete(null);
  279. });
  280. });