Allow admins to delete users

This commit is contained in:
Toby Zerner 2015-08-05 12:08:28 +09:30
parent 4d2aac7645
commit 151af395c9
4 changed files with 53 additions and 8 deletions

View File

@ -39,7 +39,8 @@ export default class UserCard extends Component {
children: controls,
className: 'UserCard-controls App-primaryControl',
menuClassName: 'Dropdown-menu--right',
buttonClassName: this.props.controlsButtonClassName
buttonClassName: this.props.controlsButtonClassName,
icon: 'ellipsis-v'
}) : ''}
<div className="UserCard-profile">

View File

@ -1,7 +1,7 @@
import Button from 'flarum/components/Button';
import Separator from 'flarum/components/Separator';
import EditUserModal from 'flarum/components/EditUserModal';
import DeleteUserModal from 'flarum/components/DeleteUserModal';
import UserPage from 'flarum/components/UserPage';
import ItemList from 'flarum/utils/ItemList';
/**
@ -80,7 +80,7 @@ export default {
destructiveControls(user) {
const items = new ItemList();
if (user.canDelete()) {
if (user.id() !== '1' && user.canDelete()) {
items.add('delete', Button.component({
icon: 'times',
children: app.trans('core.delete'),
@ -95,7 +95,15 @@ export default {
* Delete the user.
*/
deleteAction() {
app.modal.show(new DeleteUserModal({user: this}));
if (confirm('Are you sure you want to delete this user? All of the user\'s posts will be deleted.')) {
this.delete().then(() => {
if (app.current instanceof UserPage && app.current.user === this) {
app.history.back();
} else {
window.location.reload();
}
});
}
},
/**

View File

@ -37,7 +37,9 @@ class UserMetadataUpdater
*/
public function whenPostWasDeleted(PostWasDeleted $event)
{
$this->updateCommentsCount($event->post->user, -1);
if ($event->post->user->exists) {
$this->updateCommentsCount($event->post->user, -1);
}
}
/**

View File

@ -8,6 +8,7 @@ use Flarum\Events\RegisterUserPreferences;
use Illuminate\Contracts\Hashing\Hasher;
use Flarum\Core\Formatter\FormatterManager;
use Flarum\Events\UserWasDeleted;
use Flarum\Events\PostWasDeleted;
use Flarum\Events\UserWasRegistered;
use Flarum\Events\UserWasRenamed;
use Flarum\Events\UserEmailWasChanged;
@ -95,8 +96,31 @@ class User extends Model
{
parent::boot();
// Don't allow the root admin to be deleted.
static::deleting(function (User $user) {
if ($user->id == 1) {
throw new DomainException('Cannot delete the root admin');
}
});
static::deleted(function ($user) {
$user->raise(new UserWasDeleted($user));
// Delete all of the posts by the user. Before we delete them
// in a big batch query, we will loop through them and raise a
// PostWasDeleted event for each post.
$posts = $user->posts()->allTypes();
foreach ($posts->get() as $post) {
$user->raise(new PostWasDeleted($post));
}
$posts->delete();
$user->read()->detach();
$user->groups()->detach();
$user->accessTokens()->delete();
$user->notifications()->delete();
});
event(new RegisterUserPreferences);
@ -479,13 +503,23 @@ class User extends Model
}
/**
* Define the relationship with the user's activity.
* Define the relationship with the user's posts.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function activity()
public function posts()
{
return $this->hasMany('Flarum\Core\Activity\Activity');
return $this->hasMany('Flarum\Core\Posts\Post');
}
/**
* Define the relationship with the user's read discussions.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function read()
{
return $this->belongsToMany('Flarum\Core\Discussions\Discussion', 'users_discussions');
}
/**