This repository has been archived by the owner on Jun 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
auth.php
128 lines (108 loc) · 3.32 KB
/
auth.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php defined('SYSPATH') OR die('No direct script access.');
/**
* @package Controller
* @author Kyle Treubig
* @copyright (c) 2010 Kyle Treubig
* @license MIT
*/
class Controller_Admin_Auth extends Controller_Template_Admin {
/**
* Register controller as an admin controller
*/
public function before() {
parent::before();
}
/**
* Display login form and perform login
*/
public function action_login() {
Kohana::$log->add(Kohana::DEBUG, 'Executing Controller_Auth::action_login');
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() );
}
}
$post = Validate::factory($_POST)
->filter(TRUE, 'trim')
->rule('username', 'not_empty')
->rule('password', 'not_empty');
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))
{
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');
// Return message if an ajax request
if (Request::$is_ajax)
{
$this->template->content = $message;
}
// Else set flash message and redirect
else
{
Message::instance()->info($message);
Request::instance()->redirect($referrer);
}
}
else
{
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;
}
/**
* Perform user logout
*/
public function action_logout() {
Kohana::$log->add(Kohana::DEBUG, 'Executing Controller_Auth::action_logout');
$this->a1->logout();
Kohana::$log->add('ACCESS', 'Successful logout made by user.');
$message = __('You have been logged out. Goodbye!');
// Return message if an ajax request
if (Request::$is_ajax)
{
$this->template->content = $message;
}
// Else set flash message and redirect
else
{
Message::instance()->info($message);
Request::instance()->redirect( Route::get('admin_main')->uri() );
}
}
}