Dropped Redis model, rebuilt without it

The model was getting a bit more complicated than I would have liked. Went back to using straight redis commands and moved some of the user model logic into the custommodule.
This commit is contained in:
Josh Sherman 2013-12-17 00:46:47 -05:00
parent 2c5144f026
commit c1817a24be
13 changed files with 69 additions and 198 deletions

View file

@ -16,29 +16,20 @@ class user_authenticate extends AnonymousModule
public function __default()
{
try
// Checks if the email supplied is valid
if ($uid = $this->redis->get('user:email:' . trim($_POST['email'])))
{
$user = new User();
// Grabs the password hash and auth token
$user = $this->redis->hmget('user:' . $uid, array('password', 'auth'));
// Checks if the email supplied is valid
if ($uid = $user->getMapping('email', $_POST['email']))
// Checks if the password is valid
if ($user['password'] == crypt($_POST['password'], $user['password']))
{
// Checks if the password is valid
$password = $user->getPassword($uid);
if ($password == crypt($_POST['password'], $password))
{
$auth_token = $user->getAuth($uid);
setcookie('__auth', base64_encode($uid . '|' . $auth_token), time() + Time::YEAR, '/');
}
setcookie('__auth', base64_encode($uid . '|' . $user['auth']), time() + Time::YEAR, '/');
}
return array('status' => 'success', 'url' => '/');
}
catch (RedisException $e)
{
return array('error' => $e->getMessage());
}
return array('error' => 'Invalid email address or password.');
}

View file

@ -21,27 +21,37 @@ class user_create extends AnonymousModule
public function __default()
{
// Removes any stray whitespace
$_POST['email'] = trim($_POST['email']);
$_POST['username'] = trim($_POST['username']);
try
{
$user = new User();
$mapping_fields = array(
'user:email:' . $_POST['email'],
'user:username:' . $_POST['username'],
);
// Checks if the email or username is already in use
foreach (array('email', 'username') as $field)
$existing = $this->redis->mget($mapping_fields);
if ($existing[0])
{
if ($user->getMapping($field, $_POST[$field]))
{
return array('error' => 'The ' . $field . ' is already in use.');
}
throw new Exception('The email address is already in use.');
}
elseif ($existing[1])
{
throw new Exception('The username is already in use.');
}
// Grabs the next UID
$uid = $user->nextUID();
$uid = $this->redis->incr('user:uid');
// Generates the auth token
$auth_token = $user->generateToken();
$auth_token = sha1(microtime());
// Writes the user data
$user->set($uid, array(
$this->redis->hmset('user:' . $uid, array(
'username' => $_POST['username'],
'email' => $_POST['email'],
'password' => crypt($_POST['password'], '$2y$11$' . String::random(22) . '$'),
@ -49,15 +59,14 @@ class user_create extends AnonymousModule
));
// Sets the UID mappings
$user->setMapping('username', $_POST['username'], $uid);
$user->setMapping('email', $_POST['email'], $uid);
$this->redis->mset(array_combine($mapping_fields, array($uid, $uid)));
// Sets a cookie with the UID and auth token
setcookie('__auth', base64_encode($uid . '|' . $auth_token), time() + Time::YEAR, '/');
return array('status' => 'success', 'url' => '/');
}
catch (RedisException $e)
catch (Exception $e)
{
return array('error' => $e->getMessage());
}