C
C
capitancrazy2017-02-22 07:05:52
Laravel
capitancrazy, 2017-02-22 07:05:52

What is the best way to check if the content is relevant to the selected user?

I have a table of pre-orders that includes fields like this

$table->increments('id');
            $table->char('group_cd', 16);
            $table->char('group_sub_cd', 2);
            ...
            $table->integer('customer_id')->unsigned()->index();

group_cd company id and group_sub_cd branch id and cusotmer_id customer id
There are 2 methods to create a new customer.
This one doesn't get a pre-order ID
public function create()
  {
    $customer = new Customer;
    return view('folder1.customer', compact('customer'));
  }

and this one gets
public function createWithPreorder($preorderId)
  {
    $this->checkPreorderGroup($preorderId);
    $customer = new Customer;
    return view('customer.customer', compact('customer'));
  }

checkPreorderGroup checks if this preorder belongs to the same company as the logged in user.
private function checkPreorderGroup($preorderId)
    {
        $preorder = EstimateInfo::where('id', $preorderId)->group()->get();
        if ($preorder->isEmpty()) {
            return abort(404);
        }
        return;
    }

->group() from the EstimateInfo(preorder) model.
public function scopeGroup($query)
    {
        $authUser= Auth::user();
        return $query->where('group_cd', $authUser->group_cd)->where('group_sub_cd', $authUser->group_sub_cd);
    }

It seems wrong to me that the pre-order is checked in the client controller.
Also in other methods, similarly, I check the client itself, whether it belongs to the user's group.
And the fact that both methods are identical for me, I don’t like create and createWithPreorder.
1. So my question is, how do you carry out these kinds of checks?
2. If there are packages that include similar functionality and expand them? Do you use them?
I am using laravel 5.1

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2017-02-22
@capitancrazy

Use an observer , the check will not be in the controller and once.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question