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:
parent
2c5144f026
commit
c1817a24be
13 changed files with 69 additions and 198 deletions
|
@ -6,7 +6,7 @@ class AnonymousModule extends CustomModule
|
|||
{
|
||||
parent::__construct();
|
||||
|
||||
if (User::isAuthenticated())
|
||||
if ($this->uid)
|
||||
{
|
||||
Browser::goHome();
|
||||
}
|
||||
|
|
|
@ -3,12 +3,24 @@
|
|||
class CustomModule extends Module
|
||||
{
|
||||
protected $redis = false;
|
||||
protected $uid = false;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->redis = new CustomRedis();
|
||||
|
||||
if (isset($_COOKIE['__auth']))
|
||||
{
|
||||
list($uid, $auth_token) = explode('|', base64_decode($_COOKIE['__auth']));
|
||||
|
||||
if ($this->redis->hget('user:' . $uid, 'auth') === $auth_token)
|
||||
{
|
||||
$this->uid = $uid;
|
||||
$this->return['uid'] = $uid;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,111 +0,0 @@
|
|||
<?php
|
||||
|
||||
class RedisModel extends Object
|
||||
{
|
||||
protected $redis = false;
|
||||
protected $prefix = false;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->redis = new CustomRedis();
|
||||
}
|
||||
|
||||
public function key()
|
||||
{
|
||||
$parts = func_get_args();
|
||||
|
||||
if ($this->prefix)
|
||||
{
|
||||
array_unshift($parts, $this->prefix);
|
||||
}
|
||||
|
||||
return strtolower(implode(':', $parts));
|
||||
}
|
||||
|
||||
public function mappingKey($variable, $value)
|
||||
{
|
||||
return $this->key($variable, $value, 'uid');
|
||||
}
|
||||
|
||||
public function nextUID()
|
||||
{
|
||||
return $this->redis->incr($this->key('uid'));
|
||||
}
|
||||
|
||||
public function setMapping($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)
|
||||
{
|
||||
$name = strtolower($name);
|
||||
|
||||
if ($name == 'set')
|
||||
{
|
||||
// Grabs our variables
|
||||
$uid = $arguments[0];
|
||||
$variables = $arguments[1];
|
||||
$arguments = array();
|
||||
|
||||
// Assembles our new arguments
|
||||
foreach ($variables as $key => $value)
|
||||
{
|
||||
$arguments[$this->key($uid, $key)] = $value;
|
||||
}
|
||||
|
||||
// Sets us up for MSET or just SET
|
||||
if (count($arguments) > 1)
|
||||
{
|
||||
$name = 'mset';
|
||||
$arguments = array($arguments);
|
||||
}
|
||||
else
|
||||
{
|
||||
$arguments = array(key($arguments), current($arguments));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$base = substr($name, 0, 3);
|
||||
|
||||
if (in_array($base, array('set', 'get')))
|
||||
{
|
||||
$key = $this->key($arguments[0], substr($name, 3));
|
||||
$name = $base;
|
||||
}
|
||||
|
||||
switch ($base)
|
||||
{
|
||||
case 'set':
|
||||
if (isset($arguments[1]))
|
||||
{
|
||||
$arguments = $arguments[1];
|
||||
}
|
||||
else
|
||||
{
|
||||
$key = $this->key(substr($name, 3));
|
||||
var_dump($key, $arguments);
|
||||
}
|
||||
|
||||
$arguments = array($key, $arguments[1]);
|
||||
break;
|
||||
|
||||
case 'get':
|
||||
$arguments = array($key);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return call_user_func_array(array($this->redis, $name), $arguments);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -6,7 +6,7 @@ class UserModule extends CustomModule
|
|||
{
|
||||
parent::__construct();
|
||||
|
||||
if (!User::isAuthenticated())
|
||||
if (!$this->uid)
|
||||
{
|
||||
Browser::redirect('/login');
|
||||
}
|
||||
|
|
|
@ -1,53 +0,0 @@
|
|||
<?php
|
||||
|
||||
class User extends RedisModel
|
||||
{
|
||||
protected $prefix = 'user';
|
||||
|
||||
public function generateToken()
|
||||
{
|
||||
return sha1(mt_rand() . microtime());
|
||||
}
|
||||
|
||||
public function getAuthenticated($fields)
|
||||
{
|
||||
if (!is_array($fields))
|
||||
{
|
||||
$fields = array($fields);
|
||||
}
|
||||
|
||||
if ($cookie = self::getCookie())
|
||||
{
|
||||
if ($fields == array('uid'))
|
||||
{
|
||||
return $cookie['uid'];
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getCookie()
|
||||
{
|
||||
if (isset($_COOKIE['__auth']))
|
||||
{
|
||||
return array_combine(array('uid', 'token'), explode('|', base64_decode($_COOKIE['__auth'])));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function isAuthenticated()
|
||||
{
|
||||
$user = new self();
|
||||
|
||||
if ($cookie = $user->getCookie())
|
||||
{
|
||||
$auth_token = $user->getAuth($cookie['uid']);
|
||||
|
||||
return $auth_token === $cookie['token'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
8
modules/home.php
Normal file
8
modules/home.php
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?php
|
||||
|
||||
class home extends CustomModule
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
?>
|
14
modules/leaderboards.php
Normal file
14
modules/leaderboards.php
Normal file
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
class leaderboards extends UserModule
|
||||
{
|
||||
public function __default()
|
||||
{
|
||||
// Grabs the user's leaderboards
|
||||
$leaders = '';
|
||||
|
||||
return array('leaderboards' => $leaderboards);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -4,8 +4,7 @@ class logout extends UserModule
|
|||
{
|
||||
public function __default()
|
||||
{
|
||||
$user = new User();
|
||||
$user->setAuth($user->getAuthenticated('uid'), $user->generateToken());
|
||||
$this->redis->hset('user:' . $this->uid, sha1(microtime()));
|
||||
|
||||
setcookie('__auth', '', time() - Time::YEAR, '/');
|
||||
|
||||
|
|
|
@ -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.');
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -44,8 +44,7 @@
|
|||
<div class="collapse navbar-collapse pull-right">
|
||||
<ul class="nav navbar-nav">
|
||||
<?php
|
||||
|
||||
if (User::isAuthenticated())
|
||||
if (isset($this->module['uid']))
|
||||
{
|
||||
$links = array(
|
||||
'/leaderboards' => 'My Leaderboards',
|
||||
|
|
3
templates/leaderboards.phtml
Normal file
3
templates/leaderboards.phtml
Normal file
|
@ -0,0 +1,3 @@
|
|||
<h1>My Leaderboards</h1>
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue