Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Some Updates#51

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
OzanKurt wants to merge3 commits intoyajra:3.0fromOzanKurt:3.0
Closed

Some Updates#51

OzanKurt wants to merge3 commits intoyajra:3.0fromOzanKurt:3.0

Conversation

@OzanKurt
Copy link
Contributor

  • queryString:
    We have to be able to see the id of the dataTable by looking at the ajax request, since we might want to handle more than one data tables in one route.

  • Custom JS functions:
    The code now allows jQuery and JS codes to be printed as literal functions.

  • SmartDataTables:
    Also we want to specify if the request will be handled from the new SmartDataTables service.

So,
Let me explain what SmartDataTables mean for me. My company uses a s* ton of datatables and I am so tired of copy pasing a new route/method over and over again.

SmartDataTables simply adds a field called$isSmartDataTable to theabstract DataTable class which is by defaultfalse. On your child class you can overwrite this.

Ex:

/**     * Dynamically detects which DataTable to initialize for backend processing and responds.     *     * @var boolean     */protected$isSmartDataTable =true;

By doing so you let the dataTable add asmartDataTable field to the ajax request which has the value of the class which is a "SmartDataTable". If we were to make a UsersDataTable and mark it as "smart". The query will have a parameter like this:smartDataTable=App\DataTables\UsersDataTable.

The next step is to check all web requests via a Middleware if the contain the parametersmartDataTable. If they do, we will simply resolve the data table class and respond to it accordingly. (Just like therender method does.)

if (!$request->has('smartDataTable')) {return$next($request);        }$dataTable =app($request->smartDataTable);if ($request->ajax() &&$request->wantsJson()) {returnapp()->call([$dataTable,'ajax']);        }

yajra reacted with thumbs up emoji
- queryString:We have to be able to see the id of the dataTable by looking at the ajax request, since we might want to handle more than one data tables in one route.- Custom JS functions:The code now allows jQuery and JS codes to be printed as literal functions.- SmartDataTables:Also we want to specify if the request will be handled from the new SmartDataTables service.So,Let me explain what SmartDataTables mean for me. My company uses a s* ton of datatables and I am so tired of copy pasing a new route/method over and over again.SmartDataTables simply adds a field called `$isSmartDataTable` to the `abstract DataTable` class which is by default `false`. On your child class you can overwrite this.Ex:```php    /**     * Dynamically detects which DataTable to initialize for backend processing and responds.     *     *@var boolean     */    protected $isSmartDataTable = true;```By doing so you let the dataTable add a `smartDataTable` field to the ajax request which has the value of the class which is a "SmartDataTable". If we were to make a UsersDataTable and mark it as "smart". The query will have a parameter like this: `smartDataTable=App\DataTables\UsersDataTable`.The next step is to check all web requests via a Middleware if the contain the parameter `smartDataTable`. If they do, we will simply resolve the data table class and respond to it accordingly. (Just like the `render` method does.)```php        if (!$request->has('smartDataTable')) {            return $next($request);        }        $dataTable = app($request->smartDataTable);        if ($request->ajax() && $request->wantsJson()) {            return app()->call([$dataTable, 'ajax']);        }```
@yajra
Copy link
Owner

@OzanKurt the PR looks good, I have the same idea on my todo list. Glad you initiated this.

Some points:

Custom JS functions:
The code now allows jQuery and JS codes to be printed as literal functions.

  • Can you please put this function to a separate PR so that it can be merged immediately?

Just like the render method does.

  • Maybe we can refactor this code on the abstract class to avoid duplication and we have a single source of truth when processing the ajax request?

  • Does this work onpost request?

  • Please add the missing doc blocks.

Will to test this as soon as I can. Thanks!

@OzanKurt
Copy link
ContributorAuthor

OzanKurt commentedDec 26, 2017
edited
Loading

Can you please put this function to a separate PR so that it can be merged immediately?

