Nooku Server is an open source project powered by Assembla

Assembla offers free public and private SVN/Git repositories and project hosting with bug/issue tracking and collaboration tools.

nooku-server

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?php
/**
 * @version     $Id$
 * @category    Nooku
 * @package     Nooku_Server
 * @subpackage  Users
 * @copyright   Copyright (C) 2011 - 2012 Timble CVBA and Contributors. (http://www.timble.net).
 * @license     GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 * @link        http://www.nooku.org
 */

/**
 * User Database Row Class
 *
 * @author      Gergo Erdosi <http://nooku.assembla.com/profile/gergoerdosi>
 * @category    Nooku
 * @package     Nooku_Server
 * @subpackage  Users
 */
class ComUsersDatabaseRowUser extends KDatabaseRowDefault
{
    public function __get($column)
    {
            if($column == 'params' && !($this->_data['params'] instanceof JParameter))
        {
            $xml_path    = JPATH_ADMINISTRATOR.'/components/com_users/databases/rows';
            $xml_name    = str_replace(' ', '_', strtolower($this->group_name));

            if(!file_exists($file = $xml_path.'/'.$xml_name.'.xml')) {
                $file = $xml_path.'/user.xml';
            }

            $params    = new JParameter($this->_data['params']);
            $params->loadSetupFile($file);

            $this->_data['params'] = $params;
        }

        return parent::__get($column);
    }

    public function load()
    {
        if ($result = parent::load()) {
            // Negate enabled due to enabled => block column map.
            $this->enabled = $this->enabled ? 0 : 1;
        }
        return $result;
    }

