~ubuntu-branches/ubuntu/wily/steam/wily

« back to all changes in this revision

Viewing changes to server/modules/users.pike

  • Committer: Package Import Robot
  • Author(s): Felix Geyer
  • Date: 2013-10-29 19:51:18 UTC
  • mfrom: (1.1.4) (0.1.4 trusty-proposed)
  • Revision ID: package-import@ubuntu.com-20131029195118-b9bxciz5hwx5z459
Tags: 1:1.0.0.39-2ubuntu1
Add an epoch to the version number as there was an unrelated steam package
in the archive with a higher version number.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2000-2006  Thomas Bopp, Thorsten Hampel, Ludger Merkens
2
 
 *
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.
7
 
 *
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.
12
 
 *
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
16
 
 * 
17
 
 * $Id: users.pike,v 1.7 2006/09/27 20:27:27 astra Exp $
18
 
 */
19
 
 
20
 
constant cvs_version="$Id: users.pike,v 1.7 2006/09/27 20:27:27 astra Exp $";
21
 
 
22
 
inherit "/kernel/secure_mapping.pike";
23
 
 
24
 
#include <macros.h>
25
 
#include <database.h>
26
 
#include <classes.h>
27
 
#include <attributes.h>
28
 
 
29
 
//! This module keeps track of the users in the database. -
30
 
//! Therefor it maps a nickname to the related sTeam user object.
31
 
 
32
 
private static function             myfDb;
33
 
private static string          mysDbTable;
34
 
 
35
 
void load_module() 
36
 
{
37
 
  ::load_module();
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.");
41
 
    mixed err = catch {
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))");
44
 
      
45
 
      foreach( get_users(), object u) {
46
 
        update_user(u);
47
 
      }
48
 
    };
49
 
    if ( err != 0 ) 
50
 
      FATAL("Failed to create userlookup table: %O\n%O", err[0],err[1]);
51
 
  }
52
 
}
53
 
 
54
 
void update_user(object u) 
55
 
{
56
 
  if ( objectp(u) ) {
57
 
    Sql.sql_result res = myfDb()->big_query("select * from i_userlookup where oid='"+u->get_object_id()+"'");
58
 
    mixed err = catch {
59
 
      string query;
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());
68
 
      }
69
 
      else {
70
 
        query = 
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());
78
 
      }      
79
 
      myfDb()->big_query(query);
80
 
    };
81
 
    if ( err ) {
82
 
      FATAL("Failed to insert user: %O\n%O", err[0], err[1]);
83
 
    }
84
 
  }  
85
 
}
86
 
 
87
 
int register(string uname, object key) 
88
 
{
89
 
  mixed err = catch(update_user(key));
90
 
  if ( err ) 
91
 
    FATAL("While updating user: %O\n%O", err[0], err[1]);
92
 
 
93
 
  return ::register(uname, key);
94
 
}
95
 
 
96
 
static object check_user(int id) 
97
 
{
98
 
  // check if user data are still ok ?!
99
 
  object user = find_object(id);
100
 
  return user;
101
 
}
102
 
 
103
 
/**
104
 
 * Lookup a user by his name (firstname, lastname)
105
 
 *
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
110
 
 */
111
 
array(object) lookup_name(string firstname, string lastname, void|bool like)
112
 
{
113
 
  string eq = "=";
114
 
  if ( like ) eq = "like";
115
 
 
116
 
  Sql.sql_result res = myfDb()->big_query("select oid from i_userlookup where firstname "+eq+" '"+firstname +"' and lastname " + eq + " '"+lastname+"'");
117
 
  mixed row;
118
 
 
119
 
  if ( !objectp(res) )
120
 
    return 0;
121
 
 
122
 
  array result = ({ });
123
 
  while ( row = res->fetch_row() ) {
124
 
    result += ({ check_user((int)row[0]) });
125
 
  }
126
 
  destruct(res);
127
 
  return result;
128
 
}
129
 
 
130
 
/**
131
 
 * Lookup a user by his lastname
132
 
 *
133
 
 * @param string lastname the last name of the user to search
134
 
 * @return array of matching users
135
 
 */
136
 
array(object) lookup_lastname(string lastname, void|bool like)
137
 
{
138
 
  string eq = "=";
139
 
  if ( like ) eq = "like";
140
 
  Sql.sql_result res = myfDb()->big_query("select oid from i_userlookup where lastname "+eq+" '"+lastname+"'");
141
 
  mixed row;
142
 
 
143
 
  if ( !objectp(res) )
144
 
    return 0;
145
 
 
146
 
  array result = ({ });
147
 
  while ( row = res->fetch_row() ) {
148
 
    result += ({ check_user((int)row[0]) });
149
 
  }
150
 
  destruct(res);
151
 
  return result;
152
 
}
153
 
 
154
 
