Alternc  latest
Alternc logiel libre pour l'hébergement
Account.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * Account Api of AlternC, used by alternc-api package
5  */
7 
8  /** API Method from legacy class method admin->add_mem()
9  * @param $options a hash with parameters transmitted to legacy call
10  * mandatory parameters: login, pass, nom, prenom, mail,
11  * non-mandatory: canpass, type, duration, notes, force, create_dom, db_server_id
12  * @return Alternc_Api_Response whose content is the newly created UID
13  */
14  function add($options) {
15  $mandatory = array("login", "pass", "nom", "prenom", "mail");
16  $defaults = array("canpass" => 1, "type" => "default", "duration" => 0, "notes" => "", "force" => 0, "create_dom" => "");
17  $missing = "";
18  foreach ($mandatory as $key) {
19  if (!isset($options[$key])) {
20  $missing.=$key . " ";
21  }
22  }
23  if ($missing) {
24  return new Alternc_Api_Response(array("code" => self::ERR_INVALID_ARGUMENT, "message" => "Missing or invalid argument: " . $missing));
25  }
26  foreach ($defaults as $key => $value) {
27  if (!isset($options[$key])) {
28  $options[$key] = $value;
29  }
30  }
31  if (!isset($options["db_server_id"])) {
32  $stmt = $this->db->prepare("SELECT MIN(db_servers.id) AS id FROM db_servers;");
33  $stmt->execute();
34  $me = $stmt->fetch(PDO::FETCH_OBJ);
35  $options["db_server_id"] = $me->id;
36  }
37  $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"]);
38  if (!$uid) {
39  return $this->alterncLegacyErrorManager();
40  } else {
41  return new Alternc_Api_Response(array("content" => $uid));
42  }
43  }
44 
45  /** API Method from legacy class method admin->update_mem()
46  * @param $options a hash with parameters transmitted to legacy call
47  * mandatory parameters: nom, prenom, mail,
48  * non-mandatory: pass, canpass, type, duration, notes, force, create_dom, db_server_id
49  * @return Alternc_Api_Response whose content is the updated UID
50  */
51  function update($options) {
52  $defaults = array("nom", "prenom", "mail", "canpass", "enabled", "type", "duration", "notes");
53  if (!isset($options["uid"])) {
54  return new Alternc_Api_Response(array("code" => self::ERR_INVALID_ARGUMENT, "message" => "Missing or invalid argument: UID"));
55  }
56  $uid = intval($options["uid"]);
57  $old = $this->admin->get($uid);
58  if (!$old) {
59  return new Alternc_Api_Response(array("code" => self::ERR_NOT_FOUND, "message" => "User not found"));
60  }
61 
62  foreach ($defaults as $key) {
63  if (!isset($options[$key])) {
64  $options[$key] = $old[$key];
65  }
66  }
67  if (!isset($options["pass"]))
68  $options["pass"] = "";
69  $uid = $this->admin->update_mem($uid, $options["mail"], $options["nom"], $options["prenom"], $options["pass"], $options["enabled"], $options["canpass"], $options["type"], $options["duration"], $options["notes"]);
70  if (!$uid) {
71  return $this->alterncLegacyErrorManager();
72  } else {
73  return new Alternc_Api_Response(array("content" => $uid));
74  }
75  }
76 
77  /** API Method from legacy class method admin->del_mem()
78  * @param $options a hash with parameters transmitted to legacy call
79  * mandatory parameters: uid
80  * @return Alternc_Api_Response TRUE if the account has been deleted.
81  */
82  function del($options) {
83  if (!isset($options["uid"])) {
84  return new Alternc_Api_Response(array("code" => self::ERR_ALTERNC_FUNCTION, "message" => "Missing or invalid argument: UID"));
85  }
86  $result = $this->admin->del_mem(intval($options["uid"]));
87  if (!$result) {
88  return $this->alterncLegacyErrorManager();
89  } else {
90  return new Alternc_Api_Response(array("content" => true));
91  }
92  }
93 
94  /** API Method from legacy class method admin->lock_mem()
95  * @param $options a hash with parameters transmitted to legacy call
96  * mandatory parameters: uid
97  * @return Alternc_Api_Response TRUE if the account has been locked
98  */
99  function lock($options) {
100  if (!isset($options["uid"])) {
101  return new Alternc_Api_Response(array("code" => self::ERR_ALTERNC_FUNCTION, "message" => "Missing or invalid argument: UID"));
102  }
103  $result = $this->admin->lock_mem(intval($options["uid"]));
104  if (!$result) {
105  return $this->alterncLegacyErrorManager();
106  } else {
107  return new Alternc_Api_Response(array("content" => true));
108  }
109  }
110 
111  /** API Method from legacy class method admin->unlock_mem()
112  * @param $options a hash with parameters transmitted to legacy call
113  * mandatory parameters: uid
114  * @return Alternc_Api_Response TRUE if the account has been unlocked
115  */
116  function unlock($options) {
117  if (!isset($options["uid"])) {
118  return new Alternc_Api_Response(array("code" => self::ERR_ALTERNC_FUNCTION, "message" => "Missing or invalid argument: UID"));
119  }
120  $result = $this->admin->unlock_mem(intval($options["uid"]));
121  if (!$result) {
122  return $this->alterncLegacyErrorManager();
123  } else {
124  return new Alternc_Api_Response(array("content" => true));
125  }
126  }
127 
128  /** API Method from legacy class method admin->normal2su()
129  * @param $options a hash with parameters transmitted to legacy call
130  * mandatory parameters: uid
131  * @return Alternc_Api_Response TRUE if the account has been set to be an administator
132  */
133  function setAdmin($options) {
134  if (!isset($options["uid"])) {
135  return new Alternc_Api_Response(array("code" => self::ERR_ALTERNC_FUNCTION, "message" => "Missing or invalid argument: UID"));
136  }
137  $result = $this->admin->normal2su(intval($options["uid"]));
138  if (!$result) {
139  return $this->alterncLegacyErrorManager();
140  } else {
141  return new Alternc_Api_Response(array("content" => true));
142  }
143  }
144 
145  /** API Method from legacy class method admin->su2normal()
146  * @param $options a hash with parameters transmitted to legacy call
147  * mandatory parameters: uid
148  * @return Alternc_Api_Response TRUE if the account has been set to NOT be an administrator
149  */
150  function unsetAdmin($options) {
151  if (!isset($options["uid"])) {
152  return new Alternc_Api_Response(array("code" => self::ERR_ALTERNC_FUNCTION, "message" => "Missing or invalid argument: UID"));
153  }
154  $result = $this->admin->su2normal(intval($options["uid"]));
155  if (!$result) {
156  return $this->alterncLegacyErrorManager();
157  } else {
158  return new Alternc_Api_Response(array("content" => true));
159  }
160  }
161 
162  /** API Method from legacy class method admin->get_list()
163  * @param $options a hash with parameters transmitted to legacy call
164  * non-mandatory parameters: ONE OF:
165  * uid(strict), login(like %%), domain(like %%), creator(strict, by uid),
166  * Any of: offset(int=0), count(int=+inf)
167  * @return Alternc_Api_Response An array with all matching users informations as hashes
168  */
169  function find($options) {
170  $result = false;
171  if (!$result && isset($options["uid"])) {
172  $result = $this->admin->get(intval($options["uid"]));
173  if ($result)
174  $result = array($result);
175  }
176  if (!$result && isset($options["login"])) {
177  $result = $this->admin->get_list(1/* ALL */, "", $options["login"], "login");
178  }
179  if (!$result && isset($options["domain"])) {
180  $result = $this->admin->get_list(1/* ALL */, "", $options["domain"], "domaine");
181  }
182  if (!$result && isset($options["creator"])) {
183  $result = $this->admin->get_list(1/* ALL */, intval($options["creator"]));
184  }
185  if (!$result) { // everybody
186  $result = $this->admin->get_list(1/* ALL */, "");
187  }
188 
189  if (!$result) {
190  return $this->alterncLegacyErrorManager();
191  } else {
192  list($offset, $count) = $this->offsetAndCount($options, count($result));
193  if ($offset != -1 || $count != -1) {
194  $result = array_slice($result, $offset, $count);
195  }
196  return new Alternc_Api_Response(array("content" => $result));
197  }
198  }
199 
200 }
201 
202 // class Alternc_Api_Object_Account
Any Legacy AlternC Api should use this class as a parent to be able to handle properly the access rig...
Definition: Legacyobject.php:9
alterncLegacyErrorManager()
return a proper Alternc_Api_Response from an error class and error string from AlternC legacy class
offsetAndCount($options, $max)
ensure that offset & count are set properly from $options.
Account Api of AlternC, used by alternc-api package.
Definition: Account.php:6
unlock($options)
API Method from legacy class method admin->unlock_mem()
Definition: Account.php:116
del($options)
API Method from legacy class method admin->del_mem()
Definition: Account.php:82
setAdmin($options)
API Method from legacy class method admin->normal2su()
Definition: Account.php:133
update($options)
API Method from legacy class method admin->update_mem()
Definition: Account.php:51
find($options)
API Method from legacy class method admin->get_list()
Definition: Account.php:169
unsetAdmin($options)
API Method from legacy class method admin->su2normal()
Definition: Account.php:150
lock($options)
API Method from legacy class method admin->lock_mem()
Definition: Account.php:99
add($options)
API Method from legacy class method admin->add_mem()
Definition: Account.php:14
Standard Response object for the AlternC API.
Definition: Response.php:7
$value
$uid
if(empty($_POST['key'])||empty($_POST['val'])) $key
Definition: tempovars.php:14