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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
|
<?php
/**
* $Header: /cvsroot/bitweaver/_bit_users/BitPermUser.php,v 1.90 2010/04/24 19:03:37 wjames5 Exp $
*
* Lib for user administration, groups and permissions
* This lib uses pear so the constructor requieres
* Copyright (c) 2004 bitweaver.org
* Copyright (c) 2003 tikwiki.org
* Copyright (c) 2002-2003, Luis Argerich, Garland Foster, Eduardo Polidor, et. al.
* All Rights Reserved. See below for details and a complete list of authors.
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See http://www.gnu.org/copyleft/lesser.html for details
*
* $Id: BitPermUser.php,v 1.90 2010/04/24 19:03:37 wjames5 Exp $
* @package users
*/
/**
* required setup
*/
require_once( USERS_PKG_PATH.'/BitUser.php' );
/**
* Class that holds all information for a given user
*
* @author spider <spider@steelsun.com>
* @version $Revision: 1.90 $
* @package users
* @subpackage BitPermUser
*/
class BitPermUser extends BitUser {
// change this to an email address to receive debug emails from the LDAP code
// does this work? - xing - Saturday Oct 18, 2008 09:47:20 CEST
var $debug = FALSE;
// we use these to cache data
var $cUserGroups = array();
var $cGroupPerms = array( array() );
/**
* BitPermUser Initialise class
*
* @param numeric $pUserId User ID of the user we wish to load
* @param numeric $pContentId Content ID of the user we wish to load
* @access public
* @return void
*/
function BitPermUser( $pUserId=NULL, $pContentId=NULL ) {
BitUser::BitUser( $pUserId, $pContentId );
// Permission setup
$this->mAdminContentPerm = 'p_users_admin';
}
/**
* assumeUser Assume the identity of anothre user - Only admins may do this
*
* @param numeric $pUserId User ID of the user you want to hijack
* @access public
* @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
*/
function assumeUser( $pUserId ) {
global $gBitUser;
$ret = FALSE;
// make double sure the current logged in user has permission, check for p_users_admin, not admin, as that is all you need for assuming another user.
// this enables creating of a non technical site adminstrators group, eg customer support representatives.
if( $gBitUser->hasPermission( 'p_users_admin' ) ) {
$assumeUser = new BitPermUser( $pUserId );
$assumeUser->loadPermissions();
if( $assumeUser->isAdmin() ) {
$this->mErrors['assume_user'] = tra( "User administrators cannot be assumed." );
} else {
$this->mDb->query( "UPDATE `".BIT_DB_PREFIX."users_cnxn` SET `user_id`=?, `assume_user_id`=? WHERE `cookie`=?", array( $pUserId, $gBitUser->mUserId, $_COOKIE[$this->getSiteCookieName()] ) );
$ret = TRUE;
}
}
return $ret;
}
/**
* load
*
* @param boolean $pFull Load all permissions
* @param string $pUserName User login name
* @access public
* @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
*/
function load( $pFull=FALSE, $pUserName=NULL ) {
if( BitUser::load( $pFull, $pUserName ) ) {
if( $pFull ) {
unset( $this->mPerms );
$this->loadGroups();
$this->loadPermissions();
}
}
return( $this->mUserId != NULL );
}
/**
* sanitizeUserInfo Used to remove sensitive information from $this->mInfo when it is unneccessary (i.e. $gQueryUser)
*
* @access public
* @return void
*/
function sanitizeUserInfo() {
if( !empty( $this->mInfo )) {
$unsanitary = array( 'provpass', 'hash', 'challenge', 'user_password' );
foreach( array_keys( $this->mInfo ) as $key ) {
if( in_array( $key, $unsanitary )) {
unset( $this->mInfo[$key] );
}
}
}
}
/**
* store
*
* @param array $pParamHash
* @access public
* @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
*/
function store( &$pParamHash ) {
global $gBitSystem;
// keep track of newUser before calling base class
$newUser = !$this->isRegistered();
$this->mDb->StartTrans();
if( BitUser::store( $pParamHash ) && $newUser ) {
$defaultGroups = $this->getDefaultGroup();
$this->addUserToGroup( $this->mUserId, $defaultGroups );
if( $gBitSystem->isFeatureActive( 'users_eponymous_groups' ) ) {
// Create a group just for this user, for permissions assignment.
$groupParams = array(
'user_id' => $this->mUserId,
'name' => $pParamHash['user_store']['login'],
'desc' => "Personal group for ".( !empty( $pParamHash['user_store']['real_name'] ) ? $pParamHash['user_store']['real_name'] : $pParamHash['user_store']['login'] )
);
if( $this->storeGroup( $groupParams ) ) {
$this->addUserToGroup( $this->mUserId, $groupParams['group_id'] );
}
}
$this->load( TRUE );
// store any uploaded images, this can stuff mErrors, so we want to do this as the very last thing.
$pParamHash['upload']['thumbnail'] = FALSE; // i don't think this does anything - perhaps replace it by setting thumbnail_sizes
$this->storeImages( $pParamHash );
}
$this->mDb->CompleteTrans();
return( count( $this->mErrors ) == 0 );
}
/**
* groupExists work out if a given group exists
*
* @param string $pGroupName
* @param numeric $pUserId
* @access public
* @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
*/
function groupExists( $pGroupName, $pUserId = ROOT_USER_ID ) {
static $sGroups = array();
if( !isset( $sGroups[$pUserId][$pGroupName] ) ) {
$bindVars = array( $pGroupName );
$whereSql = '';
if( $pUserId != '*' ) {
$whereSql = 'AND `user_id`=?';
$bindVars[] = $pUserId;
}
$query = "
SELECT ug.`group_name`, ug.`group_id`, ug.`user_id`
FROM `".BIT_DB_PREFIX."users_groups` ug
WHERE `group_name`=? $whereSql";
if( $result = $this->mDb->getAssoc( $query, $bindVars ) ) {
if( empty( $sGroups[$pUserId] ) ) {
$sGroups[$pUserId] = array();
}
$sGroups[$pUserId][$pGroupName] = $result[$pGroupName];
} else {
$sGroups[$pUserId][$pGroupName]['group_id'] = NULL;
}
}
return( $sGroups[$pUserId][$pGroupName]['group_id'] );
}
/**
* removes user and associated private data
*
* @access public
* @return always FALSE???
* TODO: fix return
*/
function expunge( $pExpungeContent=NULL) {
global $gBitSystem, $gBitUser;
if( $this->isValid() ) {
$this->mDb->StartTrans();
if( $this->mUserId == $gBitUser->mUserId ) {
$this->mDb->RollbackTrans();
$gBitSystem->fatalError( tra( 'You cannot delete yourself' ) );
} elseif( $this->mUserId != ANONYMOUS_USER_ID ) {
$userTables = array(
'users_groups_map',
);
foreach( $userTables as $table ) {
$query = "DELETE FROM `".BIT_DB_PREFIX.$table."` WHERE `user_id` = ?";
$result = $this->mDb->query( $query, array( $this->mUserId ) );
}
if( parent::expunge( $pExpungeContent ) ) {
$this->mDb->CompleteTrans();
return TRUE;
} else {
$this->mDb->RollbackTrans();
}
} else {
$this->mDb->RollbackTrans();
$gBitSystem->fatalError( tra( 'The anonymous user cannot be deleted' ) );
}
}
return FALSE;
}
// =-=-=-=-=-=-=-=-=-=-=-= Group Functions =-=-=-=-=-=-=-=-=-=-=-=-=-=-=
/**
* loadGroups load groups into $this->mGroups
*
* @param boolean $pForceRefresh
* @access public
* @return void
*/
function loadGroups( $pForceRefresh = FALSE ) {
if( $this->isValid() ) {
$this->mGroups = $this->getGroups( NULL, $pForceRefresh );
}
}
/**
* isInGroup work out if a given user is in a group
*
* @param mixed $pGroupMixed Group ID or Group Name (deprecated)
* @access public
* @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
*/
function isInGroup( $pGroupMixed ) {
$ret = FALSE;
if( $this->isAdmin() ) {
$ret = TRUE;
} if( $this->isValid() ) {
if( empty( $this->mGroups ) ) {
$this->loadGroups();
}
if( preg_match( '/A-Za-z/', $pGroupMixed ) ) {
// Old style group name passed in
deprecated( "Please use the Group ID instead of the Group name." );
$ret = in_array( $pGroupMixed, $this->mGroups );
} else {
$ret = isset( $this->mGroups[$pGroupMixed] );
}
}
return $ret;
}
/**
* getAllGroups Get a list of all Groups
*
* @param array $pListHash List Hash
* @access public
* @return array of groups
*/
function getAllGroups( &$pListHash ) {
if( empty( $pListHash['sort_mode'] ) || $pListHash['sort_mode'] == 'name_asc' ) {
$pListHash['sort_mode'] = 'group_name_asc';
}
$this->prepGetList( $pListHash );
$sortMode = $this->mDb->convertSortmode( $pListHash['sort_mode'] );
if( !empty( $pListHash['find_groups'] ) ) {
$mid = " AND UPPER(`group_name`) like ?";
$bindvars[] = "%".strtoupper( $pListHash['find_groups'] )."%";
} elseif( !empty( $pListHash['find'] ) ) {
$mid = " AND UPPER(`group_name`) like ?";
$bindvars[] = "%".strtoupper( $pListHash['find'] )."%";
} else {
$mid = '';
$bindvars = array();
}
if( !empty( $pListHash['hide_root_groups'] )) {
$mid .= ' AND `user_id` <> '.ROOT_USER_ID;
} elseif( !empty( $pListHash['only_root_groups'] )) {
$mid .= ' AND `user_id` = '.ROOT_USER_ID;
}
if( !empty( $pListHash['user_id'] ) ){
$mid .= ' AND `user_id` = ? ';
$bindvars[] = $pListHash['user_id'];
}
if( !empty( $pListHash['is_public'] ) ) {
$mid .= ' AND `is_public` = ?';
$bindvars[] = $pListHash['is_public'];
}
if( !empty( $pListHash['visible'] ) && !$this->isAdmin() ){
global $gBitUser;
$mid .= ' AND `user_id` = ? OR `is_public` = ? ';
$bindvars[] = $gBitUser->mUserId;
$bindvars[] = 'y';
}
$mid = preg_replace('/^ AND */',' WHERE ', $mid);
$query = "
SELECT `user_id`, `group_id`, `group_name` , `group_desc`, `group_home`, `is_default`, `is_public`
FROM `".BIT_DB_PREFIX."users_groups` $mid
ORDER BY $sortMode";
$ret = array();
if( $rs = $this->mDb->query( $query, $bindvars ) ) {
while( $row = $rs->fetchRow() ) {
$groupId = $row['group_id'];
$ret[$groupId] = $row;
$ret[$groupId]['perms'] = $this->getGroupPermissions( array( 'group_id' => $groupId ));
}
}
$pListHash['cant'] = $this->mDb->getOne( "SELECT COUNT(*) FROM `".BIT_DB_PREFIX."users_groups` $mid", $bindvars );
return $ret;
}
/**
* getAllUserGroups
*
* @param numeric $pUserId
* @access public
* @return array of groups a user belongs to
*/
function getAllUserGroups( $pUserId = NULL ) {
if( empty( $pUserId ) ) {
$pUserId = $this->mUserId;
}
$sql = "
SELECT ug.`group_id` AS `hash_key`, ug.* FROM `".BIT_DB_PREFIX."users_groups` ug
WHERE `user_id`=?
ORDER BY ug.`group_name` ASC";
return $this->mDb->getAssoc( $sql, array( $pUserId ));
}
/**
* expungeGroup remove a group
*
* @param numeric $pGroupId
* @access public
* @return TRUE on success, FALSE on failure
*/
function expungeGroup( $pGroupId ) {
// we cannot remove the anonymous group
if( $pGroupId != ANONYMOUS_GROUP_ID ) {
$query = "DELETE FROM `".BIT_DB_PREFIX."users_groups_map` WHERE `group_id` = ?";
$result = $this->mDb->query( $query, array( $pGroupId ));
$query = "DELETE FROM `".BIT_DB_PREFIX."users_group_permissions` WHERE `group_id` = ?";
$result = $this->mDb->query( $query, array( $pGroupId ));
$query = "DELETE FROM `".BIT_DB_PREFIX."users_groups` WHERE `group_id` = ?";
$result = $this->mDb->query( $query, array( $pGroupId ));
return TRUE;
}
}
/**
* getDefaultGroup get the default group of a given user
*
* @param array $pGroupId pass in a Group ID to make conditional function
* @access public
* @return Default Group ID if one is set
*/
function getDefaultGroup( $pGroupId = NULL ) {
$bindvars = NULL;
$whereSql = '';
if( @BitBase::verifyId( $pGroupId )) {
$whereSql = "AND `group_id`=? ";
$bindvars = array( $pGroupId );
}
return( $this->mDb->getAssoc( "SELECT `group_id`, `group_name` FROM `".BIT_DB_PREFIX."users_groups` WHERE `is_default` = 'y' $whereSql ", $bindvars ) );
}
/**
* getGroupUsers Get a list of users who share a given group id
*
* @param array $pGroupId
* @access public
* @return list of users who are in the group id
*/
function getGroupUsers( $pGroupId ) {
$ret = array();
if( @BitBase::verifyId( $pGroupId )) {
$query = "
SELECT uu.`user_id` AS hash_key, uu.`login`, uu.`real_name`, uu.`user_id`
FROM `".BIT_DB_PREFIX."users_users` uu
INNER JOIN `".BIT_DB_PREFIX."users_groups_map` ug ON (uu.`user_id`=ug.`user_id`)
WHERE `group_id`=?";
$ret = $this->mDb->getAssoc( $query, array( $pGroupId ));
}
return $ret;
}
/**
* getGroupHome get the URL where a user of that group should be sent
*
* @param array $pGroupId
* @access public
* @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
*/
function getGroupHome( $pGroupId ) {
$ret = FALSE;
if( @BitBase::verifyId( $pGroupId )) {
$query = "SELECT `group_home` FROM `".BIT_DB_PREFIX."users_groups` WHERE `group_id`=?";
$ret = $this->mDb->getOne( $query,array( $pGroupId ) );
}
return $ret;
}
/**
* storeUserDefaultGroup
*
* @param array $pUserId
* @param array $pGroupId
* @access public
* @return TRUE on success, FALSE on failure
*/
function storeUserDefaultGroup( $pUserId, $pGroupId ) {
if( @BitBase::verifyId( $pUserId ) && @BitBase::verifyId( $pGroupId )) {
$query = "UPDATE `".BIT_DB_PREFIX."users_users` SET `default_group_id` = ? WHERE `user_id` = ?";
return $this->mDb->query( $query, array( $pGroupId, $pUserId ));
}
}
/**
* batchAssignUsersToGroup assign all users to a given group
*
* @param array $pGroupId
* @access public
* @return void
*/
function batchAssignUsersToGroup( $pGroupId ) {
if( @BitBase::verifyId( $pGroupId )) {
$users = $this->getGroupUsers( $pGroupId );
$result = $this->mDb->getCol( "SELECT uu.`user_id` FROM `".BIT_DB_PREFIX."users_users` uu" );
foreach( $result as $userId ) {
if( empty( $users[$userId] ) && $userId != ANONYMOUS_USER_ID ) {
$this->addUserToGroup( $userId, $pGroupId );
}
}
}
}
/**
* batchSetUserDefaultGroup
*
* @param array $pGroupId
* @access public
* @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
*/
function batchSetUserDefaultGroup( $pGroupId ) {
if( @BitBase::verifyId( $pGroupId )) {
$users = $this->getGroupUsers($pGroupId);
foreach( array_keys( $users ) as $userId ) {
$this->storeUserDefaultGroup( $userId, $pGroupId );
}
}
}
/**
* getGroupInfo
*
* @param array $pGroupId
* @access public
* @return group information
*/
function getGroupInfo( $pGroupId ) {
if( @BitBase::verifyId( $pGroupId )) {
$sql = "SELECT * FROM `".BIT_DB_PREFIX."users_groups` WHERE `group_id` = ?";
$ret = $this->mDb->getRow( $sql, array( $pGroupId ));
$listHash = array(
'group_id' => $pGroupId,
'sort_mode' => 'up.perm_name_asc',
);
$ret["perms"] = $this->getGroupPermissions( $listHash );
$sql = "SELECT COUNT(*) FROM `".BIT_DB_PREFIX."users_groups_map` WHERE `group_id` = ?";
$ret['num_members'] = $this->mDb->getOne( $sql, array( $pGroupId ));
return $ret;
}
}
/**
* addUserToGroup Adds user pUserId to group(s) pGroupMixed.
*
* @param numeric $pUserId User ID
* @param mixed $pGroupMixed A single group ID or an array of group IDs
* @access public
* @return Either an ADO RecordSet (success) or FALSE (failure).
*/
function addUserToGroup( $pUserId, $pGroupMixed ) {
$result = FALSE;
if( @BitBase::verifyId( $pUserId ) && !empty( $pGroupMixed )) {
$result = TRUE;
$addGroups = array();
if( is_array( $pGroupMixed ) ) {
$addGroups = array_keys( $pGroupMixed );
} elseif( @BitBase::verifyId($pGroupMixed) ) {
$addGroups = array( $pGroupMixed );
}
$currentUserGroups = $this->getGroups( $pUserId );
foreach( $addGroups AS $groupId ) {
$isInGroup = FALSE;
foreach( $currentUserGroups as $curGroupId => $curGroupInfo ) {
if( $curGroupId == $groupId ) {
$isInGroup = TRUE;
}
}
if( !$isInGroup ) {
$query = "INSERT INTO `".BIT_DB_PREFIX."users_groups_map` (`user_id`,`group_id`) VALUES(?,?)";
$result = $this->mDb->query( $query, array( $pUserId, $groupId ));
}
}
}
return $result;
}
/**
* removeUserFromGroup
*
* @param array $pUserId
* @param array $pGroupId
* @access public
* @return void
*/
function removeUserFromGroup( $pUserId, $pGroupId ) {
if( @BitBase::verifyId( $pUserId ) && @BitBase::verifyId( $pGroupId )) {
$query = "DELETE FROM `".BIT_DB_PREFIX."users_groups_map` WHERE `user_id` = ? AND `group_id` = ?";
$result = $this->mDb->query( $query, array( $pUserId, $pGroupId ));
$default = $this->getDefaultGroup();
if( $pGroupId == key( $default )) {
$query = "UPDATE `".BIT_DB_PREFIX."users_users` SET `default_group_id` = NULL WHERE `user_id` = ?";
$this->mDb->query( $query, array( $pUserId ));
}
}
}
/**
* verifyGroup
*
* @param array $pParamHash
* @access public
* @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
*/
function verifyGroup( &$pParamHash ) {
if( !empty($pParamHash['group_id'] )) {
if( @$this->verifyId( $pParamHash['group_id'] )) {
$pParamHash['group_store']['group_id'] = $pParamHash['group_id'];
} else {
$this->mErrors['groups'] = 'Unknown Group';
}
}
if( !empty( $pParamHash["name"] )) {
$pParamHash['group_store']['group_name'] = substr( $pParamHash["name"], 0, 30 );
}
if( !empty( $pParamHash["desc"] )) {
$pParamHash['group_store']['group_desc'] = substr( $pParamHash["desc"], 0, 255 );;
}
$pParamHash['group_store']['group_home'] = !empty( $pParamHash["home"] ) ? $pParamHash["home"] : '';
$pParamHash['group_store']['is_default'] = !empty( $pParamHash["is_default"] ) ? $pParamHash["is_default"] : NULL;
$pParamHash['group_store']['user_id'] = @$this->verifyId( $pParamHash["user_id"] ) ? $pParamHash["user_id"] : $this->mUserId;
$pParamHash['group_store']['is_public'] = !empty( $pParamHash['is_public'] ) ? $pParamHash['is_public'] : NULL;
$pParamHash['group_store']['after_registration_page'] = !empty( $pParamHash['after_registration_page'] ) ? $pParamHash['after_registration_page'] : '';
return( count( $this->mErrors ) == 0 );
}
/**
* storeGroup
*
* @param array $pParamHash
* @access public
* @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
*/
function storeGroup( &$pParamHash ) {
global $gBitSystem;
if ($this->verifyGroup( $pParamHash)) {
$this->mDb->StartTrans();
if( empty( $pParamHash['group_id'] ) ) {
$pParamHash['group_id'] = $this->mDb->GenID( 'users_groups_id_seq' );
$pParamHash['group_store']['group_id'] = $pParamHash['group_id'];
$result = $this->mDb->associateInsert( BIT_DB_PREFIX.'users_groups', $pParamHash['group_store'] );
} else {
$sql = "SELECT COUNT(*) FROM `".BIT_DB_PREFIX."users_groups` WHERE `group_id` = ?";
$groupExists = $this->mDb->getOne($sql, array($pParamHash['group_id']));
if ($groupExists) {
$result = $this->mDb->associateUpdate( BIT_DB_PREFIX.'users_groups', $pParamHash['group_store'], array( "group_id" => $pParamHash['group_id'] ) );
} else {
// A group_id was specified but that group does not exist yet
$pParamHash['group_store']['group_id'] = $pParamHash['group_id'];
$result = $this->mDb->associateInsert(BIT_DB_PREFIX.'users_groups', $pParamHash['group_store']);
}
}
if( isset( $_REQUEST['batch_set_default'] ) and $_REQUEST['batch_set_default'] == 'on' ) {
$gBitUser->batchSetUserDefaultGroup( $pParamHash['group_id'] );
}
$this->mDb->CompleteTrans();
}
return ( count( $this->mErrors ) == 0 );
}
/**
* getGroupUserData
*
* @param array $pGroupId
* @param array $pColumns
* @access public
* @return array of group data
*/
function getGroupUserData( $pGroupId, $pColumns ) {
$ret = array();
if( @$this->verifyId( $pGroupId ) && !empty( $pColumns ) ) {
if( is_array( $pColumns ) ) {
$col = implode( $pColumns, ',' );
$exec = 'getAssoc';
} else {
$col = '`'.$pColumns.'`';
$exec = 'getArray';
}
$query = "
SELECT $col
FROM `".BIT_DB_PREFIX."users_users` uu
INNER JOIN `".BIT_DB_PREFIX."users_groups_map` ugm ON (uu.`user_id`=ugm.`user_id`)
WHERE ugm.`group_id` = ?";
$ret = $this->mDb->$exec( $query, array( $pGroupId ));
}
return $ret;
}
// =-=-=-=-=-=-=-=-=-=-=-= PERMISSION FUNCTIONS =-=-=-=-=-=-=-=-=-=-=-=-=-=-=
/**
* loadPermissions
*
* @access public
* @return TRUE on success, FALSE if no perms were loaded
*/
function loadPermissions( $pForceReload=FALSE ) {
if( $this->isValid() && (empty( $this->mPerms ) || $pForceReload) ) {
$this->mPerms = array();
// the double up.`perm_name` is intentional - the first is for hash key, the second is for hash value
$query = "
SELECT up.`perm_name` AS `hash_key`, up.`perm_name`, up.`perm_desc`, up.`perm_level`, up.`package`
FROM `".BIT_DB_PREFIX."users_permissions` up
INNER JOIN `".BIT_DB_PREFIX."users_group_permissions` ugp ON ( ugp.`perm_name`=up.`perm_name` )
INNER JOIN `".BIT_DB_PREFIX."users_groups` ug ON ( ug.`group_id`=ugp.`group_id` )
LEFT OUTER JOIN `".BIT_DB_PREFIX."users_groups_map` ugm ON ( ugm.`group_id`=ugp.`group_id` AND ugm.`user_id` = ? )
WHERE ug.`group_id`= ".ANONYMOUS_GROUP_ID." OR ugm.`group_id`=ug.`group_id`";
$this->mPerms = $this->mDb->getAssoc( $query, array( $this->mUserId ));
// Add in override permissions
if( !empty( $this->mPermsOverride ) ) {
foreach( $this->mPermsOverride as $key => $val ) {
$this->mPerms[$key] = $val;
}
}
}
return( count( $this->mPerms ) );
}
/**
* getUnassignedPerms
*
* @access public
* @return array of permissions that have not been assigned to any group yet
*/
function getUnassignedPerms() {
$query = "SELECT up.`perm_name` AS `hash_key`, up.*
FROM `".BIT_DB_PREFIX."users_permissions` up
LEFT OUTER JOIN `".BIT_DB_PREFIX."users_group_permissions` ugp ON( up.`perm_name` = ugp.`perm_name` )
WHERE ugp.`group_id` IS NULL AND up.`perm_name` <> ?
ORDER BY `package`, up.`perm_name` ASC";
return( $this->mDb->getAssoc( $query, array( '' )));
}
/**
* isAdmin
*
* @param array $pCheckTicket
* @access public
* @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
*/
function isAdmin() {
// we can't use hasPermission here since it turn into an endless loop
return( !empty( $this->mPerms['p_admin'] ));
}
/**
* hasPermission check to see if a user has a given permission
*
* @param array $pPerm Perm name
* @access public
* @return TRUE if the user has a permission, FALSE if they don't
*/
function hasPermission( $pPerm ) {
$ret = FALSE;
if( $this->isAdmin() ) {
$ret = TRUE;
} elseif( $this->isValid() ) {
$ret = isset( $this->mPerms[$pPerm] );
}
return ( $ret );
}
/**
* verifyPermission check if a user has a given permission and if not
* it will display the error template and die()
* @param $pPermission value of a given permission
* @return none
* @access public
*/
function verifyPermission( $pPermission, $pMsg = NULL ) {
global $gBitSmarty, $gBitSystem, ${$pPermission};
if( empty( $pPermission ) || $this->hasPermission( $pPermission ) ) {
return TRUE;
} else {
$gBitSystem->fatalPermission( $pPermission, $pMsg );
}
}
/**
* getGroupPermissions
*
* @param array $pGroupId Group id, if unset, all groups are returned
* @param string $pPackage permissions to give group, if unset, all permissions are returned
* @param string $find search for a particular permission
* @param array $pSortMode sort mode of return hash
* @access public
* @return TRUE on success, FALSE on failure
*/
function getGroupPermissions( $pParamHash = NULL ) {
global $gBitSystem;
$ret = $bindVars = array();
$whereSql = $selectSql = $fromSql = '';
if( !empty( $pParamHash['sort_mode'] )) {
$sortMode = $this->mDb->convertSortmode( $pParamHash['sort_mode'] );
} else {
$sortMode = 'up.`package`, up.`perm_name` ASC';
}
if( !empty( $pParamHash['package'] )) {
$whereSql = ' WHERE `package`= ? ';
$bindVars[] = $pParamHash['package'];
}
if( @BitBase::verifyId( $pParamHash['group_id'] )) {
$selectSql = ', ugp.`perm_value` AS `hasPerm` ';
$fromSql = ' INNER JOIN `'.BIT_DB_PREFIX.'users_group_permissions` ugp ON ( ugp.`perm_name`=up.`perm_name` ) ';
if( $whereSql ) {
$whereSql .= " AND ugp.`group_id`=?";
} else {
$whereSql .= " WHERE ugp.`group_id`=?";
}
$bindVars[] = $pParamHash['group_id'];
}
if( !empty( $pParamHash['find'] )) {
if( $whereSql ) {
$whereSql .= " AND `perm_name` like ?";
} else {
$whereSql .= " WHERE `perm_name` like ?";
}
$bindVars[] = '%'.$pParamHash['find'].'%';
}
// the double up.`perm_name` is intentional - the first is for hash key, the second is for hash value
$query = "
SELECT up.`perm_name` AS `hash_key`, up.`perm_name`, up.`perm_desc`, up.`perm_level`, up.`package` $selectSql
FROM `".BIT_DB_PREFIX."users_permissions` up $fromSql $whereSql
ORDER BY $sortMode";
$perms = $this->mDb->getAssoc( $query, $bindVars );
// weed out permissions of inactive packages
$ret = array();
foreach( $perms as $key => $perm ) {
if( $gBitSystem->isPackageActive( $perm['package'] )) {
$ret[$key] = $perm;
}
}
return $ret;
}
/**
* assignLevelPermissions Assign the permissions of a given level to a given group
*
* @param array $pGroupId Group we want to assign permissions to
* @param array $pLevel permission level we wish to assign from
* @param array $pPackage limit set of permissions to a given package
* @access public
* @return void
*/
function assignLevelPermissions( $pGroupId, $pLevel, $pPackage = NULL) {
if( @BitBase::verifyId( $pGroupId ) && !empty( $pLevel )) {
$bindvars = array( $pLevel );
$whereSql = '';
if( !empty( $pPackage ) ) {
$whereSql = ' AND `package`=?';
array_push( $bindvars, $pPackage );
}
$query = "SELECT `perm_name` FROM `".BIT_DB_PREFIX."users_permissions` WHERE `perm_level` = ? $whereSql";
$result = $this->mDb->query( $query, $bindvars );
while( $row = $result->fetchRow() ) {
$this->assignPermissionToGroup( $row['perm_name'], $pGroupId );
}
}
}
/**
* getPermissionPackages Get a list of packages that have their own set of permissions
*
* @access public
* @return array of packages
*/
function getPermissionPackages() {
return( $this->mDb->getCol( "SELECT DISTINCT(`package`) FROM `".BIT_DB_PREFIX."users_permissions` ORDER BY `package`" ) );
}
/**
* assignPermissionToGroup
*
* @param array $perm
* @param array $pGroupId
* @access public
* @return TRUE on success
*/
function assignPermissionToGroup( $pPerm, $pGroupId ) {
if( @BitBase::verifyId( $pGroupId ) && !empty( $pPerm )) {
$query = "DELETE FROM `".BIT_DB_PREFIX."users_group_permissions` WHERE `group_id` = ? AND `perm_name` = ?";
$result = $this->mDb->query( $query, array( $pGroupId, $pPerm ));
$query = "INSERT INTO `".BIT_DB_PREFIX."users_group_permissions`(`group_id`, `perm_name`) VALUES(?, ?)";
$result = $this->mDb->query( $query, array( $pGroupId, $pPerm ));
return TRUE;
}
}
/**
* removePermissionFromGroup
*
* @param string $pPerm Perm name
* @param numeric $pGroupId Group ID
* @access public
* @return TRUE on success
*/
function removePermissionFromGroup( $pPerm, $pGroupId ) {
if( @BitBase::verifyId( $pGroupId ) && !empty( $pPerm )) {
$query = "DELETE FROM `".BIT_DB_PREFIX."users_group_permissions` WHERE `perm_name` = ? AND `group_id` = ?";
$result = $this->mDb->query($query, array($pPerm, $pGroupId));
return TRUE;
}
}
/**
* storeRegistrationChoice
*
* @param mixed $pGroupMixed A single group ID or an array of group IDs
* @param array $pValue Value you wish to store - use NULL to delete a value
* @access public
* @return ADO record set on success, FALSE on failure
*/
function storeRegistrationChoice( $pGroupMixed, $pValue = NULL ) {
if( !empty( $pGroupMixed )) {
$bindVars[] = $pValue;
if( is_array( $pGroupMixed )) {
$mid = implode( ',', array_fill( 0, count( $pGroupMixed ),'?' ));
$bindVars = array_merge( $bindVars, $pGroupMixed );
} else {
$bindVars[] = $pGroupMixed;
$mid = 'LIKE ?';
}
$query = "UPDATE `".BIT_DB_PREFIX."users_groups` SET `is_public`= ? where `group_id` IN ($mid)";
return $this->mDb->query( $query, $bindVars );
}
}
/**
* Grant a single permission to a given value
*/
function setPermissionOverride( $pPerm, $pValue = NULL ) {
if( $this->isAdmin() ) {
$this->mPerms[$pPerm] = TRUE;
$this->mPermsOverride[$pPerm] = TRUE;
} elseif( $this->isValid() ) {
if( $pValue == 'y' || $pValue == TRUE ) {
$this->mPermsOverride[$pPerm] = TRUE;
$this->mPerms[$pPerm] = TRUE;
} else {
unset( $this->mPermsOverride[$pPerm] );
unset( $this->mPerms[$pPerm] );
}
}
}
// {{{ ==================== deprecated methods - will be removed soon ====================
// - xing - Saturday Oct 18, 2008 11:38:05 CEST
/**
* @deprecated deprecated since version 2.1.0-beta
*/
function get_user_id( $pUserName ) {
deprecated( "This method doesn't seem to be used. Please remove this note if it is actually used." );
if( !empty( $pUserName ) ) {
$id = $this->mDb->getOne("select `user_id` from `".BIT_DB_PREFIX."users_users` where `login`=?", array($pUserName));
$id = ($id === NULL) ? -1 : $id;
return $id;
}
}
/**
* @deprecated deprecated since version 2.1.0-beta
*/
function remove_group($pGroupId) {
deprecated( 'Method has been renamed to $gBitUser->expungeGroup()' );
$this->expungeGroup( $pGroupId );
}
/**
* @deprecated deprecated since version 2.1.0-beta
*/
function get_group_users( $pGroupId ) {
deprecated( 'Method has been renamed to $gBitUser->getGroupUsers()' );
$this->getGroupUsers( $pGroupId );
}
/**
* @deprecated deprecated since version 2.1.0-beta
*/
function batch_set_user_default_group( $pGroupId ) {
deprecated( 'Method has been renamed to $gBitUser->batchSetUserDefaultGroup()' );
$this->batchSetUserDefaultGroup( $pGroupId );
}
/**
* @deprecated deprecated since version 2.1.0-beta
*/
function countGroupUsers( $pGroupId ) {
deprecated( "This method doesn't seem to be used. Please remove this note if it is actually used." );
static $sGroupUsers = array();
if( !isset( $sGroupUsers[$pGroupId] ) ) {
$query = "SELECT COUNT(`user_id`) from `".BIT_DB_PREFIX."users_groups_map` WHERE `group_id` = ?";
$sGroupUsers[$pGroupId] = $this->mDb->getOne($query, array( $pGroupId ) );
}
return $sGroupUsers[$pGroupId];
}
/**
* @deprecated deprecated since version 2.1.0-beta
*/
function changePermissionLevel($perm, $pLevel) {
deprecated( "This method doesn't seem to be used. Please remove this note if it is actually used." );
$query = "update `".BIT_DB_PREFIX."users_permissions` set `perm_level` = ?
where `perm_name` = ?";
$this->mDb->query($query, array($pLevel, $perm));
}
/**
* @deprecated deprecated since version 2.1.0-beta
*/
function removeLevelPermissions($group, $pLevel) {
deprecated( "This method doesn't seem to be used. Please remove this note if it is actually used." );
$query = "select `perm_name` from `".BIT_DB_PREFIX."users_permissions` where `perm_level` = ?";
$result = $this->mDb->query($query, array($pLevel));
while ($res = $result->fetchRow()) {
$this->removePermissionFromGroup($res['perm_name'], $group);
}
}
/**
* @deprecated deprecated since version 2.1.0-beta
*/
function createDummyLevel($pLevel) {
deprecated( "This method doesn't seem to be used. Please remove this note if it is actually used." );
$query = "delete from `".BIT_DB_PREFIX."users_permissions` where `perm_name` = ?";
$result = $this->mDb->query($query, array(''));
$query = "insert into `".BIT_DB_PREFIX."users_permissions` (`perm_name`, `perm_desc`,
`package`, `perm_level`) VALUES ('','','',?)";
$this->mDb->query($query, array($pLevel));
}
/**
* @deprecated deprecated since version 2.1.0-beta
*/
function getPermissionLevels() {
deprecated( "This method doesn't seem to be used. Please remove this note if it is actually used." );
return( $this->mDb->getCol( "SELECT DISTINCT(`perm_level`) FROM `".BIT_DB_PREFIX."users_permissions` ORDER BY `perm_level`" ));
}
/**
* @deprecated deprecated since version 2.1.0-beta
*/
function group_has_permission( $pGroupId, $pPerm ) {
deprecated( "This method doesn't seem to be used. Please remove this note if it is actually used." );
if (!isset($pPerm, $this->cGroupPerms[$pGroupId][$pPerm])) {
$query = "SELECT count(*)
FROM `".BIT_DB_PREFIX."users_group_permissions`
WHERE `group_id`=? AND `perm_name`=?";
$result = $this->mDb->getOne( $query, array( $pGroupId, $pPerm ) );
$this->cGroupPerms[$pGroupId][$pPerm] = $result;
return $result;
} else {
return $this->cGroupPerms[$pGroupId][$pPerm];
}
}
/**
* @deprecated deprecated since version 2.1.0-beta
*/
function getContentTypeList( $pUserId ) {
deprecated( "This method doesn't seem to be used. Please remove this note if it is actually used." );
foreach( $gLibertySystem->mContentTypes as $contentType ) {
// TODO: if this method is used somewhere, please fix this to use mViewContentPerm
$perm = $contentType["content_type_guid"].'_p_view';
if (!empty( $perm ) and $gBitUser->hasPermission( $perm )) {
$contentTypes[$contentType["content_type_guid"]] = $gLibertySystem->getContentTypeName( $contentType["content_type_guid"] );
}
}
}
/**
* @deprecated deprecated since version 2.1.0-beta
*/
function setPermission( $pPerm, $pValue = NULL ) {
deprecated( "This method doesn't do what it's supposed to and it seems it's not used." );
if( $this->isAdmin() ) {
$this->mPerms[$pPerm] = TRUE;
} elseif( $this->isValid() ) {
if( $pValue == 'y' || $pValue == TRUE ) {
$this->mPerms[$pPerm] = TRUE;
} else {
unset( $this->mPerms[$pPerm] );
}
}
}
// }}}
}
/* vim: :set fdm=marker : */
?>
|