    public function save()
    {
        // Load the old row if editing an existing user.
        if(!$this->isNew())
        {
            $old_row = $this->getService('com://admin/users.database.table.users')
                ->select($this->id, KDatabase::FETCH_ROW);
        }

        $user = JFactory::getUser();

        // Validate received data.
        if(($this->isNew() || $this->isModified('name')) && trim($this->name) == '')
        {
            $this->setStatus(KDatabase::STATUS_FAILED);
            $this->setStatusMessage(JText::_('Please enter a name!'));

            return false;
        }

        if(($this->isNew() || $this->isModified('username')) &&  trim($this->username) == '')
        {
            $this->setStatus(KDatabase::STATUS_FAILED);
            $this->setStatusMessage(JText::_('Please enter a username!'));

            return false;
        }

        if(($this->isNew() || $this->isModified('username')) && preg_match('#[<>"\'%;()&]#i', $this->username) || strlen(utf8_decode($this->username)) < 2)
        {
            $this->setStatus(KDatabase::STATUS_FAILED);
            $this->setStatusMessage(JText::_('Please enter a valid username. No spaces, at least 2 characters '.
                'and must contain <strong>only</strong> letters and numbers.'));

            return false;
        }

       if($this->isModified('username'))
       {
            $query = $this->getService('koowa:database.query.select')
                ->where('username', '=', $this->username)
                ->where('id', '<>', (int) $this->id);

            $total = $this->getService('com://admin/users.database.table.users')->count($query);

            if($total)
            {
                $this->setStatus(KDatabase::STATUS_FAILED);
                $this->setStatusMessage(JText::_('This username is already in use.'));

                return false;
            }
        }

        if(($this->isNew() || $this->isModified('email')) && (trim($this->email) == '') || !($this->getService('koowa:filter.email')->validate($this->email)))
        {
            $this->setStatus(KDatabase::STATUS_FAILED);
            $this->setStatusMessage(JText::_('Please enter a valid e-mail address.'));

            return false;
        }

        if($this->isModified('email'))
        {
            $query = $this->getService('koowa:database.query.select')
                ->where('email', '=', $this->email)
                ->where('id', '<>', (int) $this->id);

            $total = $this->getService('com://admin/users.database.table.users')->count($query);

            if($total)
            {
                $this->setStatus(KDatabase::STATUS_FAILED);
                $this->setStatusMessage(JText::_('This e-mail address is already registered.'));

                return false;
            }
        }

        /*
         * If username field is an email it has to be the same with email field.
         * This removes the possibilitiy that a user can get locked out of her account
         * if someone else uses that username as the email field.
         */
        if ($this->getService('koowa:filter.email')->validate($this->username) === true && $this->username !== $this->email)
        {
            $this->setStatus(KDatabase::STATUS_FAILED);
            $this->setStatusMessage(JText::_('Your e-mail and username should match if you want to use an e-mail address as your username.'));

            return false;
        }

        // Don't allow users to block themselves.
        if($this->isModified('enabled') && !$this->isNew() && $user->id == $this->id && !$this->enabled)
        {
            $this->setStatus(KDatabase::STATUS_FAILED);
            $this->setStatusMessage(JText::_("You can't block yourself!"));

            return false;
        }

        // Don't allow to save a user without a group.
        if(($this->isNew() || $this->isModified('users_group_id')) && !$this->users_group_id)
        {
            $this->setStatus(KDatabase::STATUS_FAILED);
            $this->setStatusMessage(JText::_("You can't create a user without a user group."));

            return false;
        }

        // Don't allow users below super administrator to edit a super administrator.
        if(!$this->isNew() && $this->isModified('users_group_id') && $old_row->users_group_id == 25 && $user->gid != 25)
        {
            $this->setStatus(KDatabase::STATUS_FAILED);
            $this->setStatusMessage(JText::_("You can't edit a super administrator account."));

            return false;
        }

        // Don't allow users below super administrator to create an administrators.
        if($this->isModified('users_group_id') && $this->users_group_id == 24 && !($user->gid == 25 || ($user->id == $this->id && $user->gid == 24)))
        {
            $this->setStatus(KDatabase::STATUS_FAILED);
            $this->setStatusMessage(JText::_("You can't create a user with this user group level. "
                ."Only super administrators have this ability."));

            return false;
        }

        // Don't allow users below super administrator to create a super administrator.
        if($this->isModified('users_group_id') && $this->users_group_id == 25 && $user->gid != 25)
        {
            $this->setStatus(KDatabase::STATUS_FAILED);
            $this->setStatusMessage(JText::_("You can't create a user with this user group level. "
                ."Only super administrators have this ability."));

            return false;
        }

        // Don't allow users to change the user level of the last active super administrator.
        if(isset($this->_modifid['users_group_id']) && $old_row->users_group_id != 25)
        {
            $query = $this->getService('koowa:database.query.select')
                ->where('users_group_id', '=', 25)
                ->where('enabled', '=', 1);

            $total = $this->getService('com://admin/users.database.table.users')->count($query);

            if($total <= 1)
            {
                $this->setStatus(KDatabase::STATUS_FAILED);
                $this->setStatusMessage(JText::_("You can't change this user's group because ".
                    "the user is the only active super administrator for your site."));

                return false;
            }
        }

        // Check if passwords match.
        if($this->isModified('password') && $this->password != $this->password_verify)
        {
            $this->setStatus(KDatabase::STATUS_FAILED);
            $this->setStatusMessage(JText::_("Passwords don't match!"));

            return false;
        }

        // Generate a random password if empty and the record is new.
        if($this->isNew() && !$this->password)
        {
            $this->password            = $this->getService('com://admin/users.helper.password')->getRandom();
            $this->password_verify    = $this->password;
        }

        if($this->isModified('password') && $this->password)
        {
            if ($this->encrypt_password) {
                $this->password    = $this->getService('com://admin/users.helper.password')->encrypt($this->password);
            }
        }
        else
        {
            $this->password = $old_row->password;

            unset($this->_modified['password']);
        }

        if($this->isNew()) {
            $this->registered_on = gmdate('Y-m-d H:i:s', time());
        }

        // TODO: This shouldn't be executed on every save.
        $query = $this->getService('koowa:database.query.select')
            ->columns('name')
            ->where('id = :id')
            ->bind(array('id' => $this->users_group_id));

        $this->group_name = $this->getService('com://admin/users.database.table.groups')
            ->select($query, KDatabase::FETCH_FIELD);

        // Set parameters.
        if($this->isModified('params'))
        {
            $params    = new JParameter('');
            $params->bind($this->_data['params']);

            $this->params = $params->toString();

            if(!$this->isNew() && $this->_data['params'] == $old_row->params->toString()) {
                unset($this->_modified['params']);
            }
        }

        // Need to reverse the value of 'enabled', because the mapped column is 'block'.
        if($this->isNew() || $this->isModified('enabled')) {
            $this->enabled = $this->enabled ? 0 : 1;
        }

        // Get modified columns for further use.
        $modified = $this->getModified();

        if(!parent::save()) {
            return false;
        }

        // Syncronize ACL.
        if($this->getStatus() == KDatabase::STATUS_CREATED)
        {
            $aro = $this->getService('com://admin/groups.database.row.aro')
                ->setData(array(
                    'section_value' => 'users',
                    'value' => $this->id,
                    'name' => $this->name
                ));
            // Load an existing row if any.
            $aro->load();
            $aro->save();

            $arosgroup = $this->getService('com://admin/groups.database.row.arosgroup')
                ->setData(array(
                    'group_id' => $this->users_group_id,
                    'aro_id'   => $aro->id
                ));
            // Load an existing row if any.
            $arosgroup->load();
            $arosgroup->save();
        }
        else
        {
            if(array_search('name', $modified) !== false || array_search('users_group_id', $modified) !== false)
            {
                $aro = $this->getService('com://admin/groups.database.table.aros')
                    ->select(array('value' => $this->id), KDatabase::FETCH_ROW);

                if(array_search('name', $modified) !== false)
                {
                    $aro->name = $this->name;
                    $aro->save();
                }

                if(array_search('users_group_id', $modified) !== false)
                {
                    $this->getService('com://admin/groups.database.table.arosgroups')
                        ->select(array('aro_id' => $aro->id), KDatabase::FETCH_ROW)
                        ->delete();

                    $this->getService('com://admin/groups.database.table.arosgroups')
                         ->select(null, KDatabase::FETCH_ROW)
                         ->setData(array(
                            'group_id' => $this->users_group_id,
                            'aro_id'   => $aro->id
                         ))->save();
                }
            }
        }

        return true;
    }

