]> BookStack Code Mirror - bookstack/commitdiff
Users API: Fixed sending invite when using form requests
authorDan Brown <redacted>
Wed, 13 Dec 2023 15:11:42 +0000 (15:11 +0000)
committerDan Brown <redacted>
Wed, 13 Dec 2023 15:13:54 +0000 (15:13 +0000)
- Cast send_invite value in cases where it might not have been a boolean,
  which occurs on non-JSON requests.
- Added test to cover.
- Updated API docs to mention and shown boolean usage.

app/Users/Controllers/UserApiController.php
resources/views/api-docs/parts/getting-started.blade.php
tests/Api/UsersApiTest.php

index 880165e1bc77780b46a5d234c7ba2ed7d36bea49..1ccfecd7335e12417c80a5e5cdca74a20f09843e 100644 (file)
@@ -90,7 +90,7 @@ class UserApiController extends ApiController
     public function create(Request $request)
     {
         $data = $this->validate($request, $this->rules()['create']);
-        $sendInvite = ($data['send_invite'] ?? false) === true;
+        $sendInvite = boolval($data['send_invite'] ?? false) === true;
 
         $user = null;
         DB::transaction(function () use ($data, $sendInvite, &$user) {
index 75b71c6beb7d355e15ccf32440945fac07a4acbb..229fe7dce74155e0e5b24e09d11df160ebdd1950 100644 (file)
     </em>
 </p>
 
+<p>
+    <em>
+        * Form requests can accept boolean (<code>true</code>/<code>false</code>) values via a <code>1</code> or <code>0</code>.
+    </em>
+</p>
+
 <p>
     Regardless of format chosen, ensure you set a <code>Content-Type</code> header on requests so that the system can correctly parse your request data.
     The API is primarily designed to be interfaced using JSON, since responses are always in JSON format, hence examples in this documentation will be shown as JSON.
 
 <pre><code class="language-json">{
   "name": "My new item",
+  "locked": true,
   "books": [105, 263],
   "tags": [{"name": "Tag Name", "value": "Tag Value"}],
 }</code></pre>
 
 <p><strong>x-www-form-urlencoded</strong></p>
 
-<pre><code class="language-text">name=My%20new%20item&books%5B0%5D=105&books%5B1%5D=263&tags%5B0%5D%5Bname%5D=Tag%20Name&tags%5B0%5D%5Bvalue%5D=Tag%20Value</code></pre>
+<pre><code class="language-text">name=My%20new%20item&locked=1&books%5B0%5D=105&books%5B1%5D=263&tags%5B0%5D%5Bname%5D=Tag%20Name&tags%5B0%5D%5Bvalue%5D=Tag%20Value</code></pre>
 
 <p><strong>x-www-form-urlencoded (Decoded for readability)</strong></p>
 
 <pre><code class="language-text">name=My new item
+locked=1
 books[0]=105
 books[1]=263
 tags[0][name]=Tag Name
index 6ad7272577bd3adfd8bb73056427c9fc8a7cafb7..a0c67d0d281f73612d2f2f9ada724fed833c1859 100644 (file)
@@ -143,6 +143,23 @@ class UsersApiTest extends TestCase
         Notification::assertSentTo($user, UserInviteNotification::class);
     }
 
+    public function test_create_with_send_invite_works_with_value_of_1()
+    {
+        $this->actingAsApiAdmin();
+        Notification::fake();
+
+        $resp = $this->postJson($this->baseEndpoint, [
+            'name'        => 'Benny Boris',
+            'email'       => '[email protected]',
+            'send_invite' => '1', // Submissions via x-www-form-urlencoded/form-data may use 1 instead of boolean
+        ]);
+
+        $resp->assertStatus(200);
+        /** @var User $user */
+        $user = User::query()->where('email', '=', '[email protected]')->first();
+        Notification::assertSentTo($user, UserInviteNotification::class);
+    }
+
     public function test_create_name_and_email_validation()
     {
         $this->actingAsApiAdmin();