public function testCreateMap() { $p = new RingsidePrincipal(); $p->id = -1; //strange but it works! $this->assertTrue($p->trySave()); $pid = $p->getIncremented(); $map = new RingsidePrincipalMap(); $map->principal_id = $pid; $map->app_id = 123; $map->network_id = 'abcd'; $map->uid = 1234; $map->save(); $subject_map = Doctrine::getTable('RingsidePrincipalMap')->findOneBySubject(123, 'abcd', 1234); $this->assertNotNull($subject_map); $this->assertEquals($p->id, $subject_map->principal_id); }
public function link($app_id, $existing_network_id, $existing_uid, $new_network_id, $new_uid) { $conn = $this->getManager()->getConnectionForComponent(self::PRINCIPAL_MAP_COMPONENT); $conn->beginTransaction(); $manager = $this->getManager(); try { // Find a mapping for either (or both!) subject(s) $existing_map = $this->tables[self::PRINCIPAL_MAP_COMPONENT]->findOneBySubject($app_id, $existing_network_id, $existing_uid); $new_map = $this->tables[self::PRINCIPAL_MAP_COMPONENT]->findOneBySubject($app_id, $new_network_id, $new_uid); $existing_principal_id = isset($existing_map) && false !== $existing_map ? $existing_map->principal_id : null; $new_principal_id = isset($new_map) && false !== $new_map ? $new_map->principal_id : null; if (null != $new_principal_id) { if (null == $existing_principal_id) { error_log("Existing principal is {$existing_principal_id} and new principal is {$new_principal_id} (reversing them)"); // There is a new_principal_id, but there is no existing_principal_id; swap them to match the semantics of this call $hold_principal_id = $new_principal_id; $hold_network_id = $new_network_id; $hold_uid = $new_uid; $new_principal_id = $existing_principal_id; $new_network_id = $existing_network_id; $new_uid = $existing_uid; $existing_principal_id = $hold_principal_id; $existing_network_id = $hold_network_id; $existing_uid = $hold_uid; } } /* * The state at this point is that we have ensured that existing_principal_id exists (if there is any principal) * and is set to the proper value. Otherwise, existing_principal_id will be empty, meaning we need to create a principal. */ if (null == $existing_principal_id) { error_log("For {$existing_principal_id} and {$new_principal_id} ({$existing_network_id}, {$existing_uid}, {$new_network_id}, {$new_uid})"); // Since we already have a transaction started, don't start a new one; use the internal method $existing_principal_id = $this->_createPrincipal($app_id, $existing_network_id, $existing_uid); } $map = new RingsidePrincipalMap(); $map->app_id = $app_id; $map->principal_id = $existing_principal_id; $map->network_id = $new_network_id; $map->uid = $new_uid; $map->save(); // Note the notification happens if (!empty($new_principal_id) && $new_principal_id != $existing_principal_id) { // TODO: Send a notification to the application that a mapping change has occurred } $conn->commit(); return $existing_principal_id; } catch (Exception $e) { $conn->rollback(); throw $e; } }