Last active
February 19, 2022 13:53
-
-
Save shealavington/b79ba8111a224a8fac55c85351aa4868 to your computer and use it in GitHub Desktop.
Directus custom endpoint: Registration — Change what roles can be reigstered to in the `$roleMap` array, give it a friendly api-ready name, and a role id
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // Version 3.0 | |
use Directus\Application\Http\Request; | |
use Directus\Application\Http\Response; | |
class User { | |
private $data; | |
private $role; | |
private $dbConnection; | |
public function __construct() { | |
/** | |
* Defaults — These will be default values stored against the user. | |
*/ | |
$this->data = [ | |
'timezone' => 'Europe/London', | |
'status' => 'active' | |
]; | |
$this->dbConnection = $dbConnection = \Directus\Application\Application::getInstance()->getContainer()->get('database'); | |
} | |
public function setEmail($x) { | |
isset($x) ? $this->data['email'] = $x : false; | |
return $this; | |
} | |
public function setPassword($x) { | |
$pw = \Directus\Authentication\Provider::hashPassword($x); | |
isset($x) ? $this->data['password'] = $pw : false; | |
return $this; | |
} | |
public function setFirstName($x) { | |
isset($x) ? $this->data['first_name'] = $x : false; | |
return $this; | |
} | |
public function setLastName($x) { | |
isset($x) ? $this->data['last_name'] = $x : false; | |
return $this; | |
} | |
public function setRole($x) { | |
isset($x) ? $this->role = $x : false; | |
return $this; | |
} | |
public function checkRoleExists($x) { | |
// Query the database to see if the role exists before adding user to non-existant role. | |
return true; | |
} | |
public function registerUser() { | |
if(!$this->data['email'] || !$this->data['password']){ | |
throw new \Directus\Exception\Exception( 'Sorry! You didn\'t supply an email or password to register with.' ); | |
} | |
$roleExists = $this->checkRoleExists($this->role); | |
if(!$this->role || !$roleExists){ | |
throw new \Directus\Exception\Exception( 'Sorry! We couldn\'t register you to that role, please contact a system administrator.' ); | |
} | |
$tblUsers = new \Zend\Db\TableGateway\TableGateway('directus_users', $this->dbConnection); | |
$tblUserRoles = new \Zend\Db\TableGateway\TableGateway('directus_user_roles', $this->dbConnection); | |
$tblUsers->insert($this->data); | |
$userId = $tblUsers->getLastInsertValue(); | |
$tblUserRoles->insert([ | |
'user' => $userId, | |
'role' => $this->role | |
]); | |
$results = $tblUsers->select([ | |
'id' => $userId | |
]); | |
$user = $results->current(); | |
return $user; | |
} | |
} | |
return [ | |
// '/register' => [ | |
// 'method' => 'POST', | |
// 'handler' => function (Request $request, Response $response) { | |
// $roleId = 3; // Member | |
// $POST = $request->getParsedBody(); | |
// $user = new User(); | |
// $user->setRole($roleId); | |
// $POST['first_name'] ? $user->setFirstName($POST['first_name']) : false; | |
// $POST['last_name'] ? $user->setLastName($POST['last_name']) : false; | |
// $POST['email'] ? $user->setEmail($POST['email']) : false; | |
// $POST['password'] ? $user->setPassword($POST['password']) : false; | |
// $data = $user->registerUser(); | |
// return $response->withJson([ | |
// 'data' => $data | |
// ]); | |
// } | |
// ] | |
'/register[/{role}]' => [ | |
'method' => 'POST', | |
'handler' => function (Request $request, Response $response) { | |
/** | |
* | |
* Below is a map of roles that people can register to, | |
* You can add a friendly name, and an ID, the ID must match the database. | |
* I highly suggest never enabling admin... for obvious reasons. | |
* | |
* Request (Post) : https://api.example.com/_/custom/auth/register/member | |
* Response (JSON) : The created users data if successful. | |
* | |
*/ | |
$roleMap = [ | |
"member" => 3 | |
]; | |
$POST = $request->getParsedBody(); | |
$role = $request->getAttribute('role'); | |
$roleId = $roleMap[$role]; | |
$user = new User(); | |
$user->setRole($roleId); | |
$POST['first_name'] ? $user->setFirstName($POST['first_name']) : false; | |
$POST['last_name'] ? $user->setLastName($POST['last_name']) : false; | |
$POST['email'] ? $user->setEmail($POST['email']) : false; | |
$POST['password'] ? $user->setPassword($POST['password']) : false; | |
$data = $user->registerUser(); | |
return $response->withJson([ | |
'data' => $data | |
]); | |
} | |
] | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: A tidier version of this was written by @mariorojas
https://gist.github.com/mariorojas/27e3328f3e19c70c3ef6bc337498b43e