Issue Details (XML | Word | Printable)

Key: MDL-16614
Type: Bug Bug
Status: Resolved Resolved
Resolution: Fixed
Priority: Major Major
Assignee: Tim Hunt
Reporter: Przemyslaw Stencel
Votes: 0
Watchers: 3
Operations

Add/Edit UI Mockup to this issue
If you were logged in you would be able to see more operations.
Moodle

createdby and modifiedby assigned to wrong people when course restored to another site

Created: 23/Sep/08 01:06 AM   Updated: 20/Jan/09 12:16 PM
Return to search
Component/s: Backup, Questions
Affects Version/s: 1.9.2
Fix Version/s: 1.9.4

File Attachments: 1. Text File backupsamesite.patch.txt (15 kB)
2. Text File backupsamesite.patch.txt (15 kB)
3. Text File questioncreatormodifierrestore.patch.txt (2 kB)

Issue Links:
Duplicate
 

Database: Any
Participants: Eloy Lafuente (stronk7), Martin Dougiamas, Przemyslaw Stencel and Tim Hunt
Security Level: None
Resolved date: 20/Jan/09
Affected Branches: MOODLE_19_STABLE
Fixed Branches: MOODLE_19_STABLE


 Description  « Hide
If I make a backup of a course and then restore that backup into another moodle site, the questions get assigned to a wrong person. Backup file (moodle.xml) includes the following info for each question:
            <CREATEDBY>xyz</CREATEDBY>
            <MODIFIEDBY>xyz</MODIFIEDBY>
where "xyz" is the id number of the user who created or modified the question. The problem is, if you restore the backup to another moodle site, user xyz is a totally different person (!)

I suggest that if a backup is restored to another site (it can easily be detected during the restore process by comparing <ORIGINAL_WWWROOT> from the moodle xml file to the current www root), both values (CREATEDBY and MODIFIEDBY) should be set to 0 (unknown) or 1 (admin user)

 All   Comments   Change History   Version Control      Sort Order: Ascending order - Click to sort in descending order
Przemyslaw Stencel added a comment - 23/Sep/08 03:53 PM
Of course I meant "... or 2 (admin user)"


Eloy Lafuente (stronk7) added a comment - 24/Sep/08 08:09 AM
Assigning to Tim Hunt... thanks!

Eloy Lafuente (stronk7) made changes - 24/Sep/08 08:09 AM
Field Original Value New Value
Assignee Eloy Lafuente (stronk7) [ stronk7 ] Tim Hunt [ timhunt ]
Tim Hunt made changes - 15/Jan/09 01:41 AM
Fix Version/s 1.9.4 [ 10300 ]
Przemyslaw Stencel added a comment - 15/Jan/09 04:29 AM
Tim, I see you've scheduled this issue to be fixed in 1.9.4. How are you planning to implement this? Will CREATEDBY and MODIFIEDBY be set to unknown or to admin? Or are you planning a different fix?

Best,
Przemek


Tim Hunt added a comment - 15/Jan/09 12:48 PM
I am only planning to fix what happens for future restores.

I don't think there is anything I can do about bad data already in the database. Well, if createdby or modifiedby point to a non-existent user, then I will reset it to NULL, but where they point to an actual user, there is no way for Moodle to know if that is the correct information of the result of a bad restore in the past.

As admin, you could get a list of question_category ids that came from bad backups, and then manually do a database query to fix them. Something like:

UPDATE mdl_question SET createdby = NULL, modifiedby = NULL WHERE category IN (123, 234, 345, ...);

using a tool like phpMyAdmin. However, messing around directly in the DB is not something to be undertaken lightly.


