Permalink
Browse files

Refactored into template and base controllers, resolved #20

  • Loading branch information...
vimofthevine committed May 30, 2010
1 parent 321d8ef commit 87871f1ff8c343e0a68078e75be0ed8c90d91a21
Showing with 879 additions and 961 deletions.
  1. +19 −0 LICENSE.md
  2. +22 −0 README.md
  3. +3 −0 RELEASENOTES.md
  4. +3 −0 classes/controller/admin.php
  5. +56 −75 classes/controller/admin/auth.php
  6. +178 −0 classes/controller/admin/base.php
  7. +70 −44 classes/controller/admin/main.php
  8. +1 −0 classes/controller/admin/media.php
  9. +0 −46 classes/controller/admin/support.php
  10. +94 −314 classes/controller/admin/users.php
  11. +14 −92 classes/controller/template/admin.php
  12. +3 −2 classes/{message/core.php → kohana/message.php}
  13. +1 −1 classes/message.php
  14. +0 −46 config/a1.php
  15. +0 −15 config/admin-example.php
  16. +17 −0 config/admin.php
  17. +7 −23 config/{auth-example.php → auth.php}
  18. +43 −0 guide/admin.acl.md
  19. +19 −0 guide/admin.dependencies.md
  20. +33 −0 guide/admin.layout.md
  21. +13 −0 guide/admin.overview.md
  22. +40 −0 guide/admin.resources.md
  23. +25 −0 guide/admin.template.md
  24. +8 −0 guide/menu.admin.md
  25. +4 −28 init.php
  26. +21 −0 messages/a2.php
  27. +16 −0 messages/admin.php
  28. +0 −10 messages/auth.php
  29. +0 −26 views/admin/auth/hmvc/login.php
  30. +26 −5 views/admin/auth/login.php
  31. +3 −29 views/admin/dashboard.php
  32. +3 −0 views/admin/layout/full_width.php
  33. +7 −0 views/admin/layout/narrow_column.php
  34. +9 −0 views/admin/layout/narrow_column_with_menu.php
  35. +1 −0 views/admin/layout/none.php
  36. +7 −0 views/admin/layout/wide_column_with_menu.php
  37. +0 −9 views/admin/support/widget/menu.php
  38. +4 −4 views/admin/themes/default/footer.php
  39. +19 −43 views/admin/themes/default/header.php
  40. +9 −18 views/admin/themes/default/menu.php
  41. +15 −6 views/admin/themes/default/template.php
  42. +0 −6 views/admin/themes/tpd/footer.php
  43. +0 −20 views/admin/themes/tpd/header.php
  44. +0 −9 views/admin/themes/tpd/menu.php
  45. +0 −16 views/admin/themes/tpd/template.php
  46. +13 −7 views/admin/users/delete.php
  47. +13 −7 views/admin/users/form.php
  48. +0 −11 views/admin/users/hmvc/delete.php
  49. +0 −15 views/admin/users/hmvc/form.php
  50. +0 −2 views/admin/users/hmvc/list.php
  51. +0 −5 views/admin/users/hmvc/none.php
  52. +0 −11 views/admin/users/hmvc/view.php
  53. +25 −6 views/admin/users/list.php
  54. +4 −4 views/admin/users/menu.php
  55. +11 −6 views/admin/users/view.php
