users.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. var 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. if (typeof UserList.deleteUid !== 'undefined') {
  18. //Already a user in the undo queue
  19. UserList.finishDelete(null);
  20. }
  21. UserList.deleteUid = uid;
  22. // Set undo flag
  23. UserList.deleteCanceled = false;
  24. // Provide user with option to undo
  25. $('#notification').html(t('users', 'deleted')+' '+uid+'<span class="undo">'+t('users', 'undo')+'</span>');
  26. $('#notification').data('deleteuser',true);
  27. $('#notification').fadeIn();
  28. },
  29. /**
  30. * @brief Delete a user via ajax
  31. * @param bool ready whether to use ready() upon completion
  32. *
  33. * Executes deletion via ajax of user identified by property deleteUid
  34. * if 'undo' has not been used. Completes the user deletion procedure
  35. * and reflects success in UI.
  36. */
  37. finishDelete:function( ready ){
  38. // Check deletion has not been undone
  39. if( !UserList.deleteCanceled && UserList.deleteUid ){
  40. // Delete user via ajax
  41. $.ajax({
  42. type: 'POST',
  43. url: OC.filePath('settings', 'ajax', 'removeuser.php'),
  44. async: false,
  45. data: { username: UserList.deleteUid },
  46. success: function(result) {
  47. if (result.status == 'success') {
  48. // Remove undo option, & remove user from table
  49. $('#notification').fadeOut();
  50. $('tr').filterAttr('data-uid', UserList.deleteUid).remove();
  51. UserList.deleteCanceled = true;
  52. if (ready) {
  53. ready();
  54. }
  55. } else {
  56. oc.dialogs.alert(result.data.message, t('settings', 'Unable to remove user'));
  57. }
  58. }
  59. });
  60. }
  61. },
  62. add:function(username, groups, subadmin, quota, sort) {
  63. var tr = $('tbody tr').first().clone();
  64. tr.attr('data-uid', username);
  65. tr.find('td.name').text(username);
  66. var groupsSelect = $('<select multiple="multiple" class="groupsselect" data-placehoder="Groups" title="Groups">');
  67. groupsSelect.data('username', username);
  68. groupsSelect.data('userGroups', groups);
  69. tr.find('td.groups').empty();
  70. if (tr.find('td.subadmins').length > 0) {
  71. var subadminSelect = $('<select multiple="multiple" class="subadminsselect" data-placehoder="subadmins" title="' + t('files', 'Group Admin') + '">');
  72. subadminSelect.data('username', username);
  73. subadminSelect.data('userGroups', groups);
  74. subadminSelect.data('subadmin', subadmin);
  75. tr.find('td.subadmins').empty();
  76. }
  77. var allGroups = String($('#content table').attr('data-groups')).split(', ');
  78. $.each(allGroups, function(i, group) {
  79. groupsSelect.append($('<option value="'+group+'">'+group+'</option>'));
  80. if (typeof subadminSelect !== 'undefined' && group != 'admin') {
  81. subadminSelect.append($('<option value="'+group+'">'+group+'</option>'));
  82. }
  83. });
  84. tr.find('td.groups').append(groupsSelect);
  85. UserList.applyMultiplySelect(groupsSelect);
  86. if (tr.find('td.subadmins').length > 0) {
  87. tr.find('td.subadmins').append(subadminSelect);
  88. UserList.applyMultiplySelect(subadminSelect);
  89. }
  90. if (tr.find('td.remove img').length == 0 && OC.currentUser != username) {
  91. var rm_img = $('<img>', {
  92. class: 'svg action',
  93. src: OC.imagePath('core','actions/delete'),
  94. alt: t('settings','Delete'),
  95. title: t('settings','Delete')
  96. });
  97. var rm_link = $('<a>', { class: 'action delete', href: '#'}).append(rm_img);
  98. tr.find('td.remove').append(rm_link);
  99. } else if (OC.currentUser == username) {
  100. tr.find('td.remove a').remove();
  101. }
  102. var quotaSelect = tr.find('select.quota-user');
  103. if (quota == 'default') {
  104. quotaSelect.find('option').attr('selected', null);
  105. quotaSelect.find('option').first().attr('selected', 'selected');
  106. quotaSelect.data('previous', 'default');
  107. } else {
  108. if (quotaSelect.find('option[value="'+quota+'"]').length > 0) {
  109. quotaSelect.find('option[value="'+quota+'"]').attr('selected', 'selected');
  110. } else {
  111. quotaSelect.append('<option value="'+quota+'" selected="selected">'+quota+'</option>');
  112. }
  113. }
  114. var added = false;
  115. if (sort) {
  116. username = username.toLowerCase();
  117. $('tbody tr').each(function() {
  118. if (username < $(this).attr('data-uid').toLowerCase()) {
  119. $(tr).insertBefore($(this));
  120. added = true;
  121. return false;
  122. }
  123. });
  124. }
  125. if (!added) {
  126. $(tr).appendTo('tbody');
  127. }
  128. return tr;
  129. },
  130. update:function() {
  131. if (typeof UserList.offset === 'undefined') {
  132. UserList.offset = $('tbody tr').length;
  133. }
  134. $.get(OC.Router.generate('settings_ajax_userlist', { offset: UserList.offset }), function(result) {
  135. if (result.status === 'success') {
  136. $.each(result.data, function(index, user) {
  137. var tr = UserList.add(user.name, user.groups, user.subadmin, user.quota, false);
  138. UserList.offset++;
  139. if (index == 9) {
  140. $(tr).bind('inview', function(event, isInView, visiblePartX, visiblePartY) {
  141. $(this).unbind(event);
  142. UserList.update();
  143. });
  144. }
  145. });
  146. }
  147. });
  148. },
  149. applyMultiplySelect:function(element) {
  150. var checked=[];
  151. var user=element.attr('data-username');
  152. if($(element).attr('class') == 'groupsselect'){
  153. if(element.data('userGroups')){
  154. checked=String(element.data('userGroups')).split(', ');
  155. }
  156. if(user){
  157. var checkHandeler=function(group){
  158. if(user==OC.currentUser && group=='admin'){
  159. return false;
  160. }
  161. if(!isadmin && checked.length == 1 && checked[0] == group){
  162. return false;
  163. }
  164. $.post(
  165. OC.filePath('settings','ajax','togglegroups.php'),
  166. {
  167. username:user,
  168. group:group
  169. },
  170. function(){}
  171. );
  172. };
  173. }else{
  174. checkHandeler=false;
  175. }
  176. var addGroup = function(group) {
  177. $('select[multiple]').each(function(index, element) {
  178. if ($(element).find('option[value="'+group +'"]').length == 0) {
  179. $(element).append('<option value="'+group+'">'+group+'</option>');
  180. }
  181. })
  182. };
  183. var label;
  184. if(isadmin){
  185. label = t('core', 'add group');
  186. }else{
  187. label = null;
  188. }
  189. element.multiSelect({
  190. createCallback:addGroup,
  191. createText:label,
  192. checked:checked,
  193. oncheck:checkHandeler,
  194. onuncheck:checkHandeler,
  195. minWidth: 100,
  196. });
  197. }
  198. if($(element).attr('class') == 'subadminsselect'){
  199. if(element.data('subadmin')){
  200. checked=String(element.data('subadmin')).split(', ');
  201. }
  202. var checkHandeler=function(group){
  203. if(group=='admin'){
  204. return false;
  205. }
  206. $.post(
  207. OC.filePath('settings','ajax','togglesubadmins.php'),
  208. {
  209. username:user,
  210. group:group
  211. },
  212. function(){}
  213. );
  214. };
  215. var addSubAdmin = function(group) {
  216. $('select[multiple]').each(function(index, element) {
  217. if ($(element).find('option[value="'+group +'"]').length == 0) {
  218. $(element).append('<option value="'+group+'">'+group+'</option>');
  219. }
  220. })
  221. };
  222. element.multiSelect({
  223. createCallback:addSubAdmin,
  224. createText:null,
  225. checked:checked,
  226. oncheck:checkHandeler,
  227. onuncheck:checkHandeler,
  228. minWidth: 100,
  229. });
  230. }
  231. }
  232. }
  233. $(document).ready(function(){
  234. $('tbody tr:last').bind('inview', function(event, isInView, visiblePartX, visiblePartY) {
  235. UserList.update();
  236. });
  237. function setQuota(uid,quota,ready){
  238. $.post(
  239. OC.filePath('settings','ajax','setquota.php'),
  240. {username:uid,quota:quota},
  241. function(result){
  242. if(ready){
  243. ready(result.data.quota);
  244. }
  245. }
  246. );
  247. }
  248. $('select[multiple]').each(function(index,element){
  249. UserList.applyMultiplySelect($(element));
  250. });
  251. $('td.remove>a').live('click',function(event){
  252. var row = $(this).parent().parent();
  253. var uid = $(row).attr('data-uid');
  254. $(row).hide();
  255. // Call function for handling delete/undo
  256. UserList.do_delete(uid);
  257. });
  258. $('td.password>img').live('click',function(event){
  259. event.stopPropagation();
  260. var img=$(this);
  261. var uid=img.parent().parent().attr('data-uid');
  262. var input=$('<input type="password">');
  263. img.css('display','none');
  264. img.parent().children('span').replaceWith(input);
  265. input.focus();
  266. input.keypress(function(event) {
  267. if(event.keyCode == 13) {
  268. if($(this).val().length>0){
  269. $.post(
  270. OC.filePath('settings','ajax','changepassword.php'),
  271. {username:uid,password:$(this).val()},
  272. function(result){}
  273. );
  274. input.blur();
  275. }else{
  276. input.blur();
  277. }
  278. }
  279. });
  280. input.blur(function(){
  281. $(this).replaceWith($('<span>●●●●●●●</span>'));
  282. img.css('display','');
  283. });
  284. });
  285. $('td.password').live('click',function(event){
  286. $(this).children('img').click();
  287. });
  288. $('select.quota, select.quota-user').live('change',function(){
  289. var select=$(this);
  290. var uid=$(this).parent().parent().parent().attr('data-uid');
  291. var quota=$(this).val();
  292. var other=$(this).next();
  293. if(quota!='other'){
  294. other.hide();
  295. select.data('previous',quota);
  296. setQuota(uid,quota);
  297. }else{
  298. other.show();
  299. select.addClass('active');
  300. other.focus();
  301. }
  302. });
  303. $('select.quota, select.quota-user').each(function(i,select){
  304. $(select).data('previous',$(select).val());
  305. })
  306. $('input.quota-other').live('change',function(){
  307. var uid=$(this).parent().parent().parent().attr('data-uid');
  308. var quota=$(this).val();
  309. var select=$(this).prev();
  310. var other=$(this);
  311. if(quota){
  312. setQuota(uid,quota,function(quota){
  313. select.children().attr('selected',null);
  314. var existingOption=select.children().filter(function(i,option){
  315. return ($(option).val()==quota);
  316. });
  317. if(existingOption.length){
  318. existingOption.attr('selected','selected');
  319. }else{
  320. var option=$('<option/>');
  321. option.attr('selected','selected').attr('value',quota).text(quota);
  322. select.children().last().before(option);
  323. }
  324. select.val(quota);
  325. select.removeClass('active');
  326. other.val(null);
  327. other.hide();
  328. });
  329. }else{
  330. var previous=select.data('previous');
  331. select.children().attr('selected',null);
  332. select.children().each(function(i,option){
  333. if($(option).val()==previous){
  334. $(option).attr('selected','selected');
  335. }
  336. });
  337. select.removeClass('active');
  338. other.hide();
  339. }
  340. });
  341. $('input.quota-other').live('blur',function(){
  342. $(this).change();
  343. })
  344. $('#newuser').submit(function(event){
  345. event.preventDefault();
  346. var username=$('#newusername').val();
  347. var password=$('#newuserpassword').val();
  348. if($('#content table tbody tr').filterAttr('data-uid',username).length>0){
  349. OC.dialogs.alert('The username is already being used', 'Error creating user');
  350. return;
  351. }
  352. if($.trim(username) == '') {
  353. OC.dialogs.alert('A valid username must be provided', 'Error creating user');
  354. return false;
  355. }
  356. if($.trim(password) == '') {
  357. OC.dialogs.alert('A valid password must be provided', 'Error creating user');
  358. return false;
  359. }
  360. var groups=$('#newusergroups').prev().children('div').data('settings').checked;
  361. $('#newuser').get(0).reset();
  362. $.post(
  363. OC.filePath('settings','ajax','createuser.php'),
  364. {
  365. username:username,
  366. password:password,
  367. groups:groups,
  368. },
  369. function(result){
  370. if(result.status!='success'){
  371. OC.dialogs.alert(result.data.message, 'Error creating user');
  372. } else {
  373. UserList.add(username, result.data.groups, null, 'default', true);
  374. }
  375. }
  376. );
  377. });
  378. // Handle undo notifications
  379. $('#notification').hide();
  380. $('#notification .undo').live('click', function() {
  381. if($('#notification').data('deleteuser')) {
  382. $('tbody tr').filterAttr('data-uid', UserList.deleteUid).show();
  383. UserList.deleteCanceled=true;
  384. }
  385. $('#notification').fadeOut();
  386. });
  387. UserList.useUndo=('onbeforeunload' in window)
  388. $(window).bind('beforeunload', function (){
  389. UserList.finishDelete(null);
  390. });
  391. });