~gz/ubuntu/wily/steam/new_rel_udev_rules

« back to all changes in this revision

Viewing changes to server/factories/UserFactory.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: UserFactory.pike,v 1.4 2006/08/10 08:08:50 astra Exp $
18
 
 */
19
 
 
20
 
constant cvs_version="$Id: UserFactory.pike,v 1.4 2006/08/10 08:08:50 astra Exp $";
21
 
 
22
 
inherit "/factories/ContainerFactory";
23
 
 
24
 
#include <macros.h>
25
 
#include <classes.h>
26
 
#include <database.h>
27
 
#include <roles.h>
28
 
#include <assert.h>
29
 
#include <events.h>
30
 
#include <attributes.h>
31
 
#include <types.h>
32
 
#include <access.h>
33
 
 
34
 
static int iActivation = 0;
35
 
 
36
 
/**
37
 
 * Initialize the factory with its default attributes.
38
 
 *  
39
 
 * @author <a href="mailto:astra@upb.de">Thomas Bopp</a>) 
40
 
 */
41
 
static void init_factory()
42
 
{
43
 
    ::init_factory();
44
 
    init_class_attribute(USER_ADRESS, CMD_TYPE_STRING, "user adress",
45
 
                         0, EVENT_ATTRIBUTES_CHANGE, 0,
46
 
                         CONTROL_ATTR_USER, "");
47
 
    init_class_attribute(USER_MODE,  CMD_TYPE_INT, "user mode", 
48
 
                         0, EVENT_ATTRIBUTES_CHANGE, 0,
49
 
                         CONTROL_ATTR_CLIENT, 0);
50
 
    init_class_attribute(USER_UMASK,  CMD_TYPE_MAPPING, "user umask", 
51
 
                         EVENT_ATTRIBUTES_QUERY, EVENT_ATTRIBUTES_CHANGE,0,
52
 
                         CONTROL_ATTR_USER, ([ ]));
53
 
    init_class_attribute(USER_MODE_MSG, CMD_TYPE_STRING, 
54
 
                         "user mode message", 0, 
55
 
                         EVENT_ATTRIBUTES_CHANGE, 0,CONTROL_ATTR_USER,"");
56
 
    init_class_attribute(USER_EMAIL, CMD_TYPE_STRING, "email", 
57
 
                         0, EVENT_ATTRIBUTES_CHANGE,0,
58
 
                         CONTROL_ATTR_USER, "");
59
 
    init_class_attribute(USER_FULLNAME, CMD_TYPE_STRING, "user fullname",0,
60
 
                         EVENT_ATTRIBUTES_CHANGE, 0,CONTROL_ATTR_USER, "");
61
 
    init_class_attribute(USER_WORKROOM, CMD_TYPE_OBJECT, "workroom", 0, 
62
 
                         EVENT_ATTRIBUTES_CHANGE, 0,CONTROL_ATTR_USER, 0);
63
 
    init_class_attribute(USER_LOGOUT_PLACE, CMD_TYPE_OBJECT, "logout-env",
64
 
                         0, EVENT_ATTRIBUTES_CHANGE, 0,
65
 
                         CONTROL_ATTR_USER, 0);
66
 
    init_class_attribute(USER_LAST_LOGIN, CMD_TYPE_TIME, "last-login", 
67
 
                         0, EVENT_ATTRIBUTES_CHANGE, 0,
68
 
                         CONTROL_ATTR_SERVER, 0);
69
 
    init_class_attribute(USER_BOOKMARKROOM, CMD_TYPE_OBJECT, "bookmark room",0,
70
 
                         EVENT_ATTRIBUTES_CHANGE, 0,CONTROL_ATTR_USER, 0);
71
 
    init_class_attribute(USER_FORWARD_MSG, CMD_TYPE_INT, "forward message", 
72
 
                         0, EVENT_ATTRIBUTES_CHANGE, 0,
73
 
                         CONTROL_ATTR_USER, 1);
74
 
    init_class_attribute(USER_FAVOURITES, CMD_TYPE_ARRAY, "favourites list", 
75
 
                         0, EVENT_ATTRIBUTES_CHANGE, 0,
76
 
                         CONTROL_ATTR_USER, ({ }) );
77
 
    init_class_attribute(USER_CALENDAR, CMD_TYPE_OBJECT, "calendar", 0,
78
 
                         EVENT_ATTRIBUTES_CHANGE, 0, CONTROL_ATTR_SERVER,0);
79
 
    init_class_attribute(USER_MONITOR, CMD_TYPE_OBJECT, "monitor", 0,
80
 
                         EVENT_ATTRIBUTES_CHANGE, 0, CONTROL_ATTR_SERVER,0);
81
 
    init_class_attribute(USER_ID, CMD_TYPE_STRING, "user id",
82
 
                         EVENT_ATTRIBUTES_QUERY,
83
 
                         EVENT_ATTRIBUTES_CHANGE, 
84
 
                         0,CONTROL_ATTR_USER, "");
85
 
}
86
 
 
87
 
