From 2eeb80accc663c7445564be1fd362f546cef9f19 Mon Sep 17 00:00:00 2001 From: Benjamin Sonntag Date: Thu, 19 Mar 2015 22:59:04 +0100 Subject: [PATCH] adding MySQL, fixing Mail functions, adding LOGIN to mail function --- lib/Alternc/Api/Object/Mail.php | 26 ++++ lib/Alternc/Api/Object/Mysql.php | 219 +++++++++++++++++++++++++++++++ 2 files changed, 245 insertions(+) create mode 100644 lib/Alternc/Api/Object/Mysql.php diff --git a/lib/Alternc/Api/Object/Mail.php b/lib/Alternc/Api/Object/Mail.php index 0031243e..65c25955 100644 --- a/lib/Alternc/Api/Object/Mail.php +++ b/lib/Alternc/Api/Object/Mail.php @@ -306,6 +306,32 @@ class Alternc_Api_Object_Mail extends Alternc_Api_Legacyobject { } } + + /** API Method for email authentication + * @param $options a hash with email and password parameters. + * @return Alternc_Api_Response whose content is true / false if the auth + * to this email was successfull. + */ + function login($options) { + global $cuid; + $uid = $cuid; + if ($this->isAdmin && isset($options["uid"])) { + $uid = intval($options["uid"]); + } + if (!isset($options["email"]) || !isset($options["password"])) { + return new Alternc_Api_Response(array("code" => self::ERR_INVALID_ARGUMENT, "message" => "Missing email or password argument")); + } + list($address,$domain)=explode("@",$options["email"],2); + $stmt = $this->db->prepare("SELECT enabled FROM domaines d,address a WHERE a.domain_id=d.id AND address=? AND domaine=? AND password=encrypt(?,password);"); + $stmt->execute(array($address,$domain,$options["password"])); + $me = $stmt->fetch(PDO::FETCH_OBJ); + if ($me && $me->enabled) { + return new Alternc_Api_Response(array("content" => true)); + } else { + return new Alternc_Api_Response(array("content" => false)); + } + } + } // class Alternc_Api_Object_Mail diff --git a/lib/Alternc/Api/Object/Mysql.php b/lib/Alternc/Api/Object/Mysql.php new file mode 100644 index 00000000..bdacb47d --- /dev/null +++ b/lib/Alternc/Api/Object/Mysql.php @@ -0,0 +1,219 @@ +cuid = $cuid = $service->token->uid; + $this->isAdmin = $service->token->isAdmin; + // We use the global $admin from AlternC legacy classes + $this->admin = $admin; + $this->mysql = $mysql; + // Set the legacy rights: + $this->admin->enabled = $this->isAdmin; + } + + /** API Method from legacy class method admin->add_mem() + * @param $options a hash with parameters transmitted to legacy call + * mandatory parameters: login, pass, nom, prenom, mail, + * non-mandatory: canpass, type, duration, notes, force, create_dom, db_server_id + * @return Alternc_Api_Response whose content is the newly created UID + */ + function add($options) { + $mandatory = array("login", "pass", "nom", "prenom", "mail"); + $defaults = array("canpass" => 1, "type" => "default", "duration" => 0, "notes" => "", "force" => 0, "create_dom" => ""); + $missing = ""; + foreach ($mandatory as $key) { + if (!isset($options[$key])) { + $missing.=$key . " "; + } + } + if ($missing) { + return new Alternc_Api_Response(array("code" => self::ERR_INVALID_ARGUMENT, "message" => "Missing or invalid argument: " . $missing)); + } + foreach ($defaults as $key => $value) { + if (!isset($options[$key])) { + $options[$key] = $value; + } + } + if (!isset($options["db_server_id"])) { + $stmt = $this->db->prepare("SELECT MIN(db_servers.id) AS id FROM db_servers;"); + $stmt->execute(); + $me = $stmt->fetch(PDO::FETCH_OBJ); + $options["db_server_id"] = $me->id; + } + $uid = $this->admin->add_mem($options["login"], $options["pass"], $options["nom"], $options["prenom"], $options["mail"], $options["canpass"], $options["type"], $options["duration"], $options["notes"], $options["force"], $options["create_dom"], $options["db_server_id"]); + if (!$uid) { + return $this->alterncLegacyErrorManager(); + } else { + return new Alternc_Api_Response(array("content" => $uid)); + } + } + + /** API Method from legacy class method admin->update_mem() + * @param $options a hash with parameters transmitted to legacy call + * mandatory parameters: nom, prenom, mail, + * non-mandatory: pass, canpass, type, duration, notes, force, create_dom, db_server_id + * @return Alternc_Api_Response whose content is the updated UID + */ + function update($options) { + $defaults = array("nom", "prenom", "mail", "canpass", "enabled", "type", "duration", "notes"); + if (!isset($options["uid"])) { + return new Alternc_Api_Response(array("code" => self::ERR_INVALID_ARGUMENT, "message" => "Missing or invalid argument: UID")); + } + $uid = intval($options["uid"]); + $old = $this->admin->get($uid); + if (!$old) { + return new Alternc_Api_Response(array("code" => self::ERR_NOT_FOUND, "message" => "User not found")); + } + + foreach ($defaults as $key) { + if (!isset($options[$key])) { + $options[$key] = $old[$key]; + } + } + if (!isset($options["pass"])) + $options["pass"] = ""; + $uid = $this->admin->update_mem($uid, $options["mail"], $options["nom"], $options["prenom"], $options["pass"], $options["enabled"], $options["canpass"], $options["type"], $options["duration"], $options["notes"]); + if (!$uid) { + return $this->alterncLegacyErrorManager(); + } else { + return new Alternc_Api_Response(array("content" => $uid)); + } + } + + /** API Method from legacy class method admin->del_mem() + * @param $options a hash with parameters transmitted to legacy call + * mandatory parameters: uid + * @return Alternc_Api_Response TRUE if the account has been deleted. + */ + function del($options) { + if (!isset($options["uid"])) { + return new Alternc_Api_Response(array("code" => self::ERR_ALTERNC_FUNCTION, "message" => "Missing or invalid argument: UID")); + } + $result = $this->admin->del_mem(intval($options["uid"])); + if (!$result) { + return $this->alterncLegacyErrorManager(); + } else { + return new Alternc_Api_Response(array("content" => true)); + } + } + + /** API Method from legacy class method admin->lock_mem() + * @param $options a hash with parameters transmitted to legacy call + * mandatory parameters: uid + * @return Alternc_Api_Response TRUE if the account has been locked + */ + function lock($options) { + if (!isset($options["uid"])) { + return new Alternc_Api_Response(array("code" => self::ERR_ALTERNC_FUNCTION, "message" => "Missing or invalid argument: UID")); + } + $result = $this->admin->lock_mem(intval($options["uid"])); + if (!$result) { + return $this->alterncLegacyErrorManager(); + } else { + return new Alternc_Api_Response(array("content" => true)); + } + } + + /** API Method from legacy class method admin->unlock_mem() + * @param $options a hash with parameters transmitted to legacy call + * mandatory parameters: uid + * @return Alternc_Api_Response TRUE if the account has been unlocked + */ + function unlock($options) { + if (!isset($options["uid"])) { + return new Alternc_Api_Response(array("code" => self::ERR_ALTERNC_FUNCTION, "message" => "Missing or invalid argument: UID")); + } + $result = $this->admin->unlock_mem(intval($options["uid"])); + if (!$result) { + return $this->alterncLegacyErrorManager(); + } else { + return new Alternc_Api_Response(array("content" => true)); + } + } + + /** API Method from legacy class method admin->normal2su() + * @param $options a hash with parameters transmitted to legacy call + * mandatory parameters: uid + * @return Alternc_Api_Response TRUE if the account has been set to be an administator + */ + function setAdmin($options) { + if (!isset($options["uid"])) { + return new Alternc_Api_Response(array("code" => self::ERR_ALTERNC_FUNCTION, "message" => "Missing or invalid argument: UID")); + } + $result = $this->admin->normal2su(intval($options["uid"])); + if (!$result) { + return $this->alterncLegacyErrorManager(); + } else { + return new Alternc_Api_Response(array("content" => true)); + } + } + + /** API Method from legacy class method admin->su2normal() + * @param $options a hash with parameters transmitted to legacy call + * mandatory parameters: uid + * @return Alternc_Api_Response TRUE if the account has been set to NOT be an administrator + */ + function unsetAdmin($options) { + if (!isset($options["uid"])) { + return new Alternc_Api_Response(array("code" => self::ERR_ALTERNC_FUNCTION, "message" => "Missing or invalid argument: UID")); + } + $result = $this->admin->su2normal(intval($options["uid"])); + if (!$result) { + return $this->alterncLegacyErrorManager(); + } else { + return new Alternc_Api_Response(array("content" => true)); + } + } + + /** API Method from legacy class method admin->get_list() + * @param $options a hash with parameters transmitted to legacy call + * non-mandatory parameters: ONE OF: + * uid(strict), login(like %%), domain(like %%), creator(strict, by uid), + * Any of: offset(int=0), count(int=+inf) + * @return Alternc_Api_Response An array with all matching users informations as hashes + */ + function find($options) { + $result = false; + if (!$result && isset($options["uid"])) { + $result = $this->admin->get(intval($options["uid"])); + if ($result) + $result = array($result); + } + if (!$result && isset($options["login"])) { + $result = $this->admin->get_list(1/* ALL */, "", $options["login"], "login"); + } + if (!$result && isset($options["domain"])) { + $result = $this->admin->get_list(1/* ALL */, "", $options["domain"], "domaine"); + } + if (!$result && isset($options["creator"])) { + $result = $this->admin->get_list(1/* ALL */, intval($options["creator"])); + } + if (!$result) { // everybody + $result = $this->admin->get_list(1/* ALL */, ""); + } + + if (!$result) { + return $this->alterncLegacyErrorManager(); + } else { + list($offset, $count) = $this->offsetAndCount($options, count($result)); + if ($offset != -1 || $count != -1) { + $result = array_slice($result, $offset, $count); + } + return new Alternc_Api_Response(array("content" => $result)); + } + } + +} + +// class Alternc_Api_Object_Account \ No newline at end of file