--- /dev/null
+<?php
+/**
+ * UserAPI short summary.
+ *
+ * GET : Verify if user exists and if it's administrator. Give username, name and firstname
+ * PUT : Create a non-administrator user
+ * POST : Add a project proposed by User (authentication necessary)
+ * DELETE : Delete this user (authentication necessary)
+ *
+ * @version 1.0
+ * @author Ferrand
+ */
+class AccountAPI extends RestAPI {
+ use dontAllowPost, dontAllowPostCollection;
+ use dontAllowPutCollection;
+ use dontAllowDeleteCollection;
+
+ protected function GETAction() {
+ $dbh = $this->databaseHelper();
+
+ $response = $dbh->prepare('SELECT * FROM pub_Account WHERE username = :username');
+ $response->execute(['username' => $this->element]);
+
+ if ($response === false) {
+ $this->respondeError();
+ return;
+ }
+
+ $row = $response->fetch(PDO::FETCH_ASSOC);
+ if($row === false) {
+ $this->respondeNoResult();
+ return;
+ }
+
+ $serverAccount = new ServerAccount();
+
+ $serverAccount->username = $row['username'];
+ $serverAccount->password = "";
+ $serverAccount->administrator = $row['administrator'];
+ $serverAccount->pseudo = $row['user'];
+
+ $this->responde(200, $serverAccount);
+ }
+
+ protected function GETCollectionAction() {
+ $dbh = $this->databaseHelper();
+
+ $response = $dbh->prepare("SELECT * FROM pub_Account");
+ $response->execute();
+
+ if ($response === false) {
+ $this->respondeError();
+ }
+ $data = array();
+ while (($row = $response->fetch(PDO::FETCH_ASSOC)) !== false) {
+ $serverAccount = new ServerAccount();
+
+ $serverAccount->username = $row['username'];
+ $serverAccount->password = "";
+ $serverAccount->administrator = $row['administrator'];
+ $serverAccount->pseudo = $row['user'];
+
+ array_push($data, $serverAccount);
+ }
+
+ $this->responde(200, $data);
+ }
+
+ protected function PUTAction() {
+ $dbh = $this->databaseHelper();
+
+ $response = $dbh->prepare("SELECT * FROM pub_Account");
+ $response->execute();
+
+ if ($response === false) {
+ $this->respondeError();
+ }
+ $data = array();
+ while (($row = $response->fetch(PDO::FETCH_ASSOC)) !== false) {
+ $serverAccount = new ServerAccount();
+
+ $serverAccount->username = $row['username'];
+ $serverAccount->password = "";
+ $serverAccount->administrator = $row['administrator'];
+ $serverAccount->pseudo = $row['user'];
+
+ array_push($data, $serverAccount);
+ }
+
+ $this->responde(200, $data);
+ }
+
+ /**
+ * code :
+ * 0 : Deleted
+ * 1 : Username doesn't exists
+ */
+ protected function DELETEAction() {
+ if ($this->isAuthenticated()) {
+ $dbh = new PDO("mysql:host=" . $GLOBALS['PF_HOST'] . ";dbname=" . $GLOBALS['PF_DATABASE_NAME'], $GLOBALS['PF_USERNAME'], $GLOBALS['PF_PASSWORD']);
+
+ $response = $dbh->prepare('SELECT * FROM pub_User WHERE username = :username');
+ $response->execute(['username' => $this->element]);
+
+ if ($response === false) { // Database fails hard today :'(
+ $this->respondeError();
+ return; // end this!
+ }
+
+ $row = $response->fetch();
+ if ($row === false) { // No one exists, sorry!
+ $simpleServerResponse = new SimpleServerResponse();
+ $simpleServerResponse->code = 1;
+
+ $this->responde(200, $simpleServerResponse);
+ return;
+ }
+
+ $response = $dbh->prepare('DELETE FROM pub_User WHERE username = :username');
+ $response->execute(['username' => $this->element]);
+
+ if ($response === false) { // Database fails hard today :'(
+ $this->respondeError();
+ }
+ else {
+ $simpleServerResponse = new SimpleServerResponse();
+ $simpleServerResponse->code = 0;
+
+ $this->responde(200, $simpleServerResponse);
+ }
+ } else {
+ $this->respondeUnauthorized();
+ }
+ }
+}