/**
88
 
 * Create a new user object with the following vars:
89
 
 * name     - the users name (nickname is possible too).
90
 
 * email    - the users email adress.
91
 
 * pw       - the users initial password.
92
 
 * fullname - the full name of the user.
93
 
 * firstname - the last name of the user.
94
 
 *  
95
 
 * @param mapping vars - variables for execution.
96
 
 * @return the objectp of the new user if successfully, or 0 (no access or user
97
 
 *         exists)
98
 
 * @author Thomas Bopp (astra@upb.de) 
99
 
 */
100
 
object execute(mapping vars)
101
 
{
102
 
   string name;
103
 
   object  obj;
104
 
 
105
 
   if ( stringp(vars["nickname"]) )
106
 
       name = lower_case(vars["nickname"]);
107
 
   else
108
 
       name = vars["name"];
109
 
   
110
 
   obj = MODULE_USERS->lookup(name);
111
 
   if ( objectp(obj) ) 
112
 
       steam_error("user_create(): User does already exist.");
113
 
   obj = MODULE_GROUPS->lookup(name);
114
 
   if ( objectp(obj) ) 
115
 
       steam_error("user_create(): Group with this name already exist.");
116
 
 
117
 
   mixed err = catch {
118
 
       object ouid = seteuid(USER("root"));
119
 
       obj = user_create(vars);
120
 
       seteuid(ouid);
121
 
   };
122
 
   if ( err ) {
123
 
       // try to find the user and remove
124
 
       obj = MODULE_USERS->lookup(name);
125
 
       if ( objectp(obj) ) {
126
 
           obj->delete();
127
 
       }
128
 
       throw(err);
129
 
   }
130
 
   return obj;
131
 
}
132
 
 
133
 
private static object user_create(mapping vars)
134
 
