gygabyte017

Experience

1,090

0 Best Reply Awards

  • Member Since 10 Months Ago
  • 6 Lessons Completed
  • 0 Favorites

16th May, 2017

gygabyte017 left a reply on Prefilter A Model Table With Current User • 4 months ago

Thanks, that's amazing! Very clever indeed.

gygabyte017 started a new conversation Prefilter A Model Table With Current User • 4 months ago

Hi have a classic user table (with id), and lots of Models linked to tables which have a column (id_user) matching the user id.

I would like, when I try e.g. to ->get(), to "automatically" filter each table and return only the rows where id_user is the current logged user id. Likewise, I'd like to "automatically" fill the field id_user with the current logged user id when I store a new record.

Is there a way to do this in a "smart" way?

Thanks

26th March, 2017

gygabyte017 left a reply on Best Approach For Dynamic Views • 5 months ago

Yes of course, what I'm asking is whether to directly load the div html from the ajax request, or manually write all the js to map json data to the div.

gygabyte017 started a new conversation Best Approach For Dynamic Views • 5 months ago

Suppose I have a view in which I display a table with rows fetched from a table. This is straightforward, I route the request to a controller, the controller fetches the table and returns the view with the data, and the view cycles through the array and constructs the table.

Now, suppose that when I click on a row of such a table I want to show additional data in a modal form. These data cannot be loaded when the table is created because they can change anytime and must be up-to-date. So I simply make an ajax request.

Question is: what kind of data should the ajax request return?

  • First approach: the request returns a json object; then with js I must create all the fields using the json data "by hands". I don't like this approach because it is very verbose (compared to the beautiful @foreach in the main table)
  • Second approach: the request returns directly a view (html) with the complete modal directly created in html (from a view), so with js I just need to set $(component).html(data). I don't like this approach because it's not reusable, if I need to show a different modal maybe with the same data I must write another view.

What do you recommend for populating things with ajax?

14th March, 2017

gygabyte017 left a reply on Resource Controller Security • 6 months ago

Thank you very much I didn't know about policies, however I think they are not applicable in this situation. I mean:

'can this authenticated User access this Card?' and 'can this authenticated User access this controller method?' the answer depends on what we mean by 'user'.

If 'user' means the logical user (stored in table User with an user_id etc) the answer is yes to anyone, because the website retrieves the cards data "on behalf" on the current logged user, and any logged user may access any card because the website needs all of them to display the game.

However if 'user' means the physical person (the human) the answer is no, the person should not access the resources directly without using the website.

In other words:

  • user 'Smith', logged in, may see any card shown through website (which in turn has retrieved all the cards from 'site/cards' on behalf on the logical user 'Smith')

  • user 'Smith', logged in, MAY NOT directly access 'site/cards' (with its browser, or with its own code, or whatever)

Since policies are based on the concept of the logical user, I don't think they can be useful here...

I hope I made myself clear ;-)

gygabyte017 left a reply on Handle Null Values When Following Model Relationships • 6 months ago

Excellent, that's exactly what I meant.

Thank you very much

gygabyte017 started a new conversation Resource Controller Security • 6 months ago

Hi, I watched the intro tutorial to laravel 5.4, and I found resource controllers very interesting.

However, I'm a bit concerned about security and data thieves, for instance the index and show methods. Suppose I'm writing a card game, so I have a table 'cards' with all cards details.

