Skip to content

Commit e886c59

Browse files
committed
update(example): Updated example
1 parent d173cb1 commit e886c59

File tree

5 files changed

+154
-63
lines changed

5 files changed

+154
-63
lines changed

example/delete.php

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,26 +56,7 @@
5656
</ul>
5757
</details>
5858
</div>
59-
<footer class="footer">
60-
<div class="content has-text-centered">
61-
<div class="field is-grouped" style="justify-content: center;">
62-
<p class="control">
63-
<a class="button is-link" href="index.php">
64-
Login
65-
</a>
66-
</p>
67-
<p class="control">
68-
<a class="button" href="logout.php">
69-
Logout
70-
</a>
71-
</p>
72-
<p class="control">
73-
<a class="button is-danger" href="delete.php?username=user">
74-
Delete user
75-
</a>
76-
</p>
77-
</div>
78-
</div>
79-
</footer>
59+
60+
<?php require_once 'footer.php' ?>
8061
</body>
8162
</html>

example/footer.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<footer class="footer">
2+
<div class="content has-text-centered">
3+
<div class="field is-grouped" style="justify-content: center;">
4+
<p class="control">
5+
<a class="button is-link" href="index.php">
6+
Login
7+
</a>
8+
</p>
9+
<p class="control">
10+
<a class="button" href="update.php">
11+
Update
12+
</a>
13+
</p>
14+
<p class="control">
15+
<a class="button" href="logout.php">
16+
Logout
17+
</a>
18+
</p>
19+
<p class="control">
20+
<a class="button is-danger" href="delete.php?username=user">
21+
Delete user
22+
</a>
23+
</p>
24+
</div>
25+
</div>
26+
</footer>

example/index.php

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -147,26 +147,7 @@
147147
</div>
148148
<?php } ?>
149149
</div>
150-
<footer class="footer">
151-
<div class="content has-text-centered">
152-
<div class="field is-grouped" style="justify-content: center;">
153-
<p class="control">
154-
<a class="button is-link" href="index.php">
155-
Login
156-
</a>
157-
</p>
158-
<p class="control">
159-
<a class="button" href="logout.php">
160-
Logout
161-
</a>
162-
</p>
163-
<p class="control">
164-
<a class="button is-danger" href="delete.php?username=user">
165-
Delete user
166-
</a>
167-
</p>
168-
</div>
169-
</div>
170-
</footer>
150+
151+
<?php require_once 'footer.php' ?>
171152
</body>
172153
</html>

example/logout.php

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,6 @@
4646
<?php } ?>
4747
</div>
4848

49-
<footer class="footer">
50-
<div class="content has-text-centered">
51-
<div class="field is-grouped" style="justify-content: center;">
52-
<p class="control">
53-
<a class="button is-link" href="index.php">
54-
Login
55-
</a>
56-
</p>
57-
<p class="control">
58-
<a class="button" href="logout.php">
59-
Logout
60-
</a>
61-
</p>
62-
<p class="control">
63-
<a class="button is-danger" href="delete.php?username=user">
64-
Delete user
65-
</a>
66-
</p>
67-
</div>
68-
</div>
69-
</footer>
49+
<?php require_once 'footer.php' ?>
7050
</body>
7151
</html>

example/update.php

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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

Comments
 (0)