From c1817a24bec8677bccd19318164fa6193bad2496 Mon Sep 17 00:00:00 2001 From: Josh Sherman Date: Tue, 17 Dec 2013 00:46:47 -0500 Subject: [PATCH] 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. --- classes/AnonymousModule.php | 2 +- classes/CustomModule.php | 12 ++++ classes/RedisModel.php | 111 --------------------------------- classes/UserModule.php | 2 +- models/Leaderboard.php | 0 models/User.php | 53 ---------------- modules/home.php | 8 +++ modules/leaderboards.php | 14 +++++ modules/logout.php | 3 +- modules/user/authenticate.php | 23 +++---- modules/user/create.php | 33 ++++++---- templates/__shared/index.phtml | 3 +- templates/leaderboards.phtml | 3 + 13 files changed, 69 insertions(+), 198 deletions(-) delete mode 100644 classes/RedisModel.php delete mode 100644 models/Leaderboard.php delete mode 100644 models/User.php create mode 100644 modules/home.php create mode 100644 modules/leaderboards.php create mode 100644 templates/leaderboards.phtml diff --git a/classes/AnonymousModule.php b/classes/AnonymousModule.php index 4a5a439..81072ba 100644 --- a/classes/AnonymousModule.php +++ b/classes/AnonymousModule.php @@ -6,7 +6,7 @@ class AnonymousModule extends CustomModule { parent::__construct(); - if (User::isAuthenticated()) + if ($this->uid) { Browser::goHome(); } diff --git a/classes/CustomModule.php b/classes/CustomModule.php index 221b4b9..b49c898 100644 --- a/classes/CustomModule.php +++ b/classes/CustomModule.php @@ -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; + } + } } } diff --git a/classes/RedisModel.php b/classes/RedisModel.php deleted file mode 100644 index b02f770..0000000 --- a/classes/RedisModel.php +++ /dev/null @@ -1,111 +0,0 @@ -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); - } -} - -?> diff --git a/classes/UserModule.php b/classes/UserModule.php index 9b6ad98..0698857 100644 --- a/classes/UserModule.php +++ b/classes/UserModule.php @@ -6,7 +6,7 @@ class UserModule extends CustomModule { parent::__construct(); - if (!User::isAuthenticated()) + if (!$this->uid) { Browser::redirect('/login'); } diff --git a/models/Leaderboard.php b/models/Leaderboard.php deleted file mode 100644 index e69de29..0000000 diff --git a/models/User.php b/models/User.php deleted file mode 100644 index 4948645..0000000 --- a/models/User.php +++ /dev/null @@ -1,53 +0,0 @@ -getCookie()) - { - $auth_token = $user->getAuth($cookie['uid']); - - return $auth_token === $cookie['token']; - } - } -} - -?> diff --git a/modules/home.php b/modules/home.php new file mode 100644 index 0000000..ef3d0fa --- /dev/null +++ b/modules/home.php @@ -0,0 +1,8 @@ + diff --git a/modules/leaderboards.php b/modules/leaderboards.php new file mode 100644 index 0000000..bff45cf --- /dev/null +++ b/modules/leaderboards.php @@ -0,0 +1,14 @@ + $leaderboards); + } +} + +?> diff --git a/modules/logout.php b/modules/logout.php index 25ac0d6..893c9c4 100644 --- a/modules/logout.php +++ b/modules/logout.php @@ -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, '/'); diff --git a/modules/user/authenticate.php b/modules/user/authenticate.php index bda6f16..4e007dc 100644 --- a/modules/user/authenticate.php +++ b/modules/user/authenticate.php @@ -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.'); } diff --git a/modules/user/create.php b/modules/user/create.php index 7751938..2732baf 100644 --- a/modules/user/create.php +++ b/modules/user/create.php @@ -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()); } diff --git a/templates/__shared/index.phtml b/templates/__shared/index.phtml index 96f4c50..3916808 100644 --- a/templates/__shared/index.phtml +++ b/templates/__shared/index.phtml @@ -44,8 +44,7 @@