Tim Hunt made changes - 15/Jan/09 02:37 PM
Link This issue is duplicated by MDL-17636 [ MDL-17636 ]
Tim Hunt added a comment - 15/Jan/09 02:40 PM
I'm looking for comments on this patch, particularly the advisability of the

} else if ($restore->original_wwwroot == $CFG->wwwroot) {

lines. Thanks.


Tim Hunt made changes - 15/Jan/09 02:40 PM
Przemyslaw Stencel added a comment - 15/Jan/09 05:11 PM
> I am only planning to fix what happens for future restores.

Yes, I understand that My question was about future restores.

I just wanted to know which of the following you are planning to implement as a fix for this issue:

1/ if during the restore process the original wwwroot is different than the wwwroot of the target site, then CREATEDBY and MODIFIEDBY will be set to unknown

OR

2/ if during the restore process the original wwwroot is different than the wwwroot of the target site, then CREATEDBY and MODIFIEDBY will be set to admin


Tim Hunt added a comment - 15/Jan/09 06:27 PM
Neither, read the patch.

If the backup includes users, then we will update the fields to point to the new userid of the user who created/modified the question. If the user is not in the file, we set it to null, unless $restore->original_wwwroot == $CFG->wwwroot.


Przemyslaw Stencel added a comment - 15/Jan/09 06:39 PM
Thanks for the explanation.

Tim Hunt added a comment - 16/Jan/09 06:20 PM
Eloy, please could you review my patch. It is only a small one and I would like to get this checked in. Thanks.

Tim Hunt added a comment - 16/Jan/09 07:22 PM
Right, following discussion with Eloy and Nico in developer chat:

The if ($restore->original_wwwroot == $CFG->wwwroot) is dangerous because of this scenario:

1. Admin backs up everything important in their Moodle 1.9.
2. wipes their server.
3. Installs Moodle 1.9.4.
4. Restore just the bits they want, in order to have a nice clean DB.

In this case $restore->original_wwwroot == $CFG->wwwroot but it is a different moodle.

However, there is a $CFG->siteidentifier, which is initialised on install to random_string(32).$_SERVER['HTTP_HOST']. It is used for registration, and a couple of other places. Unfortunately, at the moment, this is not stored in the backup file.

So, the plan is:

1. Change backup, so $CFG->siteidentifier is stored in every backup file.
2. Change restore so that the siteidentifier from the backup file is available during restore, as $restore->originalsiteidentifier.
3. Write a restore_is_same_site($restore) function that returns true or false to let you know if the backup file came from this site. If this is an old backup that does not include siteidentifier, then restore_is_same_site will return false.
4. Use restore_is_same_site($restore) in my code.
5. Search the rest of the restore code (briefly) to see if there are any other hacks that should be replaced by restore_is_same_site.

Hopefully I will be able to do this on Monday.


Tim Hunt added a comment - 19/Jan/09 01:16 PM
I'm looking for a code review on my revised patch: backupsamesite.patch.txt

I was suprised how many places were already using a nasty hack to see if this was the same site.

I also refactored the three or four places where $CFG->siteidentifier was initialised, so they all called a function.


Tim Hunt made changes - 19/Jan/09 01:16 PM
Attachment backupsamesite.patch.txt [ 16085 ]
Tim Hunt added a comment - 19/Jan/09 01:20 PM
Hmm. For security reasons, should I only put md5($CFG->siteidentifier) into the backup file, rather than the plain $CFG->siteidentifier?

Otherwise, people may be able to hijack each-other's site registrations.


Martin Dougiamas added a comment - 19/Jan/09 06:34 PM
Can we squeeze this into 1.9.4, Eloy and Petr?

Tim Hunt added a comment - 19/Jan/09 06:48 PM
I decided that the MD5 hashing was necessary, so attached is a revised patch for review. Would be nice to get it in to this week's weekly build, but I would really like a code review first, please.

Tim Hunt made changes - 19/Jan/09 06:48 PM
Attachment backupsamesite.patch.txt [ 16089 ]
Eloy Lafuente (stronk7) added a comment - 20/Jan/09 07:11 AM
About the backupsamesite.patch.txt, perfect IMO! Hashing the site identifier sounds like a great idea and "same site" detection will be now much better, being 100% compatible with old backups (where detection only can be done with wwwroot).

Also the questions part is ok, with those 2 lines we talked deleted. Perfect!

+1 for stable


Tim Hunt added a comment - 20/Jan/09 09:21 AM
Thanks Eloy. Will commit when I get to work in about 20 minutes.

tjhunt committed 10 files to 'Moodle CVS' on branch 'MOODLE_19_STABLE' - 20/Jan/09 12:16 PM
backup/restore: MDL-16614 more reliable test for when we are restoring a backup that was made of the same site that we are restoring to.

* Use this to fix a question restore bug.
* Replace older code that does something similar with the new test.
* Refactor initialisation of $CFG->siteidentifier into a function. There were about 4 copies of this code ;-)
MODIFY admin/index.php   Rev. 1.286.2.28    (+3 -5 lines)
MODIFY backup/backuplib.php   Rev. 1.179.2.37    (+3 -1 lines)
MODIFY backup/restorelib.php   Rev. 1.283.2.61    (+18 -6 lines)
MODIFY backup/restore_check.html   Rev. 1.48.2.11    (+2 -2 lines)
MODIFY admin/register.php   Rev. 1.25.2.3    (+2 -8 lines)
MODIFY question/restorelib.php   Rev. 1.30.2.6    (+23 -3 lines)
MODIFY backup/restore_execute.html   Rev. 1.62.4.3    (+5 -1 lines)
MODIFY backup/lib.php   Rev. 1.89.2.8    (+20 -1 lines)
MODIFY lang/en_utf8/moodle.php   Rev. 1.141.2.54    (+2 -1 lines)
MODIFY lib/moodlelib.php   Rev. 1.960.2.115    (+15 -7 lines)
tjhunt committed 10 files to 'Moodle CVS' - 20/Jan/09 12:16 PM
backup/restore: MDL-16614 more reliable test for when we are restoring a backup that was made of the same site that we are restoring to.

