### Install and Configure ACLMenu Source: https://context7.com/otifsolutions/aclmenu/llms.txt Instructions for installing the package via Composer and configuring the package settings in the Laravel configuration file. ```bash composer require otifsolutions/aclmenu php artisan vendor:publish --tag=config ``` ```php // config/laravelacl.php return [ 'redirect_url' => '/', 'models' => [ 'user' => config('auth.providers.users.model') ] ]; $redirectUrl = config('laravelacl.redirect_url'); $userModel = config('laravelacl.models.user'); ``` -------------------------------- ### Install ACLMenu via Composer Source: https://github.com/otifsolutions/aclmenu/blob/master/README.md This command installs the ACLMenu package into your Laravel project. It requires Composer to be installed and configured in your project root. ```bash composer require otifsolutions/aclmenu ``` -------------------------------- ### Team Management Workflow Source: https://github.com/otifsolutions/aclmenu/blob/master/README.md Step-by-step guide on how to create and manage teams, including assigning user roles and permissions. ```APIDOC ## Team Management Workflow ### Step 1: Create Team - A team is created with a `user_id`. ```php Team::updateOrCreate(['user_id' => 1]); ``` ### Step 2: Create User Role - The team owner creates a user role. ### Step 3: Assign Permissions to User Role - The owner assigns permissions to the created user role. - The owner can only assign permissions accessible by them. - Permissions are fetched from the `Permission` model. - Permissions are synced using the following code: ```php $userRole->permissions()->sync($request['permissions']); ``` ### Step 4: Add Team Members - Team members can be added by using the created user role. ``` -------------------------------- ### Check User Permissions Source: https://github.com/otifsolutions/aclmenu/blob/master/README.md Example of using the hasPermission method provided by ACLUserTrait to restrict access to a dashboard view. ```php if (!Auth::user()->hasPermission('READ', '/dashboard')) return 'error'; return view('dashboard'); ``` -------------------------------- ### Apply Role Middleware to Routes in PHP Source: https://github.com/otifsolutions/aclmenu/blob/master/README.md This example demonstrates how to apply a role-based middleware to a specific route in Laravel. The middleware ensures that only users with the specified role can access the route. ```php Route::get('/dashboard', [DashboardController::class, 'dashboard'])->middleware('role:dashboard'); ``` -------------------------------- ### Create User Role and Menu Item Source: https://github.com/otifsolutions/aclmenu/blob/master/README.md Demonstrates how to create a user role and associate a menu item with specific permissions using Eloquent models. ```php UserRole::updateOrCreate(['id' => 1],['name' => 'ADMIN']); $id = UserRole::Where(['name' => 'ADMIN'])->get('id'); MenuItem::updateOrCreate(['id' => 1], [ 'order_number'=> 1, 'parent_id' => null, 'icon' => 'feather icon-home', 'name' => 'dashboard', 'route' => '/dashboard', 'generate_permission' => 'ALL' ])->user_roles()->sync($id); ``` -------------------------------- ### Register and Run Artisan Commands Source: https://github.com/otifsolutions/aclmenu/blob/master/README.md Shows how to trigger the aclmenu:refresh command within a DatabaseSeeder and how to execute the seed command via CLI. ```php Artisan::call('aclmenu:refresh'); ``` ```bash php artisan db:seed ``` -------------------------------- ### Configure Multi-tenant Teams Source: https://context7.com/otifsolutions/aclmenu/llms.txt Explains the process of creating teams, assigning roles to teams, and managing team-specific permissions and members. This model supports multi-tenant architecture. ```php use OTIFSolutions\ACLMenu\Models\Team; use OTIFSolutions\ACLMenu\Models\UserRole; $team = Team::updateOrCreate(['user_id' => 1]); $teamRole = UserRole::updateOrCreate(['id' => 10], ['name' => 'TEAM_MEMBER', 'team_id' => $team->id]); $teamRole->permissions()->sync([1, 2, 3, 4]); $owner = $team->owner; $members = $team->members()->get(); $roles = $team->user_roles()->get(); $permissions = $team->permissions()->get(); $team->permissions()->sync([1, 2, 3]); ``` -------------------------------- ### Database Seeder Initialization for ACLMenu Source: https://context7.com/otifsolutions/aclmenu/llms.txt Sets up the database seeding process for ACLMenu by calling various seeders in a specific order. This includes creating user roles, menu items, users, teams, and then refreshing and syncing permissions. It relies on Laravel's Artisan command for permission generation. ```php namespace Database\Seeders; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\Artisan; class DatabaseSeeder extends Seeder { public function run() { // 1. Create user roles first $this->call(UserRolesTableSeeder::class); // 2. Create menu items and associate with roles $this->call(MenuItemsTableSeeder::class); // 3. Create users with assigned roles $this->call(UserTableSeeder::class); // 4. Create teams $this->call(TeamsTableSeeder::class); // 5. Generate permissions from menu items Artisan::call('aclmenu:refresh'); // 6. Sync permissions to user roles $this->call(DefaultUserPermissionsSync::class); } } ``` -------------------------------- ### Manage User Roles Source: https://context7.com/otifsolutions/aclmenu/llms.txt Demonstrates how to create user roles using the UserRole model and access their associated relationships such as permissions, menu items, and team data. ```php use OTIFSolutions\ACLMenu\Models\UserRole; UserRole::updateOrCreate(['id' => 1], ['name' => 'ADMIN']); $adminRole = UserRole::where('name', 'ADMIN')->first(); $permissions = $adminRole->permissions()->get(); $menuItems = $adminRole->menu_items()->get(); $users = $adminRole->users()->get(); $team = $adminRole->team; $groups = $adminRole->groups()->get(); ``` -------------------------------- ### Configure Hierarchical Menu Items Source: https://context7.com/otifsolutions/aclmenu/llms.txt Shows how to create parent and child menu items with specific route permissions and associate them with user roles for dynamic sidebar generation. ```php use OTIFSolutions\ACLMenu\Models\MenuItem; use OTIFSolutions\ACLMenu\Models\UserRole; $adminRoleId = UserRole::where('name', 'ADMIN')->pluck('id'); MenuItem::updateOrCreate(['id' => 1], [ 'order_number' => 1, 'parent_id' => null, 'icon' => 'feather icon-home', 'name' => 'Dashboard', 'route' => '/dashboard', 'generate_permission' => 'ALL', 'show_on_sidebar' => true ])->user_roles()->sync($adminRoleId); $menuItem = MenuItem::find(2); $children = $menuItem->children()->get(); $roles = $menuItem->user_roles()->get(); ``` -------------------------------- ### Group User Roles Source: https://context7.com/otifsolutions/aclmenu/llms.txt Demonstrates how to create a UserRoleGroup and associate multiple roles with it, allowing users to inherit permissions from multiple sources simultaneously. ```php use OTIFSolutions\ACLMenu\Models\UserRoleGroup; use OTIFSolutions\ACLMenu\Models\UserRole; $group = UserRoleGroup::create(['name' => 'Super Editors']); $editorRole = UserRole::where('name', 'EDITOR')->first(); $viewerRole = UserRole::where('name', 'VIEWER')->first(); $group->user_roles()->sync([$editorRole->id, $viewerRole->id]); $roles = $group->user_roles()->get(); $users = $group->users(); ``` -------------------------------- ### Manage Permissions with Permission Model Source: https://context7.com/otifsolutions/aclmenu/llms.txt Demonstrates how to query permissions, access their relationships with menu items, and synchronize permissions to user roles. These permissions are typically auto-generated via the aclmenu:refresh command. ```php use OTIFSolutions\ACLMenu\Models\Permission; use OTIFSolutions\ACLMenu\Models\UserRole; $permission = Permission::where('name', 'read_dashboard')->first(); $menuItem = $permission->menu_item; $type = $permission->type; $userRole = UserRole::where('name', 'ADMIN')->first(); $permissions = Permission::whereIn('menu_item_id', $userRole->menu_items()->pluck('id'))->pluck('id'); $userRole->permissions()->sync($permissions); $dashboardPermissions = Permission::where('menu_item_id', 1)->get(); ``` -------------------------------- ### Query Permission Types Source: https://context7.com/otifsolutions/aclmenu/llms.txt Shows how to retrieve the predefined permission types such as CREATE, READ, UPDATE, DELETE, and MANAGE. These types are seeded automatically by the system. ```php use OTIFSolutions\ACLMenu\Models\PermissionType; $readType = PermissionType::where('name', 'READ')->first(); $createType = PermissionType::where('name', 'CREATE')->first(); $updateType = PermissionType::where('name', 'UPDATE')->first(); $deleteType = PermissionType::where('name', 'DELETE')->first(); $manageType = PermissionType::where('name', 'MANAGE')->first(); $allTypes = PermissionType::all(); ``` -------------------------------- ### ACLMenu Default User Permissions Sync Source: https://context7.com/otifsolutions/aclmenu/llms.txt Synchronizes permissions for the 'ADMIN' user role by fetching all associated permissions from menu items and applying them. This seeder ensures that the administrator role has the correct set of permissions based on the defined menu structure. ```php use OTIFSolutions\ACLMenu\Models\UserRole; use OTIFSolutions\ACLMenu\Models\Permission; class DefaultUserPermissionsSync extends Seeder { public function run() { $userRole = UserRole::where('name', 'ADMIN')->first(); $permissions = Permission::whereIn( 'menu_item_id', $userRole->menu_items()->pluck('id') )->pluck('id'); $userRole->permissions()->sync($permissions); } } ``` -------------------------------- ### Sync Permissions for User Role Source: https://github.com/otifsolutions/aclmenu/blob/master/README.md Synchronizes permissions for a specific user role based on the menu items assigned to them. ```php $userRole = UserRole::where(['name' => 'ADMIN'])->first(); $permissions = Permission::whereIn('menu_item_id', $userRole->menu_items()->pluck('id'))->pluck('id'); $userRole->permissions()->sync($permissions); ``` -------------------------------- ### Create Team with User ID Source: https://github.com/otifsolutions/aclmenu/blob/master/README.md This snippet demonstrates how to create a team associated with a specific user ID. It utilizes the `updateOrCreate` method to either create a new team or find an existing one based on the provided user ID. ```PHP Team::updateOrCreate(['user_id' => 1]); ``` -------------------------------- ### ACL Menu Models Source: https://github.com/otifsolutions/aclmenu/blob/master/README.md Details about the models used in the ACL Menu system, including their relationships and descriptions. ```APIDOC ## ACL Menu Models ### MenuItem Model - **children**: one-to-many - Returns submenu items from `MenuItem` model. One menuitem can have one or more child items. - **permissions**: one-to-many - Returns list of permissions from `Permission` model. One menuitem can have one or more permissions. - **user_roles**: many-to-many - Returns list of user_roles that belong to `MenuItem`. ### Permission Model - **menu_item**: one-to-many (Inverse) - Returns menuitem from `MenuItem` model that belongs to permission. - **type**: one-to-many (Inverse) - Returns permission type which belongs to permission. ### PermissionType Model - Permission types are created through seeder. These types are "READ", "CREATE", "UPDATE", "DELETE" and "MANAGE". ### Team Model - **owner**: one-to-many (Inverse) - Returns user who creates the team. A team can have only one owner. - **permissions**: belongstoMany - Returns list of permissions from `Permission` model that belongs to `Team`. - **members**: one-to-many - Returns list of members. A team can have one or more members. - **user_roles**: one-to-many - Returns list of user_roles from `UserRole` model. Team can have one or more user roles. ### User Role Model - **permissions**: belongsToMany - Returns list of `permissions` from `Permission` model that belong to user_role. - **menu_items**: belongsToMany - Returns list of menu_items from `MenuItem` belong with user_role. - **team**: one-to-many (Inverse) - Returns team which belongs to user role. - **users**: one-to-many - Returns list of users from `User` model. A user role can one or more users. - **groups**: belongsToMany - Returns groups that belong to UserRoleGroup. ### UserRoleGroup Model - **users**: one-to-many - Returns list of users object. One user role group can have one or more users. - **user_roles**: belongsToMany - Returns list of user_roles from `UserRole` that belong with `UserRoleGroup` model. ``` -------------------------------- ### Check User Permissions with ACLUserTrait Source: https://context7.com/otifsolutions/aclmenu/llms.txt Utilize the methods provided by ACLUserTrait to check user permissions. This includes checking specific permissions for routes, multiple permission types, permissions based on the current route, and access to menu items. ```php // Check if user has READ permission for dashboard if (Auth::user()->hasPermission('READ', '/dashboard')) { return view('dashboard'); } return redirect('/')->with('error', 'Unauthorized'); // Check multiple permission types (using pipe separator) if (Auth::user()->hasPermission('CREATE|UPDATE', '/users')) { // User can create OR update users } // Check permission with current route (uses session) if (Auth::user()->hasPermission('DELETE')) { // Uses current route stored in session } // Check if user can access a specific menu item by ID if (Auth::user()->hasPermissionMenuItem($menuItemId)) { // User has access to this menu item } ``` -------------------------------- ### ACLMenu User Role Seeder Source: https://context7.com/otifsolutions/aclmenu/llms.txt Initializes essential user roles for the ACLMenu system, such as ADMIN, EDITOR, and VIEWER. It uses the `updateOrCreate` method to ensure roles are created if they don't exist or updated if they do, based on their ID and name. ```php use OTIFSolutions\ACLMenu\Models\UserRole; class UserRolesTableSeeder extends Seeder { public function run() { UserRole::updateOrCreate(['id' => 1], ['name' => 'ADMIN']); UserRole::updateOrCreate(['id' => 2], ['name' => 'EDITOR']); UserRole::updateOrCreate(['id' => 3], ['name' => 'VIEWER']); } } ``` -------------------------------- ### Sync Permissions to User Role Source: https://github.com/otifsolutions/aclmenu/blob/master/README.md This code synchronizes a list of permissions to a user role. It assumes that the `$userRole` object is already instantiated and that `$request['permissions']` contains an array of permission identifiers. This is a common operation when assigning permissions to roles. ```PHP $userRole->permissions()->sync($request['permissions']); ``` -------------------------------- ### Refresh ACL Permissions with Artisan Command Source: https://context7.com/otifsolutions/aclmenu/llms.txt Run the `aclmenu:refresh` artisan command to generate and update permissions based on menu item configurations. This command creates necessary permission types (CREATE, READ, UPDATE, DELETE, MANAGE) and generates specific permissions for each menu item according to its `generate_permission` setting. ```bash # Run the refresh command php artisan aclmenu:refresh # The command: # 1. Creates/updates PermissionTypes: CREATE, READ, UPDATE, DELETE, MANAGE # 2. For each MenuItem: # - generate_permission = 'ALL': creates create_, read_, update_, delete_ permissions # - generate_permission = 'MANAGE_ONLY': creates manage_ permission # - generate_permission = 'READ_ONLY' (default): creates read_ permission # Example: MenuItem with route '/dashboard' and generate_permission = 'ALL' creates: # - create_dashboard # - read_dashboard # - update_dashboard # - delete_dashboard ``` -------------------------------- ### Protect Routes with UserRole Middleware Source: https://context7.com/otifsolutions/aclmenu/llms.txt Use the UserRole middleware to protect routes, ensuring that only authenticated users with the necessary permissions can access them. The middleware automatically checks permissions against the route and stores the current permission in the session. ```php // routes/web.php use App\Http\Controllers\DashboardController; use App\Http\Controllers\UserController; // Protect a single route Route::get('/dashboard', [DashboardController::class, 'index']) ->middleware('role:dashboard'); // Protect with specific permission name Route::get('/users', [UserController::class, 'index']) ->middleware('role:users'); // Group routes with middleware Route::middleware(['auth', 'role:admin'])->group(function () { Route::get('/admin/settings', [AdminController::class, 'settings']); Route::post('/admin/settings', [AdminController::class, 'updateSettings']); }); // Route without explicit permission (uses request path) Route::get('/reports', [ReportController::class, 'index']) ->middleware('role'); // The middleware will: // 1. Check if user is authenticated (redirects to config redirect_url if not) // 2. Check if user's role (or group roles) has permission matching the route // 3. Store current permission in session: Session::put('current_permission', $permission) // 4. Allow access or redirect based on permission check ``` -------------------------------- ### Handle Unauthorized Redirect in PHP Source: https://github.com/otifsolutions/aclmenu/blob/master/README.md This PHP code snippet checks if a user is authenticated. If not, it redirects the user to a configured URL, typically the homepage. ```php if ($request->user() == null) return redirect(config('laravelacl.redirect_url')); ``` -------------------------------- ### Render Dynamic Sidebar with Blade Source: https://context7.com/otifsolutions/aclmenu/llms.txt This Blade template iterates through user-authorized menu items to build a sidebar. It checks for permissions, handles parent-child relationships, and applies active CSS classes based on the current request path. ```html ``` -------------------------------- ### Dynamic Sidebar Menu Generation (Blade) Source: https://github.com/otifsolutions/aclmenu/blob/master/README.md This Blade template dynamically generates a sidebar menu based on user permissions. It iterates through menu items and their submenus, displaying only those the authenticated user has access to. The active state of menu items is determined by the current request route. ```blade ``` -------------------------------- ### HTML Sidebar Structure Source: https://github.com/otifsolutions/aclmenu/blob/master/README.md This HTML snippet defines the basic structure of a sidebar component using Bootstrap classes. It includes a header for the brand logo and navigation toggles, with a placeholder for the main menu content. ```html ``` -------------------------------- ### Access User Relationships with ACLUserTrait Source: https://context7.com/otifsolutions/aclmenu/llms.txt Access various user-related relationships and properties provided by the ACLUserTrait. This includes direct access to user roles, groups, teams, and checks for child account status. ```php // Access user relationships $userRole = Auth::user()->user_role; // Get user's role $group = Auth::user()->group; // Get user's group $team = Auth::user()->team(); // Get user's team $parentTeam = Auth::user()->parent_team; // Get parent team $childTeam = Auth::user()->child_team; // Get child team $teamOwner = Auth::user()->team_owner; // Get team owner // Check if user is a child account if (Auth::user()->isChildAccount()) { // User has no team_id (is a team owner) } ``` -------------------------------- ### Configure User Model in Laravel Source: https://github.com/otifsolutions/aclmenu/blob/master/README.md This configuration snippet for Laravel specifies the user model to be used by the ACL system. It typically points to the model defined in Laravel's authentication configuration. ```php 'models' => [ 'user' => config('auth.providers.users.model') ] ``` -------------------------------- ### PHP Controller CRUD Permission Checks with ACLUserTrait Source: https://context7.com/otifsolutions/aclmenu/llms.txt This PHP code snippet illustrates how to protect CRUD operations within a Laravel controller using the `hasPermission` method provided by the ACLUserTrait. It checks for specific permissions (READ, CREATE, UPDATE, DELETE) against a given resource path before executing the action, returning unauthorized messages or responses if the user lacks the necessary rights. This ensures that only authorized users can perform sensitive operations. ```php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use App\Models\User; class UserController extends Controller { public function index() { // Check READ permission if (!Auth::user()->hasPermission('READ', '/users')) { return redirect('/')->with('error', 'Unauthorized access'); } $users = User::all(); return view('users.index', compact('users')); } public function create() { // Check CREATE permission if (!Auth::user()->hasPermission('CREATE', '/users')) { return redirect('/users')->with('error', 'You cannot create users'); } return view('users.create'); } public function store(Request $request) { if (!Auth::user()->hasPermission('CREATE', '/users')) { return response()->json(['error' => 'Unauthorized'], 403); } $user = User::create($request->validated()); return redirect('/users')->with('success', 'User created'); } public function edit($id) { if (!Auth::user()->hasPermission('UPDATE', '/users')) { return redirect('/users')->with('error', 'You cannot edit users'); } $user = User::findOrFail($id); return view('users.edit', compact('user')); } public function update(Request $request, $id) { if (!Auth::user()->hasPermission('UPDATE', '/users')) { return response()->json(['error' => 'Unauthorized'], 403); } $user = User::findOrFail($id); $user->update($request->validated()); return redirect('/users')->with('success', 'User updated'); } public function destroy($id) { if (!Auth::user()->hasPermission('DELETE', '/users')) { return response()->json(['error' => 'Unauthorized'], 403); } User::destroy($id); return redirect('/users')->with('success', 'User deleted'); } } ``` -------------------------------- ### Integrate ACLUserTrait into User Model Source: https://context7.com/otifsolutions/aclmenu/llms.txt Add the ACLUserTrait to your User model to enable permission checking, role relationships, and team management. This trait provides methods for checking permissions, accessing user roles, groups, and team structures. ```php namespace App\Models; use Illuminate\Foundation\Auth\User as Authenticatable; use OTIFSolutions\ACLMenu\Traits\ACLUserTrait; class User extends Authenticatable { use ACLUserTrait; // Your existing User model code... } ``` -------------------------------- ### Store Current Permission in Session Source: https://github.com/otifsolutions/aclmenu/blob/master/README.md This PHP code snippet retrieves the current request path and stores it in the session. This is useful for tracking user navigation and permissions within the application. ```php $permission = $request->path(); \Session::put('current_permission', $permission); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.