mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
MINOR Moving File->OwnerID setting to model layer
This commit is contained in:
parent
7718c75e3a
commit
1b4dda491a
@ -351,6 +351,9 @@ class File extends DataObject {
|
||||
protected function onBeforeWrite() {
|
||||
parent::onBeforeWrite();
|
||||
|
||||
// Set default owner
|
||||
if(!$this->ID) $this->OwnerID = (Member::currentUser() ? Member::currentUser()->ID : 0);
|
||||
|
||||
// Set default name
|
||||
if(!$this->getField('Name')) $this->Name = "new-" . strtolower($this->class);
|
||||
}
|
||||
|
@ -200,7 +200,6 @@ class FileIFrameField extends FileField {
|
||||
|
||||
$this->form->getRecord()->{$this->getName() . 'ID'} = $fileObject->ID;
|
||||
|
||||
$fileObject->OwnerID = (Member::currentUser() ? Member::currentUser()->ID : 0);
|
||||
$fileObject->write();
|
||||
}
|
||||
|
||||
|
@ -447,7 +447,6 @@ class UploadField extends FileField {
|
||||
$return['error'] = implode(' '.PHP_EOL, $this->upload->getErrors());
|
||||
} else {
|
||||
$file = $this->upload->getFile();
|
||||
$file->OwnerID = (Member::currentUser() ? Member::currentUser()->ID : 0);
|
||||
$file->write();
|
||||
$this->attachFile($file);
|
||||
$file = $this->customiseFile($file);
|
||||
|
@ -295,6 +295,28 @@ class FileTest extends SapphireTest {
|
||||
|
||||
File::$class_for_file_extension = $orig;
|
||||
}
|
||||
|
||||
function testSetsOwnerOnFirstWrite() {
|
||||
Session::set('loggedInAs', null);
|
||||
$member1 = new Member();
|
||||
$member1->write();
|
||||
$member2 = new Member();
|
||||
$member2->write();
|
||||
|
||||
$file1 = new File();
|
||||
$file1->write();
|
||||
$this->assertEquals(0, $file1->OwnerID, 'Owner not written when no user is logged in');
|
||||
|
||||
$member1->logIn();
|
||||
$file2 = new File();
|
||||
$file2->write();
|
||||
$this->assertEquals($member1->ID, $file2->OwnerID, 'Owner written when user is logged in');
|
||||
|
||||
$member2->logIn();
|
||||
$file2->forceChange();
|
||||
$file2->write();
|
||||
$this->assertEquals($member1->ID, $file2->OwnerID, 'Owner not overwritten on existing files');
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user