/**
155
 
 * Lookup a user by his first name
156
 
 *
157
 
 * @param string firstname the first name of the user to search
158
 
 * @return array of matching users
159
 
 */
160
 
array(object) lookup_firstname(string firstname, void|bool like)
161
 
{
162
 
  string eq = "=";
163
 
  if ( like ) eq = "like";
164
 
  Sql.sql_result res = myfDb()->big_query("select oid from i_userlookup where firstname "+eq+" '"+firstname+"'");
165
 
  mixed row;
166
 
 
167
 
  if ( !objectp(res) )
168
 
    return 0;
169
 
 
170
 
  array result = ({ });
171
 
  while ( row = res->fetch_row() ) {
172
 
    result += ({ check_user((int)row[0]) });
173
 
  }
174
 
  destruct(res);
175
 
  return result;
176
 
}
177
 
 
178
 
/**
179
 
 * Search users by a term
180
 
 *
181
 
 * @param string term the search term 
182
 
 * @return array of matching users
183
 
 */
184
 
array(object) search_users(string term, void|bool like)
185
 
{
186
 
  string eq = " = ";
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);
190
 
  mixed row;
191
 
 
192
 
  if ( !objectp(res) )
193
 
    return 0;
194
 
 
195
 
  array result = ({ });
196
 
  while ( row = res->fetch_row() ) {
197
 
    result += ({ check_user((int)row[0]) });
198
 
  }
199
 
  destruct(res);
200
 
  return result;
201
 
}
202
 
 
203
 
/**
204
 
 * Lookup a user by id
205
 
 *
206
 
 * @param string id the id to lookup
207
 
 * @return array of matching users
208
 
 */
209
 
array(object) lookup_id(string id) 
210
 
{
211
 
  Sql.sql_result res = myfDb()->big_query("select oid from i_userlookup where id='"+id+"'");
212
 
  mixed row;
213
 
 
214
 
  if ( !objectp(res) )
215
 
    return 0;
216
 
 
217
 
  array result = ({ });
218
 
  while ( row = res->fetch_row() ) {
219
 
    result += ({ check_user((int)row[0]) });
220
 
  }
221
 
  destruct(res);
222
 
  return result;
223
 
}
224
 
 
225
 
/**
226
 
 * Lookup a user by e-amil
227
 
 *
228
 
 * @param string e-mail the e-mail to lookup
229
 
 * @param bool like exact search flag
230
 
 * @return array of matching users
231
 
 */
232
 
array(object) lookup_email(string email, void|bool like) 
233
 
{
234
 
  string eq = "=";
235
 
  if ( like ) eq = "like";
236
 
 
237
 
  Sql.sql_result res = myfDb()->big_query("select oid from i_userlookup where email " + eq + " '"+email+"'");
238
 
  mixed row;
239
 
 
240
 
  if ( !objectp(res) )
241
 
    return 0;
242
 
 
243
 
  array result = ({ });
244
 
  while ( row = res->fetch_row() ) {
245
 
    result += ({ check_user((int)row[0]) });
246
 
  }
247
 
  destruct(res);
248
 
  return result;
249
 
}
250
 
 
251
 
/**
252
 
 * Lookup a user by login
253
 
 *
254
 
 * @param string index the login to lookup
255
 
 * @return matching user
256
 
 */
257
 
object lookup(string index)
258
 
{
259
 
  object user = ::lookup(index);
260
 
  if ( !objectp(user) )
261
 
    return _Persistence->lookup_user(index);
262
 
  return user;
263
 
}
264
 
 
265
 
string rename_user(object user, string new_name)
266
 
{
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();
273
 
  set_value(name, 0);
274
 
  set_value(new_name, user);
275
 
  return new_name;
276
 
}
277
 
 
278
 
array(object) get_users() 
279
 
{
280
 
  array(string) index  = index();
281
 
  array(object) users =   ({ });
282
 
 
283
 
  foreach ( index, string idx ) {
284
 
    object obj = get_value(idx);
285
 
    if ( objectp(obj) )
286
 
      users += ({ obj });
287
 
  }
288
 
  return users;
289
 
}
290
 
 
291
 
object get_user(string name )
292
 
{
293
 
  return get_value(name);
294
 
}
295
 
 
296
 
string get_identifier() { return "users"; }
297
 
string get_table_name() { return "users"; }
298