Update admin dashboard

This commit is contained in:
Daniel Supernault 2020-02-18 00:22:47 -07:00
parent 81f4ccf979
commit 9a0c5bc8ae
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
5 changed files with 258 additions and 32 deletions

View file

@ -0,0 +1,158 @@
<?php
namespace App\Http\Controllers\Admin;
use Cache, DB;
use Illuminate\Http\Request;
use App\ModLog;
use App\User;
use App\Mail\AdminMessage;
use Illuminate\Support\Facades\Mail;
use App\Services\ModLogService;
trait AdminUserController
{
public function users(Request $request)
{
$col = $request->query('col') ?? 'id';
$dir = $request->query('dir') ?? 'desc';
$users = User::select('id', 'username', 'status')
->withCount('statuses')
->orderBy($col, $dir)
->simplePaginate(10);
return view('admin.users.home', compact('users'));
}
public function userShow(Request $request, $id)
{
$user = User::findOrFail($id);
$profile = $user->profile;
return view('admin.users.show', compact('user', 'profile'));
}
public function userEdit(Request $request, $id)
{
$user = User::findOrFail($id);
$profile = $user->profile;
return view('admin.users.edit', compact('user', 'profile'));
}
public function userEditSubmit(Request $request, $id)
{
$user = User::findOrFail($id);
$profile = $user->profile;
$changed = false;
if($request->filled('name') && $request->input('name') != $user->name) {
$user->name = $profile->name = $request->input('name');
$changed = true;
}
if($request->filled('username') && $request->input('username') != $user->username) {
$user->username = $profile->username = $request->input('username');
$changed = true;
}
if($request->filled('email') && $request->input('email') != $user->email) {
if(filter_var($request->input('email'), FILTER_VALIDATE_EMAIL) == false) {
abort(500, 'Invalid email address');
}
$user->email = $request->input('email');
$changed = true;
}
if($request->input('bio') != $profile->bio) {
$profile->bio = $request->input('bio');
$changed = true;
}
if($request->input('website') != $profile->website) {
$profile->website = $request->input('website');
$changed = true;
}
if($changed == true) {
$profile->save();
$user->save();
}
return redirect('/i/admin/users/show/' . $user->id);
}
public function userActivity(Request $request, $id)
{
$user = User::findOrFail($id);
$profile = $user->profile;
$logs = $user->accountLog()->orderByDesc('created_at')->paginate(10);
return view('admin.users.activity', compact('user', 'profile', 'logs'));
}
public function userMessage(Request $request, $id)
{
$user = User::findOrFail($id);
$profile = $user->profile;
return view('admin.users.message', compact('user', 'profile'));
}
public function userMessageSend(Request $request, $id)
{
$this->validate($request, [
'message' => 'required|string|min:5|max:500'
]);
$user = User::findOrFail($id);
$profile = $user->profile;
$message = $request->input('message');
Mail::to($user->email)->send(new AdminMessage($message));
ModLogService::boot()
->objectUid($user->id)
->objectId($user->id)
->objectType('App\User::class')
->user($request->user())
->action('admin.user.mail')
->metadata([
'message' => $message
])
->accessLevel('admin')
->save();
return redirect('/i/admin/users/show/' . $user->id);
}
public function userModTools(Request $request, $id)
{
$user = User::findOrFail($id);
$profile = $user->profile;
return view('admin.users.modtools', compact('user', 'profile'));
}
public function userModLogs(Request $request, $id)
{
$user = User::findOrFail($id);
$profile = $user->profile;
$logs = ModLog::whereObjectUid($user->id)
->orderByDesc('created_at')
->simplePaginate(10);
return view('admin.users.modlogs', compact('user', 'profile', 'logs'));
}
public function userModLogsMessage(Request $request, $id)
{
$this->validate($request, [
'message' => 'required|string|min:5|max:500'
]);
$user = User::findOrFail($id);
$profile = $user->profile;
$msg = $request->input('message');
ModLogService::boot()
->objectUid($user->id)
->objectId($user->id)
->objectType('App\User::class')
->user($request->user())
->message($msg)
->accessLevel('admin')
->save();
return redirect('/i/admin/users/modlogs/' . $user->id);
}
public function userDelete(Request $request, $id)
{
$user = User::findOrFail($id);
$profile = $user->profile;
return view('admin.users.delete', compact('user', 'profile'));
}
}

