vipin93's avatar

How allow user specific task

Hey try to make app where two types of users one who can upload image , update there profile and one who is only allow to follow user who can upload image and update profile and comment on images and on profile any one know how ? thanks

0 likes
11 replies
bashy's avatar

Might be looking at a permission package here... User has a group or any role set? What have you tried so far? Laravel version?

taijuten's avatar

Can a user be only one of these? e.g. two types of user: Reader & Author. A reader cannot be an author as well, and vice versa.

If so, you can simply add another column in to the users table to specify which type of user they are.

If these roles vary by a "category" or "group", you could also tie these roles to those tables e.g. user A can be Author on category 1, 2 and 5. user B is Author on category 1, 2 and 3. In this case, you'd have a category_user pivot table, and could add an extra "role" column on to this pivot table.

vipin93's avatar

I have tried like in my users table i add a column name role_id and in my registration page I give option to choose for you want a author or only users but problem is that in how to filter users like 'before' => 'auth'

taijuten's avatar

This is how I'd do this, but this may not be optimal, and I only have experience with L5.

Create a method in your user model similar to below

public function isAuthor() {
    return (Auth::user()->role_id == 1) ? true : false;
}

In L5 you'd then create a middleware to check for this, but presumably you can also add this to your filters in L4.x

vipin93's avatar

like i have a custom filter ruler for my ProfilesController

Route::filter('currentUser', function($route)
{
      if (Auth::guest()) return Redirect::route('login_path');

      if (Auth::user()->username !==$route->parameter('profile')) 
        
      {
        return Redirect::home()->with(['global' => 'Sorry edit your profile.', 'type' => 'danger']);
      }

});

and my controller

<?php

use Illuminate\Database\Eloquent\ModelNotFoundException;
use Getdukaan\Forms\ProfileForm;


class ProfilesController extends \BaseController {

    
     private $profileForm;
    

     function __construct(ProfileForm $profileForm )
     {

        $this->profileForm        = $profileForm;

        $this->beforeFilter('currentUser', ['only' =>['edit','update'] ]);

        
     }

    /**
     * Display the specified resource.
     * GET /profiles/{username}
     *
     * @param  int  $username
     * @return Response
     */
    public function show($username)
    {


        try
        {
            
            $user = User::with('profile','profile.category','profile.subcategory','images')->whereUsername($username)->firstOrFail();
        }

        catch(ModelNotFoundException $e)
        {
           return Redirect::home()->with(['global'=>'User not found.', 'type' => 'danger']);
        }
        

        return View::make('profile.show')->withUser($user)->with('title', $username);
    }

    /**
     * Show the form for editing the specified resource.
     * GET /profiles/{usernmae}/edit
     *
     * @param  int  $usernmae
     * @return Response
     */
    public function edit($username)
    {
        

        $user = User::whereUsername($username)->firstOrFail();

        $categories = Category::lists('name' ,'id');

        $subcategories = Subcategory::lists('name','id');

        $blocks     = Block::all();

        return View::make('profile.edit', compact('categories' , 'subcategories' , 'blocks'))->withUser($user)->with('title' , 'Edit profile');
    }

    /**
     * Update the specified resource in storage.
     * PUT /profiles/{usernmae}
     *
     * @param  int  $usernmae
     * @return Response
     */
    public function update($username)
    {
        $user = User::whereUsername($username)->firstOrFail();
        
        $input = Input::get();

        $this->profileForm->validate($input);



        if (count($user->profile))

        {
             $user->profile->update($input);
        }

        else
        {
            $profile = Profile::create($input);

            $user->profile()->save($profile);
        }
     
            
        return Redirect::back()->with(['global' => 'Your profile has been updated successfully move ', 'type' => 'success']);
       

    }
      
       
}

how to filter users if user i have role_id 2

vipin93's avatar

in my users table i storing 1 for owner and 2 for user in role_id column i want filter like this

if user have role_id 1
@if(Auth::check())
 
  link_to_route to update profile
  link_to_route to upload image
  link_to_route your profile
  link_to_route your comment
@else
login
register
@endif
 
if user have role_id 2
@if(Auth::check())
 
  link_to_route your comment
  
@else
login
register
@endif

Please or to participate in this conversation.