Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

php - Laravel 5.2: Integrate entrust package, create role and permissions and access it

I am totally new in laravel. I install laravel 5.2 . I have done with CRUD in laravel. Now i want to integrate laravel authentication package. so i choose zizacoentrust.

I follow each steps from doc link. but i don't understand what is wrong. In doc there is not mentioned that in which file i have to add following code.

$owner = new Role();
$owner->name         = 'owner';
$owner->display_name = 'Project Owner'; // optional
$owner->description  = 'User is the owner of a given project'; // optional
$owner->save();

$admin = new Role();
$admin->name         = 'admin';
$admin->display_name = 'User Administrator'; // optional
$admin->description  = 'User is allowed to manage and edit other users'; // optional
$admin->save();

and other below code in document.

Even

class User extends Model implements AuthenticatableContract,
                                    AuthorizableContract,
                                    CanResetPasswordContract

Not mentioned about implements class.

i do

use IlluminateContractsAuthAuthenticatable as AuthenticatableContract;
use IlluminateContractsAuthAccessAuthorizable as AuthorizableContract;
use IlluminateContractsAuthCanResetPassword as CanResetPasswordContract;

but i got error

Trait 'AppAuthenticatable' not found

New learner can't get where to place code. i search alot but i can't get perfect document which give right direction.

Where to create role,permissions? Anyone please help me.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1.Install "zizaco/entrust": "5.2.x-dev"
     2.add provider and aliases in app.php
     3.php artisan vendor:publish
     4.php artisan migrate

Now add in user.php

use IlluminateAuthAuthenticatable;
use IlluminateDatabaseEloquentModel;
use IlluminateAuthPasswordsCanResetPassword;
use IlluminateContractsAuthAuthenticatable as AuthenticatableContract;
use IlluminateContractsAuthCanResetPassword as CanResetPasswordContract;
use ZizacoEntrustTraitsEntrustUserTrait;


class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use Authenticatable, CanResetPassword, EntrustUserTrait;
}

Next Create a model for Role

 use ZizacoEntrustEntrustRole
        class Role extends EntrustRole
        {
            protected $table = 'roles';
            protected $fillable = ['name', 'display_name', 'description']
        }

Now create a model for permmission

use ZizacoEntrustEntrustPermission;

class Permission extends EntrustPermission
{
protected $table = 'permissions';
    protected $fillable = ['name', 'display_name', 'description'];
}

Now create a seeding table for roles and permissions

UserTableSeeder.php
    <?php
    use IlluminateDatabaseSeeder;
    use AppPermission;
    use AppRole;
    use AppUser;
    class UserTableSeeder extends seeder
    {
        public function run()
        {
            Permission::truncate();
            Role::truncate();
            User::truncate();
            DB::table('role_user')->delete();
            DB::table('permission_role')->delete();
//create a user          
  $veeru = User::create([
                'name' => 'veeru',
                'email' => '[email protected]',
                'password' => bcrypt('qwerty'),
            ]);

    //create a role of admin
            $admin = Role::create([
                'name' => 'admin',
                'display_name' => 'Admin',
                'description' => 'Only one and only admin',
            ]);
//create a permission for role
            $manage_users = Permission::create([
                'name' => 'manage-users-roles-and-permissions',
                'display_name' => 'Manage Users,Roles and Permissions',
                'description' => 'Can manage users,roles and permission"s',
            ]);
    //here attaching permission for admin role
            $admin->attachPermission($manage_users);
//here attaching role for user
            $veeru->attachRole($admin);

    //here iam creating another role and permisssion
            $application = Role::create([
                'name' => 'appapirequestlogs',
                'display_name' => 'AppApiRequestLogs',
                'description' => 'This has full control on Application Core Request logs',
            ]);
            $corereq = Permission::create([
                'name' => 'appapireqlogindex',
                'display_name' => 'AppApiReqLogIndex',
                'description' => 'This has control on Application Core Request Logs index only',
            ]);
    here attaching roles and permissions
            $application->attachPermission($corereq);
            $veeru->attachRole($application);
    }

After seeding that create a routes file and controller for roles and permissions

routes.php

Route::group(['middleware' => 'web'], function () {
    Route::group(['prefix' => 'admin'], function () {
        Route::controller('roles', 'RolesController');
        Route::controller('permissions', 'PermissionsController');
        Route::controller('users', 'UsersController');
    });
});

RolesController.php

use AppPermission;
use AppRole;
use IlluminateHttpRequest;
use AppHttpRequests;

class RolesController extends Controller
{
    function __construct()
    {
        $this->middleware('auth');
        $this->middleware('role:admin');
        $this->middleware('permission:manage-users-roles-and-permissions');
    }

    public function getIndex(Request $request)
    {
        $roles = Role::with('perms')->get();
        return view('roles.index', ['roles' => $roles]);
    }

    public function getCreate()
    {
        return view('roles.create');
    }

    public function postCreate(Request $request)
    {
        $role = Role::create([
            'name' => $request->name,
            'display_name' => $request->display_name,
            'description' => $request->description
        ]);
        return redirect()->to('admin/roles/index');
    }

    public function getUpdate($id)
    {
        $role = Role::findOrFail($id);
        return view('roles.update', ['role' => $role]);
    }

    public function postUpdate(Request $request)
    {
        $role = Role::findOrFail($request->get('id'));
        $role->name = $request->get('name');
        $role->display_name = $request->get('display_name');
        $role->description = $request->get('description');
        $role->save();
        return redirect();
    }

    public function getDelete($id)
    {
        $role = Role::findOrFail($id);
        $role->delete();

        return redirect();
    }

    public function getAttach(Request $request)
    {
        $role = Role::where('id', '=', $request->id)->with('perms')->first();
        $permissions_id = $role->perms->pluck('id')->toArray();
        $permissionsNotAttached = Permission::whereNotIn('id', $permissions_id)->get();
        return view('roles.attach', compact('role', 'permissionsNotAttached'));
    }

    public function postAttach(Request $request)
    {
        $role = Role::findOrFail($request->id);
        $permission = Permission::findOrFail($request->permission);
        $role->attachPermission($permission);
        return redirect();
    }

    public function getDetach(Request $request)
    {
        $role = Role::findOrFail($request->role_id);
        $permission = Permission::findOrFail($request->permission_id);
        $role->detachPermission($permission);
        return redirect()->to('/admin/roles/index');

    }
}

Similarly create PermissionsController.php

use AppHttpRequestsPermissionRequest;
use AppPermission;
use AppHttpRequests;


class PermissionsController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
        $this->middleware('role:admin');
        $this->middleware('permission:manage-users-roles-and-permissions');
    }

    public function getIndex()
    {
        $permissions = Permission::all();
        return view('permissions.index', ['permissions' => $permissions]);
    }

    public function getCreate()
    {
        return view('permissions.create');
    }

    public function postCreate(Request $request)
    {
        $permission = Permission::create([
            'name' => $request->name,
            'display_name' => $request->display_name,
            'description' => $request->description
        ]);

        return redirect();
    }

    public function getUpdate($id)
    {
        $permission = Permission::findOrFail($id);
        return view('permissions.update', ['permission' => $permission]);
    }

    public function postUpdate(Request $request)
    {
        $permission = Permission::findOrFail($request->get('id'));
        $permission->name = $request->get('name');
        $permission->display_name = $request->get('display_name');
        $permission->description = $request->get('description');
        $permission->save();
        return redirect();
    }

    public function getDelete($id)
    {
        $permission = Permission::findOrFail($id);
        $permission->delete();
        return redirect();
    }


}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...