public function Update(BookableResource $resource) { if (!$this->user->IsAdmin) { $user = $this->repo->LoadById($this->user->UserId); if (!$user->IsResourceAdminFor($resource)) { // if we got to this point, the user does not have the ability to update the resource throw new Exception(sprintf('Resource Update Failed. User %s does not have admin access to resource %s.', $this->user->UserId, $resource->GetId())); } } parent::Update($resource); }
public function testUpdatesAttributes() { $id = 11; $unchanged = new AttributeValue(1, 'value'); $toChange = new AttributeValue(2, 'value'); $toAdd = new AttributeValue(3, 'value'); $resource = new FakeBookableResource($id); $resource->WithAttribute($unchanged); $resource->WithAttribute(new AttributeValue(100, 'should be removed')); $resource->WithAttribute(new AttributeValue(2, 'new value')); $attributes = array($unchanged, $toChange, $toAdd); $resource->ChangeAttributes($attributes); $this->repository->Update($resource); $addNewCommand = new AddAttributeValueCommand($toAdd->AttributeId, $toAdd->Value, $id, CustomAttributeCategory::RESOURCE); $removeOldCommand = new RemoveAttributeValueCommand(100, $id); $removeUpdated = new RemoveAttributeValueCommand($toChange->AttributeId, $id); $addUpdated = new AddAttributeValueCommand($toChange->AttributeId, $toChange->Value, $id, CustomAttributeCategory::RESOURCE); $this->assertEquals($removeOldCommand, $this->db->_Commands[1]); $this->assertEquals($removeUpdated, $this->db->_Commands[2], "need to remove before adding to make sure changed values are not immediately deleted"); $this->assertEquals($addUpdated, $this->db->_Commands[3]); $this->assertEquals($addNewCommand, $this->db->_Commands[4]); }