v0.8.0
This commit is contained in:
commit
bf5c31169a
221 files changed
+16292
No files matched your search
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Http\Controllers;
|
||||
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
|
||||
/** The built screens, served by the package itself: the app does not need Node, a build step or a publish. */
|
||||
class AssetController
|
||||
{
|
||||
private const FILES = ['manager.js' => 'application/javascript', 'manager.css' => 'text/css', 'vue.js' => 'application/javascript'];
|
||||
|
||||
public function __invoke(string $file): BinaryFileResponse
|
||||
{
|
||||
abort_unless(isset(self::FILES[$file]) && is_file($path = __DIR__.'/../../../resources/dist/'.$file), 404);
|
||||
|
||||
return response()->file($path, [
|
||||
'Content-Type' => self::FILES[$file],
|
||||
'Cache-Control' => 'public, max-age=31536000, immutable',
|
||||
'X-Content-Type-Options' => 'nosniff',
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use IriksIT\Manager\Support\Menu;
|
||||
use IriksIT\Manager\Support\ModuleRegistry;
|
||||
use IriksIT\Manager\Support\Tiles;
|
||||
|
||||
class DashboardController
|
||||
{
|
||||
public function __invoke(Request $request, ModuleRegistry $modules, Menu $menu, Tiles $tiles)
|
||||
{
|
||||
$user = $request->user();
|
||||
|
||||
// Only what this account may reach, in the face it looks at.
|
||||
$navigation = $menu->for($user, array_values(array_filter($modules->permissionValues(), fn (string $permission) => $user?->can($permission))));
|
||||
|
||||
// What the big buttons of the simple start screen say: by the address of the menu item.
|
||||
$facts = [];
|
||||
|
||||
foreach ($tiles->for($user) as $route => $fact) {
|
||||
$facts[route($route, [], false)] = $fact;
|
||||
}
|
||||
|
||||
$help = (array) config('manager.simple.help', []);
|
||||
$site = config('manager.simple.site_link', '/');
|
||||
|
||||
return $request->wantsJson()
|
||||
? response()->json([
|
||||
'navigation' => $navigation,
|
||||
'facts' => (object) $facts,
|
||||
// A way to ask for help (a name and a link, for example a WhatsApp link) and a link to the site, under the buttons.
|
||||
'help' => is_string($help['link'] ?? null) && ($help['link'] ?? '') !== '' ? ['name' => (string) ($help['name'] ?? ''), 'link' => $help['link']] : null,
|
||||
'site' => is_string($site) && $site !== '' ? $site : null,
|
||||
])
|
||||
: view('manager::dashboard', ['navigation' => $navigation]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use IriksIT\Manager\Support\LoginChecks;
|
||||
use IriksIT\Manager\Support\Names;
|
||||
|
||||
/** The manager's own login, for an app that has none. Failed attempts reach the audit log through Laravel's `Failed` event. */
|
||||
class LoginController
|
||||
{
|
||||
public function store(Request $request, LoginChecks $checks): JsonResponse
|
||||
{
|
||||
$checks->before($request);
|
||||
|
||||
$credentials = $request->validate([
|
||||
'email' => ['required', 'string', 'email', 'max:255'],
|
||||
'password' => ['required', 'string', 'max:200'],
|
||||
]);
|
||||
|
||||
$refused = null;
|
||||
|
||||
// `attemptWhen` runs the second checks after the password is right and before the login begins; a refusal is a failed login.
|
||||
$loggedIn = Auth::attemptWhen($credentials, function ($user) use ($checks, $request, &$refused) {
|
||||
try {
|
||||
$checks->after($request, $user);
|
||||
} catch (ValidationException $e) {
|
||||
$refused = $e;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}, $request->boolean('remember'));
|
||||
|
||||
if (! $loggedIn) {
|
||||
throw $refused ?? ValidationException::withMessages(['email' => trans('auth.failed')]);
|
||||
}
|
||||
|
||||
$request->session()->regenerate();
|
||||
|
||||
return response()->json(['redirect' => '/'.trim((string) config('manager.path'), '/')]);
|
||||
}
|
||||
|
||||
public function destroy(Request $request): JsonResponse
|
||||
{
|
||||
Auth::guard()->logout();
|
||||
$request->session()->invalidate();
|
||||
$request->session()->regenerateToken();
|
||||
|
||||
return response()->json(['redirect' => Route::has(Names::route('login')) ? route(Names::route('login'), [], false) : '/']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use IriksIT\Manager\Support\Markdown;
|
||||
|
||||
/** The preview of the editor: the text as the site renders it (raw HTML is escaped there, so the answer is safe to show). */
|
||||
class MarkdownController
|
||||
{
|
||||
public function __invoke(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate(['text' => ['nullable', 'string', 'max:200000']]);
|
||||
|
||||
return response()->json(['html' => Markdown::toHtml((string) ($data['text'] ?? ''))]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Response;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
|
||||
/**
|
||||
* The pictures of the tour. A picture is `<name>.webp` or `<name>.png` in `manager.mascot.path` (default: resources/mascot of
|
||||
* the package); a site can give its own set by pointing the path at a folder of its own. A picture that is not there is
|
||||
* answered with a small paw, so the tour works before the pictures do.
|
||||
*/
|
||||
class MascotController
|
||||
{
|
||||
/** What the tour asks for. */
|
||||
public const NAMES = ['welcome', 'point', 'calm', 'write', 'photos', 'pages', 'undo', 'oops', 'party', 'profile', 'peek'];
|
||||
|
||||
private const TYPES = ['webp' => 'image/webp', 'png' => 'image/png'];
|
||||
|
||||
public function __invoke(string $name): BinaryFileResponse|Response
|
||||
{
|
||||
abort_unless(in_array($name, self::NAMES, true), 404);
|
||||
|
||||
$folder = rtrim((string) (config('manager.mascot.path') ?: __DIR__.'/../../../resources/mascot'), '/');
|
||||
|
||||
foreach (self::TYPES as $extension => $type) {
|
||||
if (is_file($path = "$folder/$name.$extension")) {
|
||||
return response()->file($path, ['Content-Type' => $type, 'Cache-Control' => 'public, max-age=86400', 'X-Content-Type-Options' => 'nosniff']);
|
||||
}
|
||||
}
|
||||
|
||||
return response(self::paw(), 200, ['Content-Type' => 'image/svg+xml', 'Cache-Control' => 'public, max-age=300', 'X-Content-Type-Options' => 'nosniff']);
|
||||
}
|
||||
|
||||
/** A paw: the stand-in for every picture that is not there yet. */
|
||||
private static function paw(): string
|
||||
{
|
||||
return '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 128 128"><g fill="#f43f7a">'
|
||||
.'<ellipse cx="64" cy="84" rx="26" ry="22"/><ellipse cx="26" cy="58" rx="11" ry="15" transform="rotate(-20 26 58)"/>'
|
||||
.'<ellipse cx="50" cy="38" rx="11" ry="15" transform="rotate(-8 50 38)"/><ellipse cx="78" cy="38" rx="11" ry="15" transform="rotate(8 78 38)"/>'
|
||||
.'<ellipse cx="102" cy="58" rx="11" ry="15" transform="rotate(20 102 58)"/></g></svg>';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Validation\Rule;
|
||||
use IriksIT\Manager\Models\UserSetting;
|
||||
use IriksIT\Manager\Support\AuditTrail;
|
||||
use IriksIT\Manager\Support\Menu;
|
||||
use IriksIT\Manager\Support\Themes;
|
||||
|
||||
/** Your own name, password and look. Never the rights: those are the groups'. */
|
||||
class ProfileController
|
||||
{
|
||||
public function show(Request $request, Themes $themes): JsonResponse
|
||||
{
|
||||
$user = $request->user();
|
||||
|
||||
return response()->json(['profile' => [
|
||||
'name' => (string) $user->name,
|
||||
'email' => (string) $user->email,
|
||||
'is_super_admin' => method_exists($user, 'isManagerSuperAdmin') && $user->isManagerSuperAdmin(),
|
||||
'groups' => method_exists($user, 'managerGroups') ? $user->managerGroups()->pluck('name')->values() : [],
|
||||
'theme' => $themes->keyFor($user),
|
||||
'locale' => UserSetting::value($user, 'locale') ?? app()->getLocale(),
|
||||
'locales' => UserSetting::LOCALES,
|
||||
'tutorial' => UserSetting::value($user, 'tutorial'),
|
||||
'themes' => config('manager.user_themes')
|
||||
? collect($themes->all())->map(fn (array $theme, string $key) => ['key' => $key, 'label' => __($theme['label']), 'colors' => $theme['colors']])->values()
|
||||
: [],
|
||||
]]);
|
||||
}
|
||||
|
||||
/** The face of the manager (`full` or `simple`) an account that may switch looks at. */
|
||||
public function view(Request $request, Menu $menu): JsonResponse
|
||||
{
|
||||
abort_unless($menu->maySwitch($request->user()), 403);
|
||||
|
||||
$data = $request->validate(['view' => ['required', Rule::in([Menu::FULL, Menu::SIMPLE])]]);
|
||||
UserSetting::put($request->user(), ['view' => $data['view']]);
|
||||
|
||||
return response()->json(['view' => $data['view']]);
|
||||
}
|
||||
|
||||
/** Where the account stands with the tour: finished, skipped, or (to look at it again) pending. Any account may, for itself. */
|
||||
public function tutorial(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate(['state' => ['required', Rule::in(UserSetting::TUTORIAL)]]);
|
||||
UserSetting::put($request->user(), ['tutorial' => $data['state']]);
|
||||
|
||||
return response()->json(['tutorial' => $data['state']]);
|
||||
}
|
||||
|
||||
public function update(Request $request, Themes $themes): JsonResponse
|
||||
{
|
||||
$user = $request->user();
|
||||
app(AuditTrail::class)->about($user);
|
||||
|
||||
$data = $request->validate([
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'current_password' => ['nullable', 'string', 'max:200'],
|
||||
'password' => ['nullable', 'string', 'min:12', 'max:200', 'confirmed'],
|
||||
'theme' => ['nullable', 'string', Rule::in(array_keys($themes->all()))],
|
||||
'locale' => ['nullable', 'string', Rule::in(UserSetting::LOCALES)],
|
||||
]);
|
||||
|
||||
if (($data['password'] ?? '') !== '' && ! Hash::check((string) ($data['current_password'] ?? ''), $user->getAuthPassword())) {
|
||||
return response()->json(['message' => $wrong = __('The current password is not right.'), 'errors' => ['current_password' => [$wrong]]], 422);
|
||||
}
|
||||
|
||||
$user->name = $data['name'];
|
||||
($data['password'] ?? '') !== '' && $user->password = Hash::make($data['password']);
|
||||
$user->save();
|
||||
|
||||
if (config('manager.user_themes') && $request->has('theme')) {
|
||||
UserSetting::put($user, ['theme' => $data['theme'] ?: null]);
|
||||
}
|
||||
|
||||
if ($request->has('locale')) {
|
||||
UserSetting::put($user, ['locale' => $data['locale'] ?: null]);
|
||||
}
|
||||
|
||||
return response()->json(['saved' => true]);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user