Skip to content

Commit

Permalink
New session manager and partial groups
Browse files Browse the repository at this point in the history
This commit includes a new session manager, but there is also a lot of
edits on adding member groups that I'm not sure is complete or not, but
I found was already changed in my local copy and didn't want to discard.
  • Loading branch information
xmeltrut committed Mar 11, 2012
1 parent 50c2d91 commit 47e4fff
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 26 deletions.
11 changes: 11 additions & 0 deletions application/admin/controllers/MembersController.php
Expand Up @@ -408,6 +408,7 @@ public function index () {
*/
private function standardForm ($action, $data = array()) {

// add standard form elements
$form = new \FormBuilder();
$form->addInput("email", LANG_EMAIL, arrSet($data, "memberEmail"));
$form->addInput("forename", LANG_FORENAME, arrSet($data, "memberForename"));
Expand All @@ -417,6 +418,15 @@ private function standardForm ($action, $data = array()) {
$form->addTextArea("address", LANG_ADDRESS, arrSet($data, "memberAddress"));
$form->addTextArea("notes", LANG_NOTES, arrSet($data, "memberNotes"));

// add groups
$groups = $this->groupsModel->get();
for ($i = 0; $i < count($groups); $i++) {
$group = $groups[$i];
$form->addCheckbox("groups", LANG_GROUPS, $group->groupName, "123");
$form->addCheckbox("groups", "", "Name of element", "123");
}

// add custom fields
$fields = $this->fieldsModel->get();
foreach ($fields as $field) {
$fieldID = "custom".$field->fieldID;
Expand All @@ -432,6 +442,7 @@ private function standardForm ($action, $data = array()) {
}
}

// finish form
$form->addHidden("id", arrSet($data, "memberID"));
$form->addHidden("action", $action);
$form->addSubmit();
Expand Down
2 changes: 1 addition & 1 deletion application/admin/models/AuditEntriesModel.php
Expand Up @@ -144,7 +144,7 @@ private function htmlOutput ($data) {
public function insert ($actionID, $oldData = "", $newData = "") {

// grab the user ID
$memberID = intval($_SESSION["sp_user_id"]);
$memberID = intval(SessionManager::get("sp_user_id"));

// insert into database
$sql = "INSERT INTO ".DB_PREFIX."audit_entries (
Expand Down
4 changes: 4 additions & 0 deletions application/admin/resources/admin.css
Expand Up @@ -90,6 +90,10 @@ label {
cursor: pointer;
}

label.noclick {
cursor: auto;
}

.message {
margin-top: 10px;
padding: 1em;
Expand Down
29 changes: 12 additions & 17 deletions library/classes/Authorisation.php
Expand Up @@ -28,8 +28,8 @@ public function encodePassword ($password) {
* @return int AdminStyle
*/
public function getAdminStyle () {
if (isset($_SESSION["sp_admin_style"])) {
return intval($_SESSION["sp_admin_style"]);
if (SessionManager::get("sp_admin_style")) {
return intval(SessionManager::get("sp_admin_style"));
} else {
return 0;
}
Expand All @@ -41,25 +41,20 @@ public function getAdminStyle () {
* @return int ID
*/
public function getID () {
if (isset($_SESSION["sp_user_id"])) {
return intval($_SESSION["sp_user_id"]);
if (SessionManager::get("sp_user_id")) {
return intval(SessionManager::get("sp_user_id"));
} else {
return 0;
}
}

/**
* Singleton
*
* @param boolean $withSession Sometimes, we may just want functionality
*/
public static function getInstance ($withSession = true) {
public static function getInstance () {
if (!isset(self::$instance)) {
$className = __CLASS__;
self::$instance = new $className;
if ($withSession) {
session_start();
}
}
return self::$instance;
}
Expand All @@ -71,8 +66,8 @@ public static function getInstance ($withSession = true) {
*/
public function isLoggedIn () {

if (isset($_SESSION["sp_logged_in"])) {
if ($_SESSION["sp_logged_in"] == "true") {
if (SessionManager::get("sp_logged_in")) {
if (SessionManager::get("sp_logged_in") == "true") {
return true;
}
}
Expand Down Expand Up @@ -124,9 +119,9 @@ public function login ($email, $password, &$msg) {

// check for success
if ($success) {
$_SESSION["sp_logged_in"] = "true";
$_SESSION["sp_user_id"] = $row["memberID"];
$_SESSION["sp_admin_style"] = $row["memberAdminStyle"];
SessionManager::set("sp_logged_in", "true");
SessionManager::set("sp_user_id", $row["memberID"]);
SessionManager::set("sp_admin_style", $row["memberAdminStyle"]);
return true;
} else {
$msg = "There was no match for the username and password.";
Expand All @@ -139,7 +134,7 @@ public function login ($email, $password, &$msg) {
* Log a user out
*/
public function logout () {
$_SESSION["sp_logged_in"] = "false";
SessionManager::set("sp_logged_in", "false");
session_destroy();
}

Expand All @@ -150,7 +145,7 @@ public function logout () {
* @return boolean Success
*/
public function setAdminStyle ($value) {
$_SESSION["sp_admin_style"] = intval($value);
SessionManager::set("sp_admin_style", intval($value));
return true;
}

Expand Down
2 changes: 0 additions & 2 deletions library/classes/BaseController.php
Expand Up @@ -7,8 +7,6 @@
* @subpackage Core
*/

require("classes/TemplateEngine.php");

abstract class BaseController {

/**
Expand Down
3 changes: 1 addition & 2 deletions library/classes/Database.php
Expand Up @@ -30,8 +30,7 @@ public static function getInstance () {
self::$instance = new $className;

try {
self::$connection = new PdoWrapper("mysql:host=".DB_HOST.";dbname=".DB_NAME,DB_USER,DB_PASS);
//self::$connection = new PdoWrapper("sqlite:/tmp/societaspro.sq3");
self::$connection = new PdoWrapper(DB_TYPE.":host=".DB_HOST.";dbname=".DB_NAME,DB_USER,DB_PASS);
self::$connection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} catch (PDOException $e) {
throw new GeneralException($e->getMessage());
Expand Down
20 changes: 20 additions & 0 deletions library/classes/FormBuilder.php
Expand Up @@ -39,6 +39,26 @@ function __construct ($action = "", $method = "post") {

}

/**
* Add a checkbox
*
* @param string $name Name to give the element
* @param string $label Text for label
* @param string $description Text next to element
* @param string $value Value of element
* @param boolena $default Checked by default
*/
public function addCheckbox ($name, $label, $description, $value, $default = false) {

$checked = ($default) ? 'checked="checked"' : '';
$label = ($label == "") ? "&nbsp;" : $label;

$code = '<label class="noclick">'.$label.'</label>
<input type="checkbox" name="'.$name.'" id="'.$name.'" value="'.$name.'" $checked /> '.$description;
$this->appendRow($code);

}

/**
* Add a date/time selector.
*
Expand Down
7 changes: 5 additions & 2 deletions library/classes/FrontController.php
Expand Up @@ -150,13 +150,16 @@ public function getParam ($index) {
*/
private function parseVariables () {

// requested uri
$requestedUri = isset($_SERVER["REQUEST_URI"]) ? $_SERVER["REQUEST_URI"] : "/";

// get the requested URI
if (ROOT == "/") {
// mod_rewrite
$url = $_SERVER["REQUEST_URI"];
$url = $requestedUri;
} else {
// we're not using mod_rewrite
$url = str_replace(ROOT, "", $_SERVER["REQUEST_URI"]);
$url = str_replace(ROOT, "", $requestedUri);
}

// remove any querystring
Expand Down
71 changes: 71 additions & 0 deletions library/classes/SessionManager.php
@@ -0,0 +1,71 @@
<?php
/**
* The session manager manages all access to $_SESSION. This way
* we can isolate the functions for testing, or if we want to
* store session information in a different manner.
*
* It is implemented in a "singleton-like" pattern, where you can
* only have one instance, but that is created automatically
* when you call one of it's functions.
*
* This needs refactoring into a proper singleton, so we can allow
* people ot instance it without actually using session cookies.
*
* @author Chris Worfolk <chris@societaspro.org>
* @package SocietasPro
* @subpackage Core
*/

class SessionManager extends Singleton {

private static $instance;

/**
* Get a session variable
*
* @param string $key Session variable key
* @return mixed Value
*/
public static function get ($key) {

self::getInstance();

if (isset($_SESSION[$key])) {
return $_SESSION[$key];
} else {
return false;
}

}

/**
* Singleton
*
* @return boolean Success
*/
private static function getInstance () {
if (!isset(self::$instance)) {
$className = __CLASS__;
self::$instance = new $className;
session_start();
}
return true;
}

/**
* Set a session variable
*
* @param string $key Variable key
* @param mixed $value Value
* @return boolean Success
*/
public static function set ($key, $value) {

self::getInstance();

$_SESSION[$key] = $value;
return true;

}

}
2 changes: 1 addition & 1 deletion public_html/install/includes/install.php
Expand Up @@ -69,7 +69,7 @@ function install ($groupName, $language, $email, $password, &$msg) {
$sth->execute(array($language, "language"));

// encode user's password
$auth = Authorisation::getInstance(false);
$auth = Authorisation::getInstance();
$pass = $auth->encodePassword($password);

// create a user
Expand Down
16 changes: 15 additions & 1 deletion tests/phpunit/bootstrap.php
Expand Up @@ -12,8 +12,22 @@

// autoload function
function autoloadForTest($className) {

require("../../library/templates/{$className}.php");
}

// register autoload function
spl_autoload_register("autoloadForTest");

// set the include path
$includePaths = array (
"../../library",
"../../application/common"
);

foreach ($includePaths as $path) {
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
}

// start the session
session_start();

0 comments on commit 47e4fff

Please sign in to comment.