]> jfr.im git - z_archive/KronOS.git/commitdiff
Made JSON library; updated (some of?) the JSON output calls; made a login page.
authorJohn Runyon <redacted>
Tue, 16 Oct 2012 08:19:28 +0000 (11:19 +0300)
committerJohn Runyon <redacted>
Tue, 16 Oct 2012 08:19:28 +0000 (11:19 +0300)
There IS editing to be done to fully update to use the JSON library etc.

application/config/autoload.php
application/controllers/account.php
application/controllers/backend.php
application/libraries/Json.php [new file with mode: 0644]
application/models/json.php [deleted file]
application/models/user.php
application/views/main_view.php
dump.sql [new file with mode: 0644]
public/js/application.js

index 9e04ba827da9349f55f51ab025944d22a80f5e4c..bd602bd8d62d36134c319e07cb64be3352e7927e 100644 (file)
@@ -52,7 +52,7 @@ $autoload['packages'] = array();
 |      $autoload['libraries'] = array('database', 'session', 'xmlrpc');
 */
 
-$autoload['libraries'] = array('database');
+$autoload['libraries'] = array('database', 'json');
 
 
 /*
index 330df6b471b7f40ac08f84cd997f9e70093fa21b..baf7f63613839bd46bcfdea052f650354132c5b4 100644 (file)
@@ -12,21 +12,27 @@ class Account extends CI_Controller {
        }\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
index f451223c7675e3e8a719079974c0aee48f045121..700f3a60d6dba868d849533ba90a05cc9e78d205 100644 (file)
@@ -10,11 +10,10 @@ class Backend extends CI_Controller {
        }
 
        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() {
diff --git a/application/libraries/Json.php b/application/libraries/Json.php
new file mode 100644 (file)
index 0000000..3d04980
--- /dev/null
@@ -0,0 +1,26 @@
+<?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);
+       }
+}
diff --git a/application/models/json.php b/application/models/json.php
deleted file mode 100644 (file)
index a102b84..0000000
+++ /dev/null
@@ -1,33 +0,0 @@
-<?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);
-       }
-}
index bf36639a8e991ec4904e3ec80991227b5fef50d7..5d4ea8055db913ffe8268c6c9ddcbdf6a058603f 100644 (file)
 <?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/');
index 48f9a2a9b08fd1493499a35742d902b29006c86c..97b1e024df3bd5448ddad4286c1f75e4f50f7147 100644 (file)
@@ -20,7 +20,7 @@
                                        <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
diff --git a/dump.sql b/dump.sql
new file mode 100644 (file)
index 0000000..6de24f6
--- /dev/null
+++ b/dump.sql
@@ -0,0 +1,155 @@
+-- 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
index 91069e6fcc88e9db27c7ef7ff4ea248f06e3e9df..1a85954f84ed97aa8d5a3464824cb6eb2cbe976a 100644 (file)
@@ -1,4 +1,4 @@
-jQuery.noConflict();
+var state
 
 function loadDefaults() {
        jQuery.getJSON("/backend/logged_in", function(data) {
@@ -19,14 +19,15 @@ function loadBackground() {
 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');
        });
 
 }
@@ -50,13 +51,21 @@ function submitLogin() {
                '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();
+});