I define

    Route::get("cards, "[email protected]")->middleware('auth');
    Route::get("cards/{id}, "[email protected]")->middleware('auth');

So clearly an user must be authenticated to access routes, but after authentication instead of using the website to play the game, anyone could directly access "site/cards" and obtain directly a json object with all cards (or access "site/cards/i" for i=1,2,3...).

Question is: clearly the routes must be defined because the website needs to retrieve cards data in order to show the game. But I don't like the fact that any user may directly access routes and download any data exposed with routes.

Is this what normally happens? How do you usually deal with this kind of implementation?

gygabyte017 left a reply on Handle Null Values When Following Model Relationships • 6 months ago

Thank you, can this be nested to follow tables relationships, i.e. multiple has in one single command?

gygabyte017 left a reply on Handle Null Values When Following Model Relationships • 6 months ago

Anyone has a suggestion?

21st February, 2017

gygabyte017 started a new conversation Handle Null Values When Following Model Relationships • 7 months ago

Hi, suppose we have User, who MAY have a Post, which MAY have a Comment, which MAY be liked a User, etc. (let's assume for simplicity that they are all one-to-one relationships).

Suppose I want to access the name of the user who liked the comment related to the post made by then user with id=1. I would simply do: User::find(1)->posts->comments->users->name.

However, if this chain is broken at some point because a relationship doesn't exists, I get an Exception on the next element because I'm accessing a function/property of a null objects. (ie if User::find(1)->posts is null, User::find(1)->posts->comments throws an Exception).

What is an elegant way to return the value if the chain is successful, and null if any relationship does not exist, without being trapped in exceptions? Otherwise I would have to check is_null for each element in the chain and this is horrible:

return !is_null(User::find(1)->posts) && !is_null(User::find(1)->posts->comments) && !is_null(User::find(1)->posts->comments->users) ? User::find(1)->posts->comments->users->name : null;

11th February, 2017

gygabyte017 started a new conversation Custom Way To Authetincate Users • 7 months ago

Hi,

for a very small project, I am asked to create a custom authentication layer which works in this way:

  1. User navigate to a page for the first time 2) The authentication layer intercepts the request and understands that the user is not logged in 3) Retrieves user's ip address, and execute a shell command (to a custom application) with such ip address which returns an id 4) Checks such id on a UserTable (id, name, ecc) 5) If it exists, the user is logged in, his information (like name) are retrieved, the login information are stored to a session cookie, and the page is loaded. 6) Every page he navigates, checks if the session cookie is valid, otherwise goes back to step 2.

So basically it is an ip-based authentication, and the user doesn't need to insert any kind of other information (don't worry about security, ip spoofing and so on, it is not relevant here)

How can I do this in laravel?

Thank you

7th February, 2017

gygabyte017 left a reply on Best Practices Model->Controller And Controller->View • 7 months ago

Thank you!

6th February, 2017

gygabyte017 left a reply on Best Practices Model->Controller And Controller->View • 7 months ago

Thank you, I understand.

So may I ask you what should a controller related to a model do? If methods like getAll() (which query the relative table and do some work) shoud be moved in Transaction class (model), what kind of methods would you put in the TransactionController class?

gygabyte017 started a new conversation Best Practices Model->Controller And Controller->View • 7 months ago

I have a Model (i.e. Transaction). I have a Controller (i.e. TransactionController) which has a method getAll() to operate on its model:

public function getAll() { return Transaction::get(); }

Then, I have a view (i.e. home.blade.php). And I have a Controller (i.e. HomeController) which has a method home() (routed with Route::get('home','[email protected]');) that should retrieve data from some tables and return the view (which in turn will properly display data):

public function home() { // $data = retrieve data? return view('home', compact('data'); }

I tried doing $data = TransactionController::getAll(); but, besides the fact that this does not work because getAll is not static, seems wrong because I am accessing a controller from another controller. But how should I do it? The HomeController should get data from lots of tables, which in turn are mapped to a model (for each of them) with a relative controller (for each of them) with some methods.

The schema is:

TableA -> ModelA -> ControllerA TableB -> ModelB -> ControllerB ----> HomeController -> HomeView TableC -> ModelC -> ControllerC /

Thank you

20th November, 2016

gygabyte017 left a reply on Write To A View While Something Is Running • 10 months ago

Thank you, I moved old jobs' code into a Job class, I dispatch them triggering from a button via Ajax in the sync queue (I don't need database for this), and everything works perfectly.

However, i.e. the old job1 echoed some kind of "progress" to let the user know the output of each step while it was running. How could I replicate this in Laravel? Even though what I did so far works perfectly (i.e. the job is executed correctly) I can't think a way to display the user the Job results, like: 1) an overall status ("Job completed") and 2) some intermediate statuses ("Step 1 has started" "Step 1 processed 100 items" "Step 2 has started" ...)

What would you recommend? Thank you.

19th November, 2016

gygabyte017 started a new conversation Write To A View While Something Is Running • 10 months ago

Hi, I am currently try to port an old php script to a Laravel application. One of the page did the following:

echo "Begin"; job1(); echo "Job 1 is completed"; $a = job2(); echo "Job 2 is completed with output: " + $a;

Since job1 and job2 took some time to execute, I got the effect of "something running" i.e. echoes are executed at different time.

I understand this procedural way of doing is completely obsolete. However, I'm not confident about how should I rewrite this in Laravel. The goal is to load a view, to click on some "Start" button, and then execute the jobs in background while displaying their output in realtime, like some kind of log.

What would you recommend?

Thanks a lot. Greetings

Edit Your Profile
Update

Want to change your profile photo? We pull from gravatar.com.