/** * Fetch apine_user_groups by user * * @param integer $user * User id * @return Collection */ public static function create_by_user($user) { $database = new Database(); $request = $database->select("SELECT `group_id` FROM `apine_users_user_groups` WHERE `user_id`={$user}"); $liste = new Collection(); if ($request != null && count($request) > 0) { foreach ($request as $item) { $liste->add_item(new Apine\User\UserGroup((int) $item['group_id'])); } } return $liste; }
/** * Authentifiate a user with a combination of a user name and an * encoded password. * * @param string $name * Username * @param string $pass * Encrypted password * @return integer */ public static function authentication($name, $pass) { $database = new Apine\Core\Database(); $connect_sql_id = $database->prepare('SELECT `id` FROM `apine_users` WHERE ( `username`=? OR `email`=? ) AND `password`=? AND `type`<>10'); $ar_connect_sql = $database->execute(array($name, $name, $pass), $connect_sql_id); if ($ar_connect_sql) { $connect = end($ar_connect_sql); $connect = $connect['id']; } else { $connect = 0; // Value of false } return $connect; }
/** * Import APIne's table in the database * * @param array $entries * @throws Exception * @throws GenericException */ private function import_database($entries) { try { $database = new Database($entries['type'], $entries['host'], $entries['dbname'], $entries['username'], $entries['password'], $entries['charset']); $sql_file = file_get_contents($this->parent . '/Installation/apine_sql_tables.sql'); $result = $database->exec($sql_file); if ($result === false) { throw new \Exception('Cannot import database tables'); } } catch (DatabaseException $e) { throw new GenericException($e->getMessage(), $e->getCode(), $e); } }
/** * Fetch a password token by token string * * @param string $a_token * @return Apine\User\PasswordToken */ public static function create_by_token($a_token) { $database = new Apine\Core\Database(); $user_sql_id = $database->prepare('SELECT `id` FROM `apine_password_tokens` WHERE `token` = ?'); $ar_user_sql = $database->execute(array($a_token), $user_sql_id); if ($ar_user_sql) { $return = new Apine\User\PasswordToken((int) $ar_user_sql[0]['id']); } else { $return = null; } return $return; }
/** * Authenticate a user with a combination of a user name and a * token string. * * @param string $a_name * Username * @param string $a_token * Token string * @param int $a_delay * @return boolean */ public static function authentication($a_name, $a_token, $a_delay) { $user = UserFactory::create_by_name($a_name); if (!is_null($user)) { $database = new Database(); $token_statement_id = $database->prepare('SELECT `id` FROM `apine_api_users_tokens` WHERE `user_id` = ? AND `token` = ? AND `last_access_date` > ? AND `disabled` = false'); $ar_token = $database->execute(array($user->get_id(), $a_token, date('d M Y H:i:s', time() - $a_delay)), $token_statement_id); if ($ar_token) { $connect = end($ar_token); $return = (int) $connect['id']; } else { $return = false; } } else { $return = false; } return $return; }
/** * @see EntityInterface::delete() */ public function delete() { if ($this->loaded == 0) { $this->load(); } $db = new Apine\Core\Database(); $db->delete('apine_users_user_groups', array("user_id" => $this->get_id())); parent::_destroy(); }
/** * Save Entity state to database */ protected final function _save() { $db = new Database(); if ($this->id === null) { $this->field_loaded = 0; } if ($this->field_loaded == 0) { // This is a new or unloaded entity $new_dbf = array(); if (!empty($this->database_fields)) { foreach ($this->database_fields as $field => $val) { if (!is_numeric($field)) { $new_dbf[$field] = $val; } } } if (sizeof($new_dbf) > 0) { $this->id = $db->insert($this->table_name, $new_dbf); } $this->_load(); } else { // This is an already existing entity // Update procedure only executed if at least // one field was modified if (count($this->modified_fields) > 0) { $arUpdate = array(); foreach ($this->database_fields as $key => $value) { if (!is_numeric($key)) { if (isset($this->modified_fields[$key]) && $this->modified_fields[$key] == true) { $arUpdate[$key] = $value; } } } $db->update($this->table_name, $arUpdate, array($this->load_field => $this->id)); } } }