Wallogit.com
2017 © Pedro Peláez
A simple Laravel 5 package for handling child-apps within a main-app.
Simple Laravel 5 package for handling access to child-applications (Appls) within a main-application. Each Appl can be seen as a Moudle or Package within the Main APP. With this package you can grant or ungrant access to every single application, attaching or dettaching appls from a user., (*1)
Main App. //with 2 child apps -->Appl_1. (Module 1) -->Appl_2. (Module 2) .....
..., (*2)
Pull this package through Composer. ---- composer.json, (*3)
{
"require": {
"reivaj86/multiapps": "dev-master"
}
}
Run $ composer update, (*4)
Add the package to your application service providers in: config/app.php, (*5)
'providers' => [
'Illuminate\Foundation\Providers\ArtisanServiceProvider',
'Illuminate\Auth\AuthServiceProvider',
...
'Reivaj86\Multiapps\MultiappsServiceProvider',
],
Publish the package migrations. Publish config file to your application., (*6)
$ php artisan vendor:publish --provider="Vendor/Reivaj86/Multiapps/MultiappsServiceProvider" --tag="config" $ php artisan vendor:publish --provider="Vendor/Reivaj86/Multiapps/MultiappsServiceProvider" --tag="migrations"
Run migrations., (*7)
$ php artisan migrate
Crate your seeds // optional Run the seeder, (*8)
You can change the connection for your models, slug separator and there is also a very userfull purport option. View the config file for more information., (*9)
First of all, include IsApplUser trait and also implement IsApplUserContract inside your Usermodel or Custom model., (*10)
use Reivaj86\Multiapps\Contracts\IsApplUserContract;
use Reivaj86\Multiapps\Traits\IsApplUser;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract, IsApplUserContract {
use Authenticatable, CanResetPassword, IsUserAppl;
Done!. You can create your first appl and attach it to a User or Custom Model, (*11)
use Reivaj86\Multiapps\Models\Appl;
use App\User;
$appl = Appl::create([
'name' => 'Child_App_Name',
'slug' => 'child_app_slug',
'description' => '' // optional
]);
$user = User::find($id)->attachAppl($appl); // you can pass whole object, or just id
You can easily check if the current user uses a child_app., (*12)
if ($user->uses('child_app')) // you can pass an id or slug
{
return 'child_app_slug';
}
You can also do the following:, (*13)
if ($user->usesChild_App_Name())
{
return 'child_app_slug';
}
And also, there is a way to check if a User/Model has access to multiple appls:, (*14)
if ($user->can('child_app_1|child_app_2')) // or $user->can('child_app_1, child_app_2') and also $user->can(['child_app_1', 'child_app_2'])
{
// if user has at least one appl
}
if ($user->can('child_app_1|child_app_2', 'All')) // or $user->can('child_app_1, child_app_2', 'All') and also $user->can(['child_app_1', 'child_app_2'], 'All')
{
// if user has all appls
}
When you are creating appls, there is also optional parameter level. It is set to 1 by default, but you can overwrite it and then you can do something like this:, (*15)
if ($user->level() > 3)
{
// code
}
This option is very usefull when you want to set access levels to every child app. You can easily integrate this with a Role and level authentication package.
If user has multiple child apps, the method level returns the leve for that appl. For a basic User it shall always be 1., (*16)
There are three Blade extensions. Basically, it is replacement for classic if statements., (*17)
@appl('child_app_slug') // @if(Auth::check() && Auth::user()->uses('child_app_slug'))
// user can use child_app_slug
@endappl
@allowedappl('child_app_slug', $view) // @if(Auth::check() && Auth::user()->allowed('child_app', $view))
// show child_app specific content // Access to specific content within the child_app
@endallowedappl
@appl('child_app_1|child_app_2', 'all') // @if(Auth::check() && Auth::user()->can('child_app_1|child_app_2', 'all'))
// user can use child_app_1 and also child_app_2
@else
// something else
@endappl
For a better understanding, please have a look at IsApplUserContract., (*18)