There IS editing to be done to fully update to use the JSON library etc.
| $autoload['libraries'] = array('database', 'session', 'xmlrpc');
*/
-$autoload['libraries'] = array('database');
+$autoload['libraries'] = array('database', 'json');
/*
}\r
\r
public function login() {\r
- if ($this->user->logged_in)\r
+ if ($this->user->is_logged_in())\r
redirect('account/');\r
\r
# Login stuff\r
- if (count($this->input->post()) == 2) {\r
- $user = $this->input->post('username');\r
- $pass = $this->input->post('password');\r
+ $user = $this->input->post('username');\r
+ $pass = $this->input->post('password');\r
\r
- if ($user == 'test' && $pass == 'test') {\r
- $this->user->logged_in = True;\r
- $response = array(\r
- 'loggedIn' => True,\r
- );\r
- print json_encode($response);\r
- }\r
+ if ($user == FALSE || $pass == FALSE) {\r
+ $this->json->error('Username or password was empty');\r
+ return;\r
+ }\r
+\r
+ if ($this->user->try_login($user, $pass)) {\r
+ $data = array(\r
+ 'uid' => $this->user->uid(),\r
+ 'sid' => $this->user->sid(),\r
+ 'name' => $this->user->display_name(),\r
+ );\r
+ $this->json->reply('Logged in', $data);\r
+ } else {\r
+ $this->json->error('Incorrect credentials');\r
}\r
}\r
}\r
}
public function index() {
- pass;
}
public function logged_in() {
- print json_encode($this->user->logged_in);
+ print json_encode($this->user->is_logged_in());
}
public function login_modal() {
--- /dev/null
+<?php
+
+class Json {
+ public function get_reply($html, $data=NULL) {
+ $rep = array(
+ 'success' => TRUE,
+ 'contents' => $html,
+ 'data' => $data,
+ );
+ return json_encode($rep);
+ }
+ public function reply($html, $data=NULL) {
+ echo $this->get_reply($html, $data);
+ }
+
+ public function get_error($reason) {
+ $rep = array(
+ 'success' => FALSE,
+ 'error' => $reason,
+ );
+ return json_encode($rep);
+ }
+ public function error($reason) {
+ echo $this->get_error($reason);
+ }
+}
+++ /dev/null
-<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
-
-class Json extends CI_Model {
-
- public function __construct() {
- # Required
- parent::__construct();
- }
-
- public function index() {
- }
-
- public function success($contents, $data=NULL) {
- $resp = array(
- 'success' => TRUE,
- 'time' => time(),
- 'contents' => $contents,
- 'data' => $data,
- );
- return json_encode($resp);
- }
-
- public function error($reason) {
- $resp = array(
- 'success' => False,
- 'time' => time(),
- 'error' => array(
- 'reason' => $reason,
- ),
- );
- return json_encode($resp);
- }
-}
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Model {
-
- var $logged_in = False;
-
+ protected $cached_sid, $cached_uid, $cached_level, $cached_display_name;
public function __construct() {
# Required
parent::__construct();
}
+ public function try_login($user, $pass) {
+ $this->db->select('uid, password, salt');
+ $this->db->where('username', $user);
+ $q = $this->db->get('users');
+ if ($q->num_rows() > 0) {
+ $row = $q->row();
+ $pwdigest = sha1($row->salt.$pass);
+ if ($pwdigest == $row->password) {
+ return $this->do_login($row->uid);
+ }
+ }
+ return FALSE;
+ }
+ public function do_login($uid) {
+ $this->db->select('display_name, level');
+ $this->db->where('uid', $uid);
+ $q = $this->db->get('users');
+ if ($q->num_rows() == 0)
+ return FALSE;
+ $row = $q->row();
+ $this->uid($uid);
+ $this->display_name($row->display_name);
+ $this->level($row->level);
+
+ $sdata = array(
+ 'uid' => $uid,
+ 'started' => time(),
+ 'last' => time(),
+ 'lockip' => $this->input->ip_address(),
+ );
+ $this->db->insert('sessions', $sdata);
+ $this->sid($this->db->insert_id());
+
+ return TRUE;
+ }
+
+ public function sid($new=NULL) {
+ if (!empty($new)) {
+ $old = $this->cached_sid;
+ $this->cached_sid = $new;
+ return $old;
+ }
+
+ if (isset($this->cached_sid)) {
+ return $this->cached_sid;
+ } else {
+ return FALSE; // FIXME should we fetch SID somehow?
+ }
+ }
+ public function uid($new=NULL) {
+ if (!empty($new)) {
+ $old = $this->cached_uid;
+ $this->cached_uid = $new;
+ return $old;
+ }
+
+ if (isset($this->cached_uid)) {
+ return $this->cached_uid;
+ } else {
+ $sid = $this->sid();
+ if ($sid !== FALSE) {
+ $this->db->select('uid');
+ $this->db->where('sid', $sid);
+ $q = $this->db->get('sessions');
+ if ($q->num_rows() > 0) {
+ $row = $q->row();
+ return $this->cached_uid = $row->uid;
+ }
+ }
+ }
+ return FALSE;
+ }
+ public function display_name($new=NULL) {
+ if (!empty($new)) {
+ $old = $this->cached_display_name;
+ $this->cached_display_name = $new;
+ return $old;
+ }
+
+ if (isset($this->cached_display_name)) {
+ return $this->cached_display_name;
+ } else {
+ $uid = $this->uid();
+ if ($uid !== FALSE) {
+ $this->db->select('display_name');
+ $this->db->where('uid', $uid);
+ $q = $this->db->get('users');
+ if ($q->num_rows() > 0) {
+ $row = $q->row();
+ return $this->cached_display_name = $row->display_name;
+ }
+ }
+ }
+ return FALSE;
+ }
+ public function level($new=NULL) {
+ // TODO TODO TODO
+ $this->cached_level = $new;
+ }
+
+ public function is_logged_in() {
+ return $this->sid() > 0;
+ }
+
public function check_login() {
if (!$this->logged_in)
redirect('account/login/');
<a class="brand" href="#">WebOS Dev</a>\r
<div class="nav-collapse collapse menudiv">\r
<p class="navbar-text pull-right">\r
- Logged in as <a href="#" class="navbar-link">Username</a>\r
+ Logged in as <a href="#" class="navbar-link" id="username">Username</a>\r
</p>\r
</div>\r
</div>\r
--- /dev/null
+-- MySQL dump 10.13 Distrib 5.5.27, for debian-linux-gnu (x86_64)
+--
+-- Host: localhost Database: dime_wos
+-- ------------------------------------------------------
+-- Server version 5.5.27-1~dotdeb.0
+
+/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
+/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
+/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
+/*!40101 SET NAMES utf8 */;
+/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
+/*!40103 SET TIME_ZONE='+00:00' */;
+/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
+/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
+/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
+/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
+
+--
+-- Table structure for table `apps`
+--
+
+DROP TABLE IF EXISTS `apps`;
+/*!40101 SET @saved_cs_client = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `apps` (
+ `aid` int(10) unsigned NOT NULL AUTO_INCREMENT,
+ `appname` varchar(100) NOT NULL,
+ `parent` int(10) unsigned NOT NULL,
+ `filename` varchar(100) NOT NULL,
+ `access` enum('user','operator','manager') NOT NULL DEFAULT 'user',
+ PRIMARY KEY (`aid`),
+ UNIQUE KEY `appname` (`appname`)
+) ENGINE=InnoDB DEFAULT CHARSET=latin1;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `apps`
+--
+
+LOCK TABLES `apps` WRITE;
+/*!40000 ALTER TABLE `apps` DISABLE KEYS */;
+/*!40000 ALTER TABLE `apps` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `categories`
+--
+
+DROP TABLE IF EXISTS `categories`;
+/*!40101 SET @saved_cs_client = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `categories` (
+ `cid` int(10) unsigned NOT NULL AUTO_INCREMENT,
+ `catname` varchar(100) NOT NULL,
+ PRIMARY KEY (`cid`),
+ UNIQUE KEY `catname` (`catname`)
+) ENGINE=InnoDB DEFAULT CHARSET=latin1;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `categories`
+--
+
+LOCK TABLES `categories` WRITE;
+/*!40000 ALTER TABLE `categories` DISABLE KEYS */;
+/*!40000 ALTER TABLE `categories` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `session_apps`
+--
+
+DROP TABLE IF EXISTS `session_apps`;
+/*!40101 SET @saved_cs_client = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `session_apps` (
+ `iid` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
+ `sid` int(10) unsigned NOT NULL,
+ `aid` int(10) unsigned NOT NULL,
+ PRIMARY KEY (`iid`)
+) ENGINE=InnoDB DEFAULT CHARSET=latin1;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `session_apps`
+--
+
+LOCK TABLES `session_apps` WRITE;
+/*!40000 ALTER TABLE `session_apps` DISABLE KEYS */;
+/*!40000 ALTER TABLE `session_apps` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `sessions`
+--
+
+DROP TABLE IF EXISTS `sessions`;
+/*!40101 SET @saved_cs_client = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `sessions` (
+ `sid` int(10) unsigned NOT NULL AUTO_INCREMENT,
+ `uid` int(10) unsigned NOT NULL,
+ `started` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
+ `last` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
+ `lockip` text NOT NULL,
+ PRIMARY KEY (`sid`)
+) ENGINE=InnoDB DEFAULT CHARSET=latin1;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `sessions`
+--
+
+LOCK TABLES `sessions` WRITE;
+/*!40000 ALTER TABLE `sessions` DISABLE KEYS */;
+/*!40000 ALTER TABLE `sessions` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `users`
+--
+
+DROP TABLE IF EXISTS `users`;
+/*!40101 SET @saved_cs_client = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `users` (
+ `uid` int(10) unsigned NOT NULL AUTO_INCREMENT,
+ `username` varchar(15) NOT NULL,
+ `password` char(40) NOT NULL,
+ `salt` char(5) NOT NULL,
+ `display_name` varchar(100) NOT NULL,
+ `level` enum('user','operator','manager') NOT NULL DEFAULT 'user',
+ PRIMARY KEY (`uid`)
+) ENGINE=InnoDB DEFAULT CHARSET=latin1;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `users`
+--
+
+LOCK TABLES `users` WRITE;
+/*!40000 ALTER TABLE `users` DISABLE KEYS */;
+/*!40000 ALTER TABLE `users` ENABLE KEYS */;
+UNLOCK TABLES;
+/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
+
+/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
+/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
+/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
+/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
+/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
+/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
+/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
+
+-- Dump completed on 2012-10-11 11:19:30
-jQuery.noConflict();
+var state
function loadDefaults() {
jQuery.getJSON("/backend/logged_in", function(data) {
function loadLoginModal() {
jQuery.ajax({
url: "/backend/login_modal",
- success: function (data) { jQuery('body').append(data); },
+ success: function (data) {
+ jQuery('body').append(data);
+ jQuery('#loginModal').modal({
+ backdrop: 'static',
+ keyboard: false,
+ });
+ jQuery('#loginModal').modal('show');
+ },
dataType: 'html'
- }).done(function() {
- jQuery('#loginModal').modal({
- backdrop: 'static',
- keyboard: false,
- });
- jQuery('#loginModal').modal('show');
});
}
'username': jQuery('#inputUsername').val(),
'password': jQuery('#inputPassword').val()
};
- jQuery.post('/account/login', loginData, function(data) {
- if (data.loggedIn) {
+ jQuery.post('/account/login', loginData, function(resp) {
+ if (resp.success) {
+ for (key in resp.data) {
+ state[key] = resp.data[key]
+ }
jQuery('#loginModal').modal('hide');
loadMenu();
loadBackground();
- };
+ }
}, "json");
}
-loadDefaults();
+jQuery(function () {
+ state = {}
+
+ jQuery.noConflict();
+ loadDefaults();
+});