Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix full company scoping in user selects #12467

Merged

Conversation

marcusmoore
Copy link
Collaborator

@marcusmoore marcusmoore commented Feb 6, 2023

Description

This PR fixes a bug in the user select menu that currently returns users from other companies when a search string is provided.

Example:
With Full Companies Support enabled, Marcus is an admin for Company A. Marcus attempts to sign out an asset that belongs to Company A. When selecting the user to check the asset out to, Marcus is presented with a list of users for Company A:
image
So far so good 馃憤馃従

But, when Marcus types a query into the user search, he sees users from other companies 馃憥馃従
image

With this PR, the users returned are scoped to the user's of the company that the requester belongs to:
image

Notes

  • For super admins, the existing behavior remains and all users are returned.
  • For clarity, if an asset is assigned to a company the company name is displayed at the top of the checkout form (as seen in screenshot above).
  • I added a change to the SecurityHeaders middleware that skips it when running in testing environments.

On Testing

A few tests have been included in this PR but running the tests requires some changes that are outside of the scope of this PR and will be followed up on in the future:

  • The DB_* variables in .env.testing-ci should point to a valid MySQL database
  • DB_CONNECTION in phpunit.xml should be mysql
  • You'll also need to temporarily bypass the SecurityHeaders middleware

Type of change

  • Bug fix (non-breaking change which fixes an issue)

@what-the-diff
Copy link

what-the-diff bot commented Feb 6, 2023

  • Added a new test for the users select list API endpoint.
  • Fixed an issue where assets with no model would cause errors in asset checkout view and added company name to that page as well.
  • Removed unwanted headers from responses when running tests on CI/CD servers (GitLab, Travis).
  • Updated factories so they can be used by other classes without having to pass them around everywhere or use global state which is bad practice anyway! Also updated some of the existing factory definitions while I was at it... :)
  • Changed language file key 'company' => 'Company'. This will make things easier if we ever want to change this label later on down the line since all references are now using Laravel's translation helper function trans(). It also makes more sense than just hardcoding "Company" into our views like before because then you have two places where you need to update your code instead of one place only - not DRY :( . The same goes for any other labels throughout Snipe-IT too btw ;) !

Comment on lines 26 to 29
if (App::environment(['testing', 'testing-ci'])) {
return $next($request);
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewers: double-check that this check makes sense please. We don't want accidentally disable this middleware 馃槄

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would we want to bypass this in testing? (I'm sure there's a good reason for it, I'm just not coming up with it :) )

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great question that made me dig in a little more.

Calling header_remove in this middleware causes the tests to throw exceptions with the following error:

ErrorException : Cannot modify header information - headers already sent by (output started at /path/to/project/vendor/phpunit/phpunit/src/Util/Printer.php:104)
 /path/to/project/app/Http/Middleware/SecurityHeaders.php:112
 /path/to/project/app/Http/Middleware/SecurityHeaders.php:30
 /path/to/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:167
 /path/to/project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php:21
 /path/to/project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ConvertEmptyStringsToNull.php:31
 /path/to/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:167
 /path/to/project/app/Http/Middleware/CheckForDebug.php:25
 /path/to/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:167
 /path/to/project/app/Http/Middleware/CheckForSetup.php:25
 /path/to/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:167
 /path/to/project/vendor/fideloper/proxy/src/TrustProxies.php:57
 /path/to/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:167
 /path/to/project/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php:49
 /path/to/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:167
 /path/to/project/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php:121
 /path/to/project/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php:64
 /path/to/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:167
 /path/to/project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php:86
 /path/to/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:167
 /path/to/project/app/Http/Middleware/NoSessionStore.php:28
 /path/to/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:167
 /path/to/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:103
 /path/to/project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:142
 /path/to/project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:111
 /path/to/project/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:510
 /path/to/project/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:476
 /path/to/project/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:306
 /path/to/project/tests/Feature/Api/Users/UsersForSelectListTest.php:25

That if check in such an important middleware is sketchy though. I'm pulling it out of this and adding a comment about it in the PR description.

@marcusmoore marcusmoore marked this pull request as ready for review February 7, 2023 00:07
Copy link
Owner

@snipe snipe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, Marcus - thank you! A few small nits (and a question), but it's great work thank you!

@@ -1,6 +1,7 @@
<?php

return [
'company' => 'Company',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can just use general.company here, no need to duplicate the string.

Comment on lines 26 to 29
if (App::environment(['testing', 'testing-ci'])) {
return $next($request);
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would we want to bypass this in testing? (I'm sure there's a good reason for it, I'm just not coming up with it :) )

@@ -26,14 +26,23 @@
</div>
<div class="box-body">
{{csrf_field()}}
@if ($asset->company && $asset->company->name)
<div class="form-group">
{{ Form::label('model', trans('admin/companies/general.company'), array('class' => 'col-md-3 control-label')) }}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing as above general.company should work find here.

@snipe snipe merged commit eb63576 into snipe:develop Feb 23, 2023
@marcusmoore marcusmoore deleted the fix/scope-people-with-full-multiple-companies branch February 23, 2023 18:37
@snipe snipe mentioned this pull request Apr 16, 2023
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants