-
Notifications
You must be signed in to change notification settings - Fork 2
Refactor SQL queries: enforce parameterized bindings and consistent style #49
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
solverat
wants to merge
1
commit into
1.x
Choose a base branch
from
fix_sql_statements
base: 1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+121
−68
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,8 @@ | |
|
|
||
| namespace OpenDxp\Bundle\AdminBundle\Controller\Admin\DataObject; | ||
|
|
||
| use Doctrine\DBAL\ArrayParameterType; | ||
| use Doctrine\DBAL\ParameterType; | ||
| use Exception; | ||
| use InvalidArgumentException; | ||
| use League\Flysystem\FilesystemException; | ||
|
|
@@ -109,14 +111,15 @@ public function getSharedGridColumnConfigs(User $user, string $classId, ?string | |
| $userIds = [$user->getId()]; | ||
| // collect all roles | ||
| $userIds = [...$userIds, ...$user->getRoles()]; | ||
| $userIds = implode(',', $userIds); | ||
| $db = Db::get(); | ||
|
|
||
| $query = 'select distinct c1.id from gridconfigs c1, gridconfig_shares s | ||
| where (c1.searchType = ' . $db->quote($searchType) . ' and ((c1.id = s.gridConfigId and s.sharedWithUserId IN (' . $userIds . '))) and c1.classId = ' . $db->quote($classId) . ') | ||
| UNION distinct select c2.id from gridconfigs c2 where shareGlobally = 1 and c2.classId = '. $db->quote($classId) . ' and c2.ownerId != ' . $db->quote($user->getId()); | ||
|
|
||
| $ids = $db->fetchFirstColumn($query); | ||
| $ids = $db->fetchFirstColumn( | ||
| 'SELECT DISTINCT c1.id FROM gridconfigs c1, gridconfig_shares s | ||
| WHERE (c1.searchType = ? AND c1.id = s.gridConfigId AND s.sharedWithUserId IN (?) AND c1.classId = ?) | ||
| UNION DISTINCT SELECT c2.id FROM gridconfigs c2 WHERE shareGlobally = 1 AND c2.classId = ? AND c2.ownerId != ?', | ||
| [$searchType, $userIds, $classId, $classId, $user->getId()], | ||
| [ParameterType::STRING, ArrayParameterType::INTEGER, ParameterType::STRING, ParameterType::STRING, ParameterType::INTEGER] | ||
| ); | ||
|
|
||
| if ($ids) { | ||
| $ids = implode(',', $ids); | ||
|
|
@@ -307,8 +310,11 @@ private function doGetGridColumnConfig(Request $request, array $params, Config $ | |
| if (!$this->getAdminUser()->isAdmin()) { | ||
| $userIds = [$this->getAdminUser()->getId()]; | ||
| $userIds = [...$userIds, ...$this->getAdminUser()->getRoles()]; | ||
| $userIds = implode(',', $userIds); | ||
| $shared = ($savedGridConfig->getOwnerId() !== $userId && $savedGridConfig->isShareGlobally()) || $db->fetchOne('select 1 from gridconfig_shares where sharedWithUserId IN ('.$userIds.') and gridConfigId = '.$savedGridConfig->getId()); | ||
| $shared = ($savedGridConfig->getOwnerId() !== $userId && $savedGridConfig->isShareGlobally()) || $db->fetchOne( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. code readability? same like in AssetHelperController |
||
| 'SELECT 1 FROM gridconfig_shares WHERE sharedWithUserId IN (?) AND gridConfigId = ?', | ||
| [$userIds, $savedGridConfig->getId()], | ||
| [ArrayParameterType::INTEGER, ParameterType::INTEGER] | ||
| ); | ||
| // $shared = $savedGridConfig->isShareGlobally() || GridConfigShare::getByGridConfigAndSharedWithId($savedGridConfig->getId(), $this->getUser()->getId()); | ||
|
|
||
| if (!$shared && $savedGridConfig->getOwnerId() !== $this->getAdminUser()->getId()) { | ||
|
|
@@ -716,11 +722,10 @@ public function gridConfigApplyToAllAction(Request $request): JsonResponse | |
| $searchType = $request->request->get('searchType'); | ||
| $user = $this->getAdminUser(); | ||
| $db = Db::get(); | ||
| $db->executeQuery('delete from gridconfig_favourites where ' | ||
| . 'ownerId = ' . $user->getId() | ||
| . ' and classId = ' . $db->quote($classId) . | ||
| ' and searchType = ' . $db->quote($searchType) | ||
| . ' and objectId != ' . $objectId . ' and objectId != 0'); | ||
| $db->executeStatement( | ||
| 'DELETE FROM gridconfig_favourites WHERE ownerId = ? AND classId = ? AND searchType = ? AND objectId != ? AND objectId != 0', | ||
| [$user->getId(), $classId, $searchType, $objectId] | ||
| ); | ||
|
|
||
| return $this->adminJson(['success' => true]); | ||
| } | ||
|
|
@@ -766,12 +771,10 @@ public function gridMarkFavouriteColumnConfigAction(Request $request): JsonRespo | |
| $favourite->save(); | ||
| } | ||
| $db = Db::get(); | ||
| $count = $db->fetchOne('select * from gridconfig_favourites where ' | ||
| . 'ownerId = ' . $user->getId() | ||
| . ' and classId = ' . $db->quote($classId). | ||
| ' and searchType = ' . $db->quote($searchType) | ||
| . ' and objectId != ' . $objectId . ' and objectId != 0' | ||
| . ' and `type` != ' . $db->quote($type)); | ||
| $count = $db->fetchOne( | ||
| 'SELECT * FROM gridconfig_favourites WHERE ownerId = ? AND classId = ? AND searchType = ? AND objectId != ? AND objectId != 0 AND `type` != ?', | ||
| [$user->getId(), $classId, $searchType, $objectId, $type] | ||
| ); | ||
| $specializedConfigs = $count > 0; | ||
| } catch (Exception) { | ||
| $favourite->delete(); | ||
|
|
@@ -791,8 +794,11 @@ protected function getShareSettings(int $gridConfigId): array | |
| ]; | ||
|
|
||
| $db = Db::get(); | ||
| $allShares = $db->fetchAllAssociative('select s.sharedWithUserId, u.type from gridconfig_shares s, users u | ||
| where s.sharedWithUserId = u.id and s.gridConfigId = ' . $gridConfigId); | ||
| $allShares = $db->fetchAllAssociative( | ||
| 'SELECT s.sharedWithUserId, u.type FROM gridconfig_shares s, users u | ||
| WHERE s.sharedWithUserId = u.id AND s.gridConfigId = ?', | ||
| [$gridConfigId] | ||
| ); | ||
|
|
||
| foreach ($allShares as $share) { | ||
| $type = $share['type']; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
variable assignment with this condition and implicit type casting is hard to read. I would propose something like this: