Files
wticreatorstudio/Modules/RolePermissionPlus/Services/AugmentationService.php
T
2024-02-12 22:54:20 -05:00

206 lines
8.5 KiB
PHP

<?php
namespace Modules\RolePermissionPlus\Services;
use Modules\RolePermission\Entities\Role;
class AugmentationService
{
public function __construct() {
$this->updatePermissionController();
$this->updateUserRepository();
$this->updateRoleRepositories();
$this->updateStaffController();
$this->updateNotificationSettingEntities();
}
public function updatePermissionController() {
if (isModuleActive('RolePermission')) {
$path = module_path('RolePermission', 'Http/Controllers/PermissionController.php');
if (file_exists($path)) {
$update = false;
$content = file_get_contents($path);
$roles = Role::where('type', '!=','superadmin')->where('type', '!=','customer')->where('type', '!=','affiliate')->get();
foreach($roles as $role){
if (!preg_match("/[.\s\S]+function index\((.*?)\)[.\s\S]+?{[.\s\S]+?elseif[.\s\S]+?\'".$role->type."\'[.\s\S]+?}/", $content)) {
$matches = null;
preg_match('/[.\s\S]+function index\((.*?)\)[.\s\S]+?{[.\s\S]+?elseif[.\s\S]+?\'admin\'/', $content, $matches);
if (count($matches) > 0) {
$code = $matches[0];
$snippet = " || \$role->type == '".$role->type."'";
$code .= $snippet;
$content = preg_replace("/[.\s\S]+function index\((.*?)\)[.\s\S]+?{[.\s\S]+?elseif[.\s\S]+?\'admin\'/", $code, $content);
$update = true;
}
}
if ($update) {
file_put_contents($path, $content);
}
}
}
}
}
public function updateUserRepository() {
$path = app_path('Repositories/UserRepository.php');
if (file_exists($path)) {
$update = false;
$content = file_get_contents($path);
$roles = Role::where('type', '!=','superadmin')->where('type', '!=','admin')->where('type', '!=','customer')->where('type', '!=','affiliate')->where('type', '!=','staff')->where('type', '!=','seller')->get();
foreach($roles as $role){
if (!preg_match('/[.\s\S]+function all\((.*?)\)[.\s\S]+?\''.$role->type.'\'\)/', $content)) {
$matches = null;
preg_match('/[.\s\S]+function all\((.*?)\)[.\s\S]+?\'admin\'\)/', $content, $matches);
if (count($matches) > 0) {
$code = $matches[0];
$snippet = "->orWhere('type', '".$role->type."')";
$code .= $snippet;
$content = preg_replace("/[.\s\S]+function all\((.*?)\)[.\s\S]+?\'admin\'\)/", $code, $content);
$update = true;
}
}
if ($update) {
file_put_contents($path, $content);
}
}
if (!preg_match("/[.\s\S]+function all\((.*?)\)[.\s\S]+?RolePermissionPlus/", $content)) {
$matches = null;
preg_match('/[.\s\S]+function all\((.*?)\)[.\s\S]+?\'admin\'\)/', $content, $matches);
if (count($matches) > 0) {
$code = $matches[0];
$snippet = "\n\t\tif(isModuleActive('RolePermissionPlus')){\n\t\t\t\$roles = Role::where('id', '>', 1)->get();\n\t\t}";
$code .= $snippet;
$content = preg_replace("/[.\s\S]+function create\(\)[.\s\S]+?roles[.\s\S]+?get\(\)\;/", $code, $content);
$update = true;
}
}
if ($update) {
file_put_contents($path, $content);
}
}
}
public function updateRoleRepositories() {
if (isModuleActive('RolePermission')) {
$path = module_path('RolePermission', 'Repositories/RoleRepository.php');
if (file_exists($path)) {
$update = false;
$content = file_get_contents($path);
if (!preg_match("/[.\s\S]+create\((.*?)\)[.\s\S]+?AugmentationService/", $content)) {
$matches = null;
preg_match('/[.\s\S]+create\((.*?)\)[.\s\S]+?save\(\)\;/', $content, $matches);
if (count($matches) > 0) {
$code = $matches[0];
$snippet = "\n\n\t\tnew \Modules\RolePermissionPlus\Services\AugmentationService();";
$code .= $snippet;
$content = preg_replace("/[.\s\S]+create\((.*?)\)[.\s\S]+?save\(\)\;/", $code, $content);
$update = true;
}
}
if ($update) {
file_put_contents($path, $content);
}
}
}
}
public function updateStaffController() {
$path = app_path('Http/Controllers/StaffController.php');
if (file_exists($path)) {
$update = false;
$content = file_get_contents($path);
if (!preg_match("/[.\s\S]+function create\(\)[.\s\S]+?RolePermissionPlus/", $content)) {
$matches = null;
preg_match('/[.\s\S]+function create\(\)[.\s\S]+?roles[.\s\S]+?get\(\)\;/', $content, $matches);
if (count($matches) > 0) {
$code = $matches[0];
$snippet = "\n\t\tif(isModuleActive('RolePermissionPlus')){\n\t\t\t\$roles = Role::where('id', '>', 1)->get();\n\t\t}";
$code .= $snippet;
$content = preg_replace("/[.\s\S]+function create\(\)[.\s\S]+?roles[.\s\S]+?get\(\)\;/", $code, $content);
$update = true;
}
}
if (!preg_match("/[.\s\S]+function edit\((.*?)\)[.\s\S]+?RolePermissionPlus/", $content)) {
$matches = null;
preg_match('/[.\s\S]+function edit\((.*?)\)[.\s\S]+?roles[.\s\S]+?get\(\)\;/', $content, $matches);
if (count($matches) > 0) {
$code = $matches[0];
$snippet = "\n\t\t\tif(isModuleActive('RolePermissionPlus')){\n\t\t\t\t\$roles = Role::where('id', '>', 1)->get();\n\t\t\t}";
$code .= $snippet;
$content = preg_replace("/[.\s\S]+function edit\((.*?)\)[.\s\S]+?roles[.\s\S]+?get\(\)\;/", $code, $content);
$update = true;
}
}
if ($update) {
file_put_contents($path, $content);
}
}
}
public function updateNotificationSettingEntities() {
if (isModuleActive('GeneralSetting')) {
$path = module_path('GeneralSetting', 'Entities/NotificationSetting.php');
if (file_exists($path)) {
$update = false;
$content = file_get_contents($path);
$roles = Role::where('type', '!=','superadmin')->where('type', '!=','admin')->where('type', '!=','customer')->where('type', '!=','affiliate')->where('type', '!=','staff')->where('type', '!=','seller')->get();
foreach($roles as $role){
if (!preg_match('/[.\s\S]+function getNotificationSettingByUserRoleType\((.*?)\)[.\s\S]+?{[.\s\S]+?elseif[.\s\S]+?\"".$role->type."\"[.\s\S]+?}/', $content)) {
$matches = null;
preg_match('/[.\s\S]+function getNotificationSettingByUserRoleType\((.*?)\)[.\s\S]+?{[.\s\S]+?elseif[.\s\S]+?\"staff\"/', $content, $matches);
if (count($matches) > 0) {
$code = $matches[0];
$snippet = " || \$user->role->type == \"".$role->type."\"";
$code .= $snippet;
$content = preg_replace("/[.\s\S]+function getNotificationSettingByUserRoleType\((.*?)\)[.\s\S]+?{[.\s\S]+?elseif[.\s\S]+?\"staff\"/", $code, $content);
$update = true;
}
}
if ($update) {
file_put_contents($path, $content);
}
}
}
}
}
}