get('currentCompany'); if($role != null && $role->id != 1 && $company != null && $company->plan != null ){ // $availablePermissions = $company->plan->permissions; } $grouped = Arrays::groupBy($availablePermissions->toArray(), Functions::extractField('perm_group')); ksort($grouped); return $grouped; } public function getRoleForAdmin(){ return Role::where(function($q){ $q->where('id','=',1) ->orWhere('company_id',\Landlord::getTenantId('company_id')); })->pluck('display_name', 'name') ->toArray(); } public function getRolesByNames($rolesNames,$adminCheck = false ){ if($adminCheck){ return Role::where(function($q){ $q->where('id','=',1)->orWhere('company_id',\Landlord::getTenantId('company_id')); })->whereIn('name',$rolesNames)->get(); } return Role::where('id','<>',1) ->where('company_id',\Landlord::getTenantId('company_id')) ->whereIn('name',$rolesNames) ->excludingProtected() ->get(); } public function getRolesForCompany(){ return Role::where('id','<>',1) ->where('company_id',\Landlord::getTenantId('company_id')) ->pluck('display_name', 'name') ->toArray(); } /** * Roles a scoped "user.settings" user may assign to a user, i.e. the * company roles minus the protected ones (Admin / Company Admin). * * @return array */ public function getRolesForCompanyExcludingProtected(){ return Role::where('id','<>',1) ->where('company_id',\Landlord::getTenantId('company_id')) ->excludingProtected() ->pluck('display_name', 'name') ->toArray(); } /** * Roles a company admin (Admin Setting permission / Company Admin role) * may assign: every role of their own company including the protected * Company Admin role, but never the global Admin role (id 1). * * @return array */ public function getRolesForCompanyIncludingProtected(){ return Role::where('id','<>',1) ->where('company_id',\Landlord::getTenantId('company_id')) ->pluck('display_name', 'name') ->toArray(); } /** * Company roles (including the protected Company Admin role) matching the * given names, excluding the global Admin role (id 1). * * @param array $rolesNames * @return \Illuminate\Database\Eloquent\Collection */ public function getCompanyRolesIncludingProtectedByNames($rolesNames){ return Role::where('id','<>',1) ->where('company_id',\Landlord::getTenantId('company_id')) ->whereIn('name',$rolesNames) ->get(); } /** * @param $roleName * @param $companyId * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null */ public function getRolebyNameForCompany($roleName,$companyId){ return Role::where('company_id',$companyId)->where('name',$roleName)->first(); } /** * @param $deleteUnused - Delete Unused permissions * @return array */ public function synchModulePermissions($deleteUnused = false) { if ($deleteUnused) { $this->deletePermissions(); } $customPermissionGroups = config('custom_permissions', []); foreach ($customPermissionGroups as $groupName => $permissions) { foreach ($permissions as $permissionName) { try { $permission = Permission::findByName($permissionName); } catch (PermissionDoesNotExist $e) { $permission = Permission::create([ 'name' => $permissionName, 'guard_name' => 'web', 'perm_group' => $groupName, ]); $this->permissionCreated[] = $permission; } } } $modules = \Module::toCollection(); foreach ($modules as $module) { $config = include $module->getPath().'/Config/config.php'; if (isset($config['permissions'])) { $permissions = $config['permissions']; if ($permissions != null) { foreach ($permissions as $permissionName) { try { $permission = Permission::findByName($permissionName); } catch (PermissionDoesNotExist $e) { // Permission not exist create one $permission = Permission::create([ 'name' => $permissionName, 'guard_name' => 'web', 'perm_group' => $module->name ]); $this->permissionCreated[] = $permission; } } } } } return [ 'created' => $this->permissionCreated, 'deleted' => $this->permissionDeleted ]; } /** * Check permissions in database and delete unused */ private function deletePermissions() { $databasePermissions = Permission::all()->pluck('name'); $modules = \Module::toCollection(); $permissionCollection = []; foreach ($modules as $module) { $permissions = config($module->getLowerName() . '.permissions'); if ($permissions != null) { foreach ($permissions as $permissionName) { $permissionCollection[] = $permissionName; } } } $dif = array_diff($databasePermissions->toArray(), $permissionCollection); foreach ($dif as $perm) { $perm = Permission::findByName($perm); $this->permissionDeleted[] = $perm; $perm->delete(); } } }