users.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. $(document).ready(function(){
  7. function setQuota(uid,quota,ready){
  8. $.post(
  9. OC.filePath('settings','ajax','setquota.php'),
  10. {username:uid,quota:quota},
  11. function(result){
  12. if(ready){
  13. ready(result.data.quota);
  14. }
  15. }
  16. );
  17. }
  18. function applyMultiplySelect(element){
  19. var checked=[];
  20. var user=element.data('username');
  21. if(element.data('userGroups')){
  22. checked=element.data('userGroups').split(', ');
  23. }
  24. if(user){
  25. var checkHandeler=function(group){
  26. if(user==OC.currentUser && group=='admin'){
  27. return false;
  28. }
  29. $.post(
  30. OC.filePath('settings','ajax','togglegroups.php'),
  31. {
  32. username:user,
  33. group:group
  34. },
  35. function(){}
  36. );
  37. };
  38. }else{
  39. checkHandeler=false;
  40. }
  41. element.multiSelect({
  42. createText:'add group',
  43. checked:checked,
  44. oncheck:checkHandeler,
  45. onuncheck:checkHandeler,
  46. minWidth: 100,
  47. });
  48. }
  49. $('select[multiple]').each(function(index,element){
  50. applyMultiplySelect($(element));
  51. });
  52. $('td.remove>img').live('click',function(event){
  53. var uid=$(this).parent().parent().data('uid');
  54. $.post(
  55. OC.filePath('settings','ajax','removeuser.php'),
  56. {username:uid},
  57. function(result){
  58. }
  59. );
  60. $(this).parent().parent().remove();
  61. });
  62. $('td.password>img').live('click',function(event){
  63. event.stopPropagation();
  64. var img=$(this);
  65. var uid=img.parent().parent().data('uid');
  66. var input=$('<input type="password">');
  67. img.css('display','none');
  68. img.parent().children('span').replaceWith(input);
  69. input.focus();
  70. input.keypress(function(event) {
  71. if(event.keyCode == 13) {
  72. if($(this).val().length>0){
  73. $.post(
  74. OC.filePath('settings','ajax','changepassword.php'),
  75. {username:uid,password:$(this).val()},
  76. function(result){}
  77. );
  78. input.blur();
  79. }else{
  80. input.blur();
  81. }
  82. }
  83. });
  84. input.blur(function(){
  85. $(this).replaceWith($('<span>●●●●●●●</span>'));
  86. img.css('display','');
  87. });
  88. });
  89. $('td.password').live('click',function(event){
  90. $(this).children('img').click();
  91. });
  92. $('select.quota, select.quota-user').live('change',function(){
  93. var select=$(this);
  94. var uid=$(this).parent().parent().parent().data('uid');
  95. var quota=$(this).val();
  96. var other=$(this).next();
  97. if(quota!='other'){
  98. other.hide();
  99. select.data('previous',quota);
  100. setQuota(uid,quota);
  101. }else{
  102. other.show();
  103. select.addClass('active');
  104. other.focus();
  105. }
  106. });
  107. $('select.quota, select.quota-user').each(function(i,select){
  108. $(select).data('previous',$(select).val());
  109. })
  110. $('input.quota-other').live('change',function(){
  111. var uid=$(this).parent().parent().data('uid');
  112. var quota=$(this).val();
  113. var select=$(this).prev();
  114. var other=$(this);
  115. if(quota){
  116. setQuota(uid,quota,function(quota){
  117. select.children().attr('selected',null);
  118. var existingOption=select.children().filter(function(i,option){
  119. return ($(option).val()==quota);
  120. });
  121. if(existingOption.length){
  122. existingOption.attr('selected','selected');
  123. }else{
  124. var option=$('<option/>');
  125. option.attr('selected','selected').attr('value',quota).text(quota);
  126. select.children().last().before(option);
  127. }
  128. select.val(quota);
  129. select.removeClass('active');
  130. other.val(null);
  131. other.hide();
  132. });
  133. }else{
  134. var previous=select.data('previous');
  135. select.children().attr('selected',null);
  136. select.children().each(function(i,option){
  137. if($(option).val()==previous){
  138. $(option).attr('selected','selected');
  139. }
  140. });
  141. select.removeClass('active');
  142. other.hide();
  143. }
  144. });
  145. $('input.quota-other').live('blur',function(){
  146. $(this).change();
  147. })
  148. $('#newuser').submit(function(event){
  149. event.preventDefault();
  150. var username=$('#newusername').val();
  151. var password=$('#newuserpassword').val();
  152. if($('#content table tbody tr').filterAttr('data-uid',username).length>0){
  153. OC.dialogs.alert('The username is already being used', 'Error creating user');
  154. return;
  155. }
  156. if($.trim(username) == '') {
  157. OC.dialogs.alert('A valid username must be provided', 'Error creating user');
  158. return false;
  159. }
  160. if($.trim(password) == '') {
  161. OC.dialogs.alert('A valid password must be provided', 'Error creating user');
  162. return false;
  163. }
  164. var groups=$('#newusergroups').prev().children('div').data('settings').checked;
  165. $('#newuser').get(0).reset();
  166. $.post(
  167. OC.filePath('settings','ajax','createuser.php'),
  168. {
  169. username:username,
  170. password:password,
  171. groups:groups,
  172. },
  173. function(result){
  174. if(result.status!='success'){
  175. OC.dialogs.alert(result.data.message, 'Error creating user');
  176. }
  177. else {
  178. var tr=$('#content table tbody tr').first().clone();
  179. tr.attr('data-uid',username);
  180. tr.find('td.name').text(username);
  181. var select=$('<select multiple="multiple" data-placehoder="Groups" title="Groups">');
  182. select.data('username',username);
  183. select.data('userGroups',groups.join(', '));
  184. tr.find('td.groups').empty();
  185. var allGroups=$('#content table').data('groups').split(', ');
  186. for(var i=0;i<groups.length;i++){
  187. if(allGroups.indexOf(groups[i])==-1){
  188. allGroups.push(groups[i]);
  189. }
  190. }
  191. $.each(allGroups,function(i,group){
  192. select.append($('<option value="'+group+'">'+group+'</option>'));
  193. });
  194. tr.find('td.groups').append(select);
  195. if(tr.find('td.remove img').length==0){
  196. tr.find('td.remove').append($('<img alt="Delete" title="'+t('settings','Delete')+'" class="svg action" src="'+OC.imagePath('core','actions/delete')+'"/>'));
  197. }
  198. applyMultiplySelect(select);
  199. $('#content table tbody').last().append(tr);
  200. tr.find('select.quota-user option').attr('selected',null);
  201. tr.find('select.quota-user option').first().attr('selected','selected');
  202. tr.find('select.quota-user').data('previous','default');
  203. }
  204. }
  205. );
  206. });
  207. });