{
135
 
    int       i;
136
 
    object  obj;
137
 
 
138
 
    try_event(EVENT_EXECUTE, CALLER, obj);
139
 
 
140
 
    string name;
141
 
    if ( stringp(vars["nickname"]) )
142
 
        name = lower_case(vars["nickname"]);
143
 
    else
144
 
        name = vars["name"];
145
 
 
146
 
    if ( search(name, " ") >= 0 )
147
 
        steam_error("Whitespaces in Usernames are not allowed");
148
 
 
149
 
    string pw = vars["pw"];
150
 
    string email = vars["email"];
151
 
 
152
 
    if ( stringp(vars->fullname) && !xml.utf8_check(vars->fullname) )
153
 
      steam_error("Failed utf8-check for firstname or fullname !");
154
 
    
155
 
    if ( stringp(vars->firstname) && !xml.utf8_check(vars->firstname) )
156
 
      steam_error("Failed utf8-check for firstname or fullname !");
157
 
 
158
 
    obj = object_create(name, CLASS_NAME_USER, 0, vars["attributes"],
159
 
                vars["attributesAcquired"], vars["attributesLocked"]); 
160
 
    obj->lock_attribute(OBJ_NAME);
161
 
    if ( !objectp(obj) ) {
162
 
        SECURITY_LOG("Creation of user " + name + " failed...");
163
 
        return null; // creation failed...
164
 
    }
165
 
 
166
 
    string language;
167
 
    if (stringp(vars["language"]))
168
 
       language=vars["language"];
169
 
 
170
 
    obj->set_user_password(pw);
171
 
    obj->set_user_name(name);
172
 
    obj->set_attribute(USER_EMAIL, email);
173
 
    obj->set_attribute(USER_FULLNAME, vars["fullname"]);
174
 
    obj->set_attribute(USER_FIRSTNAME, vars["firstname"]);
175
 
    if (stringp(language)) obj->set_attribute(USER_LANGUAGE, language);
176
 
    obj->set_creator(_ROOT);
177
 
    obj->set_acquire(0);
178
 
    if ( objectp(this_user()) && this_user() != _GUEST )
179
 
      obj->sanction_object(this_user(), SANCTION_ALL);
180
 
 
181
 
    if ( stringp(vars["description"]) )
182
 
        obj->set_attribute(OBJ_DESC, vars["description"]);
183
 
    if ( stringp(vars["contact"]) )
184
 
        obj->set_attribute(USER_ADRESS, vars["contact"]);
185
 
    
186
 
    object workroom, factory, calendar;
187
 
 
188
 
    factory = _Server->get_factory(CLASS_ROOM);
189
 
    
190
 
    workroom = factory->execute((["name":name+"'s workarea",]));
191
 
    obj->move(workroom);
192
 
    obj->set_attribute(USER_WORKROOM, workroom);
193
 
    obj->lock_attribute(USER_WORKROOM);
194
 
    workroom->set_attribute(OBJ_DESC, name+"s workroom.");
195
 
    workroom->set_creator(obj->this());
196
 
    workroom->set_attribute(OBJ_OWNER, obj->this());
197
 
    workroom->sanction_object(obj->this(), SANCTION_ALL);
198
 
    workroom->sanction_object_meta(obj->this(), SANCTION_ALL);
199
 
 
200
 
    object bookmarkroom = factory->execute((["name":name+"'s bookmarks",]));
201
 
    obj->set_attribute(USER_BOOKMARKROOM, bookmarkroom);
202
 
    obj->lock_attribute(USER_BOOKMARKROOM);
203
 
    bookmarkroom->set_creator(obj->this());
204
 
    bookmarkroom->sanction_object(obj->this(), SANCTION_ALL);
205
 
    bookmarkroom->sanction_object_meta(obj->this(), SANCTION_ALL);
206
 
 
207
 
    factory = _Server->get_factory(CLASS_TRASHBIN);
208
 
    object trashbin = factory->execute((["name":"trashbin", ]));
209
 
    trashbin->set_attribute(OBJ_DESC, "Trashbin");
210
 
    trashbin->move(workroom->this());
211
 
    trashbin->set_creator(obj->this());
212
 
    trashbin->sanction_object(obj->this(), SANCTION_ALL);
213
 
    trashbin->sanction_object_meta(obj->this(), SANCTION_ALL);
214
 
    trashbin->sanction_object(_STEAMUSER, SANCTION_INSERT);
215
 
    trashbin->set_acquire(0); 
216
 
     
217
 
    obj->set_attribute(USER_TRASHBIN, trashbin);
218
 
 
219
 
    calendar = _Server->get_factory(CLASS_CALENDAR)->execute((["name":name+"'s calendar"]) );
220
 
    obj->set_attribute(USER_CALENDAR, calendar);
221
 
    obj->lock_attribute(USER_CALENDAR);
222
 
    calendar->set_creator(obj->this());
223
 
    calendar->sanction_object(obj->this(), SANCTION_ALL);
224
 
    calendar->sanction_object_meta(obj->this(), SANCTION_ALL);
225
 
    calendar->set_attribute(CALENDAR_OWNER, obj->this());
226
 
    calendar->lock_attribute(CALENDAR_OWNER);
227
 
 
228
 
    // steam users can annotate and read the users attributes.
229
 
    obj->sanction_object(_STEAMUSER, SANCTION_READ|SANCTION_ANNOTATE);
230
 
    
231
 
    object forwards = get_module("forward");
232
 
    if ( objectp(forwards) ) {
233
 
        string mname = forwards->get_mask_char() + obj->get_user_name();
234
 
        forwards->add_forward(obj->this(), mname);
235
 
        forwards->add_forward(obj->this(), email);
236
 
    }
237
 
 
238
 
    if ( name != "guest" )
239
 
      _STEAMUSER->add_member(obj->this());
240
 
    
241
 
    
242
 
    array(object) inv = workroom->get_inventory_by_class(CLASS_EXIT);
243
 
    if ( sizeof(inv) == 0 ) {
244
 
        factory = _Server->get_factory(CLASS_EXIT);
245
 
        object swa = _STEAMUSER->query_attribute(GROUP_WORKROOM);
246
 
        object exit = factory->execute( 
247
 
            ([ "name":swa->get_identifier(), "exit_to": swa, ]));
248
 
        exit->set_creator(obj->this());
249
 
        exit->move(workroom);
250
 
    }
251
 
        
252
 
    run_event(EVENT_EXECUTE, CALLER, obj);
253
 
    
254
 
    // now remove all guest privileges on this object
255
 
    if ( objectp(_GUEST) ) {
256
 
        obj->sanction_object(_GUEST, 0);
257
 
        workroom->sanction_object(_GUEST, 0);
258
 
    }
259
 
    iActivation = time() + random(100000);
260
 
    obj->set_activation(iActivation);
261
 
 
262
 
    return obj->this();
263
 
}
264
 
 
265
 
