/** * Create new entity with parsed entityid * * Create a new entity and give the user access to the entity. * * @param string $entityid Entity id for the new entity * @param string $type Entity type * * @return sspmod_janus_Entity|bool Returns the entity or false on error. * @since Method available since Release 1.0.0 */ public function createNewEntity($entityid, $type) { assert('is_string($entityid)'); assert('is_string($type)'); if ($this->isEntityIdInUse($entityid, $errorMessage)) { return $errorMessage; } if ($this->hasEntityIdBeenUsed($entityid, $errorMessage)) { return $errorMessage; } $startstate = $this->_config->getString('workflowstate.default'); // Get the default ARP $default_arp = '0'; $st = $this->execute("SELECT aid FROM " . self::$prefix . "arp WHERE is_default = TRUE AND deleted = ''"); if ($st) { $rows = $st->fetchAll(); if (count($rows) === 1) { $default_arp = $rows[0]['aid']; } } // Instantiate a new entity $entity = new sspmod_janus_Entity($this->_config, true); $entity->setEntityid($entityid); $entity->setWorkflow($startstate); $entity->setType($type); $entity->setArp($default_arp); $entity->setUser($this->_user->getUid()); $entity->setRevisionnote('Entity created.'); $entity->save(); $st = $this->execute('INSERT INTO ' . self::$prefix . 'hasEntity (`uid`, `eid`, `created`, `ip`) VALUES (?, ?, ?, ?);', array($this->_user->getUid(), $entity->getEid(), date('c'), $_SERVER['REMOTE_ADDR'])); if ($st === false) { return 'error_db'; } $ec = new sspmod_janus_EntityController($this->_config); $ec->setEntity($entity); $update = false; // Get metadatafields for new type $nm_mb = new sspmod_janus_MetadatafieldBuilder($this->_config->getArray('metadatafields.' . $type)); $metadatafields = $nm_mb->getMetadatafields(); // Add all required fileds foreach ($metadatafields as $mf) { if (isset($mf->required) && $mf->required === true) { $ec->addMetadata($mf->name, $mf->default); $update = true; } } if ($update === true) { $ec->saveEntity(); } // Reset list of entities $this->_entities = null; $this->_loadEntities(); return $entity->getEid(); }
/** * Retrieve all Eids for entities of a certain type. * * @param String $type The type of entity, e.g. "saml20-idp" * @return array all entities that have been found */ public function searchEntitiesByType($type) { $deployableWorkflowStateList = $this->_loadDeployableWorkflowStates(); $query = "\n SELECT CONNECTION_REVISION.`eid`\n ,CONNECTION_REVISION.`revisionid`\n ,CONNECTION_REVISION.`entityid`\n ,CONNECTION_REVISION.`state`\n FROM " . $this->getTablePrefix() . "connection AS CONNECTION\n INNER JOIN " . $this->getTablePrefix() . "connectionRevision AS CONNECTION_REVISION\n ON CONNECTION_REVISION.eid = CONNECTION.id\n AND CONNECTION_REVISION.revisionid = CONNECTION.revisionNr\n WHERE CONNECTION.`type` = ?\n "; $queryVariables = array($type); // Add deployabe state check $nrOfWorkflowStates = count($deployableWorkflowStateList); $fWorkflowStateInPlaceholders = substr(str_repeat('?,', $nrOfWorkflowStates), 0, -1); $query .= " AND CONNECTION_REVISION.`state` IN(" . $fWorkflowStateInPlaceholders . ")"; $queryVariables = array_merge($queryVariables, $deployableWorkflowStateList); $st = $this->execute($query, $queryVariables); if ($st === false) { return 'error_db'; } $this->_entities = array(); $rows = $st->fetchAll(PDO::FETCH_ASSOC); foreach ($rows as $row) { $entity = new sspmod_janus_Entity($this->_config); $entity->setEid($row['eid']); $entity->setRevisionid($row['revisionid']); $entity->setWorkflow($row['state']); if ($entity->load()) { $this->_entities[] = $entity; } else { SimpleSAML_Logger::error('JANUS:UserController:searchEntitiesByType - Entity could not be loaded, eid: ' . $row['eid']); } } return $this->_entities; }