I am not sure how I can split those now thou. :(

Maybe we can refactor this code on the abstract class to avoid duplication and we have a single source of truth when processing the ajax request?

Yes, this should be done. I will be working about that anyways since I am currently building a "base starter template project" for my company, everything has to be perfect.

Does this work on post request?

We can change where it works by editing theHtmlServiceProvider, where we push the middleware to the kernel. The main check I did there was$request->isAjax() || $request->wantsJson().

Please add the missing doc blocks.

Thanks a thing I should also do 👍


foreach (array_dot($parameters)as$key =>$value) {
if ($this->isCallbackFunction($value,$key)) {
if ($this->isCallbackFunction($value,$key) ||$this->isJavascriptFunction($value,$key)) {
Copy link
Owner

Choose a reason for hiding this comment

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

@OzanKurt I think this is just the line you need along with theisJavascriptFunction function for the new PR. Thanks!

- Reverted `isJavascriptFunction` check.- Added docblocks.- Did some refactoring.
@OzanKurt
Copy link
ContributorAuthor

I don't know how to make two different pull requests at once, so I reverted theisJavascriptFunction part from this one. Will make an another one hopefully once we merge this.

I added information about what I did to the commit message:86cc8ad

@yajra
Copy link
Owner

I just released your snippet for the js function. Thanks!

On the other hand, do you think it's ok to pass the FQCN on the query string? Having a look on my notes, this is how I planned to implement this.

  • Create a common end point that will handle all dataTables requests. Or in this PR, the middleware will handle all the request.

  • Create a dataTables mapping with the concept of Laravel's event & listener.
    If we will use middleware, then maybe we can create a base middleware that the user can extend and then define all the mapping? Something like:

namespaceApp\Http\Middleware;useClosure;class DataTablesMiddlewareextends SmartDataTableMiddleware{protected$dataTables = ['users' =>'App\DataTables\UsersDataTable','posts' =>'App\DataTables\PostsDataTable',    ];}

This way, we only need to includesmartDataTable=users on the query string and making our code a bit secure and not exposing our codebase structure?

Let me know what you think? Thanks!

@OzanKurt
Copy link
ContributorAuthor

I also thought about the code security as you did, for me this was the fastest solution I was able to come up with.

That$dataTables mapping seems fine, but then we will need to declare the mapping elsewhere again.
Plus, the keys might be confusing if someone uses both custom id and "smart mapping key". Let me exemplify what i mean by that.

I coded a project for British Taekwondo Federation, where I needed to display a dataTable to 4 different user roles differently. Like:Admin\UsersDataTable,Instructor\UsersDataTable,Member\UsersDataTable. So the "html id" of the dataTable will beusersDataTable all the time but smart mapping will be something like:

protected$dataTables = ['adminUsers' =>'App\DataTables\Admin\UsersDataTable','instructorUsers' =>'App\DataTables\Instructor\UsersDataTable',// And so on...    ];

The problem with this is that the classes I am using extent a base class. Where I can simply mark them as:

protected$smartDataTables =true;

In this case I would need to add$smartDataTables attribute individually and need to keep track of them being unique.

protected$smartDataTables ='adminUsers';

But still seams reasonable.

Finally, I personally believe that FQCN won't harm us in terms of security. The max. I would do is to remove the "Application Namespace" from it, so it looks like:DataTables\Admin\UsersDatatable.

Since Laravel has the command to change thisApp\\ namespace. (php artisan app:name) Companies would still keep the FQCN private.

@OzanKurt
Copy link
ContributorAuthor

We can also use a tiny en/decryption algorithm to secure the class names.

https://nazmulahsan.me/simple-two-way-function-encrypt-decrypt-string/

@lk77
Copy link
Contributor

lk77 commentedJan 10, 2018
edited
Loading

Hello,

i think using encryption is a bad thing for readability, perhaps an additionnal sign key would be suitable :

Hash::make($this->smartDataTables);

something like this, and check the hash in the request maybe.

It will be used with datatable buttons ?
perhaps we can use dataTableVariable property in this case i don't know.

@OzanKurt
Copy link
ContributorAuthor

Hash::make($this->smartDataTables); will create a looooooooooooooong string, therefore will make the query string even dirtier.

@lk77
Copy link
Contributor

lk77 commentedJan 10, 2018
edited
Loading

It depends on the used algorithm, with Bcrypt it's 60 characters, it's a bit long perhaps, but some FQCN can be as long as the hash. I think using only the base class name could be enough, but it can cause conflict with other datatables of same name, so i don't know.

In my code, i have multiple datatables on the same page, with same url, and i'm using the dataTableVariable property of service datatable, but it is set by hand in the child class so not a real problem, and im usinglcfirst(class_basename($dataTable)); by default when dataTableVariable isn't set.

@OzanKurt
Copy link
ContributorAuthor

We need a reversible encryption method to decrypt the class name after the request thou.

@drbyte
Copy link

re: FQCN, I think what@yajrasuggested earlier about a mapping list in the middleware is what this package should offer out of the box. Then if someone wants to add their own changes, either to expose their own FQCN or to Hash:: it, or whatever, they can extend this package's middleware to do their own thing.
That way this SmartDataTables feature can be released, and others can battle-test other ideas in the wild and offer alternate proposals via PR.

OzanKurt reacted with thumbs up emoji

@yajra
Copy link
Owner

Sorry was not able to review and this is already outdated. Closing, thanks!

@yajrayajra closed thisMay 7, 2022
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@yajrayajrayajra left review comments

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

4 participants

@OzanKurt@yajra@lk77@drbyte

[8]ページ先頭

©2009-2025 Movatter.jp