Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Per-wiki templates #376

Merged
merged 46 commits into from
Feb 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
95abc21
Add database schema and list UI for storing wikis in the database
supertassu Dec 5, 2020
0fa155e
Fix typos
supertassu Dec 5, 2020
de1e0c5
Remove useless constructor
supertassu Dec 5, 2020
9e0c8ed
Actually, add an index for database_name
supertassu Dec 6, 2020
fb64ef6
Make templates be per-wiki
supertassu Dec 6, 2020
6ca5799
Fix tests
supertassu Dec 6, 2020
77e0487
Create wiki objects in a trait when testing
supertassu Dec 6, 2020
29d24a3
Create wiki objects in a trait when testing
supertassu Dec 6, 2020
5c94bcd
use DatabaseMigrations instead of RefreshDatabase, seems to work better
supertassu Dec 6, 2020
668a8ec
Make templates be per-wiki
supertassu Dec 6, 2020
f07e5ea
Fix tests
supertassu Dec 6, 2020
67e0f1f
Merge branch 'per-wiki-templates' of https://github.com/UTRS2/utrs in…
supertassu Dec 6, 2020
123f83d
Add basic test for viewing templates
supertassu Dec 6, 2020
97abcec
Add confirmation prompts
supertassu Dec 7, 2020
b174203
Allow checking for general viewAny
supertassu Dec 8, 2020
dac32c4
Merge branch 'master' into create-wikis-table
supertassu Dec 8, 2020
100310d
Add migration to import initial wikis
supertassu Dec 8, 2020
51fabaf
Merge branch 'create-wikis-table' into per-wiki-templates
supertassu Dec 8, 2020
72671e9
That trait isn't needed anymore as it's a migration now
supertassu Dec 8, 2020
a603e36
forgot to actually remove that
supertassu Dec 8, 2020
58ffaad
That trait isn't needed anymore as it's a migration now
supertassu Dec 8, 2020
0c4790e
forgot to actually remove that
supertassu Dec 8, 2020
649377f
Merge branch 'create-wikis-table' into per-wiki-templates
supertassu Dec 8, 2020
e7f9a4f
Merge branch 'master' into create-wikis-table
dqwiki Dec 11, 2020
ddcac6f
Merge branch 'master' into create-wikis-table
supertassu Dec 11, 2020
f00a64e
Done as a migration, remove from readme
supertassu Dec 11, 2020
29857f1
Make templates be per-wiki
supertassu Dec 6, 2020
511e3d1
Fix tests
supertassu Dec 6, 2020
7ff3c63
Make templates be per-wiki
supertassu Dec 6, 2020
187ebe5
Fix tests
supertassu Dec 6, 2020
1255633
Create wiki objects in a trait when testing
supertassu Dec 6, 2020
5ef4600
Add basic test for viewing templates
supertassu Dec 6, 2020
db15e70
Allow checking for general viewAny
supertassu Dec 8, 2020
299d169
That trait isn't needed anymore as it's a migration now
supertassu Dec 8, 2020
6a0f066
forgot to actually remove that
supertassu Dec 8, 2020
1f76d65
Fix permission check
supertassu Dec 11, 2020
05b2314
Merge branch 'per-wiki-templates' of https://github.com/UTRS2/utrs in…
supertassu Dec 11, 2020
17a0572
Fix creation form, display wiki only if user can view more than one wiki
supertassu Dec 11, 2020
f699cd2
Merge branch 'master' into per-wiki-templates
dqwiki Feb 14, 2021
d35125c
Merge branch 'master' into per-wiki-templates
dqwiki Feb 14, 2021
d52a280
Remove duplicate import and cleanup
supertassu Feb 15, 2021
3070489
Allow moving templates between wikis
supertassu Feb 15, 2021
a571689
Merge branch 'master' into per-wiki-templates
dqwiki Feb 15, 2021
685cb93
Merge branch 'master' into per-wiki-templates
supertassu Feb 15, 2021
53cc6ff
Default to current wiki when editing templates
supertassu Feb 15, 2021
edad723
Merge branch 'per-wiki-templates' of github.com:UTRS2/utrs into per-w…
supertassu Feb 15, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 74 additions & 13 deletions app/Http/Controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use App\Models\Wiki;
use App\Models\Appeal;
use App\Models\Ban;
use App\Models\LogEntry;
Expand Down Expand Up @@ -33,33 +34,69 @@ public function listsitenotices()
return view('admin.tables', ['title' => 'All Sitenotices', 'tableheaders' => $tableheaders, 'rowcontents' => $rowcontents]);
}

public function listtemplates()
public function listtemplates(Request $request)
{
$this->authorize('viewAny', Template::class);
$alltemplates = Template::all();
/** @var User $user */
$user = $request->user();
$wikis = Wiki::get()
->filter(function (Wiki $wiki) use ($user) {
return $user->can('viewAny', [Template::class, $wiki]);
})
->pluck('id');

if ($wikis->isEmpty()) {
abort(403, "You can't view templates in any wikis!");
return '';
}

$templates = Template::with('wiki')
->whereIn('wiki_id', $wikis)
->get();

$tableheaders = ['ID', 'Name', 'Contents', 'Active'];
if ($wikis->count() > 1) {
$tableheaders[] = 'Wiki';
}

$rowcontents = [];

foreach ($alltemplates as $template) {
$idbutton = '<a href="/admin/templates/' . $template->id . '"><button type="button" class="btn btn-primary">' . $template->id . '</button></a>';
foreach ($templates as $template) {
$idbutton = '<a href="/admin/templates/' . $template->id . '" class="btn btn-primary">' . $template->id . '</a>';
$active = $template->active ? 'Yes' : 'No';

$rowcontents[$template->id] = [$idbutton, $template->name, htmlspecialchars($template->template), $active];

if ($wikis->count() > 1) {
$wikiName = $template->wiki->display_name . ' (' . $template->wiki->database_name . ')';
$rowcontents[$template->id][] = $wikiName;
}
}

return view('admin.tables', ['title' => 'All Templates', 'tableheaders' => $tableheaders, 'rowcontents' => $rowcontents, 'new' => true, 'createlink' => '/admin/templates/create', 'createtext' => 'New template']);
}

public function showNewTemplate()
public function showNewTemplate(Request $request)
{
$this->authorize('create', Template::class);
return view('admin.newtemplate');
/** @var User $user */
$user = $request->user();
$wikis = Wiki::get()
->filter(function (Wiki $wiki) use ($user) {
return $user->can('create', [Template::class, $wiki]);
})
->mapWithKeys(function (Wiki $wiki) {
return [$wiki->id => $wiki->display_name . ' (' . $wiki->database_name . ')'];
});

if ($wikis->isEmpty()) {
abort(403, "You can't create templates in any wikis!");
return '';
}

return view('admin.newtemplate', ['wikis' => $wikis]);
}

public function makeTemplate(Request $request)
{
$this->authorize('create', Template::class);

$ua = $request->userAgent();
$ip = $request->ip();
$lang = $request->header('Accept-Language');
Expand All @@ -68,8 +105,11 @@ public function makeTemplate(Request $request)
'name' => ['required', 'min:2', 'max:128', Rule::unique('templates', 'name')],
'template' => 'required|min:2|max:2048',
'default_status' => ['required', Rule::in(Appeal::REPLY_STATUS_CHANGE_OPTIONS)],
'wiki_id' => 'required|exists:wikis,id',
]);

$this->authorize('create', [Template::class, Wiki::findOrFail($data['wiki_id'])]);

$data['active'] = 1;

$template = Template::create($data);
Expand All @@ -78,10 +118,22 @@ public function makeTemplate(Request $request)
return redirect()->to('/admin/templates');
}

public function editTemplate(Template $template)
public function editTemplate(Request $request, Template $template)
{
$this->authorize('update', $template);
return view('admin.edittemplate', ["template" => $template]);

/** @var User $user */
$user = $request->user();

$wikis = Wiki::get()
->filter(function (Wiki $wiki) use ($user) {
return $user->can('create', [Template::class, $wiki]);
})
->mapWithKeys(function (Wiki $wiki) {
return [$wiki->id => $wiki->display_name . ' (' . $wiki->database_name . ')'];
});

return view('admin.edittemplate', ["template" => $template, 'wikis' => $wikis]);
}

public function updateTemplate(Request $request, Template $template)
Expand All @@ -96,10 +148,19 @@ public function updateTemplate(Request $request, Template $template)
'name' => ['required', 'min:2', 'max:128', Rule::unique('templates', 'name')->ignore($template->id)],
'template' => 'required|min:2|max:2048',
'default_status' => ['required', Rule::in(Appeal::REPLY_STATUS_CHANGE_OPTIONS)],
'wiki_id' => 'required|exists:wikis,id',
]);

$logText = 'update';

if ($data['wiki_id'] != $template->wiki_id) {
$newWiki = Wiki::findOrFail($data['wiki_id']);
$this->authorize('create', [Template::class, $newWiki]);
$logText .= ', change wiki to ' . $newWiki->database_name;
}

$template->update($data);
LogEntry::create(array('user_id' => Auth::id(), 'model_id' => $template->id, 'model_type' => Template::class, 'action' => 'update', 'ip' => $ip, 'ua' => $ua . " " . $lang));
LogEntry::create(array('user_id' => Auth::id(), 'model_id' => $template->id, 'model_type' => Template::class, 'action' => $logText, 'ip' => $ip, 'ua' => $ua . " " . $lang));
return redirect()->to('/admin/templates');
}
}
8 changes: 7 additions & 1 deletion app/Http/Controllers/AppealController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Appeal;
use App\Models\LogEntry;
use App\Models\Wiki;
use App\Models\Old\Oldappeal;
use App\Models\Old\Olduser;
use App\Models\Privatedata;
Expand Down Expand Up @@ -302,7 +303,12 @@ public function viewtemplates(Request $request, Appeal $appeal)
{
$this->authorize('update', $appeal);

$templates = Template::where('active', '=', 1)->get();
$templates = Template::where('active', true)
->whereHas('wiki', function (Builder $query) use ($appeal) {
$query->where('database_name', $appeal->wiki);
})
->get();

return view('appeals.templates', ['templates' => $templates, 'appeal' => $appeal, 'username' => $request->user()->username]);
}

Expand Down
8 changes: 8 additions & 0 deletions app/Models/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ class Template extends Model
protected $primaryKey = 'id';
public $timestamps = false;
protected $guarded = ['id'];
protected $casts = [
'active' => 'boolean',
];

public function wiki()
{
return $this->belongsTo(Wiki::class);
}
}
5 changes: 5 additions & 0 deletions app/Models/Wiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ class Wiki extends Model
protected $casts = [
'is_accepting_appeals' => 'boolean',
];

public function templates()
{
return $this->hasMany(Template::class);
}
}
21 changes: 14 additions & 7 deletions app/Policies/Admin/TemplatePolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Policies\Admin;

use App\Models\Wiki;
use App\Models\Template;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;
Expand All @@ -14,11 +15,16 @@ class TemplatePolicy
* Determine whether the user can view any templates.
*
* @param User $user
* @param Wiki|null $wiki
* @return mixed
*/
public function viewAny(User $user)
public function viewAny(User $user, ?Wiki $wiki = null)
{
return $user->hasAnySpecifiedPermsOnAnyWiki(['tooladmin']);
if (!$wiki) {
return $user->hasAnySpecifiedPermsOnAnyWiki('tooladmin');
}

return $user->hasAnySpecifiedLocalOrGlobalPerms($wiki->database_name, 'tooladmin');
}

/**
Expand All @@ -30,18 +36,19 @@ public function viewAny(User $user)
*/
public function view(User $user, Template $template)
{
return $user->hasAnySpecifiedPermsOnAnyWiki(['tooladmin']);
return $user->hasAnySpecifiedLocalOrGlobalPerms($template->wiki->database_name, 'tooladmin');
}

/**
* Determine whether the user can create templates.
*
* @param User $user
* @param Wiki $wiki
* @return mixed
*/
public function create(User $user)
public function create(User $user, Wiki $wiki)
{
return $user->hasAnySpecifiedPermsOnAnyWiki(['tooladmin']);
return $user->hasAnySpecifiedLocalOrGlobalPerms($wiki->database_name, 'tooladmin');
}

/**
Expand All @@ -53,7 +60,7 @@ public function create(User $user)
*/
public function update(User $user, Template $template)
{
return $user->hasAnySpecifiedPermsOnAnyWiki(['tooladmin']);
return $user->hasAnySpecifiedLocalOrGlobalPerms($template->wiki->database_name, 'tooladmin');
}

/**
Expand All @@ -65,6 +72,6 @@ public function update(User $user, Template $template)
*/
public function delete(User $user, Template $template)
{
return $user->hasAnySpecifiedPermsOnAnyWiki(['tooladmin']);
return $user->hasAnySpecifiedLocalOrGlobalPerms($template->wiki->database_name, 'tooladmin');
}
}
1 change: 1 addition & 0 deletions database/factories/TemplateFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function definition()
'name' => implode(' ', $this->faker->words(3)),
'template' => implode("\n\n", $this->faker->sentences(2)),
'active' => $this->faker->boolean(80),
'wiki_id' => 1, // this should always exist, as there is a seed to add it
];
}

Expand Down
41 changes: 41 additions & 0 deletions database/migrations/2020_12_06_095703_add_wiki_id_to_templates.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddWikiIdToTemplates extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('templates', function (Blueprint $table) {
$table->unsignedBigInteger('wiki_id')
->nullable(true);

$table->foreign('wiki_id')
->references('id')
->on('wikis')
->onDelete('cascade');
$table->index(['wiki_id', 'active']);
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('templates', function (Blueprint $table) {
$table->dropForeign(['wiki_id']);
$table->dropIndex(['wiki_id', 'active']);
$table->dropColumn('wiki_id');
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

use Illuminate\Database\Migrations\Migration;

class BackfillWikiIdToTemplates extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$count = DB::table('templates')->count();
if ($count == 0) {
return;
}

$wikiId = DB::table('wikis')
->where('database_name', 'enwiki')
->pluck('id')
->first();
if (!$wikiId) {
throw new RuntimeException('Please synchronize wikis to the database before running this migration.');
}

DB::table('templates')
->update([
'wiki_id' => $wikiId,
]);
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::table('templates')
->update([
'wiki_id' => null,
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class MakeWikiIdNonNullableInTemplates extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('templates', function (Blueprint $table) {
$table->unsignedBigInteger('wiki_id')
->nullable(false)
->change();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('templates', function (Blueprint $table) {
$table->unsignedBigInteger('wiki_id')
->nullable(true)
->change();
});
}
}
Loading