Added medsrv.fcgi to gitignore
[strongswan.git] / src / medsrv / filter / auth_filter.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Copyright (C) 2008 Philip Boetschi, Adrian Doerig
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "auth_filter.h"
18
19 #include <debug.h>
20
21 typedef struct private_auth_filter_t private_auth_filter_t;
22
23 /**
24 * private data of auth_filter
25 */
26 struct private_auth_filter_t {
27 /**
28 * public functions
29 */
30 auth_filter_t public;
31
32 /**
33 * user session
34 */
35 user_t *user;
36
37 /**
38 * database connection
39 */
40 database_t *db;
41 };
42
43 /**
44 * Implementation of filter_t.run
45 */
46 static bool run(private_auth_filter_t *this, request_t *request,
47 char *controller, char *action)
48 {
49 if (this->user->get_user(this->user))
50 {
51 enumerator_t *query;
52 char *login;
53
54 query = this->db->query(this->db, "SELECT login FROM user WHERE id = ?",
55 DB_INT, this->user->get_user(this->user),
56 DB_TEXT);
57 if (query && query->enumerate(query, &login))
58 {
59 request->set(request, "login", login);
60 query->destroy(query);
61 return TRUE;
62 }
63 DESTROY_IF(query);
64 this->user->set_user(this->user, 0);
65 }
66 if (controller && streq(controller, "user") && action &&
67 (streq(action, "add") || streq(action, "login") || streq(action, "help")))
68 { /* add/login allowed */
69 return TRUE;
70 }
71 request->redirect(request, "user/login");
72 return FALSE;
73 }
74
75 /**
76 * Implementation of filter_t.destroy
77 */
78 static void destroy(private_auth_filter_t *this)
79 {
80 free(this);
81 }
82
83 /*
84 * see header file
85 */
86 filter_t *auth_filter_create(user_t *user, database_t *db)
87 {
88 private_auth_filter_t *this= malloc_thing(private_auth_filter_t);
89
90 this->public.filter.destroy = (void(*)(filter_t*))destroy;
91 this->public.filter.run = (bool(*)(filter_t*, request_t*,char*,char*,char*,char*,char*,char*))run;
92
93 this->user = user;
94 this->db = db;
95
96 return &this->public.filter;
97 }
98