Skip to content
Browse files

MINOR Moving File->OwnerID setting to model layer

  • Loading branch information...
1 parent 7718c75 commit 1b4dda491a74e50986ae5b51ab42bdc3c2a64147 @chillu chillu committed
Showing with 25 additions and 2 deletions.
  1. +3 −0 filesystem/File.php
  2. +0 −1 forms/FileIFrameField.php
  3. +0 −1 forms/UploadField.php
  4. +22 −0 tests/filesystem/FileTest.php
View
3 filesystem/File.php
@@ -351,6 +351,9 @@ public function deleteDatabaseOnly() {
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);
}
View
1 forms/FileIFrameField.php
@@ -200,7 +200,6 @@ public function save($data, $form) {
$this->form->getRecord()->{$this->getName() . 'ID'} = $fileObject->ID;
- $fileObject->OwnerID = (Member::currentUser() ? Member::currentUser()->ID : 0);
$fileObject->write();
}
View
1 forms/UploadField.php
@@ -447,7 +447,6 @@ public function upload(SS_HTTPRequest $request) {
$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);
View
22 tests/filesystem/FileTest.php
@@ -295,6 +295,28 @@ function testFolderConstructChild() {
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');
+ }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////

0 comments on commit 1b4dda4

Please sign in to comment.
Something went wrong with that request. Please try again.