Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit 29f5465

Browse files
committed
ENH: refs #0340. Add controller and view for deleting a user as admin
1 parent 12f2c5d commit 29f5465

File tree

6 files changed

+192
-9
lines changed

6 files changed

+192
-9
lines changed

core/controllers/UserController.php

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,8 @@ public function userpageAction()
709709
$this->view->feeds = $this->Feed->getFeedsByUser($this->userSession->Dao, $userDao);
710710

711711
$this->view->isViewAction = ($this->logged && ($this->userSession->Dao->getKey() == $userDao->getKey() || $this->userSession->Dao->isAdmin()));
712+
$this->view->currentUser = $this->userSession->Dao;
713+
$this->view->isAdmin = $this->logged && $this->userSession->Dao->isAdmin();
712714
$this->view->information = array();
713715

714716
$this->view->disableFeedImages = true;
@@ -773,4 +775,55 @@ public function manageAction()
773775
$this->view->userCommunities = $communities;
774776
$this->view->userCommunityFolders = $communityFolders;
775777
}
776-
}//end class
778+
779+
/** Render the dialog related to user deletion */
780+
public function deletedialogAction()
781+
{
782+
$this->requireAdminPrivileges();
783+
$this->disableLayout();
784+
$userId = $this->_getParam('userId');
785+
786+
if(!isset($userId))
787+
{
788+
throw new Zend_Exception('Must set a userId parameter');
789+
}
790+
$user = $this->User->load($userId);
791+
if(!$user)
792+
{
793+
throw new Zend_Exception('Invalid user id');
794+
}
795+
$this->view->user = $user;
796+
}
797+
798+
/** Delete a user */
799+
public function deleteAction()
800+
{
801+
// make sure this gets completed even if user navigates away or it takes a long time
802+
set_time_limit(0);
803+
ignore_user_abort(true);
804+
805+
$this->requireAdminPrivileges();
806+
$userId = $this->_getParam('userId');
807+
808+
if(!isset($userId))
809+
{
810+
throw new Zend_Exception('Must set a userId parameter');
811+
}
812+
$user = $this->User->load($userId);
813+
if(!$user)
814+
{
815+
throw new Zend_Exception('Invalid user id');
816+
}
817+
if($user->isAdmin())
818+
{
819+
throw new Zend_Exception('Cannot delete an admin user');
820+
}
821+
$this->_helper->viewRenderer->setNoRender();
822+
$this->disableLayout();
823+
824+
$name = $user->getFirstname().' '.$user->getLastname();
825+
$this->User->delete($user);
826+
$this->getLogger()->info('User '.$name.' successfully deleted');
827+
echo JsonComponent::encode(array(true, 'User '.$name.' successfully deleted'));
828+
}
829+
}//end class
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
span#deleteDialogUserName {
2+
font-weight: bold;
3+
}
4+
5+
span#deleteDialogCaution {
6+
font-weight: bolder;
7+
color: #bb0000;
8+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
var midas = midas || {};
2+
midas.user = midas.user || {};
3+
midas.user.deletedialog = {};
4+
5+
/**
6+
* Toggles the Delete button based on the state of the agreement checkbox
7+
* in order to make the operation safer
8+
*/
9+
midas.user.deletedialog.agreeCheckboxChanged = function()
10+
{
11+
if($(this).attr('checked') == 'checked')
12+
{
13+
$('#deleteDialogDeleteButton').removeAttr('disabled');
14+
}
15+
else
16+
{
17+
$('#deleteDialogDeleteButton').attr('disabled', 'disabled');
18+
}
19+
}
20+
21+
/**
22+
* When the user confirms deletion request, this will get called before ajax submission
23+
*/
24+
midas.user.deletedialog.confirm = function()
25+
{
26+
$('#deleteDialogDeleteButton').attr('disabled', 'disabled');
27+
$('#deleteDialogCancelButton').attr('disabled', 'disabled');
28+
$('#deleteDialogAgreeCheckbox').attr('disabled', 'disabled');
29+
$('img#deleteDialogLoadingGif').show();
30+
// TODO add please wait message?
31+
}
32+
33+
/**
34+
* Called when our ajax request to delete the user returns
35+
*/
36+
midas.user.deletedialog.success = function(responseText, statusText, xhr, form)
37+
{
38+
$('div.MainDialog').dialog('close');
39+
$('#deleteDialogCancelButton').removeAttr('disabled');
40+
$('#deleteDialogAgreeCheckbox').removeAttr('disabled');
41+
$('#deleteDialogAgreeCheckbox').removeAttr('checked');
42+
$('input#declineApplyRecursive').removeAttr('disabled');
43+
$('img#deleteDialogLoadingGif').hide();
44+
jsonResponse = $.parseJSON(responseText);
45+
46+
if(jsonResponse == null)
47+
{
48+
createNotice('Error', 4000);
49+
return;
50+
}
51+
createNotice(jsonResponse[1], 4000);
52+
window.location.replace(json.global.webroot + '/user/index');
53+
}
54+
55+
$(document).ready(function() {
56+
$('#deleteDialogAgreeCheckbox').change(midas.user.deletedialog.agreeCheckboxChanged);
57+
$('#deleteDialogCancelButton').click(function() {
58+
$('div.MainDialog').dialog('close');
59+
});
60+
61+
$('#deleteDialogForm').ajaxForm({
62+
beforeSubmit: midas.user.deletedialog.confirm,
63+
success: midas.user.deletedialog.success
64+
});
65+
});

