PHPackages                             alsa7err90/magic\_role7 - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. alsa7err90/magic\_role7

ActivePackage

alsa7err90/magic\_role7
=======================

This package enables you to add and define user roles and permission with ease

015Blade

Since Nov 3Pushed 4y ago1 watchersCompare

[ Source](https://github.com/alsa7err90/magic_role7)[ Packagist](https://packagist.org/packages/alsa7err90/magic_role7)[ RSS](/packages/alsa7err90-magic-role7/feed)WikiDiscussions main Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

magic roles
===========

[](#magic-roles)

This package enables you to add and define user roles and permissions with ease

Installation :
==============

[](#installation-)

Install the package through Composer.

Run the Composer require command from the Terminal:

```
   composer require alsa7err90/magic_role7

```

- Open config/app.php and add this line to your Service Providers Array.

    ```
    alsa7err90\magic_role7\rolesServiceProvider::class,

    ```
- Open config/app.php and add this line to your Aliases:

    ```
    'ViewRoles' => alsa7err90\magic_role7\ViewRolesFacade::class,

    ```
- Publish files with

    ```
    php artisan vendor:publish --all

    ```
- Open app\\User.php and add this methods to "class User":

    ```
     public function hasRole(... $roles)
     {
          foreach ($roles as $role)
          {
                if ($this->roles->contains('slug', $role))
                {
                    return true;
                }
         }
         return false;
    }

    public function roles()
    {
        return $this->belongsToMany(Magrole::class);
    }

    public function assignRole(Magrole $role)
    {
        return $this->roles()->save($role);
    }

    ```
- to assign Role delault for user after register : open file "RegisterController.php" in foler "app/Http/Controllers/Auth" and edit function register from:

    ```
    this old code :
    return User::create([
          'name' => $data['name'],
          'email' => $data['email'],
          'password' => Hash::make($data['password']),
      ]);

    to new code :
     use App\Magrole; // this before class RegisterController
     ..
      $user = User::create([
          'name' => $data['name'],
          'email' => $data['email'],
          'password' => Hash::make($data['password']),
      ]);
      $role = Magrole::where('name', 'user')->first();
      $user->assignRole($role);
      return $user;

    ```

-Run the php artisan migrate command from the Terminal:

```
  php artisan migrate

```

- open file .env and add the line don't forget to replace the email by email adminstrator:

    ```
    EMAIL_ADMINISTRATOR=yourEmailAdmin@example.com

    ```

useing :
========

[](#useing-)

-go to the link:

```
http://127.0.0.1:8000/mag_permissions

```

here you can add permission Manual or auto if you want to do that automatically just click button: auto\_refresh\_permission or if you want to add Manually you shold write first name class controller or any class you use after that write "\_" after that write do as "show, store, save, ....." or any name you need :

```
          Ex:   show_PostController

```

-go to the link:

```
http://127.0.0.1:8000/mag_roles

```

add role ex: admin, editor, user After add that click on "edit permissions" to add or remove permission this role -go to the link:

```
http://127.0.0.1:8000/mag_users

```

and add role to users

1- route:

```
    resource: mag_roles
    resource: mag_permissions
    resource: mag_users

```

as link:

```
     roles

```

2 - controller:

To check if the user has permission to use this function:

```
     $magic_role = new MagicRole();
     $magic_role->chakeRole('nameController','do') ;

```

-nameController: name class controller ex: we have controller by name:

```
   use alsa7err90\magic_role7\MagicRole;
   class PostController extends Component
   {
      public function show ()
      {
              $magic_role = new MagicRole();
              $magic_role->chakeRole('PostController ', 'show');
         ...............
       }
    }

```

-do: if used auto\_insert\_permission this add 4 word you can be used:

```
   -show: use in function index and show ($ id)
   -update: use in function edit and update (Request $ request, $ id)
   -destroy1: use for softy delete
   -destroy2: use for delete
   -store: use in function create and store

```

use in blade
============

[](#use-in-blade)

to get role of the user

```
   $role_user = ViewRoles::has_role();

```

this will return name role as "admin" or "editor" etc

to check if user have any role as "admin":

```
   $has_admin = ViewRoles::is_has_role("admin");

```

this will return true or false

to check if user have permission as "show\_Controller" :

```
   ViewRoles::is_has_permission("show_MagPermissionController");

```

this will return true or false

example for check if he has permission to show links :

```
           @if(ViewRoles::is_has_permission("show_PostController"))
