users.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. /**
  2. * Copyright (c) 2014, Arthur Schiwon <blizzz@owncloud.com>
  3. * Copyright (c) 2014, Raghu Nayyar <beingminimal@gmail.com>
  4. * Copyright (c) 2011, Robin Appelman <icewind1991@gmail.com>
  5. * This file is licensed under the Affero General Public License version 3 or later.
  6. * See the COPYING-README file.
  7. */
  8. var $userList;
  9. var $userListBody;
  10. var UserDeleteHandler;
  11. var UserList = {
  12. availableGroups: [],
  13. offset: 0,
  14. usersToLoad: 10, //So many users will be loaded when user scrolls down
  15. initialUsersToLoad: 50, //initial number of users to load
  16. currentGid: '',
  17. filter: '',
  18. /**
  19. * Initializes the user list
  20. * @param $el user list table element
  21. */
  22. initialize: function($el) {
  23. this.$el = $el;
  24. // initially the list might already contain user entries (not fully ajaxified yet)
  25. // initialize these entries
  26. this.$el.find('.quota-user').singleSelect().on('change', this.onQuotaSelect);
  27. },
  28. /**
  29. * Add a user row from user object
  30. *
  31. * @param user object containing following keys:
  32. * {
  33. * 'name': 'username',
  34. * 'displayname': 'Users display name',
  35. * 'groups': ['group1', 'group2'],
  36. * 'subadmin': ['group4', 'group5'],
  37. * 'quota': '10 GB',
  38. * 'storageLocation': '/srv/www/owncloud/data/username',
  39. * 'lastLogin': '1418632333'
  40. * 'backend': 'LDAP',
  41. * 'email': 'username@example.org'
  42. * 'isRestoreDisabled':false
  43. * }
  44. * @param sort
  45. * @returns table row created for this user
  46. */
  47. add: function (user, sort) {
  48. if (this.currentGid && this.currentGid !== '_everyone' && _.indexOf(user.groups, this.currentGid) < 0) {
  49. return;
  50. }
  51. var $tr = $userListBody.find('tr:first-child').clone();
  52. // this removes just the `display:none` of the template row
  53. $tr.removeAttr('style');
  54. var subAdminsEl;
  55. var subAdminSelect;
  56. var groupsSelect;
  57. /**
  58. * Avatar or placeholder
  59. */
  60. if ($tr.find('div.avatardiv').length) {
  61. if (user.isAvatarAvailable === true) {
  62. $('div.avatardiv', $tr).avatar(user.name, 32, undefined, undefined, undefined, user.displayname);
  63. } else {
  64. $('div.avatardiv', $tr).imageplaceholder(user.displayname, undefined, 32);
  65. }
  66. }
  67. /**
  68. * add username and displayname to row (in data and visible markup)
  69. */
  70. $tr.data('uid', user.name);
  71. $tr.data('displayname', user.displayname);
  72. $tr.data('mailAddress', user.email);
  73. $tr.data('restoreDisabled', user.isRestoreDisabled);
  74. $tr.find('.name').text(user.name);
  75. $tr.find('td.displayName > span').text(user.displayname);
  76. $tr.find('td.mailAddress > span').text(user.email);
  77. $tr.find('td.displayName > .action').tooltip({placement: 'top'});
  78. $tr.find('td.mailAddress > .action').tooltip({placement: 'top'});
  79. $tr.find('td.password > .action').tooltip({placement: 'top'});
  80. /**
  81. * groups and subadmins
  82. */
  83. // make them look like the multiselect buttons
  84. // until they get time to really get initialized
  85. groupsSelect = $('<select multiple="multiple" class="groupsselect multiselect button" data-placehoder="Groups" title="' + t('settings', 'No group') + '"></select>')
  86. .data('username', user.name)
  87. .data('user-groups', user.groups);
  88. if ($tr.find('td.subadmins').length > 0) {
  89. subAdminSelect = $('<select multiple="multiple" class="subadminsselect multiselect button" data-placehoder="subadmins" title="' + t('settings', 'No group') + '">')
  90. .data('username', user.name)
  91. .data('user-groups', user.groups)
  92. .data('subadmin', user.subadmin);
  93. $tr.find('td.subadmins').empty();
  94. }
  95. $.each(this.availableGroups, function (i, group) {
  96. groupsSelect.append($('<option value="' + escapeHTML(group) + '">' + escapeHTML(group) + '</option>'));
  97. if (typeof subAdminSelect !== 'undefined' && group !== 'admin') {
  98. subAdminSelect.append($('<option value="' + escapeHTML(group) + '">' + escapeHTML(group) + '</option>'));
  99. }
  100. });
  101. $tr.find('td.groups').empty().append(groupsSelect);
  102. subAdminsEl = $tr.find('td.subadmins');
  103. if (subAdminsEl.length > 0) {
  104. subAdminsEl.append(subAdminSelect);
  105. }
  106. /**
  107. * remove action
  108. */
  109. if ($tr.find('td.remove img').length === 0 && OC.currentUser !== user.name) {
  110. var deleteImage = $('<img class="action">').attr({
  111. src: OC.imagePath('core', 'actions/delete')
  112. });
  113. var deleteLink = $('<a class="action delete">')
  114. .attr({ href: '#', 'original-title': t('settings', 'Delete')})
  115. .append(deleteImage);
  116. $tr.find('td.remove').append(deleteLink);
  117. } else if (OC.currentUser === user.name) {
  118. $tr.find('td.remove a').remove();
  119. }
  120. /**
  121. * quota
  122. */
  123. var $quotaSelect = $tr.find('.quota-user');
  124. if (user.quota === 'default') {
  125. $quotaSelect
  126. .data('previous', 'default')
  127. .find('option').attr('selected', null)
  128. .first().attr('selected', 'selected');
  129. } else {
  130. var $options = $quotaSelect.find('option');
  131. var $foundOption = $options.filterAttr('value', user.quota);
  132. if ($foundOption.length > 0) {
  133. $foundOption.attr('selected', 'selected');
  134. } else {
  135. // append before "Other" entry
  136. $options.last().before('<option value="' + escapeHTML(user.quota) + '" selected="selected">' + escapeHTML(user.quota) + '</option>');
  137. }
  138. }
  139. /**
  140. * storage location
  141. */
  142. $tr.find('td.storageLocation').text(user.storageLocation);
  143. /**
  144. * user backend
  145. */
  146. $tr.find('td.userBackend').text(user.backend);
  147. /**
  148. * last login
  149. */
  150. var lastLoginRel = t('settings', 'never');
  151. var lastLoginAbs = lastLoginRel;
  152. if(user.lastLogin !== 0) {
  153. lastLoginRel = OC.Util.relativeModifiedDate(user.lastLogin);
  154. lastLoginAbs = OC.Util.formatDate(user.lastLogin);
  155. }
  156. var $tdLastLogin = $tr.find('td.lastLogin');
  157. $tdLastLogin.text(lastLoginRel);
  158. $tdLastLogin.attr('title', lastLoginAbs);
  159. // setup tooltip with #app-content as container to prevent the td to resize on hover
  160. $tdLastLogin.tooltip({placement: 'top', container: '#app-content'});
  161. /**
  162. * append generated row to user list
  163. */
  164. $tr.appendTo($userList);
  165. if(UserList.isEmpty === true) {
  166. //when the list was emptied, one row was left, necessary to keep
  167. //add working and the layout unbroken. We need to remove this item
  168. $tr.show();
  169. $userListBody.find('tr:first').remove();
  170. UserList.isEmpty = false;
  171. UserList.checkUsersToLoad();
  172. }
  173. /**
  174. * sort list
  175. */
  176. if (sort) {
  177. UserList.doSort();
  178. }
  179. $quotaSelect.on('change', UserList.onQuotaSelect);
  180. // defer init so the user first sees the list appear more quickly
  181. window.setTimeout(function(){
  182. $quotaSelect.singleSelect();
  183. UserList.applyGroupSelect(groupsSelect);
  184. if (subAdminSelect) {
  185. UserList.applySubadminSelect(subAdminSelect);
  186. }
  187. }, 0);
  188. return $tr;
  189. },
  190. // From http://my.opera.com/GreyWyvern/blog/show.dml/1671288
  191. alphanum: function(a, b) {
  192. function chunkify(t) {
  193. var tz = [], x = 0, y = -1, n = 0, i, j;
  194. while (i = (j = t.charAt(x++)).charCodeAt(0)) {
  195. var m = (i === 46 || (i >=48 && i <= 57));
  196. if (m !== n) {
  197. tz[++y] = "";
  198. n = m;
  199. }
  200. tz[y] += j;
  201. }
  202. return tz;
  203. }
  204. var aa = chunkify(a.toLowerCase());
  205. var bb = chunkify(b.toLowerCase());
  206. for (var x = 0; aa[x] && bb[x]; x++) {
  207. if (aa[x] !== bb[x]) {
  208. var c = Number(aa[x]), d = Number(bb[x]);
  209. if (c === aa[x] && d === bb[x]) {
  210. return c - d;
  211. } else {
  212. return (aa[x] > bb[x]) ? 1 : -1;
  213. }
  214. }
  215. }
  216. return aa.length - bb.length;
  217. },
  218. preSortSearchString: function(a, b) {
  219. var pattern = this.filter;
  220. if(typeof pattern === 'undefined') {
  221. return undefined;
  222. }
  223. pattern = pattern.toLowerCase();
  224. var aMatches = false;
  225. var bMatches = false;
  226. if(typeof a === 'string' && a.toLowerCase().indexOf(pattern) === 0) {
  227. aMatches = true;
  228. }
  229. if(typeof b === 'string' && b.toLowerCase().indexOf(pattern) === 0) {
  230. bMatches = true;
  231. }
  232. if((aMatches && bMatches) || (!aMatches && !bMatches)) {
  233. return undefined;
  234. }
  235. if(aMatches) {
  236. return -1;
  237. } else {
  238. return 1;
  239. }
  240. },
  241. doSort: function() {
  242. // some browsers like Chrome lose the scrolling information
  243. // when messing with the list elements
  244. var lastScrollTop = this.scrollArea.scrollTop();
  245. var lastScrollLeft = this.scrollArea.scrollLeft();
  246. var rows = $userListBody.find('tr').get();
  247. rows.sort(function(a, b) {
  248. // FIXME: inefficient way of getting the names,
  249. // better use a data attribute
  250. a = $(a).find('.name').text();
  251. b = $(b).find('.name').text();
  252. var firstSort = UserList.preSortSearchString(a, b);
  253. if(typeof firstSort !== 'undefined') {
  254. return firstSort;
  255. }
  256. return OC.Util.naturalSortCompare(a, b);
  257. });
  258. var items = [];
  259. $.each(rows, function(index, row) {
  260. items.push(row);
  261. if(items.length === 100) {
  262. $userListBody.append(items);
  263. items = [];
  264. }
  265. });
  266. if(items.length > 0) {
  267. $userListBody.append(items);
  268. }
  269. this.scrollArea.scrollTop(lastScrollTop);
  270. this.scrollArea.scrollLeft(lastScrollLeft);
  271. },
  272. checkUsersToLoad: function() {
  273. //30 shall be loaded initially, from then on always 10 upon scrolling
  274. if(UserList.isEmpty === false) {
  275. UserList.usersToLoad = 10;
  276. } else {
  277. UserList.usersToLoad = UserList.initialUsersToLoad;
  278. }
  279. },
  280. empty: function() {
  281. //one row needs to be kept, because it is cloned to add new rows
  282. $userListBody.find('tr:not(:first)').remove();
  283. var $tr = $userListBody.find('tr:first');
  284. $tr.hide();
  285. //on an update a user may be missing when the username matches with that
  286. //of the hidden row. So change this to a random string.
  287. $tr.data('uid', Math.random().toString(36).substring(2));
  288. UserList.isEmpty = true;
  289. UserList.offset = 0;
  290. UserList.checkUsersToLoad();
  291. },
  292. hide: function(uid) {
  293. UserList.getRow(uid).hide();
  294. },
  295. show: function(uid) {
  296. UserList.getRow(uid).show();
  297. },
  298. markRemove: function(uid) {
  299. var $tr = UserList.getRow(uid);
  300. var groups = $tr.find('.groups .groupsselect').val();
  301. for(var i in groups) {
  302. var gid = groups[i];
  303. var $li = GroupList.getGroupLI(gid);
  304. var userCount = GroupList.getUserCount($li);
  305. GroupList.setUserCount($li, userCount - 1);
  306. }
  307. GroupList.decEveryoneCount();
  308. UserList.hide(uid);
  309. },
  310. remove: function(uid) {
  311. UserList.getRow(uid).remove();
  312. },
  313. undoRemove: function(uid) {
  314. var $tr = UserList.getRow(uid);
  315. var groups = $tr.find('.groups .groupsselect').val();
  316. for(var i in groups) {
  317. var gid = groups[i];
  318. var $li = GroupList.getGroupLI(gid);
  319. var userCount = GroupList.getUserCount($li);
  320. GroupList.setUserCount($li, userCount + 1);
  321. }
  322. GroupList.incEveryoneCount();
  323. UserList.getRow(uid).show();
  324. },
  325. has: function(uid) {
  326. return UserList.getRow(uid).length > 0;
  327. },
  328. getRow: function(uid) {
  329. return $userListBody.find('tr').filter(function(){
  330. return UserList.getUID(this) === uid;
  331. });
  332. },
  333. getUID: function(element) {
  334. return ($(element).closest('tr').data('uid') || '').toString();
  335. },
  336. getDisplayName: function(element) {
  337. return ($(element).closest('tr').data('displayname') || '').toString();
  338. },
  339. getMailAddress: function(element) {
  340. return ($(element).closest('tr').data('mailAddress') || '').toString();
  341. },
  342. getRestoreDisabled: function(element) {
  343. return ($(element).closest('tr').data('restoreDisabled') || '');
  344. },
  345. initDeleteHandling: function() {
  346. //set up handler
  347. UserDeleteHandler = new DeleteHandler('/settings/users/users', 'username',
  348. UserList.markRemove, UserList.remove);
  349. //configure undo
  350. OC.Notification.hide();
  351. var msg = escapeHTML(t('settings', 'deleted {userName}', {userName: '%oid'})) + '<span class="undo">' +
  352. escapeHTML(t('settings', 'undo')) + '</span>';
  353. UserDeleteHandler.setNotification(OC.Notification, 'deleteuser', msg,
  354. UserList.undoRemove);
  355. //when to mark user for delete
  356. $userListBody.on('click', '.delete', function () {
  357. // Call function for handling delete/undo
  358. var uid = UserList.getUID(this);
  359. UserDeleteHandler.mark(uid);
  360. });
  361. //delete a marked user when leaving the page
  362. $(window).on('beforeunload', function () {
  363. UserDeleteHandler.deleteEntry();
  364. });
  365. },
  366. update: function (gid, limit) {
  367. if (UserList.updating) {
  368. return;
  369. }
  370. if(!limit) {
  371. limit = UserList.usersToLoad;
  372. }
  373. $userList.siblings('.loading').css('visibility', 'visible');
  374. UserList.updating = true;
  375. if(gid === undefined) {
  376. gid = '';
  377. }
  378. UserList.currentGid = gid;
  379. var pattern = this.filter;
  380. $.get(
  381. OC.generateUrl('/settings/users/users'),
  382. { offset: UserList.offset, limit: limit, gid: gid, pattern: pattern },
  383. function (result) {
  384. var loadedUsers = 0;
  385. var trs = [];
  386. //The offset does not mirror the amount of users available,
  387. //because it is backend-dependent. For correct retrieval,
  388. //always the limit(requested amount of users) needs to be added.
  389. $.each(result, function (index, user) {
  390. if(UserList.has(user.name)) {
  391. return true;
  392. }
  393. var $tr = UserList.add(user, user.lastLogin, false, user.backend);
  394. trs.push($tr);
  395. loadedUsers++;
  396. });
  397. if (result.length > 0) {
  398. UserList.doSort();
  399. $userList.siblings('.loading').css('visibility', 'hidden');
  400. // reset state on load
  401. UserList.noMoreEntries = false;
  402. }
  403. else {
  404. UserList.noMoreEntries = true;
  405. $userList.siblings('.loading').remove();
  406. }
  407. UserList.offset += limit;
  408. }).always(function() {
  409. UserList.updating = false;
  410. });
  411. },
  412. applyGroupSelect: function (element) {
  413. var checked = [];
  414. var $element = $(element);
  415. var user = UserList.getUID($element);
  416. if ($element.data('user-groups')) {
  417. if (typeof $element.data('user-groups') === 'string') {
  418. checked = $element.data('user-groups').split(", ");
  419. }
  420. else {
  421. checked = $element.data('user-groups');
  422. }
  423. }
  424. var checkHandler = null;
  425. if(user) { // Only if in a user row, and not the #newusergroups select
  426. checkHandler = function (group) {
  427. if (user === OC.currentUser && group === 'admin') {
  428. return false;
  429. }
  430. if (!oc_isadmin && checked.length === 1 && checked[0] === group) {
  431. return false;
  432. }
  433. $.post(
  434. OC.filePath('settings', 'ajax', 'togglegroups.php'),
  435. {
  436. username: user,
  437. group: group
  438. },
  439. function (response) {
  440. if (response.status === 'success') {
  441. GroupList.update();
  442. var groupName = response.data.groupname;
  443. if (UserList.availableGroups.indexOf(groupName) === -1 &&
  444. response.data.action === 'add'
  445. ) {
  446. UserList.availableGroups.push(groupName);
  447. }
  448. if (response.data.action === 'add') {
  449. GroupList.incGroupCount(groupName);
  450. } else {
  451. GroupList.decGroupCount(groupName);
  452. }
  453. }
  454. if (response.data.message) {
  455. OC.Notification.show(response.data.message);
  456. }
  457. }
  458. );
  459. };
  460. }
  461. var addGroup = function (select, group) {
  462. $('select[multiple]').each(function (index, element) {
  463. $element = $(element);
  464. if ($element.find('option').filterAttr('value', group).length === 0 &&
  465. select.data('msid') !== $element.data('msid')) {
  466. $element.append('<option value="' + escapeHTML(group) + '">' + escapeHTML(group) + '</option>');
  467. }
  468. });
  469. GroupList.addGroup(escapeHTML(group));
  470. };
  471. var label;
  472. if (oc_isadmin) {
  473. label = t('settings', 'Add group');
  474. }
  475. else {
  476. label = null;
  477. }
  478. $element.multiSelect({
  479. createCallback: addGroup,
  480. createText: label,
  481. selectedFirst: true,
  482. checked: checked,
  483. oncheck: checkHandler,
  484. onuncheck: checkHandler,
  485. minWidth: 100
  486. });
  487. },
  488. applySubadminSelect: function (element) {
  489. var checked = [];
  490. var $element = $(element);
  491. var user = UserList.getUID($element);
  492. if ($element.data('subadmin')) {
  493. if (typeof $element.data('subadmin') === 'string') {
  494. checked = $element.data('subadmin').split(", ");
  495. }
  496. else {
  497. checked = $element.data('subadmin');
  498. }
  499. }
  500. var checkHandler = function (group) {
  501. if (group === 'admin') {
  502. return false;
  503. }
  504. $.post(
  505. OC.filePath('settings', 'ajax', 'togglesubadmins.php'),
  506. {
  507. username: user,
  508. group: group
  509. },
  510. function () {
  511. }
  512. );
  513. };
  514. var addSubAdmin = function (group) {
  515. $('select[multiple]').each(function (index, element) {
  516. if ($(element).find('option').filterAttr('value', group).length === 0) {
  517. $(element).append('<option value="' + escapeHTML(group) + '">' + escapeHTML(group) + '</option>');
  518. }
  519. });
  520. };
  521. $element.multiSelect({
  522. createCallback: addSubAdmin,
  523. createText: null,
  524. checked: checked,
  525. oncheck: checkHandler,
  526. onuncheck: checkHandler,
  527. minWidth: 100
  528. });
  529. },
  530. _onScroll: function() {
  531. if (!!UserList.noMoreEntries) {
  532. return;
  533. }
  534. if (UserList.scrollArea.scrollTop() + UserList.scrollArea.height() > UserList.scrollArea.get(0).scrollHeight - 500) {
  535. UserList.update(UserList.currentGid);
  536. }
  537. },
  538. /**
  539. * Event handler for when a quota has been changed through a single select.
  540. * This will save the value.
  541. */
  542. onQuotaSelect: function(ev) {
  543. var $select = $(ev.target);
  544. var uid = UserList.getUID($select);
  545. var quota = $select.val();
  546. if (quota === 'other') {
  547. return;
  548. }
  549. if (isNaN(parseInt(quota, 10)) || parseInt(quota, 10) < 0) {
  550. // the select component has added the bogus value, delete it again
  551. $select.find('option[selected]').remove();
  552. OC.Notification.showTemporary(t('core', 'Invalid quota value "{val}"', {val: quota}));
  553. return;
  554. }
  555. UserList._updateQuota(uid, quota, function(returnedQuota){
  556. if (quota !== returnedQuota) {
  557. $select.find(':selected').text(returnedQuota);
  558. }
  559. });
  560. },
  561. /**
  562. * Saves the quota for the given user
  563. * @param {String} [uid] optional user id, sets default quota if empty
  564. * @param {String} quota quota value
  565. * @param {Function} ready callback after save
  566. */
  567. _updateQuota: function(uid, quota, ready) {
  568. $.post(
  569. OC.filePath('settings', 'ajax', 'setquota.php'),
  570. {username: uid, quota: quota},
  571. function (result) {
  572. if (ready) {
  573. ready(result.data.quota);
  574. }
  575. }
  576. );
  577. }
  578. };
  579. $(document).ready(function () {
  580. $userList = $('#userlist');
  581. $userListBody = $userList.find('tbody');
  582. UserList.initDeleteHandling();
  583. // Implements User Search
  584. OCA.Search.users= new UserManagementFilter(UserList, GroupList);
  585. UserList.scrollArea = $('#app-content');
  586. UserList.doSort();
  587. UserList.availableGroups = $userList.data('groups');
  588. UserList.scrollArea.scroll(function(e) {UserList._onScroll(e);});
  589. $userList.after($('<div class="loading" style="height: 200px; visibility: hidden;"></div>'));
  590. // TODO: move other init calls inside of initialize
  591. UserList.initialize($('#userlist'));
  592. $('.groupsselect').each(function (index, element) {
  593. UserList.applyGroupSelect(element);
  594. });
  595. $('.subadminsselect').each(function (index, element) {
  596. UserList.applySubadminSelect(element);
  597. });
  598. $userListBody.on('click', '.password', function (event) {
  599. event.stopPropagation();
  600. var $td = $(this).closest('td');
  601. var $tr = $(this).closest('tr');
  602. var uid = UserList.getUID($td);
  603. var $input = $('<input type="password">');
  604. var isRestoreDisabled = UserList.getRestoreDisabled($td) === true;
  605. if(isRestoreDisabled) {
  606. $tr.addClass('row-warning');
  607. // add tipsy if the password change could cause data loss - no recovery enabled
  608. $input.tipsy({gravity:'s'});
  609. $input.attr('title', t('settings', 'Changing the password will result in data loss, because data recovery is not available for this user'));
  610. }
  611. $td.find('img').hide();
  612. $td.children('span').replaceWith($input);
  613. $input
  614. .focus()
  615. .keypress(function (event) {
  616. if (event.keyCode === 13) {
  617. if ($(this).val().length > 0) {
  618. var recoveryPasswordVal = $('input:password[id="recoveryPassword"]').val();
  619. $.post(
  620. OC.generateUrl('/settings/users/changepassword'),
  621. {username: uid, password: $(this).val(), recoveryPassword: recoveryPasswordVal},
  622. function (result) {
  623. if (result.status != 'success') {
  624. OC.Notification.showTemporary(t('admin', result.data.message));
  625. }
  626. }
  627. );
  628. $input.blur();
  629. } else {
  630. $input.blur();
  631. }
  632. }
  633. })
  634. .blur(function () {
  635. $(this).replaceWith($('<span>●●●●●●●</span>'));
  636. $td.find('img').show();
  637. // remove highlight class from users without recovery ability
  638. $tr.removeClass('row-warning');
  639. });
  640. });
  641. $('input:password[id="recoveryPassword"]').keyup(function() {
  642. OC.Notification.hide();
  643. });
  644. $userListBody.on('click', '.displayName', function (event) {
  645. event.stopPropagation();
  646. var $td = $(this).closest('td');
  647. var $tr = $td.closest('tr');
  648. var uid = UserList.getUID($td);
  649. var displayName = escapeHTML(UserList.getDisplayName($td));
  650. var $input = $('<input type="text" value="' + displayName + '">');
  651. $td.find('img').hide();
  652. $td.children('span').replaceWith($input);
  653. $input
  654. .focus()
  655. .keypress(function (event) {
  656. if (event.keyCode === 13) {
  657. if ($(this).val().length > 0) {
  658. var $div = $tr.find('div.avatardiv');
  659. if ($div.length) {
  660. $div.imageplaceholder(uid, displayName);
  661. }
  662. $.post(
  663. OC.generateUrl('/settings/users/{id}/displayName', {id: uid}),
  664. {username: uid, displayName: $(this).val()},
  665. function (result) {
  666. if (result && result.status==='success' && $div.length){
  667. $div.avatar(result.data.username, 32);
  668. }
  669. }
  670. );
  671. var displayName = $input.val();
  672. $tr.data('displayname', displayName);
  673. $input.blur();
  674. } else {
  675. $input.blur();
  676. }
  677. }
  678. })
  679. .blur(function () {
  680. var displayName = $tr.data('displayname');
  681. $input.replaceWith('<span>' + escapeHTML(displayName) + '</span>');
  682. $td.find('img').show();
  683. });
  684. });
  685. $userListBody.on('click', '.mailAddress', function (event) {
  686. event.stopPropagation();
  687. var $td = $(this).closest('td');
  688. var $tr = $td.closest('tr');
  689. var uid = UserList.getUID($td);
  690. var mailAddress = escapeHTML(UserList.getMailAddress($td));
  691. var $input = $('<input type="text">').val(mailAddress);
  692. $td.children('span').replaceWith($input);
  693. $td.find('img').hide();
  694. $input
  695. .focus()
  696. .keypress(function (event) {
  697. if (event.keyCode === 13) {
  698. // enter key
  699. var mailAddress = $input.val();
  700. $td.find('.loading-small').css('display', 'inline-block');
  701. $input.css('padding-right', '26px');
  702. $input.attr('disabled', 'disabled');
  703. $.ajax({
  704. type: 'PUT',
  705. url: OC.generateUrl('/settings/users/{id}/mailAddress', {id: uid}),
  706. data: {
  707. mailAddress: $(this).val()
  708. }
  709. }).success(function () {
  710. // set data attribute to new value
  711. // will in blur() be used to show the text instead of the input field
  712. $tr.data('mailAddress', mailAddress);
  713. $td.find('.loading-small').css('display', '');
  714. $input.removeAttr('disabled')
  715. .triggerHandler('blur'); // needed instead of $input.blur() for Firefox
  716. }).fail(function (result) {
  717. OC.Notification.showTemporary(result.responseJSON.data.message);
  718. $td.find('.loading-small').css('display', '');
  719. $input.removeAttr('disabled')
  720. .css('padding-right', '6px');
  721. });
  722. }
  723. })
  724. .blur(function () {
  725. if($td.find('.loading-small').css('display') === 'inline-block') {
  726. // in Chrome the blur event is fired too early by the browser - even if the request is still running
  727. return;
  728. }
  729. var $span = $('<span>').text($tr.data('mailAddress'));
  730. $input.replaceWith($span);
  731. $td.find('img').show();
  732. });
  733. });
  734. // init the quota field select box after it is shown the first time
  735. $('#app-settings').one('show', function() {
  736. $(this).find('#default_quota').singleSelect().on('change', UserList.onQuotaSelect);
  737. });
  738. $('#newuser').submit(function (event) {
  739. event.preventDefault();
  740. var username = $('#newusername').val();
  741. var password = $('#newuserpassword').val();
  742. var email = $('#newemail').val();
  743. if ($.trim(username) === '') {
  744. OC.Notification.showTemporary(t('settings', 'Error creating user: {message}', {
  745. message: t('settings', 'A valid username must be provided')
  746. }));
  747. return false;
  748. }
  749. if ($.trim(password) === '') {
  750. OC.Notification.showTemporary(t('settings', 'Error creating user: {message}', {
  751. message: t('settings', 'A valid password must be provided')
  752. }));
  753. return false;
  754. }
  755. if(!$('#CheckboxMailOnUserCreate').is(':checked')) {
  756. email = '';
  757. }
  758. if ($('#CheckboxMailOnUserCreate').is(':checked') && $.trim(email) === '') {
  759. OC.Notification.showTemporary( t('settings', 'Error creating user: {message}', {
  760. message: t('settings', 'A valid email must be provided')
  761. }));
  762. return false;
  763. }
  764. var promise;
  765. if (UserDeleteHandler) {
  766. promise = UserDeleteHandler.deleteEntry();
  767. } else {
  768. promise = $.Deferred().resolve().promise();
  769. }
  770. promise.then(function() {
  771. var groups = $('#newusergroups').val() || [];
  772. $.post(
  773. OC.generateUrl('/settings/users/users'),
  774. {
  775. username: username,
  776. password: password,
  777. groups: groups,
  778. email: email
  779. },
  780. function (result) {
  781. if (result.groups) {
  782. for (var i in result.groups) {
  783. var gid = result.groups[i];
  784. if(UserList.availableGroups.indexOf(gid) === -1) {
  785. UserList.availableGroups.push(gid);
  786. }
  787. $li = GroupList.getGroupLI(gid);
  788. userCount = GroupList.getUserCount($li);
  789. GroupList.setUserCount($li, userCount + 1);
  790. }
  791. }
  792. if(!UserList.has(username)) {
  793. UserList.add(result, true);
  794. }
  795. $('#newusername').focus();
  796. GroupList.incEveryoneCount();
  797. }).fail(function(result) {
  798. OC.Notification.showTemporary(t('settings', 'Error creating user: {message}', {
  799. message: result.responseJSON.message
  800. }, undefined, {escape: false}));
  801. }).success(function(){
  802. $('#newuser').get(0).reset();
  803. });
  804. });
  805. });
  806. if ($('#CheckboxStorageLocation').is(':checked')) {
  807. $("#userlist .storageLocation").show();
  808. }
  809. // Option to display/hide the "Storage location" column
  810. $('#CheckboxStorageLocation').click(function() {
  811. if ($('#CheckboxStorageLocation').is(':checked')) {
  812. $("#userlist .storageLocation").show();
  813. OC.AppConfig.setValue('core', 'umgmt_show_storage_location', 'true');
  814. } else {
  815. $("#userlist .storageLocation").hide();
  816. OC.AppConfig.setValue('core', 'umgmt_show_storage_location', 'false');
  817. }
  818. });
  819. if ($('#CheckboxLastLogin').is(':checked')) {
  820. $("#userlist .lastLogin").show();
  821. }
  822. // Option to display/hide the "Last Login" column
  823. $('#CheckboxLastLogin').click(function() {
  824. if ($('#CheckboxLastLogin').is(':checked')) {
  825. $("#userlist .lastLogin").show();
  826. OC.AppConfig.setValue('core', 'umgmt_show_last_login', 'true');
  827. } else {
  828. $("#userlist .lastLogin").hide();
  829. OC.AppConfig.setValue('core', 'umgmt_show_last_login', 'false');
  830. }
  831. });
  832. if ($('#CheckboxEmailAddress').is(':checked')) {
  833. $("#userlist .mailAddress").show();
  834. }
  835. // Option to display/hide the "Mail Address" column
  836. $('#CheckboxEmailAddress').click(function() {
  837. if ($('#CheckboxEmailAddress').is(':checked')) {
  838. $("#userlist .mailAddress").show();
  839. OC.AppConfig.setValue('core', 'umgmt_show_email', 'true');
  840. } else {
  841. $("#userlist .mailAddress").hide();
  842. OC.AppConfig.setValue('core', 'umgmt_show_email', 'false');
  843. }
  844. });
  845. if ($('#CheckboxUserBackend').is(':checked')) {
  846. $("#userlist .userBackend").show();
  847. }
  848. // Option to display/hide the "User Backend" column
  849. $('#CheckboxUserBackend').click(function() {
  850. if ($('#CheckboxUserBackend').is(':checked')) {
  851. $("#userlist .userBackend").show();
  852. OC.AppConfig.setValue('core', 'umgmt_show_backend', 'true');
  853. } else {
  854. $("#userlist .userBackend").hide();
  855. OC.AppConfig.setValue('core', 'umgmt_show_backend', 'false');
  856. }
  857. });
  858. if ($('#CheckboxMailOnUserCreate').is(':checked')) {
  859. $("#newemail").show();
  860. }
  861. // Option to display/hide the "E-Mail" input field
  862. $('#CheckboxMailOnUserCreate').click(function() {
  863. if ($('#CheckboxMailOnUserCreate').is(':checked')) {
  864. $("#newemail").show();
  865. OC.AppConfig.setValue('core', 'umgmt_send_email', 'true');
  866. } else {
  867. $("#newemail").hide();
  868. OC.AppConfig.setValue('core', 'umgmt_send_email', 'false');
  869. }
  870. });
  871. // calculate initial limit of users to load
  872. var initialUserCountLimit = UserList.initialUsersToLoad,
  873. containerHeight = $('#app-content').height();
  874. if(containerHeight > 40) {
  875. initialUserCountLimit = Math.floor(containerHeight/40);
  876. if (initialUserCountLimit < UserList.initialUsersToLoad) {
  877. initialUserCountLimit = UserList.initialUsersToLoad;
  878. }
  879. }
  880. //realign initialUserCountLimit with usersToLoad as a safeguard
  881. while((initialUserCountLimit % UserList.usersToLoad) !== 0) {
  882. // must be a multiple of this, otherwise LDAP freaks out.
  883. // FIXME: solve this in LDAP backend in 8.1
  884. initialUserCountLimit = initialUserCountLimit + 1;
  885. }
  886. // trigger loading of users on startup
  887. UserList.update(UserList.currentGid, initialUserCountLimit);
  888. _.defer(function() {
  889. $('#app-content').trigger($.Event('apprendered'));
  890. });
  891. });