1
/* Copyright (C) 2000-2006 Thomas Bopp, Thorsten Hampel, Ludger Merkens
3
* This program is free software; you can redistribute it and/or modify
4
* it under the terms of the GNU General Public License as published by
5
* the Free Software Foundation; either version 2 of the License, or
6
* (at your option) any later version.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
* $Id: users.pike,v 1.7 2006/09/27 20:27:27 astra Exp $
20
constant cvs_version="$Id: users.pike,v 1.7 2006/09/27 20:27:27 astra Exp $";
22
inherit "/kernel/secure_mapping.pike";
27
#include <attributes.h>
29
//! This module keeps track of the users in the database. -
30
//! Therefor it maps a nickname to the related sTeam user object.
32
private static function myfDb;
33
private static string mysDbTable;
38
[myfDb , mysDbTable] = _Database->connect_db_mapping();
39
if( search(myfDb()->list_tables(), "i_userlookup" ) == -1 ) {
40
MESSAGE("Creating i_userlookup table in users module.");
42
myfDb()->big_query("create table i_userlookup "
43
"(login char(255) not null, email char(255), firstname char(255), lastname char(255), id char(255), oid char(255) not null, UNIQUE(oid))");
45
foreach( get_users(), object u) {
50
FATAL("Failed to create userlookup table: %O\n%O", err[0],err[1]);
54
void update_user(object u)
57
Sql.sql_result res = myfDb()->big_query("select * from i_userlookup where oid='"+u->get_object_id()+"'");
60
if ( res->fetch_row() ) {
61
query = sprintf("update i_userlookup set login='%s',email='%s',firstname='%s',lastname='%s',id='%s' where oid='%s'",
62
myfDb()->quote(u->get_user_name()||""),
63
myfDb()->quote(u->query_attribute(USER_EMAIL)||""),
64
myfDb()->quote(u->query_attribute(USER_FIRSTNAME)||""),
65
myfDb()->quote(u->query_attribute(USER_FULLNAME)||""),
66
myfDb()->quote(u->query_attribute(USER_ID)||""),
67
(string)u->get_object_id());
71
sprintf("insert into i_userlookup values('%s','%s','%s','%s','%s','%s')",
72
myfDb()->quote(u->get_user_name()||""),
73
myfDb()->quote(u->query_attribute(USER_EMAIL)||""),
74
myfDb()->quote(u->query_attribute(USER_FIRSTNAME)||""),
75
myfDb()->quote(u->query_attribute(USER_FULLNAME)||""),
76
myfDb()->quote(u->query_attribute(USER_ID)||""),
77
(string)u->get_object_id());
79
myfDb()->big_query(query);
82
FATAL("Failed to insert user: %O\n%O", err[0], err[1]);
87
int register(string uname, object key)
89
mixed err = catch(update_user(key));
91
FATAL("While updating user: %O\n%O", err[0], err[1]);
93
return ::register(uname, key);
96
static object check_user(int id)
98
// check if user data are still ok ?!
99
object user = find_object(id);
104
* Lookup a user by his name (firstname, lastname)
106
* @param string firstname the firstname of the user to search
107
* @param string lastname the last name of the user to search
108
* @param bool like optional parameter to specify "like" instead of "="
109
* @return array of matching users
111
array(object) lookup_name(string firstname, string lastname, void|bool like)
114
if ( like ) eq = "like";
116
Sql.sql_result res = myfDb()->big_query("select oid from i_userlookup where firstname "+eq+" '"+firstname +"' and lastname " + eq + " '"+lastname+"'");
122
array result = ({ });
123
while ( row = res->fetch_row() ) {
124
result += ({ check_user((int)row[0]) });
131
* Lookup a user by his lastname
133
* @param string lastname the last name of the user to search
134
* @return array of matching users
136
array(object) lookup_lastname(string lastname, void|bool like)
139
if ( like ) eq = "like";
140
Sql.sql_result res = myfDb()->big_query("select oid from i_userlookup where lastname "+eq+" '"+lastname+"'");
146
array result = ({ });
147
while ( row = res->fetch_row() ) {
148
result += ({ check_user((int)row[0]) });
155
* Lookup a user by his first name
157
* @param string firstname the first name of the user to search
158
* @return array of matching users
160
array(object) lookup_firstname(string firstname, void|bool like)
163
if ( like ) eq = "like";
164
Sql.sql_result res = myfDb()->big_query("select oid from i_userlookup where firstname "+eq+" '"+firstname+"'");
170
array result = ({ });
171
while ( row = res->fetch_row() ) {
172
result += ({ check_user((int)row[0]) });
179
* Search users by a term
181
* @param string term the search term
182
* @return array of matching users
184
array(object) search_users(string term, void|bool like)
187
if ( like ) eq = " like ";
188
term = "'" + myfDb()->quote(term) + "'";
189
Sql.sql_result res = myfDb()->big_query("select distinct oid from i_userlookup where firstname "+eq + term+" or lastname "+ eq +term+" or email "+eq+term +" or id "+eq+term + " or login " + eq + term);
195
array result = ({ });
196
while ( row = res->fetch_row() ) {
197
result += ({ check_user((int)row[0]) });
204
* Lookup a user by id
206
* @param string id the id to lookup
207
* @return array of matching users
209
array(object) lookup_id(string id)
211
Sql.sql_result res = myfDb()->big_query("select oid from i_userlookup where id='"+id+"'");
217
array result = ({ });
218
while ( row = res->fetch_row() ) {
219
result += ({ check_user((int)row[0]) });
226
* Lookup a user by e-amil
228
* @param string e-mail the e-mail to lookup
229
* @param bool like exact search flag
230
* @return array of matching users
232
array(object) lookup_email(string email, void|bool like)
235
if ( like ) eq = "like";
237
Sql.sql_result res = myfDb()->big_query("select oid from i_userlookup where email " + eq + " '"+email+"'");
243
array result = ({ });
244
while ( row = res->fetch_row() ) {
245
result += ({ check_user((int)row[0]) });
252
* Lookup a user by login
254
* @param string index the login to lookup
255
* @return matching user
257
object lookup(string index)
259
object user = ::lookup(index);
260
if ( !objectp(user) )
261
return _Persistence->lookup_user(index);
265
string rename_user(object user, string new_name)
267
if ( CALLER != get_factory(CLASS_USER) )
268
steam_error("Invalid call to rename_user() !");
269
object other = get_value(new_name);
270
if ( objectp(other) && other != user )
271
steam_error("There is already a user with the name '"+new_name+"' !");
272
string name = user->get_user_name();
274
set_value(new_name, user);
278
array(object) get_users()
280
array(string) index = index();
281
array(object) users = ({ });
283
foreach ( index, string idx ) {
284
object obj = get_value(idx);
291
object get_user(string name )
293
return get_value(name);
296
string get_identifier() { return "users"; }
297
string get_table_name() { return "users"; }