jeudi 5 septembre 2019

How could I improve this code on laravel about authorization, roles and permissions? [on hold]

Please guys take a look at my function in laravel, it's becoming messy, I need to do 2 different queries to database depending of the role of the current user, it is just needed to add a where clause accordingly to user role. I feel that my code is not maintainable at all,please do you have any recommendation about rewriting this code?

I haven't much time to analyze other option, the project just need to be done. sorry

    public function index(Request $request)
    {
        $user = Auth::user();
        $data;

        if ($request->ajax()) {

            if ($user->hasRole('Super Admin') || $user->hasRole('Evaluador del Mintel'))
            {
                $data = DB::table('fulfillments')
                ->join('institutions', 'fulfillments.institution_id', '=', 'institutions.id')
                ->join('milestones', 'fulfillments.milestone_id', '=', 'milestones.id')
                ->join('fulfillment_activities', 'fulfillment_activities.fulfillment_id', '=', 'fulfillments.id')
                ->select(
                    'fulfillments.*',
                    'institutions.name',
                    'milestones.*',
                    'fulfillments.id as fulfillment_id',
                    'fulfillment_activities.*'
                )->get();

            } else
            {
                $data = DB::table('fulfillments')
                ->join('institutions', 'fulfillments.institution_id', '=', 'institutions.id')
                ->join('milestones', 'fulfillments.milestone_id', '=', 'milestones.id')
                ->join('fulfillment_activities', 'fulfillment_activities.fulfillment_id', '=', 'fulfillments.id')
                ->where('institutions.id','=', $user->institucion_id)
                ->select(
                    'fulfillments.*',
                    'institutions.name',
                    'milestones.*',
                    'fulfillments.id as fulfillment_id',
                    'fulfillment_activities.*'
                )->get();
            }

            etc, etc



Hope you can see my concern.

Aucun commentaire:

Enregistrer un commentaire