    public function delete()
    {
        $user = JFactory::getUser();

        // Don't allow users to delete themselves.
        if($user->id == $this->id)
        {
            $this->_status            = KDatabase::STATUS_FAILED;
            $this->_status_message    = JText::_("You can't delete yourself!");

            return false;
        }

        // Don't allow administrators to delete other administrators or super administrators.
        if($user->gid == 24 && ($this->users_group_id == 24 || $this->users_group_id == 25))
        {
            $this->setStatus(KDatabase::STATUS_FAILED);
            $this->setStatusMessage(JText::_("You can't delete a user with this user group level. "
                ."Only super administrators have this ability."));

            return false;
        }

        if(!parent::delete()) {
            return false;
        }

        // Syncronize ACL.
        if($this->_status == KDatabase::STATUS_DELETED)
        {
            $aro = $this->getService('com://admin/groups.database.table.aros')
                         ->select(array('value' => $this->id), KDatabase::FETCH_ROW);
            $aro->delete();

            $this->getService('com://admin/groups.database.table.arosgroups')
                 ->select(array('aro_id' => $aro->id), KDatabase::FETCH_ROW)
                 ->delete();
        }

        return true;
    }

    /**
     * Return an associative array of the data.
     *
     * @return array
     */
    public function toArray()
    {
        $data = parent::toArray();

        unset($data['password']);
        unset($data['activation']);

        $data['params'] = $this->params->toArray();
        return $data;
    }
}
Ajax-loader Loading, please wait...