aboutsummaryrefslogtreecommitdiffstats
path: root/src/medsrv/filter/auth_filter.c
blob: d21abdc46647927cf4859ca6833d8aaf3da1bd80 (plain)
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
/*
 * Copyright (C) 2008 Martin Willi
 * Copyright (C) 2008 Philip Boetschi, Adrian Doerig
 * 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_filter.h"

#include <debug.h>

typedef struct private_auth_filter_t private_auth_filter_t;

/**
 * private data of auth_filter
 */
struct private_auth_filter_t {
	/**
	 * public functions
	 */
	auth_filter_t public;

	/**
	 * user session
	 */
	user_t *user;

	/**
	 * database connection
	 */
	database_t *db;
};

METHOD(filter_t, run, bool,
	private_auth_filter_t *this, request_t *request, char *controller,
	char *action, char *p2, char *p3, char *p4, char *p5)
{
	if (this->user->get_user(this->user))
	{
		enumerator_t *query;
		char *login;

		query = this->db->query(this->db, "SELECT login FROM user WHERE id = ?",
								DB_INT, this->user->get_user(this->user),
								DB_TEXT);
		if (query && query->enumerate(query, &login))
		{
			request->set(request, "login", login);
			query->destroy(query);
			return TRUE;
		}
		DESTROY_IF(query);
		this->user->set_user(this->user, 0);
	}
	if (controller && streq(controller, "user") && action &&
		(streq(action, "add") || streq(action, "login") || streq(action, "help")))
	{	/* add/login allowed */
		return TRUE;
	}
	request->redirect(request, "user/login");
	return FALSE;
}

METHOD(filter_t, destroy, void,
	private_auth_filter_t *this)
{
	free(this);
}

/*
 * see header file
 */
filter_t *auth_filter_create(user_t *user, database_t *db)
{
	private_auth_filter_t *this;

	INIT(this,
		.public = {
			.filter = {
				.destroy = _destroy,
				.run = _run,
			},
		},
		.user = user,
		.db = db,
	);

	return &this->public.filter;
}