Skip to content

Commit

Permalink
CAKEPHP5 upgrade fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ioannis committed Sep 12, 2025
1 parent b04aba0 commit 7396790
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 93 deletions.
20 changes: 9 additions & 11 deletions app/src/Controller/ApiV2Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ public function add() {
/** var string $modelsName */
$modelsName = $this->getName();
/** var Cake\ORM\Table $table */
$table = $this->fetchTable($modelsName);
// $tableName = models
$tableName = $this->tableName;
$table = $this->getCurrentTable();

$json = $this->request->getData(); // Parsed by BodyParserMiddleware

Expand All @@ -97,13 +95,13 @@ public function add() {

foreach($json[$modelsName] as $rec) {
try {
$obj = $this->$modelsName->newEntity($rec);
$obj = $table->newEntity($rec);

if($this->$modelsName->saveOrFail($obj)) {
if($table->saveOrFail($obj)) {
$results[] = ['id' => $obj->id];

// Trigger provisioning, letting errors bubble up (AR-GMR-5)
if(method_exists($this->$modelsName, "requestProvisioning")) {
if(method_exists($table, "requestProvisioning")) {
$this->llog('rule', "AR-GMR-5 Requesting provisioning for $modelsName " . $obj->id);
$table->requestProvisioning(id: $obj->id, context: ProvisioningContextEnum::Automatic);
}
Expand Down Expand Up @@ -185,8 +183,8 @@ public function calculateRequestedCOID(): ?int {
public function delete($id) {
/** var string $modelsName */
$modelsName = $this->getName();
/** var Cake\ORM\Table $table */
$table = $this->fetchTable($modelsName);
/** var Cake\ORM\Table $table */
$table = $this->getCurrentTable();
// $tableName = models
$tableName = $table->getTable();

Expand Down Expand Up @@ -237,7 +235,7 @@ protected function dispatchIndex(string $mode = 'default') {
// $modelsName = Models
$modelsName = $this->getName();
/** var Cake\ORM\Table $table */
$table = $this->fetchTable($modelsName);
$table = $this->getCurrentTable();

$reqParameters = [...$this->request->getQuery()];
$pickerMode = ($mode === 'picker');
Expand Down Expand Up @@ -271,7 +269,7 @@ public function edit($id) {
/** var string $modelsName */
$modelsName = $this->getName();
/** var Cake\ORM\Table $table */
$table = $this->fetchTable($modelsName);
$table = $this->getCurrentTable();
// $tableName = models
$tableName = $table->getTable();

Expand Down Expand Up @@ -393,7 +391,7 @@ public function view($id = null) {
/** var string $modelsName */
$modelsName = $this->getName();
/** var Cake\ORM\Table $table */
$table = $this->fetchTable($modelsName);
$table = $this->getCurrentTable();
// $tableName = models
$tableName = $table->getTable();

Expand Down
90 changes: 44 additions & 46 deletions app/src/Controller/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,6 @@ public function beforeFilter(\Cake\Event\EventInterface $event) {
*/

public function beforeRender(\Cake\Event\EventInterface $event) {
/** var string $modelsName */
$modelsName = $this->getName();

// Views can also inspect the request object to determine the current
// controller and action, but it seems slightly easier to do it once here.
$this->set('vv_controller', $this->request->getParam('controller'));
Expand Down Expand Up @@ -269,6 +266,25 @@ public function getCOID(): ?int {
return $cur_co ? $cur_co->id : null;
}

/**
* Get the current Table instance for this controller, respecting plugin context.
*
* @since COmanage Registry v5.2.0
* @return \Cake\ORM\Table
*/
protected function getCurrentTable(): \Cake\ORM\Table
{
/** @var string $modelsName */
$modelsName = $this->getName();

$alias = $this->plugin !== null
? $this->plugin . '.' . $modelsName
: $modelsName;

return $this->fetchTable($alias);
}


/**
* @param string $potentialPrimaryLink
*
Expand All @@ -278,14 +294,11 @@ public function getCOID(): ?int {

protected function primaryLinkOnGet(string $potentialPrimaryLink): Object|bool
{
/** var string $modelsName */
$modelsName = $this->getName();

// If this action allows unkeyed, asserted primary link IDs, check the query
// string (e.g.: 'add' or 'index' allow matchgrid_id to be passed in)
$actionParam = $this->request->getParam('action');
$allowsUnkeyed = $this->$modelsName->allowUnkeyedPrimaryLink($actionParam);
$allowsLookup = $this->$modelsName->allowLookupPrimaryLink($actionParam);
$allowsUnkeyed = $this->getCurrentTable()->allowUnkeyedPrimaryLink($actionParam);
$allowsLookup = $this->getCurrentTable()->allowLookupPrimaryLink($actionParam);
$param = (int)$this->request->getParam('pass.0');

if($allowsUnkeyed) {
Expand All @@ -299,7 +312,7 @@ protected function primaryLinkOnGet(string $potentialPrimaryLink): Object|bool
return false;
}

return $this->$modelsName->findPrimaryLink($param);
return $this->getCurrentTable()->findPrimaryLink($param);
}

/**
Expand Down Expand Up @@ -345,20 +358,18 @@ protected function primaryLinkOnPost(string $potentialPrimaryLink): Object|bool

protected function primaryLinkOnPut(): Object|bool
{
/** var string $modelsName */
$modelsName = $this->getName();
$param = (int)$this->request->getParam('pass.0');

// Put = edit, so we should look up the parent ID via the object itself
if (
empty($param)
||
!$this->$modelsName->allowLookupPrimaryLink($this->request->getParam('action'))
!$this->getCurrentTable()->allowLookupPrimaryLink($this->request->getParam('action'))
) {
return false;
}

return $this->$modelsName->findPrimaryLink($param);
return $this->getCurrentTable()->findPrimaryLink($param);
}

/**
Expand All @@ -368,11 +379,9 @@ protected function primaryLinkOnPut(): Object|bool
*/
protected function populatedPrimaryLink(string $potentialPrimaryLink): Object
{
/** var string $modelsName */
$modelsName = $this->getName();
// $potentialPrimaryLink will be something like 'attribute_collector_id'
// $potentialPrimaryLinkTable will be something like 'CoreEnroller.AttributeCollectors'
$potentialPrimaryLinkTable = $this->$modelsName->getPrimaryLinkTableName($potentialPrimaryLink);
$potentialPrimaryLinkTable = $this->getCurrentTable()->getPrimaryLinkTableName($potentialPrimaryLink);

// For looking up values in records here, we want only the attribute
// itself and not the plugin name (used for hacky notation by
Expand Down Expand Up @@ -406,9 +415,8 @@ protected function populatedPrimaryLink(string $potentialPrimaryLink): Object

protected function primaryLinkLookup(): void
{
/** var string $modelsName */
$modelsName = $this->getName();
$availablePrimaryLinks = $this->$modelsName->getPrimaryLinks();
$table = $this->getCurrentTable();
$availablePrimaryLinks = $table->getPrimaryLinks();

// Iterate over all the potential primary links and pick the appropriate one
foreach($availablePrimaryLinks as $potentialPrimaryLink) {
Expand All @@ -429,7 +437,7 @@ protected function primaryLinkLookup(): void

// At the end we need to have a Primary Link
if(empty($this->cur_pl->value)
&& !$this->$modelsName->allowEmptyPrimaryLink($this->request->getParam('action'))
&& !$table->allowEmptyPrimaryLink($this->request->getParam('action'))
&& $this->request->getParam('action') != 'deleted') {
throw new \RuntimeException(__d('error', 'primary_link'));
}
Expand Down Expand Up @@ -478,13 +486,10 @@ public function getPrimaryLink(bool $lookup=false): Object
return $this->cur_pl;
}

/** var string $modelsName */
$modelsName = $this->getName();

$this->cur_pl = new \stdClass();

if(!(method_exists($this->$modelsName, 'getPrimaryLinks')
&& $this->$modelsName->getPrimaryLinks())
if(!(method_exists($this->getCurrentTable(), 'getPrimaryLinks')
&& $this->getCurrentTable()->getPrimaryLinks())
) {
return $this->cur_pl;
}
Expand All @@ -504,7 +509,7 @@ public function getPrimaryLink(bool $lookup=false): Object

// Look up the link value to find the related entity

$linkTableName = $this->$modelsName->getPrimaryLinkTableName($this->cur_pl->attr);
$linkTableName = $this->getCurrentTable()->getPrimaryLinkTableName($this->cur_pl->attr);
$linkTable = $this->getTableLocator()->get($linkTableName);

$this->set('vv_primary_link_model', $linkTableName);
Expand Down Expand Up @@ -540,12 +545,9 @@ public function getPrimaryLink(bool $lookup=false): Object
*/

protected function getRedirectGoal(string $action): ?string {
/** var string $modelsName */
$modelsName = $this->getName();

// PrimaryLinkTrait
if(method_exists($this->$modelsName, "getRedirectGoal")) {
return $this->$modelsName->getRedirectGoal($this->request->getParam('action'));
if(method_exists($this->getCurrentTable(), "getRedirectGoal")) {
return $this->getCurrentTable()->getRedirectGoal($this->request->getParam('action'));
}

return 'index';
Expand Down Expand Up @@ -674,11 +676,10 @@ protected function setCO() {
if($this->request->is('restful')
&& !empty($attrs['params']['model'])) {
$modelsName = \Cake\Utility\Inflector::camelize($attrs['params']['model']);
$this->$modelsName = TableRegistry::getTableLocator()->get($modelsName);
}

if(!method_exists($this->$modelsName, "requiresCO")
|| !$this->$modelsName->requiresCO()) {
if(!method_exists($this->getCurrentTable(), "requiresCO")
|| !$this->getCurrentTable()->requiresCO()) {
// Nothing to do, CO not required by this model/controller
return;
}
Expand All @@ -700,13 +701,13 @@ protected function setCO() {
}

if(!$coid
&& $this->$modelsName->allowUnkeyedCO($this->request->getParam('action'))
&& $this->getCurrentTable()->allowUnkeyedCO($this->request->getParam('action'))
&& !empty($this->request->getQuery('co_id'))) {
$coid = $this->request->getQuery('co_id');
}

if(!$coid
&& !$this->$modelsName->allowEmptyCO()
&& !$this->getCurrentTable()->allowEmptyCO()
&& !$this->request->is('restful')) {
// If we get this far without a CO ID, something went wrong.
throw new \RuntimeException(__d('error', 'coid'));
Expand All @@ -728,7 +729,7 @@ protected function setCO() {
throw new \InvalidArgumentException(__d('error', 'inactive', [__d('controller', 'Cos', [1]), $coid]));
}

if(!empty($modelsName) && !empty($this->$modelsName)) {
if(!empty($modelsName) && !empty($this->getCurrentTable())) {
// We store the CO ID in Configuration to facilitate its access from
// model contexts such as validation where passing the value via the
// Controller is not particularly feasible. Note that for API calls
Expand All @@ -737,9 +738,9 @@ protected function setCO() {

// This only works for the current model, not related models. For
// relatedmodels, we use the event listener approach below.
if(method_exists($this->$modelsName, "acceptsCoId")
&& $this->$modelsName->acceptsCoId()) {
$this->$modelsName->setCurCoId((int)$coid);
if(method_exists($this->getCurrentTable(), "acceptsCoId")
&& $this->getCurrentTable()->acceptsCoId()) {
$this->getCurrentTable()->setCurCoId((int)$coid);
}

// This doesn't work for the current model since it has already been
Expand All @@ -753,7 +754,7 @@ protected function setCO() {
// a use case to do so, though note it's possible a child associations
// wants the CO ID even though the parent doesn't.

foreach($this->$modelsName->associations()->getIterator() as $a) {
foreach($this->getCurrentTable()->associations()->getIterator() as $a) {
$aTable = $a->getTarget();

if(method_exists($aTable, "acceptsCoId")
Expand All @@ -773,9 +774,6 @@ protected function setCO() {
*/

protected function setTZ() {
/** var string $modelsName */
$modelsName = $this->getName();

// See if we've collected it from the browser in a previous page load. Otherwise,
// use the system default. If the user set a preferred timezone, we'll catch that below.

Expand All @@ -802,9 +800,9 @@ protected function setTZ() {

$this->set('vv_tz', $tz);

if($this->$modelsName->behaviors()->has('Timezone')) {
if($this->getCurrentTable()->behaviors()->has('Timezone')) {
// Tell TimezoneBehavior what the current timezone is
$this->$modelsName->setTimeZone($tz);
$this->getCurrentTable()->setTimeZone($tz);
}
}
}
6 changes: 4 additions & 2 deletions app/src/Controller/MVEAController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class MVEAController extends StandardController {
public function beforeFilter(\Cake\Event\EventInterface $event) {
/** var string $modelsName */
$modelsName = $this->getName();
$table = $this->getCurrentTable();

if(!$this->request->is('restful') && $this->request->getParam('action') != 'deleted') {
// Provide additional hints to BreadcrumbsComponent. This needs to be here
Expand All @@ -62,7 +63,7 @@ public function beforeFilter(\Cake\Event\EventInterface $event) {
} else {
$parentModel = StringUtilities::foreignKeyToClassName($primaryLink->attr);

$parentPrimaryLink = $this->$modelsName->$parentModel->findPrimaryLink((int)$primaryLink->value);
$parentPrimaryLink = $table->$parentModel->findPrimaryLink((int)$primaryLink->value);

$this->Breadcrumb->injectPrimaryLink($parentPrimaryLink);
$this->Breadcrumb->injectPrimaryLink($primaryLink);
Expand Down Expand Up @@ -140,12 +141,13 @@ public function beforeFilter(\Cake\Event\EventInterface $event) {
public function beforeRender(\Cake\Event\EventInterface $event) {
/** var string $modelsName */
$modelsName = $this->getName();
$table = $this->getCurrentTable();
// field = model (or model_name)
$fieldName = Inflector::underscore(Inflector::singularize($modelsName));

if(!$this->request->is('restful') && $this->request->getParam('action') != 'deleted') {
// If there is a default type setting for this model, pass it to the view
if($this->$modelsName->getSchema()->hasColumn('type_id')) {
if($table->getSchema()->hasColumn('type_id')) {
$defaultTypeField = "default_" . $fieldName . "_type_id";

$CoSettings = TableRegistry::getTableLocator()->get('CoSettings');
Expand Down
8 changes: 4 additions & 4 deletions app/src/Controller/MultipleAuthenticatorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function beforeFilter(\Cake\Event\EventInterface $event) {
// $authModelName = eg SshKeyAuthenticators
$authModelsName = Inflector::singularize($modelsName) . "Authenticators";
/** var Cake\ORM\Table $table */
$Table = $this->$modelsName;
$Table = $this->getCurrentTable();
// $authFK = eg ssh_key_authenticator_id
$authFK = StringUtilities::classNameToForeignKey($authModelsName);

Expand Down Expand Up @@ -102,7 +102,7 @@ public function generateRedirect($entity) {
// $authModelName = eg SshKeyAuthenticators
$authModelsName = Inflector::singularize($modelsName) . "Authenticators";
/** var Cake\ORM\Table $table */
$Table = $this->$modelsName;
$Table = $this->getCurrentTable();
// $authFK = eg ssh_key_authenticator_id
$authFK = StringUtilities::classNameToForeignKey($authModelsName);

Expand Down Expand Up @@ -135,7 +135,7 @@ public function index() {
/** var string $modelsName */
$modelsName = $this->getName();
/** var Cake\ORM\Table $table */
$table = $this->fetchTable($modelsName);
$table = $this->getCurrentTable();
// $tableName = models
$tableName = $table->getTable();
// Construct the Query
Expand Down Expand Up @@ -185,7 +185,7 @@ public function willHandleAuth(\Cake\Event\EventInterface $event): string {
// $authModelName = eg SshKeyAuthenticators
$authModelsName = Inflector::singularize($modelsName) . "Authenticators";
/** var Cake\ORM\Table $table */
$Table = $this->$modelsName;
$Table = $this->getCurrentTable();
// $authFK = eg ssh_key_authenticator_id
$authFK = StringUtilities::classNameToForeignKey($authModelsName);

Expand Down
2 changes: 1 addition & 1 deletion app/src/Controller/SingleAuthenticatorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function manage() {
// $authModelName = eg PasswordAuthenticators
$authModelsName = Inflector::singularize($modelsName) . "Authenticators";
/** var Cake\ORM\Table $table */
$Table = $this->$modelsName;
$Table = $this->getCurrentTable();
// $authFK = eg password_authenticator_id
$authFK = StringUtilities::classNameToForeignKey($authModelsName);

Expand Down
Loading

0 comments on commit 7396790

Please sign in to comment.