View
@@ -0,0 +1,19 @@
+Copyright (c) 2010 Kyle Treubig
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
View
@@ -0,0 +1,22 @@
+# Admin Overview
+
+The admin module is meant to facilitate the rapid development of an end-user
+administration site. There are essentially two aspects provided by the module,
+the template and the framework.
+
+## Features
+ - Template integration, courtesy of Colonel-Rosa
+ - Resource handling
+ - Access control
+ - Automated differentiation between external requests (full layout)
+ and internal requests (partial layout)
+
+### The Admin Framework
+
+The admin module also provides a framework for creating administration pages.
+The abstract base admin controller, `Controller_Admin`, may be extended to make
+use of the template and other common functions. The common functions provided
+by the base admin controller are ACL checking (using Wouter's A2 library) and
+request handling (differentiating between main/external requests and ajax/internal
+requests).
+
View
@@ -0,0 +1,3 @@
+# Admin Module Release Notes
+
+* June 15, 2010 - **API change** - Refactored functions into separate template and base controllers (issue #20)
@@ -0,0 +1,3 @@
+<?php defined('SYSPATH') or die('No direct script access.');
+
+class Controller_Admin extends Controller_Admin_Base { }
@@ -1,104 +1,78 @@
-<?php defined('SYSPATH') OR die('No direct script access.');
+<?php defined('SYSPATH') or die('No direct script access.');
/**
- * @package Controller
+ * Authentication Controller
+ *
+ * @package Admin
+ * @category Controller
* @author Kyle Treubig
* @copyright (c) 2010 Kyle Treubig
* @license MIT
*/
-class Controller_Admin_Auth extends Controller_Template_Admin {
+class Controller_Admin_Auth extends Controller_Admin {
- /**
- * Register controller as an admin controller
- */
- public function before() {
- parent::before();
- }
+ protected $_view_map = array(
+ 'login' => 'admin/layout/narrow_column',
+ );
+
+ protected $_current_nav = 'admin/login';
/**
* Display login form and perform login
*/
public function action_login() {
Kohana::$log->add(Kohana::DEBUG, 'Executing Controller_Auth::action_login');
+ // If user is already logged in, redirect to admin main
if ($this->a2->logged_in())
{
Kohana::$log->add('ACCESS', "Attempt to login made by logged-in user");
- $message = __('You are already logged in.');
-
- // Return message if an ajax request
- if (Request::$is_ajax)
- {
- $this->template->content = $message;
- }
- // Else set flash message and redirect
- else
- {
- Message::instance()->error($message);
- Request::instance()->redirect( Route::get('admin_main')->uri() );
- }
+ Kohana::$log->add(Kohana::DEBUG, "Attempt to login made by logged-in user");
+ Message::instance()->error(Kohana::message('a2', 'login.already'));
+ $this->request->redirect( Route::get('admin')->uri() );
}
+ $this->template->content = View::factory('admin/auth/login')
+ ->bind('post', $post)
+ ->bind('errors', $errors);
+
$post = Validate::factory($_POST)
->filter(TRUE, 'trim')
->rule('username', 'not_empty')
- ->rule('password', 'not_empty');
+ ->rule('password', 'not_empty')
+ ->callback('username', array($this, 'check_username'));
if ($post->check())
{
- $user = Sprig::factory('user', array('username'=>$post['username']))->load();
-
- $remember = isset($post['remember']) ? (bool) $post['remember'] : FALSE;
-
- if ( ! $user->loaded())
- {
- Kohana::$log->add('ACCESS', 'Attempt to login made with unknown username, '.$post['username']);
- $post->error('username', 'not_found');
- }
- elseif ($this->a1->login($post['username'], $post['password'], $remember))
+ if ($this->a1->login($post['username'], $post['password'],
+ ! empty($post['remember'])))
{
- Kohana::$log->add('ACCESS', 'Successful login made with username, '.$user->username);
- $message = __('Welcome back, :name!', array(':name'=>$user->username));
-
- // Get referring URI, if any
- $referrer = $this->session->get('referrer');
- $referrer = empty($referrer) ? Route::get('admin_main')->uri() : $referrer;
- $this->session->delete('referrer');
+ Kohana::$log->add('ACCESS', 'Successful login made with username, '
+ .$post['username']);
+ Message::instance()->info(Kohana::message('a2', 'login.success'),
+ array(':name' => $post['username']));
- // Return message if an ajax request
- if (Request::$is_ajax)
- {
- $this->template->content = $message;
- }
- // Else set flash message and redirect
- else
+ // If external request, redirect to referring URL or admin main
+ if ( ! $this->_internal)
{
- Message::instance()->info($message);
- Request::instance()->redirect($referrer);
+ // Get referring URI, if any
+ $referrer = $this->session->get('referrer')
+ ? $this->session->get('referrer')
+ : Route::get('admin')->uri();
+ $this->session->delete('referrer');
+
+ $this->request->redirect($referrer);
}
}
else
{
- Kohana::$log->add('ACCESS', 'Unsuccessful login attempt made with username, '.$post['username']);
+ Kohana::$log->add('ACCESS', 'Unsuccessful login attempt made with username, '
+ .$post['username']);
$post->error('password', 'incorrect');
}
}
- $form = $errors = array(
- 'username' => '',
- 'password' => '',
- 'remember' => '',
- );
-
- $hmvc = View::factory('admin/auth/hmvc/login')
- ->set('form', Arr::overwrite($form, $post->as_array()))
- ->set('errors', Arr::overwrite($errors, $post->errors('auth')));
-
- $view = View::factory('admin/auth/login')
- ->set('form', $hmvc);
-
- // Set request response
- $this->template->content = $this->internal_request ? $hmvc : $view;
+ $errors = $post->errors('admin');
}
/**
@@ -109,20 +83,27 @@ public function action_logout() {
$this->a1->logout();
Kohana::$log->add('ACCESS', 'Successful logout made by user.');
- $message = __('You have been logged out. Goodbye!');
+ Message::instance()->info(Kohana::message('a2', 'logout.success'));
- // Return message if an ajax request
- if (Request::$is_ajax)
- {
- $this->template->content = $message;
- }
- // Else set flash message and redirect
- else
+ if ( ! $this->_internal)
{
- Message::instance()->info($message);
- Request::instance()->redirect( Route::get('admin_main')->uri() );
+ $this->request->redirect( Route::get('admin')->uri() );
}
}
+ /**
+ * Username callback to check if username is valid
+ */
+ public function check_username(Validate $array, $field)
+ {
+ $exists = (bool) DB::select(array('COUNT("*")', 'total_count'))
+ ->from('users')
+ ->where('username', '=', $array[$field])
+ ->execute()->get('total_count');
+
+ if ( ! $exists)
+ $array->error($field, 'not_found', array($array[$field]));
+ }
+
}
Oops, something went wrong.

0 comments on commit 87871f1

Please sign in to comment.