/**
266
 
 * Queries and resets the activation code for an user. Thus
267
 
 * it is required, that the creating object immidiately calls
268
 
 * this function and sends the activation code to the user.
269
 
 *  
270
 
 * @return activation code for the last created user
271
 
 * @author <a href="mailto:astra@upb.de">Thomas Bopp</a>) 
272
 
 */
273
 
int get_activation()
274
 
{
275
 
    int res = iActivation;
276
 
    iActivation = 0;
277
 
    return res;
278
 
}
279
 
 
280
 
string rename_user(object user, string new_name)
281
 
{
282
 
  _SECURITY->check_access(user,CALLER,SANCTION_WRITE,ROLE_WRITE_ALL,false);
283
 
 
284
 
  object users = get_module("users");
285
 
  
286
 
  if ( users->lookup(user->get_user_name()) != user )
287
 
    user->set_user_name(new_name);
288
 
  
289
 
  if ( users->rename_user(user, new_name) == new_name )
290
 
    user->set_user_name(new_name);
291
 
  return user->get_user_name();
292
 
}
293
 
 
294
 
array recover_users()
295
 
{
296
 
    array result = ({ });
297
 
    int t = time();
298
 
    MESSAGE("Starting User RECOVERY !");
299
 
    foreach( get_module("users")->index(), string uname ) {
300
 
        object user = get_module("users")->lookup(uname);
301
 
        if ( objectp(user) && user->get_user_name() == 0 ) {
302
 
            result += ({ user });
303
 
            user->set_user_name(uname);
304
 
            // heuristics
305
 
            object workroom;
306
 
            int oid = user->get_object_id();
307
 
            mixed err = catch {
308
 
              user->set_attribute(USER_WORKROOM, find_object(oid+1));
309
 
              user->set_attribute(USER_BOOKMARKROOM, find_object(oid+2));
310
 
              user->set_attribute(USER_TRASHBIN, find_object(oid+3));
311
 
            };
312
 
            
313
 
            array groups = user->get_groups();
314
 
            foreach ( get_module("groups")->get_groups(), object grp ) {
315
 
                if ( grp->is_member(user) && search(groups, grp) == -1 ) {
316
 
                    grp->remove_member(user);
317
 
                    grp->add_member(user);
318
 
                }
319
 
            }
320
 
        }
321
 
    }
322
 
    MESSAGE("Finished RECOVERY in %d", time() - t);
323
 
    return result;
324
 
}
325
 
 
326
 
void reset_guest()
327
 
{
328
 
  USER("guest")->unlock_attribute(OBJ_NAME);
329
 
  USER("guest")->set_attribute(OBJ_NAME, "guest");
330
 
  USER("guest")->set_user_password("guest");
331
 
  USER("guest")->set_user_name("guest");
332
 
  foreach(USER("guest")->get_groups(), object grp) {
333
 
    if ( grp != GROUP("everyone") )
334
 
      grp->remove_member(USER("guest"));
335
 
  }
336
 
}
337
 
 
338
 
string get_identifier() { return "User.factory"; }
339
 
string get_class_name() { return "User"; }
340
 
int get_class_id() { return CLASS_USER; }