Commit 53c0129e authored by Vincent Petry's avatar Vincent Petry Committed by GitHub

Merge pull request #27333 from AlexLaroche/master

Enable / Disable user from web application
parents 13f30293 d31f7e15
......@@ -190,6 +190,7 @@ class UsersController extends Controller {
'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,
......@@ -661,4 +662,70 @@ class UsersController extends Controller {
]);
}
}
/**
* @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
);
}
}
......@@ -224,6 +224,7 @@ span.usersLastLoginTooltip { white-space: nowrap; }
position: relative;
}
#userlist .mailAddress,
#userlist .enabled,
#userlist .storageLocation,
#userlist .userBackend,
#userlist .lastLogin {
......
......@@ -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: '',
......@@ -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);
},
......@@ -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'
......@@ -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
*/
......@@ -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
*/
......@@ -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();
}
......
......@@ -49,6 +49,7 @@ $application->registerRoutes($this, [
['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'],
......
......@@ -44,6 +44,13 @@ translation('settings');
<?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"'); ?> />
......
......@@ -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>
......@@ -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'>
......
......@@ -119,6 +119,7 @@ $tmpl->assign('defaultQuotaIsUserDefined', $defaultQuotaIsUserDefined);
$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'));
......
......@@ -98,6 +98,10 @@ class UsersControllerTest extends \Test\TestCase {
->expects($this->once())
->method('getEMailAddress')
->will($this->returnValue('foo@bar.com'));
$foo
->expects($this->once())
->method('isEnabled')
->will($this->returnValue(true));
$foo
->expects($this->once())
->method('getQuota')
......@@ -126,6 +130,10 @@ class UsersControllerTest extends \Test\TestCase {
->expects($this->once())
->method('getEMailAddress')
->will($this->returnValue('admin@bar.com'));
$admin
->expects($this->once())
->method('isEnabled')
->will($this->returnValue(true));
$admin
->expects($this->once())
->method('getQuota')
......@@ -156,6 +164,10 @@ class UsersControllerTest extends \Test\TestCase {
->expects($this->once())
->method('getEMailAddress')
->will($this->returnValue('bar@dummy.com'));
$bar
->expects($this->once())
->method('isEnabled')
->will($this->returnValue(false));
$bar
->expects($this->once())
->method('getQuota')
......@@ -226,6 +238,7 @@ class UsersControllerTest extends \Test\TestCase {
'displayname' => 'M. Foo',
'groups' => ['Users', 'Support'],
'subadmin' => [],
'isEnabled' => true,
'quota' => 1024,
'storageLocation' => '/home/foo',
'lastLogin' => 500000,
......@@ -239,6 +252,7 @@ class UsersControllerTest extends \Test\TestCase {
'displayname' => 'S. Admin',
'groups' => ['admins', 'Support'],
'subadmin' => [],
'isEnabled' => true,
'quota' => 404,
'storageLocation' => '/home/admin',
'lastLogin' => 12000,
......@@ -252,6 +266,7 @@ class UsersControllerTest extends \Test\TestCase {
'displayname' => 'B. Ar',
'groups' => ['External Users'],
'subadmin' => [],
'isEnabled' => false,
'quota' => 2323,
'storageLocation' => '/home/bar',
'lastLogin' => 3999000,
......@@ -290,6 +305,10 @@ class UsersControllerTest extends \Test\TestCase {
->expects($this->once())
->method('getEMailAddress')
->will($this->returnValue('foo@bar.com'));
$foo
->expects($this->once())
->method('isEnabled')
->will($this->returnValue(true));
$foo
->expects($this->once())
->method('getQuota')
......@@ -318,6 +337,10 @@ class UsersControllerTest extends \Test\TestCase {
->expects($this->once())
->method('getEMailAddress')
->will($this->returnValue('admin@bar.com'));
$admin
->expects($this->once())
->method('isEnabled')
->will($this->returnValue(true));
$admin
->expects($this->once())
->method('getQuota')
......@@ -348,6 +371,10 @@ class UsersControllerTest extends \Test\TestCase {
->expects($this->once())
->method('getEMailAddress')
->will($this->returnValue('bar@dummy.com'));
$bar
->expects($this->once())
->method('isEnabled')
->will($this->returnValue(false));
$bar
->expects($this->once())
->method('getQuota')
......@@ -432,6 +459,7 @@ class UsersControllerTest extends \Test\TestCase {
'displayname' => 'B. Ar',
'groups' => ['SubGroup1'],
'subadmin' => [],
'isEnabled' => false,
'quota' => 2323,
'storageLocation' => '/home/bar',
'lastLogin' => 3999000,
......@@ -445,6 +473,7 @@ class UsersControllerTest extends \Test\TestCase {
'displayname' => 'M. Foo',
'groups' => ['SubGroup2', 'SubGroup1'],
'subadmin' => [],
'isEnabled' => true,
'quota' => 1024,
'storageLocation' => '/home/foo',
'lastLogin' => 500000,
......@@ -458,6 +487,7 @@ class UsersControllerTest extends \Test\TestCase {
'displayname' => 'S. Admin',
'groups' => ['SubGroup2'],
'subadmin' => [],
'isEnabled' => true,
'quota' => 404,
'storageLocation' => '/home/admin',
'lastLogin' => 12000,
......@@ -494,6 +524,10 @@ class UsersControllerTest extends \Test\TestCase {
->expects($this->once())
->method('getEMailAddress')
->will($this->returnValue('foo@bar.com'));
$foo
->expects($this->once())
->method('isEnabled')
->will($this->returnValue(true));
$foo
->expects($this->once())
->method('getQuota')
......@@ -522,6 +556,10 @@ class UsersControllerTest extends \Test\TestCase {
->expects($this->once())
->method('getEMailAddress')
->will($this->returnValue('admin@bar.com'));
$admin
->expects($this->once())
->method('isEnabled')
->will($this->returnValue(true));
$admin
->expects($this->once())
->method('getQuota')
......@@ -552,6 +590,10 @@ class UsersControllerTest extends \Test\TestCase {
->expects($this->once())
->method('getEMailAddress')
->will($this->returnValue('bar@dummy.com'));
$bar
->expects($this->once())
->method('isEnabled')
->will($this->returnValue(false));
$bar
->expects($this->once())
->method('getQuota')
......@@ -595,6 +637,7 @@ class UsersControllerTest extends \Test\TestCase {
'displayname' => 'M. Foo',
'groups' => ['Users', 'Support'],
'subadmin' => [],
'isEnabled' => true,
'quota' => 1024,
'storageLocation' => '/home/foo',
'lastLogin' => 500000,
......@@ -608,6 +651,7 @@ class UsersControllerTest extends \Test\TestCase {
'displayname' => 'S. Admin',
'groups' => ['admins', 'Support'],
'subadmin' => [],
'isEnabled' => true,
'quota' => 404,
'storageLocation' => '/home/admin',
'lastLogin' => 12000,
......@@ -621,6 +665,7 @@ class UsersControllerTest extends \Test\TestCase {
'displayname' => 'B. Ar',
'groups' => ['External Users'],
'subadmin' => [],
'isEnabled' => false,
'quota' => 2323,
'storageLocation' => '/home/bar',
'lastLogin' => 3999000,
......@@ -652,6 +697,10 @@ class UsersControllerTest extends \Test\TestCase {
->expects($this->once())
->method('getEMailAddress')
->will($this->returnValue(null));
$user
->expects($this->once())
->method('isEnabled')
->will($this->returnValue(true));
$user
->expects($this->once())
->method('getQuota')
......@@ -697,6 +746,7 @@ class UsersControllerTest extends \Test\TestCase {
'displayname' => 'M. Foo',
'groups' => null,
'subadmin' => [],
'isEnabled' => true,
'quota' => 'none',
'storageLocation' => '/home/foo',
'lastLogin' => 500000,
......@@ -771,6 +821,7 @@ class UsersControllerTest extends \Test\TestCase {
'backend' => 'bar',
'lastLogin' => null,
'displayname' => null,
'isEnabled' => null,
'quota' => null,
'subadmin' => [],
'email' => null,
......@@ -860,6 +911,7 @@ class UsersControllerTest extends \Test\TestCase {
'backend' => 'bar',
'lastLogin' => 0,
'displayname' => null,
'isEnabled' => null,
'quota' => null,
'subadmin' => [],
'email' => null,
......@@ -943,6 +995,7 @@ class UsersControllerTest extends \Test\TestCase {
'backend' => 'bar',
'lastLogin' => null,
'displayname' => null,
'isEnabled' => null,
'quota' => null,
'subadmin' => [],
'email' => null,
......@@ -1039,6 +1092,7 @@ class UsersControllerTest extends \Test\TestCase {
'backend' => 'bar',
'lastLogin' => 0,
'displayname' => null,
'isEnabled' => null,
'quota' => null,
'subadmin' => [],
'email' => null,
......@@ -1506,7 +1560,7 @@ class UsersControllerTest extends \Test\TestCase {
$this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
}
private function mockUser($userId = 'foo', $displayName = 'M. Foo',
private function mockUser($userId = 'foo', $displayName = 'M. Foo', $isEnabled = true,
$lastLogin = 500, $home = '/home/foo', $backend = 'OC_User_Database') {
$user = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock();
......@@ -1518,6 +1572,9 @@ class UsersControllerTest extends \Test\TestCase {
->expects($this->once())
->method('getDisplayName')
->will($this->returnValue($displayName));
$user
->method('isEnabled')
->will($this->returnValue($isEnabled));
$user
->method('getLastLogin')
->will($this->returnValue($lastLogin));
......@@ -1534,6 +1591,7 @@ class UsersControllerTest extends \Test\TestCase {
'displayname' => $displayName,
'groups' => null,
'subadmin' => [],
'isEnabled' => $isEnabled,
'quota' => null,
'storageLocation' => $home,
'lastLogin' => $lastLogin * 1000,
......@@ -2029,4 +2087,345 @@ class UsersControllerTest extends \Test\TestCase {
$response = $this->container['UsersController']->setDisplayName($user->getUID(), 'newDisplayName');
$this->assertEquals($expectedResponse, $response);
}
public function testDisableSelfAdmin() {
$this->container['IsAdmin'] = true;
$user = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock();
$user
->expects($this->once())
->method('getUID')
->will($this->returnValue('myself'));
$this->container['UserSession']
->method('getUser')
->will($this->returnValue($user));
$expectedResponse = new DataResponse(
[
'status' => 'error',
'data' => [
'message' => 'Forbidden'
]
],
Http::STATUS_FORBIDDEN
);
$response = $this->container['UsersController']->setEnabled('myself', 'false');
$this->assertEquals($expectedResponse, $response);
}
public function testEnableSelfAdmin() {
$this->container['IsAdmin'] = true;
$user = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock();
$user
->expects($this->once())
->method('getUID')
->will($this->returnValue('myself'));
$this->container['UserSession']
->method('getUser')
->will($this->returnValue($user));
$expectedResponse = new DataResponse(
[
'status' => 'error',
'data' => [
'message' => 'Forbidden'
]
],
Http::STATUS_FORBIDDEN
);
$response = $this->container['UsersController']->setEnabled('myself', 'true');
$this->assertEquals($expectedResponse, $response);
}
public function testDisableSelfSubadmin() {
$this->container['IsAdmin'] = false;
$user = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock();
$user
->expects($this->once())
->method('getUID')
->will($this->returnValue('myself'));
$this->container['UserSession']
->method('getUser')
->will($this->returnValue($user));
$expectedResponse = new DataResponse(
[
'status' => 'error',
'data' => [
'message' => 'Forbidden'
]
],
Http::STATUS_FORBIDDEN
);
$response = $this->container['UsersController']->setEnabled('myself', 'false');
$this->assertEquals($expectedResponse, $response);
}
public function testEnableSelfSubadmin() {
$this->container['IsAdmin'] = false;
$user = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock();
$user
->expects($this->once())
->method('getUID')
->will($this->returnValue('myself'));
$this->container['UserSession']
->method('getUser')
->will($this->returnValue($user));
$expectedResponse = new DataResponse(
[
'status' => 'error',
'data' => [
'message' => 'Forbidden'
]
],
Http::STATUS_FORBIDDEN
);
$response = $this->container['UsersController']->setEnabled('myself', 'true');
$this->assertEquals($expectedResponse, $response);
}
public function testDisableAdmin() {
$this->container['IsAdmin'] = true;
$user = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock();
$user
->expects($this->once())
->method('getUID')
->will($this->returnValue('Admin'));
$toDisableUser = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock();
$this->container['UserSession']
->method('getUser')