From 0338872c2613ae4a6d74d4ca9a6a89a6772c75f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Jaussoin?= Date: Wed, 5 Jan 2022 14:40:54 +0100 Subject: [PATCH] Fix the scope handling admins_manage_multi_domains in the booted() method Update the documentation Update and complete the tests Update the dependencies --- flexiapi/app/Account.php | 11 +- .../Controllers/Api/AccountController.php | 6 +- .../Api/Admin/AccountController.php | 10 +- flexiapi/composer.lock | 147 ++++++++++-------- .../api/documentation_markdown.blade.php | 3 +- flexiapi/tests/Feature/AccountApiTest.php | 69 ++++++++ flexisip-account-manager.spec | 2 +- 7 files changed, 163 insertions(+), 85 deletions(-) diff --git a/flexiapi/app/Account.php b/flexiapi/app/Account.php index edd0fa5..b0e8589 100644 --- a/flexiapi/app/Account.php +++ b/flexiapi/app/Account.php @@ -50,13 +50,12 @@ class Account extends Authenticatable */ protected static function booted() { - $user = Auth::user(); - - if (!$user || !$user->admin || !config('app.admins_manage_multi_domains')) { - static::addGlobalScope('domain', function (Builder $builder) { + static::addGlobalScope('domain', function (Builder $builder) { + $user = Auth::user(); + if (!$user || !$user->admin || !config('app.admins_manage_multi_domains')) { $builder->where('domain', config('app.sip_domain')); - }); - } + } + }); } public function scopeSip($query, string $sip) diff --git a/flexiapi/app/Http/Controllers/Api/AccountController.php b/flexiapi/app/Http/Controllers/Api/AccountController.php index 3c6f8eb..1bdf100 100644 --- a/flexiapi/app/Http/Controllers/Api/AccountController.php +++ b/flexiapi/app/Http/Controllers/Api/AccountController.php @@ -54,12 +54,12 @@ class AccountController extends Controller 'required', new NoUppercase, Rule::unique('accounts', 'username')->where(function ($query) use ($request) { - $query->where('domain', $request->has('domain') && config('app.admins_manage_multi_domains') + $query->where('domain', $request->has('domain') && config('app.everyone_is_admin') && config('app.admins_manage_multi_domains') ? $request->get('domain') : config('app.sip_domain')); }), Rule::unique('accounts_tombstones', 'username')->where(function ($query) use ($request) { - $query->where('domain', $request->has('domain') && config('app.admins_manage_multi_domains') + $query->where('domain', $request->has('domain') && config('app.everyone_is_admin') && config('app.admins_manage_multi_domains') ? $request->get('domain') : config('app.sip_domain')); }), @@ -85,7 +85,7 @@ class AccountController extends Controller $account->username = $request->get('username'); $account->email = $request->get('email'); $account->activated = false; - $account->domain = $request->has('domain') && config('app.admins_manage_multi_domains') + $account->domain = ($request->has('domain') && config('app.everyone_is_admin') && config('app.admins_manage_multi_domains')) ? $request->get('domain') : config('app.sip_domain'); $account->ip_address = $request->ip(); diff --git a/flexiapi/app/Http/Controllers/Api/Admin/AccountController.php b/flexiapi/app/Http/Controllers/Api/Admin/AccountController.php index c50a085..b76e408 100644 --- a/flexiapi/app/Http/Controllers/Api/Admin/AccountController.php +++ b/flexiapi/app/Http/Controllers/Api/Admin/AccountController.php @@ -42,17 +42,17 @@ class AccountController extends Controller return Account::without(['passwords', 'admin'])->paginate(20); } - public function show(Request $request, $id) + public function show($id) { return Account::without(['passwords', 'admin'])->findOrFail($id)->makeVisible(['confirmation_key']); } - public function search(Request $request, string $sip) + public function search(string $sip) { return Account::sip($sip)->firstOrFail(); } - public function destroy(Request $request, $id) + public function destroy($id) { $account = Account::findOrFail($id); @@ -68,7 +68,7 @@ class AccountController extends Controller $account->delete(); } - public function activate(Request $request, $id) + public function activate($id) { $account = Account::findOrFail($id); $account->activated = true; @@ -79,7 +79,7 @@ class AccountController extends Controller return $account; } - public function deactivate(Request $request, $id) + public function deactivate($id) { $account = Account::findOrFail($id); $account->activated = false; diff --git a/flexiapi/composer.lock b/flexiapi/composer.lock index 2ead925..e0b25ac 100644 --- a/flexiapi/composer.lock +++ b/flexiapi/composer.lock @@ -823,16 +823,16 @@ }, { "name": "dragonmantank/cron-expression", - "version": "v3.1.0", + "version": "v3.2.2", "source": { "type": "git", "url": "https://github.com/dragonmantank/cron-expression.git", - "reference": "7a8c6e56ab3ffcc538d05e8155bb42269abf1a0c" + "reference": "c9e208317b0cf679097cf976ffbb0b0eec81d4df" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/dragonmantank/cron-expression/zipball/7a8c6e56ab3ffcc538d05e8155bb42269abf1a0c", - "reference": "7a8c6e56ab3ffcc538d05e8155bb42269abf1a0c", + "url": "https://api.github.com/repos/dragonmantank/cron-expression/zipball/c9e208317b0cf679097cf976ffbb0b0eec81d4df", + "reference": "c9e208317b0cf679097cf976ffbb0b0eec81d4df", "shasum": "" }, "require": { @@ -872,7 +872,7 @@ ], "support": { "issues": "https://github.com/dragonmantank/cron-expression/issues", - "source": "https://github.com/dragonmantank/cron-expression/tree/v3.1.0" + "source": "https://github.com/dragonmantank/cron-expression/tree/v3.2.2" }, "funding": [ { @@ -880,7 +880,7 @@ "type": "github" } ], - "time": "2020-11-24T19:55:57+00:00" + "time": "2022-01-05T06:05:42+00:00" }, { "name": "egulias/email-validator", @@ -1459,16 +1459,16 @@ }, { "name": "laravel/framework", - "version": "v8.77.1", + "version": "v8.78.0", "source": { "type": "git", "url": "https://github.com/laravel/framework.git", - "reference": "994dbac5c6da856c77c81a411cff5b7d31519ca8" + "reference": "3b0e46985c65e06bfe3fafd2a28ab122667b20f4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/laravel/framework/zipball/994dbac5c6da856c77c81a411cff5b7d31519ca8", - "reference": "994dbac5c6da856c77c81a411cff5b7d31519ca8", + "url": "https://api.github.com/repos/laravel/framework/zipball/3b0e46985c65e06bfe3fafd2a28ab122667b20f4", + "reference": "3b0e46985c65e06bfe3fafd2a28ab122667b20f4", "shasum": "" }, "require": { @@ -1627,7 +1627,7 @@ "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, - "time": "2021-12-21T20:22:29+00:00" + "time": "2022-01-04T16:23:21+00:00" }, { "name": "laravel/serializable-closure", @@ -4040,21 +4040,24 @@ }, { "name": "symfony/polyfill-ctype", - "version": "v1.23.0", + "version": "v1.24.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-ctype.git", - "reference": "46cd95797e9df938fdd2b03693b5fca5e64b01ce" + "reference": "30885182c981ab175d4d034db0f6f469898070ab" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/46cd95797e9df938fdd2b03693b5fca5e64b01ce", - "reference": "46cd95797e9df938fdd2b03693b5fca5e64b01ce", + "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/30885182c981ab175d4d034db0f6f469898070ab", + "reference": "30885182c981ab175d4d034db0f6f469898070ab", "shasum": "" }, "require": { "php": ">=7.1" }, + "provide": { + "ext-ctype": "*" + }, "suggest": { "ext-ctype": "For best performance" }, @@ -4099,7 +4102,7 @@ "portable" ], "support": { - "source": "https://github.com/symfony/polyfill-ctype/tree/v1.23.0" + "source": "https://github.com/symfony/polyfill-ctype/tree/v1.24.0" }, "funding": [ { @@ -4115,25 +4118,28 @@ "type": "tidelift" } ], - "time": "2021-02-19T12:13:01+00:00" + "time": "2021-10-20T20:35:02+00:00" }, { "name": "symfony/polyfill-iconv", - "version": "v1.23.0", + "version": "v1.24.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-iconv.git", - "reference": "63b5bb7db83e5673936d6e3b8b3e022ff6474933" + "reference": "f1aed619e28cb077fc83fac8c4c0383578356e40" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-iconv/zipball/63b5bb7db83e5673936d6e3b8b3e022ff6474933", - "reference": "63b5bb7db83e5673936d6e3b8b3e022ff6474933", + "url": "https://api.github.com/repos/symfony/polyfill-iconv/zipball/f1aed619e28cb077fc83fac8c4c0383578356e40", + "reference": "f1aed619e28cb077fc83fac8c4c0383578356e40", "shasum": "" }, "require": { "php": ">=7.1" }, + "provide": { + "ext-iconv": "*" + }, "suggest": { "ext-iconv": "For best performance" }, @@ -4179,7 +4185,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-iconv/tree/v1.23.0" + "source": "https://github.com/symfony/polyfill-iconv/tree/v1.24.0" }, "funding": [ { @@ -4195,20 +4201,20 @@ "type": "tidelift" } ], - "time": "2021-05-27T09:27:20+00:00" + "time": "2022-01-04T09:04:05+00:00" }, { "name": "symfony/polyfill-intl-grapheme", - "version": "v1.23.1", + "version": "v1.24.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-intl-grapheme.git", - "reference": "16880ba9c5ebe3642d1995ab866db29270b36535" + "reference": "81b86b50cf841a64252b439e738e97f4a34e2783" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-intl-grapheme/zipball/16880ba9c5ebe3642d1995ab866db29270b36535", - "reference": "16880ba9c5ebe3642d1995ab866db29270b36535", + "url": "https://api.github.com/repos/symfony/polyfill-intl-grapheme/zipball/81b86b50cf841a64252b439e738e97f4a34e2783", + "reference": "81b86b50cf841a64252b439e738e97f4a34e2783", "shasum": "" }, "require": { @@ -4260,7 +4266,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-intl-grapheme/tree/v1.23.1" + "source": "https://github.com/symfony/polyfill-intl-grapheme/tree/v1.24.0" }, "funding": [ { @@ -4276,20 +4282,20 @@ "type": "tidelift" } ], - "time": "2021-05-27T12:26:48+00:00" + "time": "2021-11-23T21:10:46+00:00" }, { "name": "symfony/polyfill-intl-idn", - "version": "v1.23.0", + "version": "v1.24.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-intl-idn.git", - "reference": "65bd267525e82759e7d8c4e8ceea44f398838e65" + "reference": "749045c69efb97c70d25d7463abba812e91f3a44" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-intl-idn/zipball/65bd267525e82759e7d8c4e8ceea44f398838e65", - "reference": "65bd267525e82759e7d8c4e8ceea44f398838e65", + "url": "https://api.github.com/repos/symfony/polyfill-intl-idn/zipball/749045c69efb97c70d25d7463abba812e91f3a44", + "reference": "749045c69efb97c70d25d7463abba812e91f3a44", "shasum": "" }, "require": { @@ -4347,7 +4353,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-intl-idn/tree/v1.23.0" + "source": "https://github.com/symfony/polyfill-intl-idn/tree/v1.24.0" }, "funding": [ { @@ -4363,11 +4369,11 @@ "type": "tidelift" } ], - "time": "2021-05-27T09:27:20+00:00" + "time": "2021-09-14T14:02:44+00:00" }, { "name": "symfony/polyfill-intl-normalizer", - "version": "v1.23.0", + "version": "v1.24.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-intl-normalizer.git", @@ -4431,7 +4437,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-intl-normalizer/tree/v1.23.0" + "source": "https://github.com/symfony/polyfill-intl-normalizer/tree/v1.24.0" }, "funding": [ { @@ -4451,21 +4457,24 @@ }, { "name": "symfony/polyfill-mbstring", - "version": "v1.23.1", + "version": "v1.24.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-mbstring.git", - "reference": "9174a3d80210dca8daa7f31fec659150bbeabfc6" + "reference": "0abb51d2f102e00a4eefcf46ba7fec406d245825" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/9174a3d80210dca8daa7f31fec659150bbeabfc6", - "reference": "9174a3d80210dca8daa7f31fec659150bbeabfc6", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/0abb51d2f102e00a4eefcf46ba7fec406d245825", + "reference": "0abb51d2f102e00a4eefcf46ba7fec406d245825", "shasum": "" }, "require": { "php": ">=7.1" }, + "provide": { + "ext-mbstring": "*" + }, "suggest": { "ext-mbstring": "For best performance" }, @@ -4511,7 +4520,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.23.1" + "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.24.0" }, "funding": [ { @@ -4527,11 +4536,11 @@ "type": "tidelift" } ], - "time": "2021-05-27T12:26:48+00:00" + "time": "2021-11-30T18:21:41+00:00" }, { "name": "symfony/polyfill-php72", - "version": "v1.23.0", + "version": "v1.24.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php72.git", @@ -4587,7 +4596,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-php72/tree/v1.23.0" + "source": "https://github.com/symfony/polyfill-php72/tree/v1.24.0" }, "funding": [ { @@ -4607,16 +4616,16 @@ }, { "name": "symfony/polyfill-php73", - "version": "v1.23.0", + "version": "v1.24.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php73.git", - "reference": "fba8933c384d6476ab14fb7b8526e5287ca7e010" + "reference": "cc5db0e22b3cb4111010e48785a97f670b350ca5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-php73/zipball/fba8933c384d6476ab14fb7b8526e5287ca7e010", - "reference": "fba8933c384d6476ab14fb7b8526e5287ca7e010", + "url": "https://api.github.com/repos/symfony/polyfill-php73/zipball/cc5db0e22b3cb4111010e48785a97f670b350ca5", + "reference": "cc5db0e22b3cb4111010e48785a97f670b350ca5", "shasum": "" }, "require": { @@ -4666,7 +4675,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-php73/tree/v1.23.0" + "source": "https://github.com/symfony/polyfill-php73/tree/v1.24.0" }, "funding": [ { @@ -4682,20 +4691,20 @@ "type": "tidelift" } ], - "time": "2021-02-19T12:13:01+00:00" + "time": "2021-06-05T21:20:04+00:00" }, { "name": "symfony/polyfill-php80", - "version": "v1.23.1", + "version": "v1.24.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php80.git", - "reference": "1100343ed1a92e3a38f9ae122fc0eb21602547be" + "reference": "57b712b08eddb97c762a8caa32c84e037892d2e9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/1100343ed1a92e3a38f9ae122fc0eb21602547be", - "reference": "1100343ed1a92e3a38f9ae122fc0eb21602547be", + "url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/57b712b08eddb97c762a8caa32c84e037892d2e9", + "reference": "57b712b08eddb97c762a8caa32c84e037892d2e9", "shasum": "" }, "require": { @@ -4749,7 +4758,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-php80/tree/v1.23.1" + "source": "https://github.com/symfony/polyfill-php80/tree/v1.24.0" }, "funding": [ { @@ -4765,20 +4774,20 @@ "type": "tidelift" } ], - "time": "2021-07-28T13:41:28+00:00" + "time": "2021-09-13T13:58:33+00:00" }, { "name": "symfony/polyfill-php81", - "version": "v1.23.0", + "version": "v1.24.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php81.git", - "reference": "e66119f3de95efc359483f810c4c3e6436279436" + "reference": "5de4ba2d41b15f9bd0e19b2ab9674135813ec98f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-php81/zipball/e66119f3de95efc359483f810c4c3e6436279436", - "reference": "e66119f3de95efc359483f810c4c3e6436279436", + "url": "https://api.github.com/repos/symfony/polyfill-php81/zipball/5de4ba2d41b15f9bd0e19b2ab9674135813ec98f", + "reference": "5de4ba2d41b15f9bd0e19b2ab9674135813ec98f", "shasum": "" }, "require": { @@ -4828,7 +4837,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-php81/tree/v1.23.0" + "source": "https://github.com/symfony/polyfill-php81/tree/v1.24.0" }, "funding": [ { @@ -4844,7 +4853,7 @@ "type": "tidelift" } ], - "time": "2021-05-21T13:25:03+00:00" + "time": "2021-09-13T13:58:11+00:00" }, { "name": "symfony/process", @@ -6730,16 +6739,16 @@ }, { "name": "phpdocumentor/type-resolver", - "version": "1.5.1", + "version": "1.6.0", "source": { "type": "git", "url": "https://github.com/phpDocumentor/TypeResolver.git", - "reference": "a12f7e301eb7258bb68acd89d4aefa05c2906cae" + "reference": "93ebd0014cab80c4ea9f5e297ea48672f1b87706" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/a12f7e301eb7258bb68acd89d4aefa05c2906cae", - "reference": "a12f7e301eb7258bb68acd89d4aefa05c2906cae", + "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/93ebd0014cab80c4ea9f5e297ea48672f1b87706", + "reference": "93ebd0014cab80c4ea9f5e297ea48672f1b87706", "shasum": "" }, "require": { @@ -6774,9 +6783,9 @@ "description": "A PSR-5 based resolver of Class names, Types and Structural Element Names", "support": { "issues": "https://github.com/phpDocumentor/TypeResolver/issues", - "source": "https://github.com/phpDocumentor/TypeResolver/tree/1.5.1" + "source": "https://github.com/phpDocumentor/TypeResolver/tree/1.6.0" }, - "time": "2021-10-02T14:08:47+00:00" + "time": "2022-01-04T19:58:01+00:00" }, { "name": "phpspec/prophecy", diff --git a/flexiapi/resources/views/api/documentation_markdown.blade.php b/flexiapi/resources/views/api/documentation_markdown.blade.php index c1be3e7..6039842 100644 --- a/flexiapi/resources/views/api/documentation_markdown.blade.php +++ b/flexiapi/resources/views/api/documentation_markdown.blade.php @@ -173,7 +173,8 @@ JSON parameters: * `username` unique username, minimum 6 characters * `password` required minimum 6 characters * `algorithm` required, values can be `SHA-256` or `MD5` -* `domain` **not configurable by default, except if `admins_manage_multi_domains` is set to `true` in the configuration** the value is enforced to the default registration domain set in the global configuration +* `domain` **not configurable by default. The value is enforced to the default domain set in the global configuration (`app.sip_domain`)** +The `domain` field is taken into account ONLY when `app.admins_manage_multi_domains` is set to `true` in the global configuration * `activated` optional, a boolean, set to `false` by default * `display_name` optional, string * `admin` optional, a boolean, set to `false` by default, create an admin account diff --git a/flexiapi/tests/Feature/AccountApiTest.php b/flexiapi/tests/Feature/AccountApiTest.php index 64cebf0..64c828a 100644 --- a/flexiapi/tests/Feature/AccountApiTest.php +++ b/flexiapi/tests/Feature/AccountApiTest.php @@ -24,8 +24,11 @@ use App\Account; use App\AccountTombstone; use App\ActivationExpiration; use App\Admin; + use Carbon\Carbon; use Illuminate\Foundation\Testing\RefreshDatabase; +use Illuminate\Testing\Fluent\AssertableJson; + use Tests\TestCase; class AccountApiTest extends TestCase @@ -94,6 +97,8 @@ class AccountApiTest extends TestCase $username = 'foobar'; $domain = 'example.com'; + config()->set('app.admins_manage_multi_domains', false); + $response0 = $this->generateFirstResponse($password); $response1 = $this->generateSecondResponse($password, $response0) ->json($this->method, $this->route, [ @@ -115,6 +120,70 @@ class AccountApiTest extends TestCase $this->assertFalse(empty($response1['confirmation_key'])); } + public function testAdminMultiDomains() + { + $configDomain = 'sip.domain.com'; + config()->set('app.sip_domain', $configDomain); + config()->set('app.admins_manage_multi_domains', true); + + $admin = Admin::factory()->create(); + $password = $admin->account->passwords()->first(); + $password->account->generateApiKey(); + $password->account->save(); + + $username = 'foobar'; + $domain1 = 'example.com'; + $domain2 = 'foobar.com'; + + $response0 = $this->keyAuthenticated($password->account) + ->json($this->method, $this->route, [ + 'username' => $username, + 'domain' => $domain1, + 'algorithm' => 'SHA-256', + 'password' => '123456', + ]); + + $response0 + ->assertStatus(200) + ->assertJson([ + 'username' => $username, + 'domain' => $domain1 + ]); + + $response1 = $this->keyAuthenticated($password->account) + ->json($this->method, $this->route, [ + 'username' => $username, + 'domain' => $domain2, + 'algorithm' => 'SHA-256', + 'password' => '123456', + ]); + + $response1 + ->assertStatus(200) + ->assertJson([ + 'username' => $username, + 'domain' => $domain2 + ]); + + $this->keyAuthenticated($password->account) + ->get($this->route) + ->assertStatus(200) + ->assertJson(['data' => [ + [ + 'username' => $admin->account->username, + 'domain' => $admin->account->domain + ], + [ + 'username' => $username, + 'domain' => $domain1 + ], + [ + 'username' => $username, + 'domain' => $domain2 + ] + ]]); + } + public function testDomainInTestDeployment() { $configDomain = 'testdomain.com'; diff --git a/flexisip-account-manager.spec b/flexisip-account-manager.spec index d2ab28a..459c4ed 100644 --- a/flexisip-account-manager.spec +++ b/flexisip-account-manager.spec @@ -8,7 +8,7 @@ #%define _datadir %{_datarootdir} #%define _docdir %{_datadir}/doc -%define build_number 119 +%define build_number 120 %define var_dir /var/opt/belledonne-communications %define opt_dir /opt/belledonne-communications/share/flexisip-account-manager