Skip to content

Commit

Permalink
Merge pull request #27333 from AlexLaroche/master
Browse files Browse the repository at this point in the history
Enable / Disable user from web application
  • Loading branch information
Vincent Petry authored Mar 21, 2017
2 parents 13f3029 + d31f7e1 commit 53c0129
Show file tree
Hide file tree
Showing 8 changed files with 556 additions and 1 deletion.
67 changes: 67 additions & 0 deletions settings/Controller/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ private function formatUserForIndex(IUser $user, array $userGroups = null) {
'displayname' => $user->getDisplayName(),
'groups' => (empty($userGroups)) ? $this->groupManager->getUserGroupIds($user, 'management') : $userGroups,
'subadmin' => $subAdminGroups,
'isEnabled' => $user->isEnabled(),
'quota' => $user->getQuota(),
'storageLocation' => $user->getHome(),
'lastLogin' => $user->getLastLogin() * 1000,
Expand Down Expand Up @@ -661,4 +662,70 @@ public function setDisplayName($username, $displayName) {
]);
}
}

/**
* @NoAdminRequired
*
* @param string $id
* @return DataResponse
*/
public function setEnabled($id, $enabled) {
$userId = $this->userSession->getUser()->getUID();
$user = $this->userManager->get($id);

if($userId === $id ||
(!$this->isAdmin &&
!$this->groupManager->getSubAdmin()->isUserAccessible($this->userSession->getUser(), $user))) {
return new DataResponse(
array(
'status' => 'error',
'data' => array(
'message' => (string)$this->l10n->t('Forbidden')
)
),
Http::STATUS_FORBIDDEN
);
}


if(!$user){
return new DataResponse(
array(
'status' => 'error',
'data' => array(
'message' => (string)$this->l10n->t('Invalid user')
)
),
Http::STATUS_UNPROCESSABLE_ENTITY
);
}


$value = filter_var($enabled, FILTER_VALIDATE_BOOLEAN);
if(!isset($value) || is_null($value))
{
return new DataResponse(
array(
'status' => 'error',
'data' => array(
'message' => (string)$this->l10n->t('Unable to enable/disable user.')
)
),
Http::STATUS_FORBIDDEN
);
}

$user->setEnabled($value);

return new DataResponse(
[
'status' => 'success',
'data' => [
'username' => $id,
'enabled' => $enabled
]
],
Http::STATUS_OK
);
}
}
1 change: 1 addition & 0 deletions settings/css/settings.css
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ span.usersLastLoginTooltip { white-space: nowrap; }
position: relative;
}
#userlist .mailAddress,
#userlist .enabled,
#userlist .storageLocation,
#userlist .userBackend,
#userlist .lastLogin {
Expand Down
75 changes: 75 additions & 0 deletions settings/js/users/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var UserList = {
offset: 0,
usersToLoad: 10, //So many users will be loaded when user scrolls down
initialUsersToLoad: 50, //initial number of users to load
currentUser: '',
currentGid: '',
filter: '',

Expand All @@ -25,8 +26,11 @@ var UserList = {
initialize: function($el) {
this.$el = $el;

UserList.currentUser = OC.getCurrentUser().uid;

// initially the list might already contain user entries (not fully ajaxified yet)
// initialize these entries
this.$el.find('.isEnabled').on('change', this.onEnabledChange);
this.$el.find('.quota-user').singleSelect().on('change', this.onQuotaSelect);
},

Expand All @@ -39,6 +43,7 @@ var UserList = {
* 'displayname': 'Users display name',
* 'groups': ['group1', 'group2'],
* 'subadmin': ['group4', 'group5'],
* 'enabled' 'true'
* 'quota': '10 GB',
* 'storageLocation': '/srv/www/owncloud/data/username',
* 'lastLogin': '1418632333'
Expand Down Expand Up @@ -95,6 +100,17 @@ var UserList = {
this._updateGroupListLabel($tdSubadmins, user.subadmin);
$tdSubadmins.find('.action').tooltip({placement: 'top'});

/**
* enabled
*/
var $tdEnabled = $tr.find('.isEnabled');
if(user.name !== UserList.currentUser) {
$tdEnabled.attr("checked", user.isEnabled);
$tdEnabled.on('change', UserList.onEnabledChange);
} else {
$tdEnabled.remove();
}

/**
* remove action
*/
Expand Down Expand Up @@ -558,6 +574,50 @@ var UserList = {
);
},

/**
* Event handler for when a enabled value has been changed.
* This will save the value.
*/
onEnabledChange: function() {
var $select = $(this);
var uid = UserList.getUID($select);
var enabled = $select.prop('checked') ? 'true' : 'false';

UserList._updateEnabled(uid, enabled,
function(returnedEnabled){
if (enabled !== returnedEnabled) {
$select.prop('checked', user.isEnabled);
}
});
},


/**
* Saves the enabled value for the given user
* @param {String} [uid] optional user id, sets default quota if empty
* @param {String} enabled value
* @param {Function} ready callback after save
*/
_updateEnabled: function(uid, enabled, ready) {
$.post(
OC.generateUrl('/settings/users/{id}/enabled', {id: uid}),
{username: uid, enabled: enabled},
function (result) {
if(result.status == 'success') {
OC.Notification.showTemporary(t('admin', 'User {uid} has been {state}!',
{uid: uid,
state: result.data.enabled === 'true' ?
t('admin', 'enabled') :
t('admin', 'disabled')}
));
} else {
OC.Notification.showTemporary(t('admin', result.data.message));
}
}
);
},


/**
* Creates a temporary jquery.multiselect selector on the given group field
*/
Expand Down Expand Up @@ -882,6 +942,21 @@ $(document).ready(function () {
});
});

if ($('#CheckboxIsEnabled').is(':checked')) {
$("#userlist .enabled").show();
}
// Option to display/hide the "Enabled" column
$('#CheckboxIsEnabled').click(function() {
if ($('#CheckboxIsEnabled').is(':checked')) {
$("#userlist .enabled").show();
OC.AppConfig.setValue('core', 'umgmt_show_is_enabled', 'true');
} else {
$("#userlist .enabled").hide();
OC.AppConfig.setValue('core', 'umgmt_show_is_enabled', 'false');
}
});


if ($('#CheckboxStorageLocation').is(':checked')) {
$("#userlist .storageLocation").show();
}
Expand Down
1 change: 1 addition & 0 deletions settings/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
['name' => 'SecuritySettings#trustedDomains', 'url' => '/settings/admin/security/trustedDomains', 'verb' => 'POST'],
['name' => 'Users#setDisplayName', 'url' => '/settings/users/{username}/displayName', 'verb' => 'POST'],
['name' => 'Users#setMailAddress', 'url' => '/settings/users/{id}/mailAddress', 'verb' => 'PUT'],
['name' => 'Users#setEnabled', 'url' => '/settings/users/{id}/enabled', 'verb' => 'POST'],
['name' => 'Users#stats', 'url' => '/settings/users/stats', 'verb' => 'GET'],
['name' => 'LogSettings#setLogLevel', 'url' => '/settings/admin/log/level', 'verb' => 'POST'],
['name' => 'LogSettings#download', 'url' => '/settings/admin/log/download', 'verb' => 'GET'],
Expand Down
7 changes: 7 additions & 0 deletions settings/templates/users/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@
<?php print_unescaped($this->inc('users/part.setquota')); ?>

<div id="userlistoptions">
<p>
<input type="checkbox" name="IsEnabled" value="IsEnabled" id="CheckboxIsEnabled"
class="checkbox" <?php if ($_['show_is_enabled'] === 'true') print_unescaped('checked="checked"'); ?> />
<label for="CheckboxIsEnabled">
<?php p($l->t('Show enabled/disabled option')) ?>
</label>
</p>
<p>
<input type="checkbox" name="StorageLocation" value="StorageLocation" id="CheckboxStorageLocation"
class="checkbox" <?php if ($_['show_storage_location'] === 'true') print_unescaped('checked="checked"'); ?> />
Expand Down
4 changes: 4 additions & 0 deletions settings/templates/users/part.userlist.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<?php if(is_array($_['subadmins']) || $_['subadmins']): ?>
<th id="headerSubAdmins" scope="col"><?php p($l->t('Group Admin for')); ?></th>
<?php endif;?>
<th class="enabled" scope="col"><?php p($l->t('Enabled')); ?></th>
<th id="headerQuota" scope="col"><?php p($l->t('Quota')); ?></th>
<th class="storageLocation" scope="col"><?php p($l->t('Storage Location')); ?></th>
<th class="userBackend" scope="col"><?php p($l->t('User Backend')); ?></th>
Expand Down Expand Up @@ -46,6 +47,9 @@
><span class="title groupsList"></span><span class="icon-triangle-s"></span></div>
</td>
<?php endif;?>
<td class="enabled">
<input type="checkbox" class="isEnabled" checked="checked">
</td>
<td class="quota">
<select class="quota-user" data-inputtitle="<?php p($l->t('Please enter storage quota (ex: "512 MB" or "12 GB")')) ?>">
<option value='default'>
Expand Down
1 change: 1 addition & 0 deletions settings/users.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
$tmpl->assign('recoveryAdminEnabled', $recoveryAdminEnabled);
$tmpl->assign('enableAvatars', \OC::$server->getConfig()->getSystemValue('enable_avatars', true) === true);

$tmpl->assign('show_is_enabled', $config->getAppValue('core', 'umgmt_show_is_enabled', 'false'));
$tmpl->assign('show_storage_location', $config->getAppValue('core', 'umgmt_show_storage_location', 'false'));
$tmpl->assign('show_last_login', $config->getAppValue('core', 'umgmt_show_last_login', 'false'));
$tmpl->assign('show_email', $config->getAppValue('core', 'umgmt_show_email', 'false'));
Expand Down
Loading

0 comments on commit 53c0129

Please sign in to comment.