-
Notifications
You must be signed in to change notification settings - Fork 2
/
my_roles.php
212 lines (197 loc) · 7.82 KB
/
my_roles.php
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
<?php
/**
* my roles
*
* @copyright (c) 2004-15 bitweaver.org
*
* @package users
* @subpackage functions
*/
/**
* required setup
*/
require_once( '../kernel/includes/setup_inc.php' );
global $gBitUser, $gBitSystem;
// PERMISSIONS: registered user required
if ( !$gBitUser->isRegistered() ) {
$gBitSystem->fatalError( tra( "You are not logged in." ));
}
if( !empty( $_REQUEST["cancel"] ) ) {
bit_redirect( USERS_PKG_URL.'my_roles.php' );
}
if ( $gBitUser->hasPermission('p_users_create_personal_roles' ) ) {
if( !empty( $_REQUEST['role_id'] ) ) {
$allPerms = $gBitUser->getRolePermissions( array( 'sort_mode' => !empty( $_REQUEST['sort_mode'] ) ? $_REQUEST['sort_mode'] : NULL ));
// get rolelist separately from the $users stuff to avoid splitting of data due to pagination
$listHash = array( 'sort_mode' => 'role_name_asc' );
$roleList = $gBitUser->getAllRoles( $listHash );
} else {
// get rolelist separately from the $users stuff to avoid splitting of data due to pagination
$listHash = array( 'sort_mode' => !empty( $_REQUEST['sort_mode'] ) ? $_REQUEST['sort_mode'] : 'role_name_asc' );
$roleList = $gBitUser->getAllRoles( $listHash );
}
// Remember a package limit if it is set.
$gBitSmarty->assign( 'package',isset( $_REQUEST['package'] ) ? $_REQUEST['package'] : 'all' );
// Save the join
if( isset($_REQUEST["save"] ) ) {
if( empty($_REQUEST["name"] ) ) {
$_REQUEST["name"] = $_REQUEST["olrole"];
}
if( $gBitUser->storeRole( $_REQUEST ) ) {
$successMsg = tra("Role changes were saved sucessfully.");
} else {
$errorMsg = $gBitUser->mErrors['roles'];
}
// Update Permissions
} elseif (isset($_REQUEST['updateperms'])) {
$listHash = array( 'role_id' => $_REQUEST['role_id'] );
$updatePerms = $gBitUser->getrolePermissions( $listHash );
foreach (array_keys($_REQUEST['perm']) as $per) {
if( isset($_REQUEST['perm'][$per]) && !isset($updatePerms[$per]) ) {
// we have an unselected perm that is now selected
$gBitUser->assignPermissionToRole($per, $_REQUEST['role_id']);
} elseif( empty($_REQUEST['perm'][$per]) && isset($updatePerms[$per]) ) {
// we have a selected perm that is now UNselected
$gBitUser->removePermissionFromRole($per, $_REQUEST['role_id']);
}
}
// let's reload just to be safe.
$allPerms = $gBitUser->getRolePermissions();
// Do some action
} elseif (isset($_REQUEST["action"])) {
// Process a form to remove a role
if( $_REQUEST["action"] == 'delete' ) {
if( $gBitUser->getDefaultRole( $_REQUEST['role_id'] ) ) {
$errorMsg = tra("You cannot remove this role, as it is currently set as your 'Default' role");
} else {
$gBitUser->expungeRole( $_REQUEST['role_id'] );
$successMsg = tra("The role was deleted.");
unset( $_REQUEST['role_id'] );
}
// remove a permission from a role
} elseif ($_REQUEST["action"] == 'remove') {
$gBitUser->removePermissionFromRole( $_REQUEST["permission"], $_REQUEST['role_id'] );
$successMsg = tra("Permission Removed");
$mid = 'bitpackage:users/my_role_edit.tpl';
// Create a new role
} elseif( $_REQUEST["action"] == 'create' ) {
$gBitSystem->setBrowserTitle( tra('Create New Role') );
$mid = 'bitpackage:users/my_role_edit.tpl';
// Assign a permission to a role
} elseif ($_REQUEST["action"] == 'assign') {
$gBitUser->assignPermissionToRole($_REQUEST["perm"], $_REQUEST['role_id']);
$successMsg = tra("Permission Assigned");
$mid = 'bitpackage:users/my_role_edit.tpl';
}
// Search for users to add
} elseif (!empty($_REQUEST['submitUserSearch'])) {
$searchParams = array('find' => $_REQUEST['find']);
$gBitUser->getList($searchParams);
$foundUsers = $searchParams['data'];
$mid = 'bitpackage:users/my_role_edit.tpl';
$gBitSmarty->assignByRef('foundUsers', $foundUsers);
} elseif (!empty($_REQUEST['assignuser'])) {
if( !empty($_REQUEST['role_id'] ) ) {
if ($_REQUEST['role_id'] != -1 && $roleList[$_REQUEST['role_id']]['user_id'] == $gBitUser->mUserId) {
$gBitUser->addUserToRole( $_REQUEST['assignuser'], $_REQUEST['role_id'] );
}
else {
$errorMsg = tra("You can not assign users to this role.");
}
}
$mid = 'bitpackage:users/my_role_edit.tpl';
}
// get pagination url
// get rolelist separately from the $users stuff to avoid splitting of data due to pagination
$listHash = array( 'sort_mode' => 'role_name_asc' );
$roleList = $gBitUser->getAllUserRoles();
if( !empty( $_REQUEST['role_id'] ) ) {
// we don't want our own role listed when editing
if( !empty( $roleList[$_REQUEST['role_id']] ) ) {
unset( $roleList[$_REQUEST['role_id']] );
}
$roleInfo = $gBitUser->getRoleInfo( $_REQUEST['role_id'] );
$roleUsers = $gBitUser->getRoleUsers( $_REQUEST['role_id'] );
$gBitSmarty->assignByRef('roleUsers', $roleUsers);
$gBitSmarty->assignByRef('roleInfo', $roleInfo);
$gBitSmarty->assignByRef( 'allPerms', $allPerms );
$gBitSystem->setBrowserTitle( 'Admininster Role: '.$roleInfo['role_name'].' '.(isset( $_REQUEST['tab'] ) ? $_REQUEST['tab'] : '') );
$mid = 'bitpackage:users/my_role_edit.tpl';
}
$gBitSmarty->assign('roles', $roleList);
// $gBitSmarty->assign( (!empty( $_REQUEST['tab'] ) ? $_REQUEST['tab'] : 'edit').'TabSelect', 'tdefault' );
}
/* join or leave a public role. */
if ( ( !empty( $_REQUEST['add_public_role'] ) || !empty( $_REQUEST['remove_public_role'] ) ) && !empty( $_REQUEST['public_role_id'] ) ) {
$roleInfo = $gBitUser->getRoleInfo( $_REQUEST['public_role_id'] );
if ( empty($roleInfo) || $roleInfo['is_public'] != 'y' ) {
if (empty($_REQUEST['add_public_role'])) {
$errorMsg[] = tra("You can't join this role.");
}
else {
$errorMsg[] = tra("You can't leave this role.");
}
} elseif ( !empty( $_REQUEST['add_public_role'] ) ) {
$gBitUser->addUserToRole( $gBitUser->mUserId, $_REQUEST['public_role_id'] );
} elseif ( !empty( $_REQUEST['remove_public_role'] ) ) {
$gBitUser->removeUserFromRole( $gBitUser->mUserId, $_REQUEST['public_role_id'] );
}
$gBitUser->loadPermissions();
if ( !empty( $_REQUEST['add_public_role'] ) && !empty( $roleInfo['after_registration_page'] ) ) {
if ( $gBitUser->verifyId( $roleInfo['after_registration_page'] ) ) {
$url = BIT_ROOT_URL."index.php?content_id=".$roleInfo['after_registration_page'];
} elseif( strpos( $roleInfo['after_registration_page'], '/' ) === FALSE ) {
$url = BitPage::getDisplayUrl( $roleInfo['after_registration_page'] );
} else {
$url = $roleInfo['after_registration_page'];
}
header( 'Location: '.$url );
exit;
}
}
/* Load up public roles and check if the user can join or leave them */
$systemRoles = $gBitUser->getRoles( $gBitUser->mUserId, TRUE );
$gBitSmarty->assignByRef( 'systemRoles', $systemRoles);
$listHash = array(
'is_public'=>'y',
'sort_mode' => array( 'is_default_asc', 'role_desc_asc' ),
);
$publicRoles = $gBitUser->getAllRoles( $listHash );
if( count( $publicRoles )) {
foreach ( $systemRoles as $roleId=>$roleInfo ) {
foreach ( $publicRoles as $key=>$publicRole) {
if ( $publicRoles[$key]['role_id'] == $roleId) {
if ($publicRoles[$key]['is_default'] != 'y' ) {
$systemRoles[$roleId]['public'] = 'y';
$canRemovePublic = 'y';
}
$publicRoles[$key]['used'] = 'y';
break;
}
}
}
foreach ( $publicRoles as $roleInfo) {
if ( empty($roleInfo['used'] ) && $roleInfo['is_default'] != 'y' ) {
$gBitSmarty->assign( 'canAddPublic' , 'y');
break;
}
}
$gBitSmarty->assignByRef( 'publicRoles', $publicRoles );
if (isset($canRemovePublic)) {
$gBitSmarty->assign( 'canRemovePublic' , 'y');
}
}
// Remember error and success messages.
if (!empty($errorMsg)) {
$gBitSmarty->assign('errorMsg',$errorMsg);
}
if (!empty($successMsg)) {
$gBitSmarty->assign('successMsg',$successMsg);
}
// Default the template if we aren't doing an edit.
if (empty($mid)) {
$mid = 'bitpackage:users/my_roles_list.tpl';
}
// Display the template for role administration
$gBitSystem->display( $mid , NULL, array( 'display_mode' => 'display' ));
?>