FEATURE: add soft deletes to users
This commit is contained in:
@@ -15,7 +15,6 @@ class UserStates extends BaseEnumeration
|
||||
public static function init()
|
||||
{
|
||||
$items = [
|
||||
User::STATE_DELETED => 'admin/userManagement.status_' . User::STATE_DELETED,
|
||||
User::STATE_BLOCKED => 'admin/userManagement.status_' . User::STATE_BLOCKED,
|
||||
User::STATE_ACTIVE => 'admin/userManagement.status_' . User::STATE_ACTIVE
|
||||
];
|
||||
|
||||
@@ -25,8 +25,7 @@ class EditUser extends Controller
|
||||
// @todo add some authorization (e.g. via can/cannot methods)
|
||||
|
||||
// try to find given user
|
||||
$user = User::where('status', '!=', User::STATE_DELETED)
|
||||
->findOrFail($id);
|
||||
$user = User::findOrFail($id);
|
||||
|
||||
$states = UserStates::getWithTranslations();
|
||||
$roles = UserRoles::getWithTranslations();
|
||||
|
||||
@@ -12,13 +12,11 @@ class UsersOverview extends Controller
|
||||
{
|
||||
public function getOverview($status = null, $perPage = 12)
|
||||
{
|
||||
$users = User::where('status', '<>', User::STATE_DELETED)
|
||||
->orderBy('id', 'desc');
|
||||
$users = User::orderBy('id', 'desc');
|
||||
|
||||
// get statuses
|
||||
$statuses = UserStates::getWithTranslations();
|
||||
$statuses[''] = trans('admin/userManagement.allStatuses');
|
||||
unset($statuses[User::STATE_DELETED]);
|
||||
|
||||
// filter using status
|
||||
if (!is_null($status) && isset($statuses[$status])) {
|
||||
|
||||
+3
-2
@@ -6,6 +6,7 @@ use App\Helpers\Gravatar;
|
||||
use Illuminate\Auth\Authenticatable;
|
||||
use Illuminate\Contracts\Auth\Authenticatable as AuthenticableInterface;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Foundation\Auth\Access\Authorizable;
|
||||
|
||||
/**
|
||||
@@ -13,8 +14,8 @@ use Illuminate\Foundation\Auth\Access\Authorizable;
|
||||
*/
|
||||
class User extends Model implements AuthenticableInterface
|
||||
{
|
||||
use Authenticatable, Authorizable;
|
||||
public $timestamps = false;
|
||||
use SoftDeletes, Authenticatable, Authorizable;
|
||||
public $timestamps = true;
|
||||
protected $fillable = [
|
||||
'email',
|
||||
'username',
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
|
||||
class AddSoftDeletesToUsers extends Migration
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$platform = Schema::getConnection()
|
||||
->getDoctrineSchemaManager()
|
||||
->getDatabasePlatform();
|
||||
$platform->registerDoctrineTypeMapping('enum', 'string');
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('users', function (Blueprint $table) {
|
||||
$table->timestamp('created_at')
|
||||
->nullable()
|
||||
->after('id');
|
||||
$table->timestamp('updated_at')
|
||||
->nullable()
|
||||
->after('created_at');
|
||||
$table->timestamp('deleted_at')
|
||||
->nullable()
|
||||
->after('updated_at');
|
||||
});
|
||||
|
||||
DB::statement("
|
||||
ALTER TABLE `users`
|
||||
CHANGE `status` `status` enum('blocked','active') COLLATE 'utf8_unicode_ci' NOT NULL DEFAULT 'active'
|
||||
");
|
||||
|
||||
DB::statement('UPDATE users SET created_at=NOW(), updated_at=NOW()');
|
||||
DB::statement('UPDATE users SET status="blocked", deleted_at=NOW() WHERE status=""');
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('users', function (Blueprint $table) {
|
||||
$table->dropTimestamps();
|
||||
$table->dropSoftDeletes();
|
||||
});
|
||||
|
||||
DB::statement("
|
||||
ALTER TABLE `users`
|
||||
CHANGE `status` `status` enum('blocked','active', 'deleted') COLLATE 'utf8_unicode_ci' NOT NULL DEFAULT 'active'
|
||||
");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user