Ejemplo n.º 1
0
 /**
  * Ensures provided datasource is containing data sets required for backing
  * roles management.
  *
  * @param db $source
  * @param boolean $force true to force validating provided source
  * @return db validated datasource provided in $source
  * @throws \RuntimeException
  */
 public static function validateDatasource(db $source = null, $force = false)
 {
     if ($source === null) {
         $source = datasource::getDefault();
     }
     if (!self::$validated || $force) {
         if (!$source->exists('role')) {
             if (false === $source->createDataset('role', array('name' => 'CHAR(32) NOT NULL UNIQUE', 'label' => 'CHAR(64) NOT NULL'))) {
                 throw new \RuntimeException('failed to prepare role set');
             }
         }
         if (!$source->exists('user_role')) {
             if (false === $source->createDataset('user_role', array('id' => null, 'user_uuid' => 'CHAR(36) NOT NULL', 'role_id' => 'INT UNSIGNED NOT NULL'), array('user_uuid', 'role_id'))) {
                 throw new \RuntimeException('failed to prepare role/user mapping set');
             }
         }
         self::$validated = true;
     }
     return $source;
 }
Ejemplo n.º 2
0
 /**
  * Creates query for browsing model's items stored in provided datasource.
  *
  * @param connection $source datasource containing model's items, omit for using default datasource
  * @param string $alias optional name of alias to explicitly use on model's data set in retrieved query
  * @return query query for listing items of current model
  */
 public static function browse(connection $source = null, $alias = null)
 {
     if ($source === null) {
         $source = datasource::getDefault();
     }
     if (!$source instanceof connection) {
         throw new \InvalidArgumentException(\de\toxa\txf\_L('missing link to datasource'));
     }
     static::updateSchema($source);
     $setName = preg_replace('/\\s+/', ' ', trim(static::$set_prefix . static::$set));
     if ($alias) {
         $parts = explode(' ', $setName);
         if (!is_string($alias)) {
             throw new \InvalidArgumentException('invalid type of alias');
         }
         $alias = explode(' ', preg_replace('/\\s+/', ' ', trim($alias)));
         $setName = $parts[0] . ' ' . $alias[0];
     }
     return $source->createQuery($setName);
 }
Ejemplo n.º 3
0
 /**
  * Retrieves connection to configured datasource containing users database.
  *
  * @throws \Exception
  * @return datasource\connection connection to datasource
  */
 public function datasource()
 {
     if (!is_array($this->configuration)) {
         throw new \RuntimeException(_L('Missing user source configuration.'));
     }
     $conf = $this->configuration;
     $hash = sha1(serialize($conf));
     if (!array_key_exists($hash, self::$datasources)) {
         // gain access on datasource configured to contain users
         if ($conf['datasource']) {
             $ds = datasource::selectConfigured($conf['datasource']);
         } else {
             $ds = datasource::selectConfigured('default');
         }
         if (!$ds instanceof datasource\pdo) {
             throw new \UnexpectedValueException(_L('Unsupported kind of datasource for managing users.'));
         }
         // apply optionally configured mapping of a user's properties
         $definition = array('uuid' => 'CHAR(36) NOT NULL', 'loginname' => 'CHAR(64) NOT NULL', 'password' => 'CHAR(128) NOT NULL', 'name' => 'CHAR(128)', 'lock' => 'CHAR(128)', 'email' => 'CHAR(128)');
         $mappedDefinition = name_mapping::map($definition, 'txf.sql_user');
         // create data set in datasource on demand
         if (!$ds->createDataset($conf['set'], $mappedDefinition)) {
             throw $ds->exception(_L('failed to create dataset for managing users'));
         }
         // ensure to have a single user at least by default
         if (!intval($ds->createQuery($conf['set'])->execute(true)->cell())) {
             $record = name_mapping::map(array('uuid' => uuid::createRandom(), 'loginname' => 'admin', 'password' => blowfish::get('nimda'), 'name' => _L('Administrator'), 'lock' => '', 'email' => ''), 'txf.sql_user');
             $currentUser = $this;
             $ds->transaction()->wrap(function (datasource\connection $conn) use($record, $conf, $currentUser) {
                 $names = array_map(function ($n) use($conn) {
                     return $conn->quoteName($n);
                 }, array_keys($record));
                 $markers = array_map(function () {
                     return '?';
                 }, $record);
                 $newUserID = $conn->nextID($conf['set']);
                 $values = array_values($record);
                 array_unshift($values, $newUserID);
                 $sql = sprintf('INSERT INTO %s (id,%s) VALUES (?,%s)', $conn->qualifyDatasetName($conf['set']), implode(',', $names), implode(',', $markers));
                 if (!$conn->test($sql, $values)) {
                     throw $conn->exception(_L('failed to create default user'));
                 }
                 // load created user for adopting administrator role
                 sql_role::select($conn, 'administrator')->makeAdoptedBy(user::load($newUserID));
                 return true;
             });
         }
         self::$datasources[$hash] = $ds;
     }
     return self::$datasources[$hash];
 }
Ejemplo n.º 4
0
 protected function __construct(connection $datasource = null, $formName = null)
 {
     if ($datasource === null) {
         $datasource = datasource::selectConfigured('default');
     }
     $this->datasource = $datasource;
     $this->formName = \de\toxa\txf\_1($formName, 'model_editor');
 }