Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Kohana 3.1 upgrade

  • Loading branch information...
commit 1c0206a3680aa542e38b1b839a4b228d90addd7e 1 parent f806fd4
@deepwinter deepwinter authored
Showing with 794 additions and 68 deletions.
  1. +10 −0 application/classes/controller/welcome.php
  2. +16 −0 application/config/auth.php
  3. 0  application/logs/.gitignore
  4. +2 −3 modules/mopcms/classes/model/content.php
  5. +52 −55 modules/mopcms/classes/model/page.php
  6. +3 −1 modules/mopcms/classes/mop/cmsinterface.php
  7. +3 −0  modules/mopcms/config/cms.php
  8. +0 −2  modules/mopcms/config/mop_cms.php
  9. +4 −2 modules/mopcore/classes/controller/ajax.php
  10. +53 −1 modules/mopcore/classes/controller/mop.php
  11. +103 −0 modules/mopcore/classes/moputil.php
  12. +0 −1  modules/mopcore/views/footer_admin.php
  13. +1 −1  modules/mopcore/views/header_admin.php
  14. +12 −0 modules/mopextras/classes/controller/keepalive.php
  15. +189 −0 modules/mopextras/classes/controller/mopauth.php
  16. +19 −0 modules/mopextras/views/auth/forgot.php
  17. +61 −0 modules/mopextras/views/auth/install.php
  18. +14 −0 modules/mopextras/views/auth/template.php
  19. +7 −0 modules/mopextras/views/logged_in_as.php
  20. +1 −0  modules/mopextras/views/noaccess.php
  21. +226 −0 modules/usermanagement/classes/controller/usermanagement.php
  22. +18 −2 modules/usermanagement/config/usermanagement.php