core/public/js/user/user.userpage.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
var midas = midas || {};
2+
midas.user = midas.user || {};
3+
14
$(document).ready(function() {
2-
5+
36
$( "#tabsGeneric" ).tabs({
47
select: function(event, ui) {
58
$('div.genericAction').show();
@@ -13,7 +16,7 @@
1316
});
1417
$("#tabsGeneric").show();
1518
$('img.tabsLoading').hide();
16-
19+
1720
$("#browseTable").treeTable({
1821
onFirstInit: enableRangeSelect,
1922
onNodeShow: enableRangeSelect,
@@ -27,8 +30,8 @@
2730
$("img.tableLoading").hide();
2831
$("table#browseTable").show();
2932
});
30-
31-
33+
34+
3235
//dependance: common/browser.js
3336
var ajaxSelectRequest='';
3437
function callbackSelect(node)
@@ -40,16 +43,24 @@
4043
$('div.websiteBlock').hide();
4144
$('div.viewInfo').show();
4245
$('div.viewAction').show();
43-
genericCallbackSelect(node);
46+
genericCallbackSelect(node);
4447
}
4548

4649
function callbackDblClick(node)
4750
{
4851
genericCallbackDblClick(node);
4952
}
50-
53+
5154
function callbackCheckboxes(node)
5255
{
5356
genericCallbackCheckboxes(node);
5457
}
55-
58+
59+
/**
60+
* Will render the delete user dialog for the specified user
61+
*/
62+
midas.user.showDeleteDialog = function(userId)
63+
{
64+
loadDialog('userId'+userId, '/user/deletedialog?userId='+userId);
65+
showDialog('Delete User', false);
66+
}

core/views/user/deletedialog.phtml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/*=========================================================================
3+
MIDAS Server
4+
Copyright (c) Kitware SAS. 20 rue de la Villette. All rights reserved.
5+
69328 Lyon, FRANCE.
6+
7+
See Copyright.txt for details.
8+
This software is distributed WITHOUT ANY WARRANTY; without even
9+
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10+
PURPOSE. See the above copyright notices for more information.
11+
=========================================================================*/
12+
13+
echo '<script type="text/javascript" src="'.$this->coreWebroot.'/public/js/user/user.deletedialog.js?'.time().'"></script>';
14+
$this->headScript()->appendFile($this->coreWebroot . '/public/js/jquery/jquery.form.js');
15+
?>
16+
<link href="<?php echo $this->coreWebroot?>/public/css/user/user.deletedialog.css" rel="stylesheet" type="text/css" />
17+
18+
<span id="deleteDialogCaution">CAUTION:</span> Are you sure you want to delete the user
19+
<span id="deleteDialogUserName"><?php echo $this->user->getFirstname().' '.$this->user->getLastname(); ?></span>?
20+
<br /><br />
21+
The user's entire folder tree and all data within it will be deleted as well. This action cannot be undone.
22+
<div>
23+
<br />
24+
<form id="deleteDialogForm" class="genericForm" method="POST" action="<?php echo $this->webroot?>/user/delete">
25+
<input type='hidden' name="userId" value="<?php echo $this->user->getKey(); ?>" />
26+
<input type="checkbox" id="deleteDialogAgreeCheckbox">I understand the implications and wish to proceed.</input>
27+
<br /><br />
28+
<input class="genericButton" disabled="disabled" type='submit' name='submitButton' id='deleteDialogDeleteButton'
29+
value='<?php echo $this->t('Delete')?>' />
30+
<input class="genericButton" style="margin-left: 15px;" type='button' name='deleteDialogCancelButton' id='deleteDialogCancelButton'
31+
value='<?php echo $this->t('Cancel')?>' />
32+
</form>
33+
<img id="deleteDialogLoadingGif" style="display: none; float: left" alt="" src="<?php echo $this->coreWebroot?>/public/images/icons/loading.gif" />
34+
</div>

core/views/user/userpage.phtml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,19 @@ $this->headScript()->appendFile($this->coreWebroot.'/public/js/common/common.bro
165165
<ul>
166166
<li class="myAccountLink" userid="'.$this->user->getKey().'"><a><img alt="" src="'.$this->coreWebroot.'/public/images/icons/user.png"/> '.$this->t('Manage Profile').'</a></li>
167167
<li><a href="'.$this->webroot.'/user/manage/?userId='.$this->user->getKey().'"><img alt="" src="'.$this->coreWebroot.'/public/images/icons/data.png"/> '.$this->t('Manage Files').'</a></li>
168-
<li><a onclick="createNewFolder('.$this->mainFolder->getKey().');"><img alt="" src="'.$this->coreWebroot.'/public/images/icons/folder_add.png"/> '.$this->t('Create a top level Folder').'</a></li>
168+
<li><a onclick="createNewFolder('.$this->mainFolder->getKey().');"><img alt="" src="'.$this->coreWebroot.'/public/images/icons/folder_add.png"/> '.$this->t('Create a top level Folder').'</a></li>';
169+
170+
if($this->isAdmin && !$this->user->isAdmin())
171+
{
172+
echo '
173+
<li><a onclick="midas.user.showDeleteDialog('.$this->user->getKey().');"><img alt="" src="'.$this->coreWebroot.'/public/images/icons/close.png"/> '.$this->t('Delete User').'</a></li>';
174+
}
175+
else if(!$this->user->isAdmin() && $this->user->getKey() == $this->currentUser->getKey())
176+
{
177+
echo '
178+
<li><a onclick="midas.user.showDeleteDialog('.$this->user->getKey().');"><img alt="" src="'.$this->coreWebroot.'/public/images/icons/close.png"/> '.$this->t('Delete My Account').'</a></li>';
179+
}
180+
echo '
169181
</ul>
170182
</div>';
171183
}

0 commit comments

Comments
 (0)