From 89ec9a5081caa95d5c3bbddd3f09015cc74329b7 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Thu, 4 Aug 2022 17:24:04 +0100 Subject: [PATCH 1/9] Sprinkled in some user language validation For #3615 --- .../Controllers/Api/UserApiController.php | 4 ++-- app/Http/Controllers/UserController.php | 4 ++-- tests/User/UserManagementTest.php | 24 +++++++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/Api/UserApiController.php b/app/Http/Controllers/Api/UserApiController.php index 4f0d30034..03d2a0f06 100644 --- a/app/Http/Controllers/Api/UserApiController.php +++ b/app/Http/Controllers/Api/UserApiController.php @@ -41,7 +41,7 @@ class UserApiController extends ApiController 'required', 'min:2', 'email', new Unique('users', 'email'), ], 'external_auth_id' => ['string'], - 'language' => ['string'], + 'language' => ['string', 'max:15', 'alpha_dash'], 'password' => [Password::default()], 'roles' => ['array'], 'roles.*' => ['integer'], @@ -55,7 +55,7 @@ class UserApiController extends ApiController (new Unique('users', 'email'))->ignore($userId ?? null), ], 'external_auth_id' => ['string'], - 'language' => ['string'], + 'language' => ['string', 'max:15', 'alpha_dash'], 'password' => [Password::default()], 'roles' => ['array'], 'roles.*' => ['integer'], diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 3110f1a98..88d44565c 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -83,7 +83,7 @@ class UserController extends Controller $validationRules = [ 'name' => ['required'], 'email' => ['required', 'email', 'unique:users,email'], - 'language' => ['string'], + 'language' => ['string', 'max:15', 'alpha_dash'], 'roles' => ['array'], 'roles.*' => ['integer'], 'password' => $passwordRequired ? ['required', Password::default()] : null, @@ -143,7 +143,7 @@ class UserController extends Controller 'email' => ['min:2', 'email', 'unique:users,email,' . $id], 'password' => ['required_with:password_confirm', Password::default()], 'password-confirm' => ['same:password', 'required_with:password'], - 'language' => ['string'], + 'language' => ['string', 'max:15', 'alpha_dash'], 'roles' => ['array'], 'roles.*' => ['integer'], 'external_auth_id' => ['string'], diff --git a/tests/User/UserManagementTest.php b/tests/User/UserManagementTest.php index c09ce8cb3..f0bc7c2f0 100644 --- a/tests/User/UserManagementTest.php +++ b/tests/User/UserManagementTest.php @@ -234,4 +234,28 @@ class UserManagementTest extends TestCase $this->assertDatabaseMissing('activities', ['type' => 'USER_CREATE']); } + + public function test_user_create_update_fails_if_locale_is_invalid() + { + $user = $this->getEditor(); + + // Too long + $resp = $this->asAdmin()->put($user->getEditUrl(), ['language' => 'this_is_too_long']); + $resp->assertSessionHasErrors(['language' => 'The language may not be greater than 15 characters.']); + session()->flush(); + + // Invalid characters + $resp = $this->put($user->getEditUrl(), ['language' => 'enassertSessionHasErrors(['language' => 'The language may only contain letters, numbers, dashes and underscores.']); + session()->flush(); + + // Both on create + $resp = $this->post('/settings/users/create', [ + 'language' => 'en 'My name', + 'email' => 'jimmy@example.com', + ]); + $resp->assertSessionHasErrors(['language' => 'The language may not be greater than 15 characters.']); + $resp->assertSessionHasErrors(['language' => 'The language may only contain letters, numbers, dashes and underscores.']); + } } From 4209f27f1acabfccff0c2dea08f8e151ed82144f Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Tue, 9 Aug 2022 12:40:59 +0100 Subject: [PATCH 2/9] Set a fairly sensible limit on user name validation Also updated controller properties with types within modified files. Related to #3614 --- app/Http/Controllers/Api/UserApiController.php | 4 ++-- app/Http/Controllers/Auth/RegisterController.php | 8 ++++---- app/Http/Controllers/UserController.php | 8 ++++---- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/Api/UserApiController.php b/app/Http/Controllers/Api/UserApiController.php index 03d2a0f06..64e9d732d 100644 --- a/app/Http/Controllers/Api/UserApiController.php +++ b/app/Http/Controllers/Api/UserApiController.php @@ -36,7 +36,7 @@ class UserApiController extends ApiController { return [ 'create' => [ - 'name' => ['required', 'min:2'], + 'name' => ['required', 'min:2', 'max:100'], 'email' => [ 'required', 'min:2', 'email', new Unique('users', 'email'), ], @@ -48,7 +48,7 @@ class UserApiController extends ApiController 'send_invite' => ['boolean'], ], 'update' => [ - 'name' => ['min:2'], + 'name' => ['min:2', 'max:100'], 'email' => [ 'min:2', 'email', diff --git a/app/Http/Controllers/Auth/RegisterController.php b/app/Http/Controllers/Auth/RegisterController.php index 9399e8b7f..b0aec1177 100644 --- a/app/Http/Controllers/Auth/RegisterController.php +++ b/app/Http/Controllers/Auth/RegisterController.php @@ -30,9 +30,9 @@ class RegisterController extends Controller use RegistersUsers; - protected $socialAuthService; - protected $registrationService; - protected $loginService; + protected SocialAuthService $socialAuthService; + protected RegistrationService $registrationService; + protected LoginService $loginService; /** * Where to redirect users after login / registration. @@ -69,7 +69,7 @@ class RegisterController extends Controller protected function validator(array $data) { return Validator::make($data, [ - 'name' => ['required', 'min:2', 'max:255'], + 'name' => ['required', 'min:2', 'max:100'], 'email' => ['required', 'email', 'max:255', 'unique:users'], 'password' => ['required', Password::default()], ]); diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 88d44565c..895481d02 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -18,8 +18,8 @@ use Illuminate\Validation\ValidationException; class UserController extends Controller { - protected $userRepo; - protected $imageRepo; + protected UserRepo $userRepo; + protected ImageRepo $imageRepo; /** * UserController constructor. @@ -81,7 +81,7 @@ class UserController extends Controller $passwordRequired = ($authMethod === 'standard' && !$sendInvite); $validationRules = [ - 'name' => ['required'], + 'name' => ['required', 'max:100'], 'email' => ['required', 'email', 'unique:users,email'], 'language' => ['string', 'max:15', 'alpha_dash'], 'roles' => ['array'], @@ -139,7 +139,7 @@ class UserController extends Controller $this->checkPermissionOrCurrentUser('users-manage', $id); $validated = $this->validate($request, [ - 'name' => ['min:2'], + 'name' => ['min:2', 'max:100'], 'email' => ['min:2', 'email', 'unique:users,email,' . $id], 'password' => ['required_with:password_confirm', Password::default()], 'password-confirm' => ['same:password', 'required_with:password'], From a90446796a113c0e9da916f0e2ea4fb61e7b6a46 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Tue, 9 Aug 2022 12:58:10 +0100 Subject: [PATCH 3/9] Fixed issue preventing selection of activity type in audit log For #3623 --- resources/views/settings/audit.blade.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/resources/views/settings/audit.blade.php b/resources/views/settings/audit.blade.php index b856d1150..2daeb8a82 100644 --- a/resources/views/settings/audit.blade.php +++ b/resources/views/settings/audit.blade.php @@ -13,7 +13,12 @@