/** * Copyright (c) 2014, Arthur Schiwon * Copyright (c) 2014, Raghu Nayyar * Copyright (c) 2011, Robin Appelman * This file is licensed under the Affero General Public License version 3 or later. * See the COPYING-README file. */ var $userList; var $userListBody; var UserDeleteHandler; var UserList = { availableGroups: [], offset: 0, usersToLoad: 10, //So many users will be loaded when user scrolls down initialUsersToLoad: 250, //initial number of users to load currentGid: '', filter: '', /** * Initializes the user list * @param $el user list table element */ initialize: function($el) { this.$el = $el; // initially the list might already contain user entries (not fully ajaxified yet) // initialize these entries this.$el.find('.quota-user').singleSelect().on('change', this.onQuotaSelect); }, /** * Add a user row from user object * * @param user object containing following keys: * { * 'name': 'username', * 'displayname': 'Users display name', * 'groups': ['group1', 'group2'], * 'subadmin': ['group4', 'group5'], * 'quota': '10 GB', * 'storageLocation': '/srv/www/owncloud/data/username', * 'lastLogin': '1418632333' * 'backend': 'LDAP', * 'email': 'username@example.org' * 'isRestoreDisabled':false * } * @param sort * @returns table row created for this user */ add: function (user, sort) { if (this.currentGid && this.currentGid !== '_everyone' && _.indexOf(user.groups, this.currentGid) < 0) { return; } var $tr = $userListBody.find('tr:first-child').clone(); // this removes just the `display:none` of the template row $tr.removeAttr('style'); var subAdminsEl; var subAdminSelect; var groupsSelect; /** * Avatar or placeholder */ if ($tr.find('div.avatardiv').length){ $tr.find('.avatardiv').imageplaceholder(user.name, user.displayname); $('div.avatardiv', $tr).avatar(user.name, 32); } /** * add username and displayname to row (in data and visible markup) */ $tr.data('uid', user.name); $tr.data('displayname', user.displayname); $tr.data('mailAddress', user.email); $tr.data('restoreDisabled', user.isRestoreDisabled); $tr.find('.name').text(user.name); $tr.find('td.displayName > span').text(user.displayname); $tr.find('td.mailAddress > span').text(user.email); $tr.find('td.displayName > .action').tooltip({placement: 'top'}); $tr.find('td.mailAddress > .action').tooltip({placement: 'top'}); $tr.find('td.password > .action').tooltip({placement: 'top'}); /** * groups and subadmins */ // make them look like the multiselect buttons // until they get time to really get initialized groupsSelect = $('') .data('username', user.name) .data('user-groups', user.groups); if ($tr.find('td.subadmins').length > 0) { subAdminSelect = $(''); var isRestoreDisabled = UserList.getRestoreDisabled($td) === true; if(isRestoreDisabled) { $tr.addClass('row-warning'); // add tipsy if the password change could cause data loss - no recovery enabled $input.tipsy({gravity:'s'}); $input.attr('title', t('settings', 'Changing the password will result in data loss, because data recovery is not available for this user')); } $td.find('img').hide(); $td.children('span').replaceWith($input); $input .focus() .keypress(function (event) { if (event.keyCode === 13) { if ($(this).val().length > 0) { var recoveryPasswordVal = $('input:password[id="recoveryPassword"]').val(); $.post( OC.generateUrl('/settings/users/changepassword'), {username: uid, password: $(this).val(), recoveryPassword: recoveryPasswordVal}, function (result) { if (result.status != 'success') { OC.Notification.show(t('admin', result.data.message)); } } ); $input.blur(); } else { $input.blur(); } } }) .blur(function () { $(this).replaceWith($('●●●●●●●')); $td.find('img').show(); // remove highlight class from users without recovery ability $tr.removeClass('row-warning'); }); }); $('input:password[id="recoveryPassword"]').keyup(function() { OC.Notification.hide(); }); $userListBody.on('click', '.displayName', function (event) { event.stopPropagation(); var $td = $(this).closest('td'); var $tr = $td.closest('tr'); var uid = UserList.getUID($td); var displayName = escapeHTML(UserList.getDisplayName($td)); var $input = $(''); $td.find('img').hide(); $td.children('span').replaceWith($input); $input .focus() .keypress(function (event) { if (event.keyCode === 13) { if ($(this).val().length > 0) { var $div = $tr.find('div.avatardiv'); if ($div.length) { $div.imageplaceholder(uid, displayName); } $.post( OC.generateUrl('/settings/users/{id}/displayName', {id: uid}), {username: uid, displayName: $(this).val()}, function (result) { if (result && result.status==='success' && $div.length){ $div.avatar(result.data.username, 32); } } ); $input.blur(); } else { $input.blur(); } } }) .blur(function () { var displayName = $input.val(); $tr.data('displayname', displayName); $input.replaceWith('' + escapeHTML(displayName) + ''); $td.find('img').show(); }); }); $userListBody.on('click', '.mailAddress', function (event) { event.stopPropagation(); var $td = $(this).closest('td'); var $tr = $td.closest('tr'); var uid = UserList.getUID($td); var mailAddress = escapeHTML(UserList.getMailAddress($td)); var $input = $('').val(mailAddress); $td.children('span').replaceWith($input); $input .focus() .keypress(function (event) { if (event.keyCode === 13) { if ($(this).val().length > 0) { $input.blur(); $.ajax({ type: 'PUT', url: OC.generateUrl('/settings/users/{id}/mailAddress', {id: uid}), data: { mailAddress: $(this).val() } }).fail(function (result) { OC.Notification.show(result.responseJSON.data.message); // reset the values $tr.data('mailAddress', mailAddress); $tr.children('.mailAddress').children('span').text(mailAddress); }); } else { $input.blur(); } } }) .blur(function () { var mailAddress = $input.val(); var $span = $('').text(mailAddress); $tr.data('mailAddress', mailAddress); $input.replaceWith($span); }); }); // init the quota field select box after it is shown the first time $('#app-settings').one('show', function() { $(this).find('#default_quota').singleSelect().on('change', UserList.onQuotaSelect); }); $('#newuser').submit(function (event) { event.preventDefault(); var username = $('#newusername').val(); var password = $('#newuserpassword').val(); var email = $('#newemail').val(); if ($.trim(username) === '') { OC.dialogs.alert( t('settings', 'A valid username must be provided'), t('settings', 'Error creating user')); return false; } if ($.trim(password) === '') { OC.dialogs.alert( t('settings', 'A valid password must be provided'), t('settings', 'Error creating user')); return false; } if(!$('#CheckboxMailOnUserCreate').is(':checked')) { email = ''; } if ($('#CheckboxMailOnUserCreate').is(':checked') && $.trim(email) === '') { OC.dialogs.alert( t('settings', 'A valid email must be provided'), t('settings', 'Error creating user')); return false; } var promise; if (UserDeleteHandler) { promise = UserDeleteHandler.deleteEntry(); } else { promise = $.Deferred().resolve().promise(); } promise.then(function() { var groups = $('#newusergroups').val() || []; $.post( OC.generateUrl('/settings/users/users'), { username: username, password: password, groups: groups, email: email }, function (result) { if (result.groups) { for (var i in result.groups) { var gid = result.groups[i]; if(UserList.availableGroups.indexOf(gid) === -1) { UserList.availableGroups.push(gid); } $li = GroupList.getGroupLI(gid); userCount = GroupList.getUserCount($li); GroupList.setUserCount($li, userCount + 1); } } if(!UserList.has(username)) { UserList.add(result, true); } $('#newusername').focus(); GroupList.incEveryoneCount(); }).fail(function(result, textStatus, errorThrown) { OC.dialogs.alert(result.responseJSON.message, t('settings', 'Error creating user')); }).success(function(){ $('#newuser').get(0).reset(); }); }); }); if ($('#CheckboxStorageLocation').is(':checked')) { $("#userlist .storageLocation").show(); } // Option to display/hide the "Storage location" column $('#CheckboxStorageLocation').click(function() { if ($('#CheckboxStorageLocation').is(':checked')) { $("#userlist .storageLocation").show(); OC.AppConfig.setValue('core', 'umgmt_show_storage_location', 'true'); } else { $("#userlist .storageLocation").hide(); OC.AppConfig.setValue('core', 'umgmt_show_storage_location', 'false'); } }); if ($('#CheckboxLastLogin').is(':checked')) { $("#userlist .lastLogin").show(); } // Option to display/hide the "Last Login" column $('#CheckboxLastLogin').click(function() { if ($('#CheckboxLastLogin').is(':checked')) { $("#userlist .lastLogin").show(); OC.AppConfig.setValue('core', 'umgmt_show_last_login', 'true'); } else { $("#userlist .lastLogin").hide(); OC.AppConfig.setValue('core', 'umgmt_show_last_login', 'false'); } }); if ($('#CheckboxEmailAddress').is(':checked')) { $("#userlist .mailAddress").show(); } // Option to display/hide the "Mail Address" column $('#CheckboxEmailAddress').click(function() { if ($('#CheckboxEmailAddress').is(':checked')) { $("#userlist .mailAddress").show(); OC.AppConfig.setValue('core', 'umgmt_show_email', 'true'); } else { $("#userlist .mailAddress").hide(); OC.AppConfig.setValue('core', 'umgmt_show_email', 'false'); } }); if ($('#CheckboxUserBackend').is(':checked')) { $("#userlist .userBackend").show(); } // Option to display/hide the "User Backend" column $('#CheckboxUserBackend').click(function() { if ($('#CheckboxUserBackend').is(':checked')) { $("#userlist .userBackend").show(); OC.AppConfig.setValue('core', 'umgmt_show_backend', 'true'); } else { $("#userlist .userBackend").hide(); OC.AppConfig.setValue('core', 'umgmt_show_backend', 'false'); } }); if ($('#CheckboxMailOnUserCreate').is(':checked')) { $("#newemail").show(); } // Option to display/hide the "E-Mail" input field $('#CheckboxMailOnUserCreate').click(function() { if ($('#CheckboxMailOnUserCreate').is(':checked')) { $("#newemail").show(); OC.AppConfig.setValue('core', 'umgmt_send_email', 'true'); } else { $("#newemail").hide(); OC.AppConfig.setValue('core', 'umgmt_send_email', 'false'); } }); // calculate initial limit of users to load var initialUserCountLimit = UserList.initialUsersToLoad, containerHeight = $('#app-content').height(); if(containerHeight > 40) { initialUserCountLimit = Math.floor(containerHeight/40); if (initialUserCountLimit < UserList.initialUsersToLoad) { initialUserCountLimit = UserList.initialUsersToLoad; } } //realign initialUserCountLimit with usersToLoad as a safeguard while((initialUserCountLimit % UserList.usersToLoad) !== 0) { // must be a multiple of this, otherwise LDAP freaks out. // FIXME: solve this in LDAP backend in 8.1 initialUserCountLimit = initialUserCountLimit + 1; } // trigger loading of users on startup UserList.update(UserList.currentGid, initialUserCountLimit); _.defer(function() { $('#app-content').trigger($.Event('apprendered')); }); });