|
| 1 | +<?php /** @noinspection ForgottenDebugOutputInspection */ |
| 2 | + |
| 3 | +use Dotenv\Dotenv; |
| 4 | +use Illuminate\Support\Arr; |
| 5 | +use Maicol07\SSO\Addons\Groups; |
| 6 | + |
| 7 | +// Note: Since this is called from the example folder, the vendor folder is located in the previous tree level |
| 8 | +require_once __DIR__ . '/../vendor/autoload.php'; |
| 9 | + |
| 10 | +// Load .env |
| 11 | +$env = Dotenv::createImmutable(__DIR__); |
| 12 | +$env->load(); |
| 13 | + |
| 14 | +if (Arr::exists($_POST, 'username')) { |
| 15 | + require_once __DIR__ . '/flarum.php'; |
| 16 | + /** @var $flarum <-- Fix PHPStorm hints */ |
| 17 | + |
| 18 | + // Create the user to work with |
| 19 | + $flarum_user = $flarum->user(Arr::get($_POST, 'username')); |
| 20 | + |
| 21 | + // Check if user exists in flarum |
| 22 | + if (!empty($flarum_user->id)) { |
| 23 | + // Set his attributes (from form) |
| 24 | + $flarum_user->attributes->nickname = Arr::get($_POST, 'nickname'); |
| 25 | + $flarum_user->attributes->avatarUrl = Arr::get($_POST, 'avatar'); |
| 26 | + $flarum_user->attributes->bio = Arr::get($_POST, 'bio'); |
| 27 | + |
| 28 | + // Let's add to it some groups (optional, only for demonstration) |
| 29 | + // First, let's add the Groups addon (note that the Groups class is imported at the top with the use statement) |
| 30 | + $flarum->loadAddon(Groups::class); |
| 31 | + $flarum->setAddonProperties(Groups::class, ['set_groups_admins' => env('SET_GROUPS_ADMINS') ?? true]); |
| 32 | + // Then, add the groups (as an array) to the correct attribute in user relationships |
| 33 | + $flarum_user->relationships->groups = ['Premium', 'Novice']; |
| 34 | + |
| 35 | + // Update the user |
| 36 | + $success = $flarum_user->update(); |
| 37 | + |
| 38 | + // Redirect to Flarum |
| 39 | + if (!empty($_GET['redirect'])) { |
| 40 | + $flarum->redirect(); |
| 41 | + } |
| 42 | + } else { |
| 43 | + $success = false; |
| 44 | + } |
| 45 | +} elseif (!empty($username) || !empty($password)) { |
| 46 | + $success = false; |
| 47 | +} |
| 48 | +?> |
| 49 | +<!DOCTYPE html> |
| 50 | +<html lang="en"> |
| 51 | +<head> |
| 52 | + <title>Update user</title> |
| 53 | + <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 54 | + <!-- Lightweight CSS only to make this page beauty --> |
| 55 | + <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.0/css/bulma.min.css" |
| 56 | + integrity="sha256-aPeK/N8IHpHsvPBCf49iVKMdusfobKo2oxF8lRruWJg=" crossorigin="anonymous"> |
| 57 | +</head> |
| 58 | +<body> |
| 59 | +<div class="container"> |
| 60 | + <div class="box" style="margin-top: 25px;"> |
| 61 | + <h1 class="title">Update user</h1> |
| 62 | + |
| 63 | + <form method="post"> |
| 64 | + <p class="mb-3">Note: This is only a test, so the infos you can edit here are limited!</p> |
| 65 | + <div class="columns"> |
| 66 | + <div class="column"> |
| 67 | + <label class="label" for="username">Username (doesn't change)</label> |
| 68 | + <input id="username" type="text" class="input" name="username" placeholder="Username"> |
| 69 | + |
| 70 | + <label class="label" for="nickname">Nickname</label> |
| 71 | + <input id="nickname" type="text" class="input" name="nickname" placeholder="Nickname"> |
| 72 | + </div> |
| 73 | + <div class="column"> |
| 74 | + <label class="label mt-3" for="avatar">Avatar URL</label> |
| 75 | + <input id="avatar" type="url" class="input" name="avatar" placeholder="Avatar URL"> |
| 76 | + |
| 77 | + <label class="label mt-3" for="bio">Bio</label> |
| 78 | + <textarea id="bio" name="bio" class="textarea" placeholder="Update user bio"></textarea> |
| 79 | + </div> |
| 80 | + </div> |
| 81 | + <button class="button is-centered is-center" type="submit" style="display: block; margin: 0 auto;">Update |
| 82 | + </button> |
| 83 | + </form> |
| 84 | + |
| 85 | + <?php if (isset($flarum) and !empty($success)) { ?> |
| 86 | + <div class="notification is-success"> |
| 87 | + <button class="delete"></button> |
| 88 | + Successfully updated! Click the button below to go to Flarum user! |
| 89 | + <br> |
| 90 | + <a class="button is-rounded mt-5" |
| 91 | + href="<?php echo $flarum->url . (isset($_POST['username']) ? "/u/{$_POST['username']}" : '') ?>" |
| 92 | + style="display: block; margin: 0 auto; width: max-content;"> |
| 93 | + Go to user in Flarum |
| 94 | + </a> |
| 95 | + <details> |
| 96 | + <summary>User details</summary> |
| 97 | + <pre style="margin: 16px 0;"> |
| 98 | + <?php |
| 99 | + if (isset($flarum_user) and $flarum_user->fetch()) { |
| 100 | + $is_admin = $flarum_user->isAdmin ? 'Yes' : 'No'; |
| 101 | + echo "User ID: $flarum_user->id<br>Is user admin? <b>$is_admin</b><br>User attributes: <br>"; |
| 102 | + var_export($flarum_user->getAttributes()); |
| 103 | + echo "<br>User relationships: <br>"; |
| 104 | + var_export($flarum_user->getRelationships()); |
| 105 | + } else { |
| 106 | + echo "Can't fetch user from Flarum!"; |
| 107 | + } |
| 108 | + ?> |
| 109 | + </pre> |
| 110 | + </details> |
| 111 | + </div> |
| 112 | + <?php } elseif (isset($success) and empty($success)) { ?> |
| 113 | + <div class="notification is-danger"> |
| 114 | + <button class="delete"></button> |
| 115 | + Update failed |
| 116 | + </div> |
| 117 | + <?php } ?> |
| 118 | + </div> |
| 119 | +</div> |
| 120 | + |
| 121 | +<?php require_once 'footer.php' ?> |
| 122 | +</body> |
| 123 | +</html> |
0 commit comments