View file

@ -21,7 +21,8 @@ use App\Http\Controllers\Admin\{
AdminReportController,
AdminMediaController,
AdminSettingsController,
AdminSupportController
AdminSupportController,
AdminUserController
};
use Illuminate\Validation\Rule;
use App\Services\AdminStatsService;
@ -32,11 +33,13 @@ class AdminController extends Controller
AdminDiscoverController,
AdminMediaController,
AdminSettingsController,
AdminInstanceController;
AdminInstanceController,
AdminUserController;
public function __construct()
{
$this->middleware('admin');
$this->middleware('dangerzone');
$this->middleware('twofactor');
}
@ -46,25 +49,6 @@ class AdminController extends Controller
return view('admin.home', compact('data'));
}
public function users(Request $request)
{
$col = $request->query('col') ?? 'id';
$dir = $request->query('dir') ?? 'desc';
$users = User::select('id', 'username', 'status')
->withCount('statuses')
->orderBy($col, $dir)
->simplePaginate(10);
return view('admin.users.home', compact('users'));
}
public function editUser(Request $request, $id)
{
$user = User::findOrFail($id);
$profile = $user->profile;
return view('admin.users.edit', compact('user', 'profile'));
}
public function statuses(Request $request)
{
$statuses = Status::orderBy('id', 'desc')->simplePaginate(10);
@ -109,22 +93,25 @@ class AdminController extends Controller
'nullable',
'string',
Rule::in(['all', 'local', 'remote'])
],
'limit' => 'nullable|integer|min:1|max:50'
]
]);
$search = $request->input('search');
$filter = $request->input('filter');
$limit = 12;
if($search) {
$profiles = Profile::select('id','username')
->where('username', 'like', "%$search%")
->orderBy('id','desc')
->simplePaginate($limit);
} else if($filter) {
$profiles = Profile::select('id','username')->withCount(['likes','statuses','followers'])->orderBy($filter, $order)->simplePaginate($limit);
} else {
$profiles = Profile::select('id','username')->orderBy('id','desc')->simplePaginate($limit);
->whereNull('status')
->when($search, function($q, $search) {
return $q->where('username', 'like', "%$search%");
})->when($filter, function($q, $filter) {
if($filter == 'local') {
return $q->whereNull('domain');
}
if($filter == 'remote') {
return $q->whereNotNull('domain');
}
return $q;
})->orderByDesc('id')
->simplePaginate($limit);
return view('admin.profiles.home', compact('profiles'));
}

36
app/ModLog.php Normal file
View file

@ -0,0 +1,36 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class ModLog extends Model
{
protected $visible = ['id'];
public function admin()
{
return $this->belongsTo(User::class, 'user_id');
}
public function actionToText()
{
$msg = 'Unknown action';
switch ($this->action) {
case 'admin.user.message':
$msg = "Sent Email Message";
break;
case 'admin.user.action.cw.warn':
$msg = "Sent CW reminder";
break;
default:
$msg = 'Unknown action';
break;
}
return $msg;
}
}

View file

@ -83,4 +83,9 @@ class User extends Authenticatable
return 'profile:storage:used:' . $this->id;
}
public function accountLog()
{
return $this->hasMany(AccountLog::class);
}
}

View file

@ -0,0 +1,40 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateModLogsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('mod_logs', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id')->unsigned()->index();
$table->string('user_username')->nullable();
$table->bigInteger('object_uid')->nullable()->unsigned()->index();
$table->bigInteger('object_id')->nullable()->unsigned()->index();
$table->string('object_type')->nullable()->index();
$table->string('action')->nullable();
$table->text('message')->nullable();
$table->json('metadata')->nullable();
$table->string('access_level')->default('admin')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('mod_logs');
}
}