* Use this to fix a question restore bug.
* Replace older code that does something similar with the new test.
* Refactor initialisation of $CFG->siteidentifier into a function. There were about 4 copies of this code ;-)
MODIFY backup/backuplib.php   Rev. 1.226    (+3 -1 lines)
MODIFY backup/restore_execute.html   Rev. 1.68    (+5 -1 lines)
MODIFY admin/register.php   Rev. 1.32    (+3 -7 lines)
MODIFY backup/restore_check.html   Rev. 1.65    (+2 -2 lines)
MODIFY lang/en_utf8/moodle.php   Rev. 1.224    (+2 -1 lines)
MODIFY lib/moodlelib.php   Rev. 1.1159    (+15 -7 lines)
MODIFY backup/lib.php   Rev. 1.111    (+20 -1 lines)
MODIFY question/restorelib.php   Rev. 1.41    (+23 -3 lines)
MODIFY admin/index.php   Rev. 1.359    (+1 -3 lines)
MODIFY backup/restorelib.php   Rev. 1.365    (+18 -6 lines)
Tim Hunt added a comment - 20/Jan/09 12:16 PM
I know it is testing day, but this code has been written for a few days, was just waiting for review, so I committed it anyway. Thanks Eloy and others for your help.

Tim Hunt made changes - 20/Jan/09 12:16 PM
Status Open [ 1 ] Resolved [ 5 ]
Resolution Fixed [ 1 ]
Mitsuhiro Yoshida committed 3 files to 'Lang CVS' - 23/Jan/09 01:33 AM
MDL-16614 Translated a new string for restoring backups.
MDL-17871 Translated new strings for question bank.
Brushed up translated strings fro LDAP enrollment.
MODIFY ja_utf8/README   Rev. 1.923    (+1 -1 lines)
MODIFY ja_utf8/moodle.php   Rev. 1.344    (+4 -1 lines)
MODIFY ja_utf8/enrol_ldap.php   Rev. 1.15    (+6 -10 lines)
martignoni committed 1 file to 'Lang CVS' - 24/Jan/09 04:46 AM
MDL-16614 String added
MODIFY fr_utf8/moodle.php   Rev. 1.276    (+7 -6 lines)