1

I have found a plugin that extends joomla's core registration by adding a new field named User group selector. There is a trigger method in this plugin named onUserAfterSave that save the custom field after saving other fields.

My question is this: Since it uses another function for saving user group (that selected by user) and there is no query transaction for this, what happen if the the second one fails?? Is there any other way?

Plugin name : usergroupselector

2
+50

I don't know the plugin and am not entirely sure I can think what you might have in mind that concerns you about a possible failure of this.

But my logic says that if a function fails under any possible conditions that could lead to a function fail, then it won't complete its operation, therefore we won't get the expected outcome.

In the case of the plugin as you have described it, it simply won't add the user to his selected usergroup.

Now it's always on the good design aspect of code to take into consideration any possible errors and return an error message or something else that will be useful, if an error occurs.

Update:

First query runs fine, while usergroup is a necessary field that must be filled out. In a custom php script, we can use sql transaction, but when we extend joomla plugin, how can we do this?

This sounds like a rare situation to have a failure right after executing the user save function and before making it to assign it to the selected usergroup - but if you go that far, it could be possible.

However the scope of the plugin you use seems to be an easy-going workflow of letting users change their usergroup, than a very strict one that should never fail to assign a new registered user to a selected usergroup.

In such case, you can write your own registration plugin with a onUserBeforeSave function, so you will pass the selected usergroup alongside with the user creation. Yet, I am not sure if there is a transaction involved in this workflow, so it will perform all or nothing updates in the related tables.

  • am not entirely sure I can think what you might have in mind that concerns you about a possible failure of this : No difference! Imagine server downs in the second query. First query runs fine, while usergroup is a necessary field that must be filled out. In a custom php script, we can use sql transaction, but when we extend joomla plugin, how can we do this? (Remember this scenario is just an example) – Vahid Najafi Feb 23 '17 at 7:14
  • Thanks for the update part. It was exactly what I wanted to know. – Vahid Najafi Feb 23 '17 at 12:37
  • And a question, If I use onUserBeforeSave, what does joomla do in it's registeration? Does registration will be apply twice? – Vahid Najafi Feb 23 '17 at 12:39
2

User plugin's onUserAfterSave function does not return any value, means there will be no action taken by calling function based on result of this function.

In core registration, the user will be saved irrespective of the result from onUserAfterSave function. So if your logic fails in this function, the user will still be saved, although your user group selector value will not be saved.

Here is the code which invokes this function:

// Fire the after save event
$dispatcher->trigger($this->event_after_save, array($table->getProperties(), false, true, null));
  • although your user group selector value will not be saved , The problem is here. user group selector is a necessary field that must be filled out. Imagine we work in own php script or framework. We can use sql transaction, so if second one fails, first one will be rolled back. But here it runs in another function. – Vahid Najafi Feb 23 '17 at 7:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.