View
10 application/classes/controller/welcome.php
@@ -0,0 +1,10 @@
+<?php defined('SYSPATH') or die('No direct script access.');
+
+class Controller_Welcome extends Controller {
+
+ public function action_index()
+ {
+ $this->response->body('hello, world!');
+ }
+
+} // End Welcome
View
16 application/config/auth.php
@@ -0,0 +1,16 @@
+<?php defined('SYSPATH') or die('No direct access allowed.');
+
+return array(
+
+ 'driver' => 'file',
+ 'hash_method' => 'sha256',
+ 'hash_key' => 'aasd9asdf093as903s',
+ 'lifetime' => 1209600,
+ 'session_key' => 'auth_user',
+
+ // Username/password combinations for the Auth File driver
+ 'users' => array(
+ // 'admin' => 'b3154acf3a344170077d11bdb5fff31532f679a1919e716a02',
+ ),
+
+);
View
0  application/logs/.gitignore
No changes.
View
5 modules/mopcms/classes/model/content.php
@@ -41,15 +41,14 @@ public function __get($column){
if(in_array($column, $this->nonmappedfields)){
return parent::__get($column);
}
- //echo '<br>getting'.$column.'<br>';
-
+
//check for dbmap
$object = ORM::Factory('page', parent::__get('page_id'));
//echo 'FROM '.$object->id.'<br>';
$column = mop::dbmap( $object->template_id, $column);
//echo 'which maps to'.$column;
if(!$column){
- return null;
+ throw new Kohana_Exception('Column :column not found in content model', array(':column', $column));
}
if(strstr($column, 'object')){
View
107 modules/mopcms/classes/model/page.php
@@ -5,62 +5,59 @@
*
*/
class Model_Page extends ORM {
- protected $_belongs_to = array(
- 'template'=>array()
- );
- protected $_has_one = array(
- 'template'=>array()
- );
-
- public $content = null;
-
- private $object_fields = array('loaded', 'template', 'primary_key', 'primary_val');
-
- public function __construct($id=NULL){
- parent::__construct($id);
- // $this->object_fields = array_merge($this->object_fields, array_keys($this->_column_cache) );
- }
- /**
- * * Allows a model to be loaded by username or email address.
- * */
- public function unique_key($id)
- {
- if ( ! empty($id) AND is_string($id) AND ! ctype_digit($id))
- {
- return 'slug';
- }
-
- return parent::unique_key($id);
- }
-
- /*
- * Function: __get
- * Custom getter for this model, links in appropriate content table
- * when related object 'content' is requested
- * */
- public function __get($column){
-
- if($column=='contenttable' && !isset($this->_related[$column])){
- $content = ORM::factory( inflector::singular('contents') );
- $content->setTemplateName($this->template->templatename); //set the templatename for dbmapping
- $this->_related[$column]=$content->where('page_id','=',$this->id)->find();
- if(!$this->_related[$column]->_loaded){
- throw new Kohana_User_Exception('BAD_MOP_DB', 'no content record for page '.$this->id);
- }
- return $this->_related[$column];
- } else if($column=='parent'){
- //return ORM::Factory('page', $this->parentid);
- } else {
- return parent::__get($column);
- }
- }
-
-
- /*
- Function: __set
- Custom setter, saves to appropriate contenttable
- */
+ protected $_belongs_to = array(
+ 'template' => array()
+ );
+ protected $_has_one = array(
+ 'template' => array()
+ );
+ public $content = null;
+ private $object_fields = array('loaded', 'template', 'primary_key', 'primary_val');
+
+ public function __construct($id=NULL) {
+ parent::__construct($id);
+ // $this->object_fields = array_merge($this->object_fields, array_keys($this->_column_cache) );
+ }
+
+ /**
+ * * Allows a model to be loaded by username or email address.
+ * */
+ public function unique_key($id) {
+ if (!empty($id) AND is_string($id) AND !ctype_digit($id)) {
+ return 'slug';
+ }
+
+ return parent::unique_key($id);
+ }
+
+ /*
+ * Function: __get
+ * Custom getter for this model, links in appropriate content table
+ * when related object 'content' is requested
+ * */
+
+ public function __get($column) {
+
+ if ($column == 'contenttable' && !isset($this->_related[$column])) {
+ $content = ORM::factory(inflector::singular('contents'));
+ $content->setTemplateName($this->template->templatename); //set the templatename for dbmapping
+ $this->_related[$column] = $content->where('page_id', '=', $this->id)->find();
+ if (!$this->_related[$column]->_loaded) {
+ throw new Kohana_User_Exception('BAD_MOP_DB', 'no content record for page ' . $this->id);
+ }
+ return $this->_related[$column];
+ } else if ($column == 'parent') {
+ return ORM::Factory('page', $this->parentid);
+ } else {
+ return parent::__get($column);
+ }
+ }
+
+ /*
+ Function: __set
+ Custom setter, saves to appropriate contenttable
+ */
public function __set($column, $value){
if($column=='contenttable'){
$this->_changed[$column] = $column;
View
4 modules/mopcms/classes/mop/cmsinterface.php
@@ -140,7 +140,9 @@ public function action_savefield($id){
}
$page = ORM::Factory('page')->find($id);
- return array('value'=>$page->contenttable->$_POST['field']);
+ //echo $_POST['field'];
+ $value = $page->contenttable->$_POST['field'];
+ $this->response->data(array('value'=>$value));
}
/*
View
3  modules/mopcms/config/cms.php
@@ -2,4 +2,7 @@
$config['navigationRequest'] = 'navigation';
+$config['layout'] = 'LayoutAdmin';
+//$config['authrole'] = 'admin';
+
return $config;
View
2  modules/mopcms/config/mop_cms.php
@@ -14,8 +14,6 @@
'modules/mop/MoPModules.js'
);
-$config['layout'] = 'LayoutAdmin';
-$config['authrole'] = 'admin';
$config['defaultsettings']['editable_title'] = true;
//- - if set all titles editable
View
6 modules/mopcore/classes/controller/ajax.php
@@ -27,11 +27,13 @@ public function action_data($uri)
$subRequest = Request::Factory($uri);
$data = $subRequest->execute()->data();
} catch (Exception $e) {
+ //return HTML from exception
+ $message = $e->getString() . $e->getTrace();
$ajaxResponse = array(
'returnValue' => TRUE,
+ 'response' => $message
+
);
- echo "CALL A FUNCITON HERE TO COMMUNICATION EXCEPTION TO CLIENTSIDE";
- throw $e;
return;
}
$ajaxResponse = array(
View
54 modules/mopcore/classes/controller/mop.php
@@ -18,17 +18,69 @@ class Controller_MOP extends Controller {
public function __construct($request, $response){
parent::__construct($request, $response);
+ $this->controllerName = strtolower(substr(get_class($this), 11));
+ $this->checkAccess();
if($request->is_initial()){
self::$topController = $this;
}
//look up all matching js and css based off controller name
- $this->controllerName = strtolower(substr(get_class($this), 11));
$this->loadResources($this->controllerName);
}
+ /*
+ * Function: checkAccess()
+ * Default function for acccess checking for a controller. Can be overridden in child classes
+ * Checks logged in user against authrole array in config file for controller
+ * Parameters:nothing, except config file
+ * Returns: nothing
+ */
+ public function checkAccess(){
+ //Authentication check
+ $role = Kohana::config(strtolower($this->controllerName).'.authrole', FALSE, FALSE);
+
+ //checked if logged in
+ if($role && !Auth::instance()->logged_in()){
+ Request::current()->redirect('auth/login/'.Request::initial()->uri());
+ exit;
+ }
+
+ if(is_array($role)){
+ $accessGranted = false;
+ foreach($role as $aRole){
+ if($role=='admin'){
+ if(Kohana::config('mop.staging_enabled') && !Kohana::config('mop.staging')){
+ $redirect = 'staging/'. Router::$current_uri;
+ url::redirect($redirect);
+ }
+ }
+
+ if(moputil::checkRoleAccess($aRole)){
+ $accessGranted = true;
+ }
+ }
+ } else {
+ if($role=='admin'){
+ if(Kohana::config('mop.staging_enabled') && !Kohana::config('mop.staging')){
+ $redirect = 'staging/'. Router::$current_uri;
+ url::redirect($redirect);
+ }
+ }
+
+ $accessGranted = moputil::checkRoleAccess($role);
+ }
+
+ if(!$accessGranted){
+ $redirect = 'accessdenied';
+ url::redirect($redirect);
+ exit;
+ }
+
+ }
+
+
protected function loadResources($key){
if(is_subclass_of(self::$topController, 'Controller_MOP')){
//should add to self, then merge into topController
View
103 modules/mopcore/classes/moputil.php
@@ -0,0 +1,103 @@
+<?
+
+/*
+ * Class: moputil
+ * Utility helper class
+ */
+Class moputil {
+
+ /*
+ * Function: getMicroSeconds()
+ * Returns a microseconds of current time as a 3 place float
+ * Returns: Microseconds
+ */
+ public static function getMicroSeconds(){
+ list($usec, $sec) = explode(" ", microtime());
+ return number_format((float)$usec, 3);
+ }
+
+ /*
+ * Function getMicroTimestamp()
+ * Creates a timestamp including microseconds
+ * Returns: Microsecond timestamp
+ */
+ public static function getMicroTimestamp(){
+ $timestamp = date('YmdHis') . substr(moputil::getMicroSeconds(), 1) ;
+ Kohana::log('info', 'TIMESTAMP: '.$timestamp);
+ return $timestamp;
+ }
+
+ /*
+ * Function: img($file, $prefix, $alt, $extra = null){
+ * Echos an image tag as built from a mop file object.
+ * Parameters:
+ * $file - mop file object
+ * $prefix - the prefix to output
+ * $alt - alt tag phrase
+ * $extra - extra stuff to go inside the tag attributes area
+ * Returns: if file exists, return the img src tag code, otherwise return null
+ */
+ public static function img($file, $prefix, $alt, $extra = null){
+ if(!$file->$prefix->fullpath
+ || !file_exists($file->$prefix->fullpath)){
+ return null;
+ }
+ $img = sprintf('<img src="%s" width="%s" height="%s" alt="%s" %s>',
+ $file->$prefix->fullpath,
+ $file->$prefix->width,
+ $file->$prefix->height,
+ $alt,
+ $extra);
+ return $img;
+ }
+
+ /*
+ * Function: checkRoleAccess($role)
+ * Checks whether the currently logged in user has a certain role
+ * Parameters:
+ * $role - the role to check against
+ * Returns: true or false
+ */
+ public static function checkRoleAccess($role){
+
+ if($role && !Auth::instance()->logged_in($role)){
+ return false;
+ } else {
+ return true;
+ }
+ }
+
+ /*
+ * Function: decode_recurse($value)
+ */
+ private static function decode_recurse($value){
+ //handle object?
+ if(!is_array($value)){
+ return html_entity_decode($value);
+ } else {
+ for($i=0, $keys=array_keys($value), $count=count($value); $i<$count; $i++){
+ $value[$keys[$i]] = moputil::decode_recurse($value[$keys[$i]]);
+ }
+ return $value;
+ }
+ }
+
+ public static $modulos;
+ public static $modulosOptionsCount;
+ public static function modulo($identifier, $options){
+ self::$modulosOptionsCount = count( $options );
+ if(!is_array(self::$modulos)){
+ self::$modulos = array();
+ }
+ if(!isset(self::$modulos[$identifier])){
+ self::$modulos[$identifier] = 0;
+ }
+ $index = self::$modulos[$identifier];
+ self::$modulos[$identifier]++;
+ return $options[$index%self::$modulosOptionsCount];
+
+ }
+
+
+
+}
View
1  modules/mopcore/views/footer_admin.php
@@ -1 +0,0 @@
-This is the default public footer
View
2  modules/mopcore/views/header_admin.php
@@ -1 +1 @@
-This is the default public header
+<? echo Request::Factory('mopauth')->execute()->body() ;?>
View
12 modules/mopextras/classes/controller/keepalive.php
@@ -0,0 +1,12 @@
+<?
+
+class KeepAlive_Controller extends Controller {
+
+ public function index(){
+ if(Auth::instance()->logged_in()){
+ echo 'true';
+ } else {
+ echo 'false';
+ }
+ }
+}
View
189 modules/mopextras/classes/controller/mopauth.php
@@ -0,0 +1,189 @@
+<?php defined('SYSPATH') or die('No direct script access.');
+/**
+ * Auth module demo controller. This controller should NOT be used in production.
+ * It is for demonstration purposes only!
+ *
+ * $Id: auth_demo.php 3267 2008-08-06 03:44:02Z Shadowhand $
+ *
+ * @package MopAuth
+ * @author Deepwinter
+ * @copyright (c) 2007-2008 Kohana Team
+ * @license http://kohanaphp.com/license.html
+ */
+class Controller_MopAuth extends Controller {
+
+ // Do not allow to run in production
+ const ALLOW_PRODUCTION = TRUE;
+
+ // Use the default Kohana template
+ public $defaulttemplate = 'auth/template';
+
+ public $message = '';
+
+ public function __construct($request, $response){
+ parent::__construct($request, $response);
+ $this->view = new View($this->defaulttemplate);
+
+ }
+
+ public function action_index()
+ {
+ return;
+ $this->action_login();
+ }
+
+ public function action_create()
+ {
+ $this->view->title = 'Create User';
+
+ $form = new Forge('auth/create');
+ $form->input('email')->label(TRUE)->rules('required|length[4,32]|valid_email');
+ $form->input('username')->label(TRUE)->rules('required|length[4,32]');
+ $form->password('password')->label(TRUE)->rules('required|length[5,40]');
+ $form->submit('Create New User');
+
+ if ($form->validate())
+ {
+ // Create new user
+ $user = ORM::factory('user');
+
+ if ( ! $user->username_exists($form->username->value))
+ {
+ foreach ($form->as_array() as $key => $val)
+ {
+ // Set user data
+ $user->$key = $val;
+ }
+
+ if ($user->save() AND $user->add(ORM::factory('role', 'login')))
+ {
+ Auth::instance()->login($user, $form->password->value);
+
+ // Redirect to the login page
+ url::redirect('auth/login');
+ }
+
+ } else {
+ }
+ }
+
+ // Display the form
+ $this->view->content = $form->render();
+ }
+
+ public function action_login($redirect = null)
+ {
+ if (Auth::instance()->logged_in())
+ {
+ if($this->message){
+ $this->view->title = $this->message;
+ } else if($redirect = Kohana::config('auth.redirect')){
+ url::redirect($redirect);
+ } else {
+ $this->view->title = 'User Logout';
+ }
+
+ $form = new Forge('auth/logout');
+ $form->submit('Logout Now');
+ }
+ else
+ {
+ if($redirect == 'resetPasswordSuccess'){
+ $this->view->message = Kohana::lang('auth.resetPasswordSuccess');
+ $redirect = null;
+ }
+ $this->view->title = 'User Login';
+
+ $form = new Forge('auth/login');
+ $form->input('username')->label(TRUE)->rules('required|length[4,32]');
+ $form->password('password')->label(TRUE)->rules('required|length[5,40]');
+ if($redirect){
+ $form->hidden('redirect')->value($redirect);
+ }
+ $form->submit('Attempt Login');
+
+ if ( $form->validate())
+ {
+ // Load the user
+ $user = ORM::factory('user', $form->username->value);
+
+ if (Auth::instance()->login($user, $form->password->value))
+ {
+ // Login successful, redirect
+ if($form->redirect){
+ url::redirect($form->redirect->value);
+ } else if($redirect = Kohana::config('auth.redirect')){
+ url::redirect($redirect);
+ } else {
+ url::redirect('auth/login');
+ }
+ }
+ else
+ {
+ $form->password->add_error('login_failed', 'Invalid username or password.');
+ }
+ }
+ }
+
+ // Display the form
+ $this->view->content = $form->render();
+ }
+
+ public function action_logout()
+ {
+ // Force a complete logout
+ Auth::instance()->logout(TRUE);
+
+ // Redirect back to the login page
+ url::redirect('auth/login');
+ }
+
+ public function action_noaccess($controller = NULL){
+ $this->message = 'You do not have access to the requested page';
+ $this->login($controller);
+ }
+
+ public function action_forgot(){
+ if(isset($_POST['email'])){
+ $user = ORM::Factory('user')->where('email', $_POST['email'])->find();
+ if($user->loaded){
+ $password = $this->randomPassword();
+ $user->password = $password;
+ $user->save();
+ $body = Kohana::lang('auth.forgotPasswordEmailBody');
+ $body = str_replace('___MOP___username___MOP___', $user->username, $body);
+ $body = str_replace('___MOP___password___MOP___', $password, $body);
+ mail($user->email, Kohana::lang('auth.forgotPasswordEmailSubject'), $body);
+ url::redirect('auth/login/resetPasswordSuccess');
+
+ } else {
+ $this->view = new View('auth/forgot');
+ $this->view->message = Kohana::lang('auth.resetPasswordFailed');
+
+ }
+ } else {
+ $this->view = new View('auth/forgot');
+ }
+ }
+
+ public function randomPassword(){
+ $password_length = 9;
+
+ function make_seed() {
+ list($usec, $sec) = explode(' ', microtime());
+ return (float) $sec + ((float) $usec * 100000);
+ }
+
+ srand(make_seed());
+
+ $alfa = "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
+ $token = "";
+ for($i = 0; $i < $password_length; $i ++) {
+ $token .= $alfa[rand(0, strlen($alfa)-1)];
+ }
+ return $token;
+
+
+ }
+
+} // End Auth Controller
View
19 modules/mopextras/views/auth/forgot.php
@@ -0,0 +1,19 @@
+<div class="modalContainer">
+ <div class="modal">
+ <h3>Forgot Password</h3>
+ <div class="container">
+ <?if(isset($message)):?><?=$message;?><?endif;?>
+ <form action="<?=Kohana::config('config.site_path');?>auth/forgot/" method="POST">
+ Please enter your email. A new password will be sent to you.<br>
+
+ <label for="emailInput">Email</label>
+ <input type="text" name="email" />
+
+ <input type="submit">
+
+ </form>
+
+ </div>
+ </div>
+ <a href="#" class="modalAnchor"></a>
+</div>
View
61 modules/mopextras/views/auth/install.php
@@ -0,0 +1,61 @@
+<div class="box">
+
+<p>The following tables must be installed in your database: <code>users</code>, <code>roles</code>, <code>roles_users</code>, and <code>user_tokens</code>. If you have not already installed these tables, please run the installation query below.</p>
+
+<p>After the tables have been installed, <?php echo html::anchor('auth_demo/create', 'create a user') ?>.</p>
+
+<p><em>This query is MySQL-specific, but should be easy to adapt to an database that supports foreign keys.</em></p>
+
+<?php echo form::textarea('query', <<<EOF
+CREATE TABLE IF NOT EXISTS `roles` (
+ `id` int(11) unsigned NOT NULL auto_increment,
+ `name` varchar(32) NOT NULL,
+ `description` varchar(255) NOT NULL,
+ PRIMARY KEY (`id`),
+ UNIQUE KEY `uniq_name` (`name`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+
+INSERT INTO `roles` (`id`, `name`, `description`) VALUES(1, 'login', 'Login privileges, granted after account confirmation');
+INSERT INTO `roles` (`id`, `name`, `description`) VALUES(2, 'admin', 'Administrative user, has access to everything.');
+
+CREATE TABLE IF NOT EXISTS `roles_users` (
+ `user_id` int(10) unsigned NOT NULL,
+ `role_id` int(10) unsigned NOT NULL,
+ PRIMARY KEY (`user_id`,`role_id`),
+ KEY `fk_role_id` (`role_id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+
+CREATE TABLE IF NOT EXISTS `users` (
+ `id` int(11) unsigned NOT NULL auto_increment,
+ `email` varchar(127) NOT NULL,
+ `username` varchar(32) NOT NULL default '',
+ `password` char(50) NOT NULL,
+ `logins` int(10) unsigned NOT NULL default '0',
+ `last_login` int(10) unsigned,
+ PRIMARY KEY (`id`),
+ UNIQUE KEY `uniq_username` (`username`),
+ UNIQUE KEY `uniq_email` (`email`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+
+CREATE TABLE IF NOT EXISTS `user_tokens` (
+ `id` int(11) unsigned NOT NULL auto_increment,
+ `user_id` int(11) unsigned NOT NULL,
+ `user_agent` varchar(40) NOT NULL,
+ `token` varchar(32) NOT NULL,
+ `created` int(10) unsigned NOT NULL,
+ `expires` int(10) unsigned NOT NULL,
+ PRIMARY KEY (`id`),
+ UNIQUE KEY `uniq_token` (`token`),
+ KEY `fk_user_id` (`user_id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+
+ALTER TABLE `roles_users`
+ ADD CONSTRAINT `roles_users_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
+ ADD CONSTRAINT `roles_users_ibfk_2` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`) ON DELETE CASCADE;
+
+ALTER TABLE `user_tokens`
+ ADD CONSTRAINT `user_tokens_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE;
+EOF
+, 'style="width:90%;height:30em;padding:0.5em"') ?>
+
+</div>
View
14 modules/mopextras/views/auth/template.php
@@ -0,0 +1,14 @@
+<div class="modalContainer">
+ <div class="modal">
+ <h3><?=$title;?></h3>
+ <div class="container">
+ <?if(isset($message)):?><?=$message;?><?endif;?>
+ <?=$content;?>
+ <a href="auth/forgot/">Forgot your password?</a>
+ </div>
+ </div>
+ <a class="modalAnchor"></a>
+ <script type="text/javascript">
+ window.addEvent( "domready", function(){ $("username").focus(); } );
+ </script>
+</div>
View
7 modules/mopextras/views/logged_in_as.php
@@ -0,0 +1,7 @@
+<div class="loginstatus">
+ <div class="controls">
+ <?if(isset($username)):?>
+ You are logged in as: <?=$username;?> <a class="button" href="<?=Kohana::config('config.site_path');?>auth/logout/">logout</a>
+ <?endif;?>
+ </div>
+</div>
View
1  modules/mopextras/views/noaccess.php
@@ -0,0 +1 @@
+You do not have access to the requested page
View
226 modules/usermanagement/classes/controller/usermanagement.php
@@ -0,0 +1,226 @@
+<?
+/*
+ * Class: UserManagement_Controllers
+ * Controller for usermangement module within MoPCMS, allows for settings up users
+ * and passwords. Can be configured deal with roles using the managedroles variables.
+ */
+
+Class Controller_UserManagement extends Controller_MOP {
+
+ /*
+ * Variable: model
+ * Defines the database table where users are stored
+ */
+ protected $table = 'user';
+
+ /*
+ * Variable: viewName
+ * Name of the main view to load, also used for javascript class and instance.
+ */
+ protected $viewName = 'usermanagement';
+
+ /*
+ * Variable: managedRoles
+ * Roles set up in the config file are loaded into this class variable
+ */
+ protected $managedRoles;
+
+ /*
+ * Function: __construct()
+ * Loads managedRoles and calls the parent constructor
+ */
+ public function __construct($request, $response){
+ parent::__construct($request, $response);
+
+ $this->managedRoles = Kohana::config(strtolower($this->controllerName).'.managedRoles');
+
+ }
+
+ /*
+ * Function: createIndexView()
+ * Subclassed function to build the top view
+ * Parameters: none
+ * Returns: nothing, sets up view to render in this->view
+ */
+ public function action_index(){
+ //cleanup on an initial load
+ $incompletes = ORM::Factory($this->table)->where('status', '=', 'INCOMPLETE')->find_all();
+ foreach($incompletes as $incomplete){
+ $incomplete->delete();
+ }
+
+ $this->view = new View($this->viewName);
+ $this->view->instance = $this->viewName;
+ $this->view->class = $this->viewName;
+
+ $users = ORM::Factory($this->table)->find_all();
+ $html = '';
+ foreach($users as $user){
+ $usertemplate = new View($this->viewName.'_item');
+ $data['id'] = $user->id;
+ $data['username'] = $user->username;
+ $data['email'] = $user->email;
+
+ if(strlen($user->password)){
+ $data['password'] = '******';
+ } else {
+ $data['password'] = '';
+ }
+
+ //find role'] = null;
+ $data['role'] = null;
+ foreach($this->managedRoles as $label=>$role){
+ if($user->has(ORM::Factory('role', $role))){
+ $data['role'] = $role;
+ }
+ }
+
+ $usertemplate->data = $data;
+
+ $usertemplate->managedRoles = $this->managedRoles;
+ $html .= $usertemplate->render();
+ }
+
+ $this->view->items = $html;
+ $this->response->body($this->view->render());
+
+ }
+
+ /*
+ * Function: addItem($pageid)
+ * Ajax interface to add a new user object to the users table.
+ * Parameters:
+ * $pageid - unused variable, interface needs to be updated
+ * Returns: Rendered html editing page for new user object
+ */
+ public function action_addItem($pageid){
+ $user = $this->createUser();
+ $data = $user->as_array();
+
+ //set no managedRole
+ $data['role'] = null;
+
+ $this->view = new View($this->viewName.'_item');
+ $this->view->data = $data;
+ $this->view->managedRoles = $this->managedRoles;
+ return $this->view->render();
+ }
+
+ /*
+ * Function: createUser()
+ * Utility function to create an empty user object in the database, with status INCOMPLETE
+ * and placeholder content for username and email
+ * Parameters: none
+ * Returns: User ORM Object, pre-saved
+ */
+ protected function action_createUser(){
+ $user = ORM::factory($this->table);
+ $user->status = 'INCOMPLETE';
+ $user->username = 'PLACEHOLDER_'.microtime();
+ $user->email = 'PLACEHOLDER_'.microtime();
+ $user->save();
+
+ //add the login role
+ $user->add(ORM::Factory('role', 'login'));
+ $user->add(ORM::Factory('role', 'admin'));
+ $user->add(ORM::Factory('role', 'staging'));
+ $user->save();
+
+ return $user;
+
+ }
+
+ /*
+ * Function: deleteItem($id)
+ * Deletes a user from the database
+ * Parameters:
+ * $id - the unique key id of the record to delete
+ * Returns: nothing
+ */
+ public function action_deleteItem($id){
+ $user = ORM::factory($this->table, $id);
+ $user->delete($id);
+
+ }
+
+ /*
+ * Function: saveField($id)
+ * Ajax interface to update a field in the database table with a new value
+ * Parameters:
+ * $id - the unique key id of the record to update
+ * $_POST['field'] - the field to update
+ * $_POST['value'] - the new value to save
+ * Returns: array('value'=>{value})
+ */
+ public function action_saveField($id){
+ $user = ORM::factory($this->table, $id);
+
+ switch($_POST['field']){
+ case 'role':
+ //first remove other managedRoles
+ foreach($this->managedRoles as $label => $role){
+ $roleObj = ORM::Factory('role', $role);
+ if($user->has($roleObj)){
+ $user->remove($roleObj);
+ }
+ }
+ $user->add(ORM::Factory('role', $_POST['value']));
+ $user->save();
+ $return = array('value'=>$_POST['value']);
+ break;
+
+ default:
+
+ $errors = $user->checkValue($_POST['field'], $_POST['value']);
+ Kohana::log('info', 'return from errors '.var_export($errors, true) );
+ if(!$errors){
+ $user->$_POST['field'] = $_POST['value'];
+ $user->save();
+
+ //this might be the first edit on a new record
+ //so set the record to active status
+ $this->activateRecord($user);
+
+
+ if($_POST['field'] == 'password'){
+ $body = new View('usermanagement_passwordchangeemail');
+ $body->username = $user->username;
+ $body->password = $_POST['value'];
+
+ mail($user->email, Kohana::config('usermanagement.passwordchangeemail.subject'), $body->render());
+ $md5 = $user->password;
+ $return = array('value'=>$md5);
+ } else {
+ $return = array('value'=>$_POST['value']);
+ }
+ } else {
+ $firstkey = array_keys($errors);
+ $firstkey = $firstkey[0];
+ if($_POST['field']=='password'){
+ $rval = null;
+ } else {
+ $rval = $_POST['value'];
+ }
+ $return = array('value'=>$rval, 'error'=>'true', 'message'=>$errors[$firstkey]);
+ }
+ break;
+ }
+ return $return;
+
+ }
+
+ /*
+ * Function: activateRecord(& $user)
+ * Switches a user record from status INCOMPLETE to status ACTIVE
+ * Parameters:
+ * $user - an ORM object to update
+ * Returns: nothing
+ */
+ protected function action_activateRecord(& $user){
+ if($user->status == 'INCOMPLETE'){
+ $user->status = 'ACTIVE';
+ $user->save();
+ }
+ }
+
+}
View
20 modules/usermanagement/config/usermanagement.php
@@ -1,7 +1,23 @@
<?
-$config['displaycontroller'] = 'DisplayAdmin';
+//this could be something like a standard include?
+$config['resources']['librarycss'] = array(
+ 'modules/mop/thirdparty/960Grid/reset.css',
+ 'modules/mop/thirdparty/960Grid/960.css'
+);
+$config['resources']['libraryjs'] = array(
+ 'modules/mop/thirdparty/mootools/mootools-1.2.4-core-nc.js',
+ 'modules/mop/thirdparty/mootools/mootools-more.js',
+ 'modules/mop/thirdparty/digitarald/fancyupload/Swiff.Uploader.js',
+ 'modules/mop/thirdparty/monkeyphysics/datepicker-nc.js',
+ 'modules/mop/MoPCore.js',
+ 'modules/mop/MoPUI.js',
+ 'modules/mop/MoPModules.js'
+);
+
+$config['layout'] = 'LayoutAdmin';
$config['authrole'] = 'admin';
+
/*
* Config: ['resources']['css']
*/
@@ -28,4 +44,4 @@
*/
$config['managedRoles'] = array();
-//array('Call Center'=>'callcenter', 'Admin'=>'admin');
+return $config;
Please sign in to comment.
Something went wrong with that request. Please try again.