diff --git a/Init.php b/Init.php index 7a8e57a..743a345 100644 --- a/Init.php +++ b/Init.php @@ -22,11 +22,15 @@ use FacturaScripts\Core\Base\DataBase; use FacturaScripts\Core\Template\InitClass; +use FacturaScripts\Core\Model\Role; +use FacturaScripts\Core\Model\RoleAccess; +use FacturaScripts\Core\Where; use FacturaScripts\Dinamic\Model\Service; use FacturaScripts\Dinamic\Model\ServiceRegular; final class Init extends InitClass { + const ROLE_NAME = 'OpenServBus'; public function init(): void { // se ejecuta cada vez que carga FacturaScripts (si este plugin está activado). @@ -44,6 +48,8 @@ public function update(): void new ServiceRegular(); $this->deleteColumnFromTable(); $this->changeNameEmployee(); + + $this->createRoleForPlugin(); } private function changeNameEmployee(): void @@ -77,4 +83,58 @@ protected function deleteColumnFromTable(): void } } } + + private function createRoleForPlugin(): void + { + // force table checks for Role and RoleAccess + new Role(); + new RoleAccess(); + + $dataBase = new DataBase(); + $dataBase->beginTransaction(); + + // creates the role if not exists + $role = new Role(); + if (false === $role->load(self::ROLE_NAME)) { + $role->codrole = $role->descripcion = self::ROLE_NAME; + if (false === $role->save()) { + $dataBase->rollback(); + return; + } + } + + // checks the role permissions (only plugin's own controllers) + $nameControllers = [ + 'ListVehicleDocumentation', 'ListVehicle', 'ListTarjeta', 'ListServiceRegular', 'ListServiceAssembly', 'ListService', 'ListHelper', 'ListFuelKm', 'ListEmployeeOpen', 'ListEmployeeAttendanceManagement', 'ListDriver', 'ListAdvertismentUser', + + 'EditVehicleType', 'EditVehicleEquipamentType', 'EditVehicleEquipament', 'EditVehicleDocumentation', 'EditVehicle', 'EditUser', 'EditTarjetaType', 'EditTarjeta', 'EditStop', 'EditServiceValuationType', 'EditServiceValuation', 'EditServiceType', 'EditServiceRegularValuation', 'EditServiceRegularPeriod', 'EditServiceRegularItinerary', 'EditServiceRegularCombinationServ', 'EditServiceRegularCombination', 'EditServiceRegular', 'EditServiceItinerary', 'EditServiceAssembly', 'EditFuelKm', 'EditService', 'EditIdentificationMean', 'EditHelper', 'EditGarage', 'EditFuelType', 'EditEmployeeOpen', 'EditFuelPump', 'EditEmployeeDocumentation', 'EditEmployeeContractType', 'EditEmployeeContract', 'EditEmployeeAttendanceManagementYn', 'EditEmployeeAttendanceManagement', 'EditDriver', 'EditDocumentationType', 'EditDepartment', 'EditCollaborator', 'EditAdvertismentUser', 'EditAbsenceReason', 'ConfigOpenServBus' + ]; + $nameControllers = array_unique($nameControllers); + + // check/create the role permissions + foreach ($nameControllers as $nameController) { + $roleAccess = new RoleAccess(); + $where = [ + Where::eq('codrole', self::ROLE_NAME), + Where::eq('pagename', $nameController) + ]; + if ($roleAccess->loadWhere($where)) { + continue; + } + + // creates the permission if not exists + $roleAccess->allowdelete = true; + $roleAccess->allowupdate = true; + $roleAccess->codrole = self::ROLE_NAME; + $roleAccess->pagename = $nameController; + $roleAccess->onlyownerdata = false; + if (false === $roleAccess->save()) { + $dataBase->rollback(); + return; + } + } + + // without problems = Commit + $dataBase->commit(); + } } \ No newline at end of file