PHP code example of delgont / armor

1. Go to this page and download the library: Download delgont/armor library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

delgont / armor example snippets


/**
 * Permission Demiliter
 * Defines the separator for listing multiple permissions in middleware. For example, 
 * setting permission:view-dashboard|edit-settings allows access if the user has
 * either permission.
 */
'permission_delimiter' => '|',

/**
 * Defines an array of classes that statically declare permissions for various user actions within your 
 * application. Each class listed here represents a group 
 * of permissions tied to specific user roles or functionalities.
 */
'permission_registrars' => [
    App\Permissions\ExamplePermissionRegistrar::class,
],

use Delgont\Armor\Concerns\MultiAuthCredentials;

public function username()
{
    return 'username_email';
}

 /**
 * Get the second colum that will be used with email and the second field by default name column defined in the user table
 * @return string
 */
protected function getSecondaryColumn ()
{
    return 'name';
}

protected function credentials(Request $request)
{
    return $this->multiAuthCredentials($request);
}

<input type="text" class="form-control @error('username_email') is-invalid @enderror" id="username_email" name="username_email" placeholder="Username or Email" value="{{ old('username_email') }}" />



namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Foundation\Auth\ThrottlesLogins;

use Delgont\Armor\Concerns\MultiAuthCredentials;
use Illuminate\Http\Request;

use Illuminate\Support\Facades\Auth;


class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */
    use AuthenticatesUsers, MultiAuthCredentials, ThrottlesLogins;

    /**
     * Where to redirect users after login - You can redirect to users default home page
     *
     * @var string
     */
    protected $redirectTo;

    /**
     * Create a new controller instance. 
     *
     * @return void
     */
    public function __construct()
    {
        //Artisan::call('permission:sync');
        $this->middleware('guest')->except('logout');
    }

    /**
     * Override this method for multi user authentication to work
     */
    protected function credentials(Request $request)
    {
        return $this->multiAuthCredentials($request);
    }


   

    public function username()
    {
        return 'username_email';
    }

     /**
     * Get the second colum that will be used with email and the second field by default name column defined in the user table
     * @return string
     */
    protected function getSecondaryColumn ()
    {
        return 'name';
    }
    
}



..............
Schema::table('users', function (Blueprint $table) {
    $table->nullableMorphs('user');
});



namespace App;

use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Delgont\Cms\Notifications\Auth\ResetPassword as ResetPasswordNotification;

use Delgont\Auth\Concerns\HasUserTypes;

class User extends Authenticatable
{
    use Notifiable, HasUserTypes;



namespace App;

use Illuminate\Database\Eloquent\Model;

class Customer extends Model
{
    public function user()
    {
        return $this->morphOne('App\User', 'user');
    }
}

@usertype('userType1|userType2')
    <p>You are authorized to access this section.</p>
@else
    <p>You do not have access to this section.</p>
@endusertype

@can('permissionone|permissiontwo')
    <p>You are authorized to access this section.</p>
@else
    <p>You do not have access to this section.</p>
@endcan

@rolecan('permissionone|permissiontwo')
    <p>You are authorized to access this section your role has the necessary permissions.</p>
@elserolecan
    <p>Your role does not have the necessary permission to access this resource</p>
@endrolecan

<input id="username_email" type="text" class="form-control @error('username_email') is-invalid @enderror" name="username_email" value="{{ old('username_email') }}" 

Route::get('/page', function ($pageName) {
    // Your page logic here
})->middleware('track.page-access:page-name');

php artisan armor:install

php artisan armor:permissions:give-all-to {userId} modelKey

php artisan armor:permissions:deny-all-from userId modelkey
 php artisan vendor:publish --provider="Delgont\Armor\ArmorServiceProvider" 
 config/armor.php 


namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Delgont\Auth\Concerns\MultiAuthCredentials;
use Illuminate\Http\Request;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller - Multi Authentication using email or username
    |--------------------------------------------------------------------------
    | Use Delgont\Auth\Concerns\MultiAuthCredentials trait
    | You must override the credentials and username functions as shown below
    |
    */
    use AuthenticatesUsers, MultiAuthCredentials;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    protected function credentials(Request $request)
    {
        return $this->multiAuthCredentials($request);
    }

    public function username()
    {
        return 'username_email';
    }
}
cmd
php artisan make:roleRegistrar Roles/ExampleRoleRegistrar
cmd
php artisan role:sync
cmd
php artisan permissions:sync