-
Notifications
You must be signed in to change notification settings - Fork 10
/
groups.php
328 lines (269 loc) · 9.47 KB
/
groups.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
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
<?php
// $Id$
// --------------------------------------------------------------
// Red México Common Utilities
// A framework for Red México Modules
// Author: Eduardo Cortés <i.bitcero@gmail.com>
// Email: i.bitcero@gmail.com
// License: GPL 2.0
// --------------------------------------------------------------
// Constant to specify the internal location
// Could be useful for themes, plugins and modules
require dirname(dirname(__DIR__)) . '/include/cp_header.php';
$common->location = 'groups';
/**
* Shows existing groups
*/
function show_groups_list()
{
global $xoopsDB;
define('RMCSUBLOCATION', 'allgroups');
list($total) = $xoopsDB->fetchRow($xoopsDB->query('SELECT COUNT(*) FROM ' . $xoopsDB->prefix('groups')));
$navigation = new RMPageNav($total, 20, RMHttpRequest::get('page', 'integer', 1));
$navigation->target_url(RMCURL . '/users.php?action=groups&page={PAGE_NUM}');
$sql = 'SELECT g.*, (SELECT COUNT(*) FROM ' . $xoopsDB->prefix('groups_users_link') . '
WHERE groupid = g.groupid) as total_users FROM ' . $xoopsDB->prefix('groups') . ' as g ORDER BY g.name
LIMIT ' . $navigation->start() . ', 20';
$result = $xoopsDB->query($sql);
$groups = [];
while (false !== ($row = $xoopsDB->fetchArray($result))) {
$groups[] = (object) $row;
}
$form = new RMForm('');
$bc = RMBreadCrumb::get();
$bc->add_crumb(__('Users Management', 'rmcommon'), RMCURL . '/users.php');
$bc->add_crumb(__('Groups', 'rmcommon'));
RMTemplate::get()->assign('xoops_pagetitle', __('Groups Management', 'rmcommon'));
RMFunctions::get()->create_toolbar();
RMTemplate::get()->add_script('cu-groups.js', 'rmcommon', ['footer' => 1]);
include RMCPATH . '/js/cu-js-language.php';
RMTemplate::get()->header();
include RMTemplate::get()->get_template('rmc-groups.php', 'module', 'rmcommon');
RMTemplate::get()->footer();
}
function show_group_form()
{
global $xoopsDB, $common;
$common->ajax()->prepare();
$id = RMHttpRequest::get('id', 'integer', 0);
if ($id > 0) {
$group = new Rmcommon_Group($id);
if ($group->isNew()) {
$common->ajax()->notifyError(__('The specified group does not exists!', 'rmcommon'));
}
} else {
$group = new Rmcommon_Group();
}
$form = new RMForm('');
$result = $xoopsDB->query('SELECT * FROM ' . $xoopsDB->prefix('modules') . ' ORDER BY name ASC');
$modules = [];
$admin_rights = $group->load_permissions('module_admin');
$access_rights = $group->load_permissions('module_read');
while (false !== ($row = $xoopsDB->fetchArray($result))) {
$modules[$row['mid']] = (object) $row;
$modules[$row['mid']]->permissions = RMPrivileges::module_permissions($row['dirname']);
$modules[$row['mid']]->privileges = RMPrivileges::read_permissions($row['dirname'], $group->id());
$modules[$row['mid']]->admin = isset($admin_rights[ $row['mid'] ]);
$modules[$row['mid']]->read = isset($access_rights[ $row['mid'] ]);
}
ob_start();
include RMTemplate::getInstance()::path('rmc-groups-form.php', 'module', 'rmcommon');
$content = ob_get_clean();
$common->ajax()->response(
$group->isNew() ? __('Create new group', 'rmcommon') : sprintf(__('Edit group "%s"', 'rmcommon'), $group->getVar('name')),
0,
1,
[
'content' => $content,
'width' => 'xlarge',
'color' => 'green',
'windowId' => 'window-form-groups',
'id' => 'groups-dialog',
]
);
}
function save_group_data()
{
global $xoopsSecurity;
$ajax = new Rmcommon_Ajax();
$ajax->prepare_ajax_response();
if (!$xoopsSecurity->validateToken(false, true, 'CUTOKEN')) {
$ajax->ajax_response(
__('Session token expired', 'rmcommon'),
1,
0,
[
'reload' => 'reload',
]
);
}
$name = RMHttpRequest::post('name', 'string', '');
$description = RMHttpRequest::post('description', 'string', '');
$icon = RMHttpRequest::post('icon', 'string', '');
$id = RMHttpRequest::post('id', 'integer', 0);
if ('' == $name) {
$ajax->ajax_response(
__('No name has been provided.', 'rmcommon'),
1,
1
);
}
if ('' == $description) {
$ajax->ajax_response(
__('Description has not been provided.', 'rmcommon'),
1,
1
);
}
if ($id > 0) {
$group = new Rmcommon_Group($id);
if ($group->isNew()) {
$ajax->ajax_response(
__('Specified group does not exists. Please try again.', 'rmcommon'),
1,
1,
[
'closeWindow' => 'window-form-groups',
]
);
}
} else {
$group = new Rmcommon_Group();
}
$group->setVar('name', $name);
$group->setVar('description', $description);
$group->setVar('icon', $icon);
if (!$group->save()) {
$ajax->ajax_response(
__('Errors ocurred while trying to save group data.', 'rmcommon') . "\n" . $group->errors(),
1,
1
);
}
// Guardamos los permisos de administración
$admin = RMHttpRequest::post('admin_modules', 'array', []);
if (!$group->set_admin_permissions($admin)) {
$ajax->ajax_response(
__('Administrative rights could not be saved.', 'rmcommon') . "\n" . $group->errors(),
1,
1,
[
'reload' => 'reload',
]
);
}
// Guardamos los permisos operativos
$read = RMHttpRequest::post('read_modules', 'array', []);
if (!$group->set_read_permissions($read)) {
$ajax->ajax_response(
__('Access rights could not be saved.', 'rmcommon') . "\n" . $group->errors(),
1,
1,
[
'reload' => 'reload',
]
);
}
// Guardamos los permisos específicos
$admin = RMHttpRequest::post('specific_perms', 'array', []);
if (!$group->set_specific_permissions($admin)) {
$ajax->ajax_response(
__('Specific permissions could not be saved.', 'rmcommon') . "\n" . $group->errors(),
1,
1,
[
'reload' => 'reload',
]
);
}
showMessage(__('Data saved successfully.', 'rmcommon'), RMMSG_SUCCESS, 'fa fa-hd');
// ëxito en las operaciones
$ajax->ajax_response(
__('Data saved successfully.', 'rmcommon'),
0,
1,
[
'reload' => 'reload',
]
);
}
function delete_group_data()
{
global $xoopsSecurity, $xoopsDB;
$ajax = new Rmcommon_Ajax();
$ajax->prepare_ajax_response();
if (!$xoopsSecurity->validateToken(false, true, 'CUTOKEN')) {
$ajax->ajax_response(
__('Session token expired!', 'rmcommon'),
1,
0,
['action' => 'reload']
);
}
$ids = RMHttpRequest::post('ids', 'array', []);
if (empty($ids)) {
$ajax->ajax_response(__('You must select at least one group. Please, try again.', 'rmcommon'), 1, 1);
}
$to_delete = array_search(XOOPS_GROUP_ADMIN, $ids, true);
if (false !== $to_delete) {
unset($ids[$to_delete]);
}
$to_delete = array_search(XOOPS_GROUP_USERS, $ids, true);
if (false !== $to_delete) {
unset($ids[$to_delete]);
}
$to_delete = array_search(XOOPS_GROUP_ANONYMOUS, $ids, true);
if (false !== $to_delete) {
unset($ids[$to_delete]);
}
if (empty($ids)) {
$ajax->ajax_response(
__('No valid groups has been selected. Note that system groups could not be deleted.', 'rmcommon'),
1,
1
);
}
$errors = '';
// Eliminar permisos del grupo
$sql = 'DELETE FROM ' . $xoopsDB->prefix('group_permission') . ' WHERE gperm_groupid IN (' . implode(',', $ids) . ')';
if (!$xoopsDB->queryF($sql)) {
$errors .= $xoopsDB->error();
}
// Eliminar permisos específicos
$sql = 'DELETE FROM ' . $xoopsDB->prefix('mod_rmcommon_permissions') . ' WHERE `group` IN (' . implode(',', $ids) . ')';
if (!$xoopsDB->queryF($sql)) {
$errors .= '<br>' . $xoopsDB->error();
}
// Eliminar relaciones con usuarios
$sql = 'DELETE FROM ' . $xoopsDB->prefix('groups_users_link') . ' WHERE `groupid` IN (' . implode(',', $ids) . ')';
if (!$xoopsDB->queryF($sql)) {
$errors .= '<br>' . $xoopsDB->error();
}
// Eliminar datos del grupo
$sql = 'DELETE FROM ' . $xoopsDB->prefix('groups') . ' WHERE `groupid` IN (' . implode(',', $ids) . ')';
if (!$xoopsDB->queryF($sql)) {
$errors .= '<br>' . $xoopsDB->error();
}
if ('' == $errors) {
showMessage(__('Selected groups has been deleted.', 'rmcommon'), RMMSG_SUCCESS, 'fa fa-remove-circle');
$ajax->ajax_response('', 0, 1, [ 'reload' => true ]);
} else {
$ajax->ajax_response(__('Errors ocurred while trying to delete selected groups.', 'rmcommon') . "\n" . $errors, 1, 1);
}
}
// get the action
$action = RMHttpRequest::request('action', 'string', '');
switch ($action) {
case 'new-group':
show_group_form();
break;
case 'save-group':
save_group_data();
break;
case 'delete-group':
delete_group_data();
break;
default:
show_groups_list();
break;
}