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
|
/*
* Copyright (C) 2007 Martin Willi
* HSR Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include "auth_controller.h"
#include "../manager.h"
#include <library.h>
typedef struct private_auth_controller_t private_auth_controller_t;
/**
* private data of the task manager
*/
struct private_auth_controller_t {
/**
* public functions
*/
auth_controller_t public;
/**
* manager instance
*/
manager_t *manager;
};
static void login(private_auth_controller_t *this, fast_request_t *request)
{
request->set(request, "action", "check");
request->set(request, "title", "Login");
request->render(request, "templates/auth/login.cs");
}
static void check(private_auth_controller_t *this, fast_request_t *request)
{
char *username, *password;
username = request->get_query_data(request, "username");
password = request->get_query_data(request, "password");
if (username && password &&
this->manager->login(this->manager, username, password))
{
request->redirect(request, "ikesa/list");
}
else
{
request->redirect(request, "auth/login");
}
}
static void logout(private_auth_controller_t *this, fast_request_t *request)
{
this->manager->logout(this->manager);
request->redirect(request, "auth/login");
}
METHOD(fast_controller_t, get_name, char*,
private_auth_controller_t *this)
{
return "auth";
}
METHOD(fast_controller_t, handle, void,
private_auth_controller_t *this, fast_request_t *request, char *action,
char *p2, char *p3, char *p4, char *p5)
{
if (action)
{
if (streq(action, "login"))
{
return login(this, request);
}
else if (streq(action, "check"))
{
return check(this, request);
}
else if (streq(action, "logout"))
{
return logout(this, request);
}
}
request->redirect(request, "auth/login");
}
METHOD(fast_controller_t, destroy, void,
private_auth_controller_t *this)
{
free(this);
}
/*
* see header file
*/
fast_controller_t *auth_controller_create(fast_context_t *context, void *param)
{
private_auth_controller_t *this;
INIT(this,
.public = {
.controller = {
.get_name = _get_name,
.handle = _handle,
.destroy = _destroy,
},
},
.manager = (manager_t*)context,
);
return &this->public.controller;
}
|