/** * @param string $path * * @param module\User $owner * * @return File */ public function createFile($path, User $owner) { $document = new \stdClass(); $document->_id = 'stack:/' . $path; $document->permissions = []; $document->owner = $owner->getUname(); $file = new File($document, new Module_Default(new \stdClass())); $this->storeFile($file); return $file; }
/** * @param File $file * @param string $priviledge * * @return bool */ protected function checkPermissions($file, $priviledge) { if (isset($this->user)) { // check if current user is owner of the file if ($file->getOwner() == $this->user->getUname()) { return true; } } foreach ($file->getPermissions() as $permission) { if ($permission->priviledge != $priviledge) { // provided priviledge is not requested continue; } if ($permission->context == Security_Permission::CONTEXT_ALL) { // provided priviledge applies to everyone return true; } if (!isset($this->user)) { // no user is set, can't check for user / group permissions continue; } if ($permission->context == Security_Permission::CONTEXT_USER && $permission->subject == $this->user->getUname()) { // permission is explicitly granted for this user return true; } if ($permission->context == Security_Permission::CONTEXT_GROUP && in_array($permission->subject, $this->user->getGroups())) { // permission is granted for all users in group return true; } } return false; }
/** * Make sure createFile works as intended */ public function testCRUD() { $cabinet = $this->createCabinet(); $owner = User::create('testUser'); // create a file $created = $cabinet->createFile('/gnark', $owner); // fetch it from the cabinet $read = $cabinet->fetchFile('/gnark'); // make sure both have the same path $this->assertEquals($created->getPath(), $read->getPath()); // add some data to the read file $read->getModule()->getData()->foo = 'bar'; $cabinet->storeFile($read); // add some more data, save again $read->getModule()->getData()->qux = 'kos'; $cabinet->storeFile($read); // read again $readAgain = $cabinet->fetchFile('/gnark'); $this->assertEquals($readAgain->getModule()->getData()->foo, 'bar'); $this->assertEquals($readAgain->getModule()->getData()->qux, 'kos'); // delete file, make sure it's gone $cabinet->deleteFile($readAgain); $this->assertFalse($cabinet->fileExists('/gnark')); }
protected function createDummyUser() { return User::create('dummy'); }