Finished up user creation in Redis

This commit is contained in:
Josh Sherman 2013-12-16 15:37:07 -05:00
parent 9fe9eb7acf
commit cedc38917a
2 changed files with 26 additions and 13 deletions

View file

@ -24,14 +24,24 @@ class RedisModel extends Object
return strtolower(implode(':', $parts)); return strtolower(implode(':', $parts));
} }
public function mappingKey($variable, $value)
{
return $this->key($variable, $value, 'uid');
}
public function nextUID() public function nextUID()
{ {
return $this->redis->incr($this->key('uid')); return $this->redis->incr($this->key('uid'));
} }
public function getUID($variable, $value) public function setMapping($variable, $value, $uid)
{ {
return $this->redis->get($this->key($variable, $value, 'uid')); $this->redis->set($this->mappingKey($variable, $value), $uid);
}
public function getMapping($variable, $value)
{
return $this->redis->get($this->mappingKey($variable, $value));
} }
public function __call($name, $arguments) public function __call($name, $arguments)

View file

@ -23,19 +23,12 @@ class user_create extends CustomModule
{ {
try try
{ {
$_POST = array(
'email' => 'foo@bar.com',
'username' => 'fubar',
'password' => 'insecure123',
);
$user = new User(); $user = new User();
// Checks if the email or username is already in use // Checks if the email or username is already in use
foreach (array('email', 'username') as $field) foreach (array('email', 'username') as $field)
{ {
if ($user->getUID($field, $_POST[$field])) if ($user->getMapping($field, $_POST[$field]))
{ {
return array('error' => 'The ' . $field . ' is already in use.'); return array('error' => 'The ' . $field . ' is already in use.');
} }
@ -44,15 +37,25 @@ class user_create extends CustomModule
// Grabs the next UID // Grabs the next UID
$uid = $user->nextUID(); $uid = $user->nextUID();
// Generates the auth token
$auth_token = sha1(mt_rand() . microtime());
// Writes the user data // Writes the user data
$user->set('1001', array( $user->set($uid, array(
'username' => $_POST['username'], 'username' => $_POST['username'],
'email' => $_POST['email'], 'email' => $_POST['email'],
'password' => crypt($_POST['password'], '$2y$11$' . String::random(22) . '$'), 'password' => crypt($_POST['password'], '$2y$11$' . String::random(22) . '$'),
'auth' => $auth_token,
)); ));
// Sets up the authentication cookie // Sets the UID mappings
//$user->setAuth() $user->setMapping('username', $_POST['username'], $uid);
$user->setMapping('email', $_POST['email'], $uid);
// Sets a cookie with the UID and auth token
setcookie('auth', $uid . '|' . $auth_token, Time::YEAR);
return array('status' => 'success', 'url' => '/');
} }
catch (RedisException $e) catch (RedisException $e)
{ {