Skip to content
This repository has been archived by the owner on Jun 3, 2020. It is now read-only.

Commit

Permalink
Refactored into template and base controllers, resolved #20
Browse files Browse the repository at this point in the history
  • Loading branch information
vimofthevine committed Jun 16, 2010
1 parent 321d8ef commit 87871f1
Show file tree
Hide file tree
Showing 55 changed files with 879 additions and 961 deletions.
19 changes: 19 additions & 0 deletions LICENSE.md
@@ -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.
22 changes: 22 additions & 0 deletions README.md
@@ -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).

3 changes: 3 additions & 0 deletions RELEASENOTES.md
@@ -0,0 +1,3 @@
# Admin Module Release Notes

* June 15, 2010 - **API change** - Refactored functions into separate template and base controllers (issue #20)
3 changes: 3 additions & 0 deletions classes/controller/admin.php
@@ -0,0 +1,3 @@
<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Admin extends Controller_Admin_Base { }
131 changes: 56 additions & 75 deletions classes/controller/admin/auth.php
@@ -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');
}

/**
Expand All @@ -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]));
}

}

0 comments on commit 87871f1

Please sign in to comment.