diff options
author | marvin-borner@live.com | 2018-05-12 12:44:04 +0200 |
---|---|---|
committer | marvin-borner@live.com | 2018-05-12 12:44:04 +0200 |
commit | d70be1a7a2b94cf1f30f6f4193a27eabcc84fe54 (patch) | |
tree | db30654b695f5b6dd00fa296d0e9e1770e37bfe6 /main/app/sprinkles/admin/src/Controller/UserController.php | |
parent | 16014c5bbd25afbb445784c17646607c53cfe56b (diff) | |
parent | 1f05a05eec9fc88aca7d3e4e7d9e891ae31ba4eb (diff) |
Merge branch 'master' of github.com:marvinborner/SocialNetwork
Diffstat (limited to 'main/app/sprinkles/admin/src/Controller/UserController.php')
-rw-r--r-- | main/app/sprinkles/admin/src/Controller/UserController.php | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/main/app/sprinkles/admin/src/Controller/UserController.php b/main/app/sprinkles/admin/src/Controller/UserController.php index 46d0f0f..be98f02 100644 --- a/main/app/sprinkles/admin/src/Controller/UserController.php +++ b/main/app/sprinkles/admin/src/Controller/UserController.php @@ -231,6 +231,73 @@ class UserController extends SimpleController return $response->withStatus(200); } + + /** + * Sets the users public key + * Request type: POST + */ + public function setPublicKey($request, $response, $args) { + $requestedUser = $this->getUserFromParams($args); + + if (!$requestedUser) { + throw new NotFoundException($request, $response); + } + + $PublicKey = $request->getParsedBody()["PublicKey"]; + + if ($this->ci->currentUser->id === $requestedUser->id && (Capsule::table('public_keys') + ->where('UserID', "=", $requestedUser->id) + ->exists()) === FALSE) { + Capsule::table('public_keys') + ->insert(['UserID' => $requestedUser->id, 'Key' => substr(substr($PublicKey, 100), 0,-40)]); + return $response->withStatus(200); + } else if ($this->ci->currentUser->id === $requestedUser->id) { + Capsule::table('public_keys') + ->where('UserID', $requestedUser->id) + ->update(['Key' => substr(substr($PublicKey, 100), 0,-40)]); + return $response->withStatus(200); + } else { + throw new ForbiddenException(); + } + } + + /** + * Gets the users public key + * Request type: GET + */ + public function getPublicKey($request, $response, $args) { + $requestedUser = $this->getUserFromParams($args); + + if (!$requestedUser) { + throw new NotFoundException($request, $response); + } + + if ((Capsule::table('public_keys') + ->where('UserID', "=", $requestedUser->id) + ->exists()) === TRUE) { + + $RawPublicKey = Capsule::table('public_keys') + ->where('UserID', "=", $requestedUser->id) + ->value('Key'); + $PublicKey = "-----BEGIN PGP PUBLIC KEY BLOCK-----\nVersion: OpenPGP.js v3.0.9\nComment: https://openpgpjs.org\n\n" . $RawPublicKey . "\n-----END PGP PUBLIC KEY BLOCK-----"; + + $ContentType = explode(',', $request->getHeaderLine('Accept'))[0]; + switch ($ContentType) { + case 'application/json': + $response->write(json_encode(array('user_id' => $requestedUser->id, 'PublicKey' => $PublicKey))); + break; + case 'text/html': + $response->write("<pre>" . $PublicKey); + break; + default: + $response->write($PublicKey); + } + return $response->withStatus(200); + } else { + throw new NotFoundException(); + } + } + /** * Processes the request to delete an existing user. * |